1: <?php
2: /**
3: * Pry Framework
4: *
5: * LICENSE
6: *
7: * This source file is subject to the new BSD license that is bundled
8: * with this package in the file LICENSE.txt.
9: *
10: * @version $Revision: 276 $
11: */
12:
13: /**
14: * Element ColorPicker. Permet de choisir une couleur dans une palette
15: * @category Pry
16: * @package Form
17: * @subpackage Form_Element
18: * @version 1.5.0
19: * @author Olivier ROGER <oroger.fr>
20: *
21: */
22: class Form_Element_Colorpicker extends Form_Element_Text
23: {
24:
25: /**
26: * Constructeur. Défini la couleur de base à rouge
27: *
28: * @param string $nom
29: * @param Form_Form $form
30: */
31: public function __construct($nom,$form)
32: {
33: parent::__construct($nom,$form);
34: $this->value('#FF0000');
35: }
36: /**
37: * Défini une couleur de base en RGB
38: *
39: * @param array $rgb
40: * @access public
41: * @return Form_Element_Colorpicker
42: */
43: public function setRGBColor($rgb)
44: {
45: for($i=0;$i<2;$i++)
46: {
47: if($rgb[$i]<0 || $rgb[$i]>255)
48: throw new Image_Exception('La valeur RGB est incorrecte (compris en 0 et 255');
49: }
50: $this->value('#'.str_pad((dechex($rgb[0]).dechex($rgb[1]).dechex($rgb[2])),6,"0",STR_PAD_LEFT));
51: return $this;
52: }
53:
54: /**
55: * Défini une couleur de base en Hexa
56: *
57: * @param string $color
58: * @access public
59: * @return Form_Element_Colorpicker
60: */
61: public function setHexaColor($color)
62: {
63: if($color[0] =='#')
64: $this->value($color);
65: else
66: $this->value('#'.$color);
67:
68: return $this;
69: }
70:
71: /**
72: * Vérifie la couleur recu
73: *
74: * @param string $value
75: * @access public
76: * @return boolean
77: */
78: public function isValid($value)
79: {
80: if(parent::isValid($value))
81: {
82: if(!preg_match('`^#([0-9a-fA-F]{6})$`',$value))
83: {
84: $this->errorMsg = Form_Error::COLOR;
85: return false;
86: }
87: return true;
88: }
89: }
90: /**
91: * Ecrit l'objet
92: *
93: * @return unknown
94: */
95: public function __toString()
96: {
97: $field ='';
98: $field.=parent::__toString();
99: $this->form->javascript .='$("#'.$this->attrs['id'].'").gccolor({
100: onChange : function(target,color){
101: target.val("#"+color);
102: }
103: });';
104: return $field;
105: }
106:
107: }
108: ?>