A bit of refactoring in Form.class.php.
[timetracker.git] / WEB-INF / lib / form / ActionForm.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import("DateAndTime");
30
31 class ActionForm {
32         var $mName              = "";
33         var $mSessionCell;
34     var $mValues        = array(); // values without localisation
35     var $mVariables = array();
36     var $mForm          = null;
37     var $mInitForm      = false;
38
39     function __construct($name, &$form, $request=null) {
40         $this->setName($name);
41         $form->setRequest($request);
42                 $this->setForm($form);
43                 //if ($request) $this->initAttributes($request);
44                 $this->initAttributes($request);
45     }
46     
47     function setForm(&$form) {
48         $this->mForm = $form;
49         $elements = $form->getElements();
50         if (is_array($elements))
51                 $this->setVariablesNames(array_keys($elements));
52     }
53     
54     function &getFormElement($name) {
55         if ($this->mForm!=null) {
56                         return  $this->mForm->mElements[$name];
57         }
58         return null;
59     }
60     
61     function getName() {
62                 return $this->mName;
63         }
64
65     function setName($name) {
66                 $this->mName = $name;
67                 $this->mSessionCell = "formbean_".$this->mName;
68         }
69     
70     /**
71      * init parameters and form
72      *
73      * @param object $request
74      */
75     function initAttributes(&$request) {
76         //$submit_flag = $this->isSubmit();
77         $submit_flag = (is_object($request) && ($request->isPost()));
78                 
79         if ($submit_flag) {
80                 // fill ActionForm and Form from Request
81
82                 foreach ($this->mVariables as $name) {
83                         if ($this->mForm->mElements[$name] && $request->getParameter($name)) {
84                             $this->mForm->mElements[$name]->setValue($request->getParameter($name));
85                             $this->mValues[$name] = $this->mForm->mElements[$name]->getValue();
86                         }
87                 }
88         } else {
89                 // fill ActionForm from Session
90                 $this->loadBean();
91         }
92         
93         // fill Form by ActionForm
94         if ($this->mForm) {
95                 $elements = $this->mForm->getElements();
96                         foreach ($elements as $name=>$el) {
97                         if ($this->mForm->mElements[$name] && isset($this->mValues[$name])) {
98                                     $this->mForm->mElements[$name]->setValue($this->mValues[$name]);
99                     }
100                 }
101                 $this->mInitForm = true;
102         }
103     }
104     
105     /**
106      * Init custom variables
107      *
108      * @param unknown $request
109      * @param unknown $respons
110      */
111     function initVariables(&$request, &$respons) {
112         
113     }
114     
115     function setVariablesNames($namelist) {
116         $this->mVariables = $namelist;
117     }
118
119     function setAttribute($name,$value) {
120         global $user;
121         
122         $this->mValues[$name] = $value;
123         if ($this->mForm) {
124                 if (isset($this->mForm->mElements[$name])) {
125                         if ($this->mForm->mElements[$name]->cClassName=="DateField") {
126                                 $dt = new DateAndTime($user->date_format, $value);
127                                         $value = $dt->toString(DB_DATEFORMAT);
128                         }
129                         $this->mForm->mElements[$name]->setValueSafe($value);
130                 }
131         }
132     }
133
134     function getAttribute($name) {
135         return @$this->mValues[$name];
136     }
137         
138         function getAttributes() {
139         return $this->mValues;
140     }
141
142     function validate(&$actionMapping, &$request) {
143         return null;
144     }
145
146         function setAttributes($value) {
147                 global $user;
148                 
149         $this->mValues = $value;
150         if (is_array($this->mValues))
151         foreach ($this->mValues as $name=>$value) {
152                 if ($this->mForm) {
153                         if (isset($this->mForm->mElements[$name])) {
154                                 if ($this->mForm->mElements[$name]->cClassName=="DateField") {
155                                         $dt = new DateAndTime($user->date_format, $value);
156                                                 $value = $dt->toString(DB_DATEFORMAT);
157                                 }
158                                 $this->mForm->mElements[$name]->setValueSafe($value);
159                         }
160                 }
161         }
162     }
163     
164     function dump() {
165         print_r($this->mValues);
166     }
167     
168     function isSubmit() {
169         $res = false;
170         if (is_object($this->mForm)) {
171             $res = $this->mForm->isSubmit();
172         }
173         return $res;
174     }
175     
176     function saveBean() {
177         if ($this->mForm) {
178                 $elements = $this->mForm->getElements();
179                 $el_list = array();
180                 foreach ($elements as $el) {
181                         $el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
182                         
183                                 $_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
184                 }
185                 $_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
186         }
187         //print_r($_SESSION);
188     }
189     
190     function loadBean() {
191         $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
192         if (is_array($el_list)) {
193                 foreach ($el_list as $ref_el) {
194                         
195                         // restore form elements
196                         import('form.'.$ref_el["class"]);
197                         $class_name = $ref_el["class"];
198                         $el = new $class_name($ref_el["name"]);
199                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
200                         $el->setValueSafe(@$_SESSION[$this->mSessionCell . "_" .$el->getName()]);
201                         
202                                 if ($this->mForm && !isset($this->mForm->mElements[$ref_el["name"]])) {
203                                         $this->mForm->mElements[$ref_el["name"]] = &$el;
204                                 }
205                         $this->mValues[$el->getName()] = $el->getValue();
206                 }
207         }
208                 //print_r($_SESSION);
209     }
210     
211     function destroyBean() {
212         $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
213         if (is_array($el_list)) {
214                 foreach ($el_list as $ref_el) {
215                         unset($_SESSION[$this->mSessionCell . "_" .$ref_el["name"]]);
216                 }
217         }
218         unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
219     }
220     
221     function isSaved() {
222         return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);
223     }
224 }
225