1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Form;
13:
14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class ColorPicker extends Text
27: {
28: 29: 30: 31: 32: 33: 34:
35: public function __construct($caption, $name = null, $value = '#FFFFFF')
36: {
37: if (is_array($caption)) {
38: parent::__construct($caption);
39: $value = $this->get('value','');
40: if (empty($value)) {
41: $this->set('value', '#FFFFFF');
42: }
43: $this->setIfNotSet('size', 10);
44: $this->setIfNotSet('maxlength', 16);
45: } else {
46: parent::__construct([]);
47: $this->set('caption', $caption);
48: $this->setWithDefaults('name', $name, 'name_error');
49: $this->set('size', 10);
50: $this->set('maxlength', 16);
51: $this->set('value', $value);
52: }
53: $this->setIfNotSet('type', 'text');
54: }
55:
56: 57: 58: 59: 60:
61: public function render()
62: {
63: $xoops = \Xoops::getInstance();
64: if ($xoops->theme()) {
65: $xoops->theme()->addScript('include/color-picker.js');
66: } else {
67: echo '<script type="text/javascript" src="' . $xoops->url('/include/color-picker.js') . '"></script>';
68: }
69: $temp = $this->get('value', '');
70: if (!empty($temp)) {
71: $this->set('style', 'background-color:' . $temp . ';');
72: }
73: return parent::render() . "<button class='btn' type='button' onclick=\"return TCP.popup('"
74: . $xoops->url('/include/') . "',document.getElementById('" . $this->getName() . "'));\"> ... </button>";
75:
76: }
77:
78: 79: 80: 81: 82:
83: public function renderValidationJS()
84: {
85: $eltname = $this->getName();
86: $eltcaption = $this->getCaption();
87: $eltmsg = empty($eltcaption)
88: ? sprintf(\XoopsLocale::F_ENTER, $eltname)
89: : sprintf(\XoopsLocale::F_ENTER, $eltcaption);
90:
91: return "if ( !(new RegExp(\"^#[0-9a-fA-F]{6}\",\"i\").test(myform.{$eltname}.value)) )"
92: . " { window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }";
93: }
94: }
95: