1 | <?php |
---|
2 | # ***** BEGIN LICENSE BLOCK ***** |
---|
3 | # Copyright (c) 2009 Olivier Azeau and contributors. All rights reserved. |
---|
4 | # |
---|
5 | # This is free software; you can redistribute it and/or modify |
---|
6 | # it under the terms of the GNU General Public License as published by |
---|
7 | # the Free Software Foundation; either version 2 of the License, or |
---|
8 | # (at your option) any later version. |
---|
9 | # |
---|
10 | # This is distributed in the hope that it will be useful, |
---|
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | # GNU General Public License for more details. |
---|
14 | # |
---|
15 | # You should have received a copy of the GNU General Public License |
---|
16 | # along with DotClear; if not, write to the Free Software |
---|
17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
18 | # |
---|
19 | # ***** END LICENSE BLOCK ***** |
---|
20 | |
---|
21 | class MyFormsComboChoice |
---|
22 | { |
---|
23 | public $id, $value, $isSelected; |
---|
24 | } |
---|
25 | |
---|
26 | class MyFormsComboField extends MyFormsField |
---|
27 | { |
---|
28 | private $choices; |
---|
29 | |
---|
30 | public function __construct() { |
---|
31 | parent::__construct(func_get_args()); |
---|
32 | } |
---|
33 | |
---|
34 | public function addChoice($id,$value,$isSelected) { |
---|
35 | $choice = new MyFormsComboChoice(); |
---|
36 | $choice->id = $id; |
---|
37 | $choice->value = $value; |
---|
38 | $choice->isSelected = $isSelected; |
---|
39 | $this->choices[$id] = $choice; |
---|
40 | } |
---|
41 | |
---|
42 | public function Display() { |
---|
43 | return "<select ".$this->AttributesAsString().">".$this->content."</select>"; |
---|
44 | } |
---|
45 | |
---|
46 | // override Field Value : use string value from selected choice instead of id |
---|
47 | public function Value($defaultValue=false) { |
---|
48 | return $this->choices[$this->Input($defaultValue)]->value; |
---|
49 | } |
---|
50 | |
---|
51 | public static function Register() { |
---|
52 | global $core; |
---|
53 | $core->tpl->addBlock('myformsCombo',array('MyFormsComboField','Combo')); |
---|
54 | $core->tpl->addBlock('myformsCombo_Declare',array('MyFormsComboField','Combo_Declare')); |
---|
55 | $core->tpl->addBlock('myformsComboChoice',array('MyFormsComboField','ComboChoice')); |
---|
56 | $core->tpl->addBlock('myformsComboChoice_Declare',array('MyFormsComboField','ComboChoice_Declare')); |
---|
57 | } |
---|
58 | |
---|
59 | // Display Combo Field |
---|
60 | public static function Combo($attr,$content) |
---|
61 | { |
---|
62 | return MyFormsField::DisplayObject(__CLASS__,$attr,$content); |
---|
63 | } |
---|
64 | |
---|
65 | // Declare Combo Field |
---|
66 | public static function Combo_Declare($attr,$content) |
---|
67 | { |
---|
68 | return MyFormsField::BuildDeclaration(__CLASS__,$attr,$content); |
---|
69 | } |
---|
70 | |
---|
71 | // Display choice in Combo Field |
---|
72 | public static function ComboChoice($attr,$content) |
---|
73 | { |
---|
74 | return "<option value='".$attr['id']."'".(isset($attr['selected'])?" selected='selected'":"")."><?php ob_start(); ?>".$content."<?php print ob_get_clean(); ?></option>"; |
---|
75 | } |
---|
76 | |
---|
77 | // Declare choice in Combo Field |
---|
78 | public static function ComboChoice_Declare($attr,$content) |
---|
79 | { |
---|
80 | return "<?php ob_start(); ?>" |
---|
81 | .$content |
---|
82 | ."<?php \$field->addChoice('".$attr['id']."',ob_get_clean(),".(isset($attr['selected'])?1:0)."); ?>\n"; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | MyFormsComboField::Register(); |
---|
87 | |
---|
88 | ?> |
---|