From 377ee2390a3213bf7e9528613abff67907c65265 Mon Sep 17 00:00:00 2001 From: Wehr Mario Date: Thu, 20 Jul 2023 11:00:58 +0200 Subject: [PATCH] #286 Added flavour SCSS validation. --- classes/form/colourpicker_form_element.php | 18 ++++++---- classes/form/flavour_edit_form.php | 40 ++++++++++++++++++++-- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/classes/form/colourpicker_form_element.php b/classes/form/colourpicker_form_element.php index 7ccb0296e30..292848c6181 100644 --- a/classes/form/colourpicker_form_element.php +++ b/classes/form/colourpicker_form_element.php @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . +namespace theme_boost_union\form; + +use renderer_base; + defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -27,7 +31,7 @@ * @copyright 2023 Mario Wehr * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class theme_boost_union_colourpicker_form_element extends HTML_QuickForm_element implements templatable { +class theme_boost_union_colourpicker_form_element extends \HTML_QuickForm_element implements \templatable { // String html for help button, if empty then no help. public $_helpbutton = ''; @@ -102,7 +106,7 @@ public function getvalue() { public function tohtml() { global $PAGE, $OUTPUT; - $icon = new pix_icon('i/loading', get_string('loading', 'admin'), 'moodle', ['class' => 'loadingicon']); + $icon = new \pix_icon('i/loading', get_string('loading', 'admin'), 'moodle', ['class' => 'loadingicon']); $context = (object) [ 'icon' => $icon->export_for_template($OUTPUT), 'name' => $this->getAttribute('name'), @@ -135,7 +139,7 @@ public function export_for_template(renderer_base $output) { * @copyright 2023 Mario Wehr * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class theme_boost_union_colourpicker_rule extends HTML_QuickForm_Rule { +class theme_boost_union_colourpicker_rule extends \HTML_QuickForm_Rule { /** * Validates the colour that was entered by the user @@ -188,13 +192,13 @@ public function validate($value, $options = null) { return $value; } else if (in_array(strtolower($value), $colornames)) { return $value; - } else if (preg_match('/rgb\(\d{0,3}%?\, ?\d{0,3}%?, ?\d{0,3}%?\)/i', $value)) { + } else if (preg_match('/rgb\(\d{0,3}%?, ?\d{0,3}%?, ?\d{0,3}%?\)/i', $value)) { return $value; - } else if (preg_match('/rgba\(\d{0,3}%?\, ?\d{0,3}%?, ?\d{0,3}%?\, ?\d(\.\d)?\)/i', $value)) { + } else if (preg_match('/rgba\(\d{0,3}%?, ?\d{0,3}%?, ?\d{0,3}%?, ?\d(\.\d)?\)/i', $value)) { return $value; - } else if (preg_match('/hsl\(\d{0,3}\, ?\d{0,3}%, ?\d{0,3}%\)/i', $value)) { + } else if (preg_match('/hsl\(\d{0,3}, ?\d{0,3}%, ?\d{0,3}%\)/i', $value)) { return $value; - } else if (preg_match('/hsla\(\d{0,3}\, ?\d{0,3}%,\d{0,3}%\, ?\d(\.\d)?\)/i', $value)) { + } else if (preg_match('/hsla\(\d{0,3}, ?\d{0,3}%,\d{0,3}%, ?\d(\.\d)?\)/i', $value)) { return $value; } else if (($value == 'transparent') || ($value == 'currentColor') || ($value == 'inherit')) { return $value; diff --git a/classes/form/flavour_edit_form.php b/classes/form/flavour_edit_form.php index a0f6e0d7300..64b3936cdcc 100644 --- a/classes/form/flavour_edit_form.php +++ b/classes/form/flavour_edit_form.php @@ -26,6 +26,9 @@ namespace theme_boost_union\form; +use ScssPhp\ScssPhp\Exception\CompilerException; +use ScssPhp\ScssPhp\Exception\ParserException; + defined('MOODLE_INTERNAL') || die(); // Require forms library. @@ -133,11 +136,11 @@ public function definition() { // Register custom colourpicker. \MoodleQuickForm::registerElementType('boost_union_colourpicker', $CFG->dirroot . '/theme/boost_union/classes/form/colourpicker_form_element.php', - 'theme_boost_union_colourpicker_form_element'); + 'theme_boost_union\form\theme_boost_union_colourpicker_form_element'); // Register validation rule for colourpicker. \MoodleQuickForm::registerRule('theme_boost_union_colourpicker_rule', null, - 'theme_boost_union_colourpicker_rule', + 'theme_boost_union\form\theme_boost_union_colourpicker_rule', $CFG->dirroot . '/theme/boost_union/classes/form/colourpicker_form_element.php'); // Add brandcolour as colourpicker element. @@ -272,4 +275,37 @@ public function definition() { // Add the action buttons. $this->add_action_buttons(); } + + /** + * Theme Boost Union - Flavours edit form validation + * + * @package theme_boost_union + * @param array $data array of ("fieldname"=>value) of submitted data + * @param array $files array of uploaded files "element_name"=>tmp_file_path + * @return array of "element_name"=>"error_description" if there are errors, + * or an empty array if everything is OK (true allowed for backwards compatibility too). + */ + public function validation($data, $files) { + global $PAGE; + + $errors = []; + + if (!empty($data['look_rawscss']) && !empty($data)) { + + $scss = new \core_scss(); + try { + if ($scssproperties = $PAGE->theme->get_scss_property()) { + $scss->setImportPaths($scssproperties[0]); + } + $scss->compile($data['look_rawscss']); + } catch (ParserException $e) { + $errors['look_rawscss'] = get_string('scssinvalid', 'admin', $e->getMessage()); + } catch (CompilerException $e) { + $errors['look_rawscss'] = get_string('scssinvalid', 'admin', $e->getMessage()); + } + $scss = null; + unset($scss); + } + return $errors; + } }