ee30f7d169604cbbdca3875b6700a65b5cffe2a6
[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                 $this->setForm($form);
42                 //if ($request) $this->initAttributes($request);
43                 $this->initAttributes($request);
44     }
45     
46     function setForm(&$form) {
47         $this->mForm = $form;
48         $elements = $form->getElements();
49         if (is_array($elements))
50                 $this->setVariablesNames(array_keys($elements));
51     }
52     
53     function &getFormElement($name) {
54         if ($this->mForm!=null) {
55             return  $this->mForm->elements[$name];
56         }
57         return null;
58     }
59     
60     function getName() {
61                 return $this->mName;
62         }
63
64     function setName($name) {
65                 $this->name = $name;
66                 $this->mSessionCell = "formbean_".$this->name;
67         }
68     
69     /**
70      * init parameters and form
71      *
72      * @param object $request
73      */
74     function initAttributes(&$request) {
75         $submit_flag = (is_object($request) && ($request->isPost()));
76                 
77         if ($submit_flag) {
78                 // fill ActionForm and Form from Request
79
80                 foreach ($this->mVariables as $name) {
81                         if ($this->mForm->elements[$name] && $request->getParameter($name)) {
82                             $this->mForm->elements[$name]->setValue($request->getParameter($name));
83                             $this->mValues[$name] = $this->mForm->elements[$name]->getValue();
84                         }
85                 }
86         } else {
87                 // fill ActionForm from Session
88                 $this->loadBean();
89         }
90         
91         // fill Form by ActionForm
92         if ($this->mForm) {
93                 $elements = $this->mForm->getElements();
94                         foreach ($elements as $name=>$el) {
95                         if ($this->mForm->elements[$name] && isset($this->mValues[$name])) {
96                                     $this->mForm->elements[$name]->setValue($this->mValues[$name]);
97                     }
98                 }
99                 $this->mInitForm = true;
100         }
101     }
102     
103     /**
104      * Init custom variables
105      *
106      * @param unknown $request
107      * @param unknown $respons
108      */
109     function initVariables(&$request, &$respons) {
110         
111     }
112     
113     function setVariablesNames($namelist) {
114         $this->mVariables = $namelist;
115     }
116
117     function setAttribute($name,$value) {
118         global $user;
119         
120         $this->mValues[$name] = $value;
121         if ($this->mForm) {
122                 if (isset($this->mForm->elements[$name])) {
123                         if ($this->mForm->elements[$name]->class=="DateField") {
124                                 $dt = new DateAndTime($user->date_format, $value);
125                                         $value = $dt->toString(DB_DATEFORMAT);
126                         }
127                         $this->mForm->elements[$name]->setValueSafe($value);
128                 }
129         }
130     }
131
132     function getAttribute($name) {
133         return @$this->mValues[$name];
134     }
135         
136         function getAttributes() {
137         return $this->mValues;
138     }
139
140     function validate(&$actionMapping, &$request) {
141         return null;
142     }
143
144         function setAttributes($value) {
145                 global $user;
146                 
147         $this->mValues = $value;
148         if (is_array($this->mValues))
149         foreach ($this->mValues as $name=>$value) {
150                 if ($this->mForm) {
151                         if (isset($this->mForm->elements[$name])) {
152                                 if ($this->mForm->elements[$name]->class=="DateField") {
153                                         $dt = new DateAndTime($user->date_format, $value);
154                                                 $value = $dt->toString(DB_DATEFORMAT);
155                                 }
156                                 $this->mForm->elements[$name]->setValueSafe($value);
157                         }
158                 }
159         }
160     }
161     
162     function dump() {
163         print_r($this->mValues);
164     }
165     
166     function saveBean() {
167         if ($this->mForm) {
168                 $elements = $this->mForm->getElements();
169                 $el_list = array();
170                 foreach ($elements as $el) {
171                         $el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
172                         
173                                 $_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
174                 }
175                 $_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
176         }
177         //print_r($_SESSION);
178     }
179     
180     function loadBean() {
181         $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
182         if (is_array($el_list)) {
183                 foreach ($el_list as $ref_el) {
184                         
185                         // restore form elements
186                         import('form.'.$ref_el["class"]);
187                         $class_name = $ref_el["class"];
188                         $el = new $class_name($ref_el["name"]);
189                         if (isset($GLOBALS["I18N"])) $el->setLocalization($GLOBALS["I18N"]);
190                         $el->setValueSafe(@$_SESSION[$this->mSessionCell . "_" .$el->getName()]);
191                         
192                                 if ($this->mForm && !isset($this->mForm->elements[$ref_el["name"]])) {
193                                         $this->mForm->elements[$ref_el["name"]] = &$el;
194                                 }
195                         $this->mValues[$el->getName()] = $el->getValue();
196                 }
197         }
198                 //print_r($_SESSION);
199     }
200     
201     function destroyBean() {
202         $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
203         if (is_array($el_list)) {
204                 foreach ($el_list as $ref_el) {
205                         unset($_SESSION[$this->mSessionCell . "_" .$ref_el["name"]]);
206                 }
207         }
208         unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
209     }
210     
211     function isSaved() {
212         return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);
213     }
214 }