Skip to content

Commit

Permalink
#155 Allow overriding of brand colors in flavours
Browse files Browse the repository at this point in the history
  • Loading branch information
mwehr committed Jul 18, 2023
1 parent fb42d50 commit 0701f6c
Show file tree
Hide file tree
Showing 8 changed files with 780 additions and 7 deletions.
205 changes: 205 additions & 0 deletions classes/form/colourpicker_form_element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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

global $CFG;

require_once($CFG->dirroot . '/lib/form/editor.php');

/**
* Form element for handling the colour picker.
*
* @package theme_boost_union
* @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 {

// String html for help button, if empty then no help.
public $_helpbutton = '';

/**
* Class constructor
*
* @param string Name of the element
* @param mixed Label(s) for the element
* @param mixed Associative array of tag attributes or HTML attributes name="value" pairs
* @since 1.0
* @access public
* @return void
*/
public function __construct($elementname=null, $elemenlabel=null, $attributes=null) {
parent::__construct($elementname, $elemenlabel, $attributes);
$this->_type = 'static';
}

/**
* Sets name of editor
*
* @param string $name name of element
*/
// @codingStandardsIgnoreStart
public function setName($name) {
$this->updateAttributes(array('name' => $name));
}
// @codingStandardsIgnoreEnd
/**
* Returns name of element
*
* @return string
*/
// @codingStandardsIgnoreStart
function getName() {
return $this->getAttribute('name');
}
// @codingStandardsIgnoreEnd
/**
* get html for help button
*
* @return string html for help button
*/
// @codingStandardsIgnoreStart
public function getHelpButton() {
return $this->_helpbutton;
}
// @codingStandardsIgnoreEnd
/**
* Sets the value of the form element
*
* @param string $value
*/
// @codingStandardsIgnoreStart
public function setvalue($value) {
$this->updateAttributes(array('value' => $value));
}
// @codingStandardsIgnoreEnd
/**
* Gets the value of the form element
*/
public function getvalue() {
return $this->getAttribute('value');
}

/**
* Returns the html string to display this element.
*
* @return string
*/
public function tohtml() {
global $PAGE, $OUTPUT;

$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'),
'id' => $this->getAttribute('id'),
'value' => $this->getAttribute('value'),
"readonly" => false,
'haspreviewconfig' => false,
];
$PAGE->requires->js_init_call('M.util.init_colour_picker', array($this->getAttribute('id'), null));
return $OUTPUT->render_from_template('core_admin/setting_configcolourpicker', $context);
}

/**
* Function to export the renderer data in a format that is suitable for a mustache template.
*
* @param \renderer_base $output Used to do a final render of any components that need to be rendered for export.
* @return \stdClass|array
*/
public function export_for_template(renderer_base $output) {
$context['html'] = $this->toHtml();
$context['id'] = $this->getAttribute('id');
return $context;
}
}

/**
* Colour picker validation rule
*
* @package theme_boost_union
* @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 {

/**
* Validates the colour that was entered by the user
*
* @param string $value Value to check
* @param int|string|array $options Not used yet
* @return bool true if value is not empty
*/
public function validate($value, $options = null) {

// List of valid HTML colour names.
$colornames = array(
'aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure',
'beige', 'bisque', 'black', 'blanchedalmond', 'blue',
'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse',
'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson',
'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray',
'darkgrey', 'darkgreen', 'darkkhaki', 'darkmagenta',
'darkolivegreen', 'darkorange', 'darkorchid', 'darkred',
'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray',
'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink',
'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick',
'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro',
'ghostwhite', 'gold', 'goldenrod', 'gray', 'grey', 'green',
'greenyellow', 'honeydew', 'hotpink', 'indianred', 'indigo',
'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen',
'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan',
'lightgoldenrodyellow', 'lightgray', 'lightgrey', 'lightgreen',
'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue',
'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow',
'lime', 'limegreen', 'linen', 'magenta', 'maroon',
'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple',
'mediumseagreen', 'mediumslateblue', 'mediumspringgreen',
'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream',
'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive',
'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod',
'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip',
'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'red',
'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown',
'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue',
'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan',
'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white',
'whitesmoke', 'yellow', 'yellowgreen'
);

if (preg_match('/^#?([[:xdigit:]]{3}){1,2}$/', $value)) {
if (strpos($value, '#') !== 0) {
$value = '#'.$value;
}
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)) {
return $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)) {
return $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;
} else {
return false;
}
}
}
92 changes: 92 additions & 0 deletions classes/form/flavour_edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class flavour_edit_form extends \moodleform {
* @throws \coding_exception
*/
public function definition() {
global $CFG, $OUTPUT;

// Get an easier handler for the form.
$mform = $this->_form;

Expand Down Expand Up @@ -120,6 +122,96 @@ public function definition() {
]);
$mform->addHelpButton('flavours_look_backgroundimage', 'flavoursbackgroundimage', 'theme_boost_union');

// Create brand colors heading.
$context = new \stdClass();
$context->title = get_string('brandcolorsheading', 'theme_boost_union', null, true);
$mform->addElement(
'html',
'<div id="adminsettings">'. $OUTPUT->render_from_template('core_admin/setting_heading', $context), '</div>'
);

// 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');
// Register validation rule for colourpicker.
\MoodleQuickForm::registerRule('theme_boost_union_colourpicker_rule',
null,
'theme_boost_union_colourpicker_rule',
$CFG->dirroot . '/theme/boost_union/classes/form/colourpicker_form_element.php');

// Add brandcolour as colourpicker element.
$mform->addElement(
'boost_union_colourpicker',
'brandcolor',
get_string('brandcolor', 'theme_boost'),
[
'id' => 'colourpicker_brandcolour',
]);
$mform->setDefault('brandcolor', '');
$mform->addHelpButton('brandcolor', 'flavoursbrandcolour', 'theme_boost_union');
// Add validation rule.
$mform->addRule('brandcolor', get_string('validateerror', 'admin'), 'theme_boost_union_colourpicker_rule');

// Create Bootstrap colors heading.
$context = new \stdClass();
$context->title = get_string('bootstrapcolorsheading', 'theme_boost_union', null, true);
$mform->addElement(
'html',
'<div id="adminsettings">'. $OUTPUT->render_from_template('core_admin/setting_heading', $context), '</div>'
);

// Add Bootstrap color for 'success' as colourpicker element.
$mform->addElement(
'boost_union_colourpicker',
'bootstrapcolorsuccess',
get_string('bootstrapcolorsuccesssetting', 'theme_boost_union'),
[
'id' => 'colourpicker-bootstrapcolorsuccess',
]);
$mform->setDefault('bootstrapcolorsuccess', '');
$mform->addHelpButton('bootstrapcolorsuccess', 'flavoursbootstrapcolorsuccess', 'theme_boost_union');
// Add validation rule.
$mform->addRule('bootstrapcolorsuccess', get_string('validateerror', 'admin'), 'theme_boost_union_colourpicker_rule');

// Add Bootstrap color for 'info' as colourpicker element.
$mform->addElement(
'boost_union_colourpicker',
'bootstrapcolorinfo',
get_string('bootstrapcolorinfosetting', 'theme_boost_union'),
[
'id' => 'colourpicker-bootstrapcolorinfo',
]);
$mform->setDefault('bootstrapcolorinfo', '');
$mform->addHelpButton('bootstrapcolorinfo', 'flavoursbootstrapcolorinfo', 'theme_boost_union');
// Add validation rule.
$mform->addRule('bootstrapcolorinfo', get_string('validateerror', 'admin'), 'theme_boost_union_colourpicker_rule');

// Add Bootstrap color for 'warning' as colourpicker element.
$mform->addElement(
'boost_union_colourpicker',
'bootstrapcolorwarning',
get_string('bootstrapcolorwarningsetting', 'theme_boost_union'),
[
'id' => 'colourpicker-bootstrapcolorwarning',
]);
$mform->addHelpButton('bootstrapcolorwarning', 'flavoursbootstrapcolorwarning', 'theme_boost_union');
// Add validation rule.
$mform->addRule('bootstrapcolorwarning', get_string('validateerror', 'admin'), 'theme_boost_union_colourpicker_rule');

// Add Bootstrap color for 'danger' as colourpicker element.
$mform->addElement(
'boost_union_colourpicker',
'bootstrapcolordanger',
get_string('bootstrapcolordangersetting', 'theme_boost_union'),
[
'id' => 'colourpicker-bbootstrapcolordanger',
]);
$mform->setDefault('bootstrapcolordanger', '');
$mform->addHelpButton('bootstrapcolordanger', 'flavoursbootstrapcolordanger', 'theme_boost_union');
// Add validation rule.
$mform->addRule('bootstrapcolordanger', get_string('validateerror', 'admin'), 'theme_boost_union_colourpicker_rule');

// Add custom css as textarea element.
// Note: In the current state of implementation, this setting only allows the usage of custom CSS, not SCSS.
// It will be appended to the stack of CSS code which is shipped to the browser.
Expand Down
5 changes: 5 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
<FIELD NAME="look_favicon" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="look_backgroundimage" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="look_rawscss" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="brandcolor" TYPE="char" LENGTH="32" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="bootstrapcolorsuccess" TYPE="char" NOTNULL="false" LENGTH="32" SEQUENCE="false"/>
<FIELD NAME="bootstrapcolorinfo" TYPE="char" NOTNULL="false" LENGTH="32" SEQUENCE="false"/>
<FIELD NAME="bootstrapcolorwarning" TYPE="char" NOTNULL="false" LENGTH="32" SEQUENCE="false"/>
<FIELD NAME="bootstrapcolordanger" TYPE="char" NOTNULL="false" LENGTH="32" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
33 changes: 33 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,38 @@ function xmldb_theme_boost_union_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2022080922, 'theme', 'boost_union');
}

/*if ($oldversion < 0000000) {
$table = new xmldb_table('theme_boost_union_flavours');
$field = new xmldb_field('brandcolor', XMLDB_TYPE_CHAR, '32', null, null, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('bootstrapcolorsuccess', XMLDB_TYPE_CHAR, '32', null, null, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('bootstrapcolorwarning', XMLDB_TYPE_CHAR, '32', null, null, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('bootstrapcolorwarning', XMLDB_TYPE_CHAR, '32', null, null, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
$field = new xmldb_field('bootstrapcolordanger', XMLDB_TYPE_CHAR, '32', null, null, null, null);
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Boost_union savepoint reached.
upgrade_plugin_savepoint(true, 00000000, 'theme', 'boost_union');
}*/

return true;
}
Loading

0 comments on commit 0701f6c

Please sign in to comment.