Skip to content

Commit

Permalink
#286 Added flavour SCSS validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mwehr committed Jul 20, 2023
1 parent c661d67 commit 377ee23
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
18 changes: 11 additions & 7 deletions classes/form/colourpicker_form_element.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace theme_boost_union\form;

use renderer_base;

defined('MOODLE_INTERNAL') || die();

global $CFG;
Expand All @@ -27,7 +31,7 @@
* @copyright 2023 Mario Wehr <[email protected]>
* @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 = '';
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -135,7 +139,7 @@ public function export_for_template(renderer_base $output) {
* @copyright 2023 Mario Wehr <[email protected]>
* @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
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 38 additions & 2 deletions classes/form/flavour_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 377ee23

Please sign in to comment.