Adjusted PDF reports to print notes on separate lines.
[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     function setVariablesNames($namelist) {
104         $this->mVariables = $namelist;
105     }
106
107     function setAttribute($name,$value) {
108         global $user;
109         
110         $this->mValues[$name] = $value;
111         if ($this->mForm) {
112                 if (isset($this->mForm->elements[$name])) {
113                         if ($this->mForm->elements[$name]->class=="DateField") {
114                                 $dt = new DateAndTime($user->date_format, $value);
115                                         $value = $dt->toString(DB_DATEFORMAT);
116                         }
117                         $this->mForm->elements[$name]->setValueSafe($value);
118                 }
119         }
120     }
121
122     function getAttribute($name) {
123         return @$this->mValues[$name];
124     }
125         
126         function getAttributes() {
127         return $this->mValues;
128     }
129
130     function validate(&$actionMapping, &$request) {
131         return null;
132     }
133
134         function setAttributes($value) {
135                 global $user;
136                 
137         $this->mValues = $value;
138         if (is_array($this->mValues))
139         foreach ($this->mValues as $name=>$value) {
140                 if ($this->mForm) {
141                         if (isset($this->mForm->elements[$name])) {
142                                 if ($this->mForm->elements[$name]->class=="DateField") {
143                                         $dt = new DateAndTime($user->date_format, $value);
144                                                 $value = $dt->toString(DB_DATEFORMAT);
145                                 }
146                                 $this->mForm->elements[$name]->setValueSafe($value);
147                         }
148                 }
149         }
150     }
151     
152     function dump() {
153         print_r($this->mValues);
154     }
155     
156     function saveBean() {
157         if ($this->mForm) {
158                 $elements = $this->mForm->getElements();
159                 $el_list = array();
160                 foreach ($elements as $el) {
161                         $el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
162                         
163                                 $_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
164                 }
165                 $_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
166         }
167         //print_r($_SESSION);
168     }
169
170   // saveDetachedAttribute saves a "detached" from form named attributed in session.
171   // There is no element in the form for it.
172   // Intended use is to add something to the session, when a form bean created on one page
173   // is used on other pages (ex.: reportForm).
174   // For example, to generate a timesheet we need a user_id, which is determined when a report
175   // is generated on report.php, using a bean created in reports.php.
176   function saveDetachedAttribute($name, $value) {
177     $_SESSION[$this->mSessionCell .'_'.$name] = $value;
178   }
179
180   function getDetachedAttribute($name) {
181     return $_SESSION[$this->mSessionCell.'_'.$name];
182   }
183     
184     function loadBean() {
185         $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
186         if (is_array($el_list)) {
187                 foreach ($el_list as $ref_el) {
188                         
189                         // restore form elements
190                         import('form.'.$ref_el["class"]);
191                         $class_name = $ref_el["class"];
192                         $el = new $class_name($ref_el["name"]);
193                         $el->localize();
194                         $el->setValueSafe(@$_SESSION[$this->mSessionCell . "_" .$el->getName()]);
195                         
196                                 if ($this->mForm && !isset($this->mForm->elements[$ref_el["name"]])) {
197                                         $this->mForm->elements[$ref_el["name"]] = &$el;
198                                 }
199                         $this->mValues[$el->getName()] = $el->getValue();
200                 }
201         }
202                 //print_r($_SESSION);
203     }
204     
205     function destroyBean() {
206         $el_list = @$_SESSION[$this->mSessionCell . "session_store_elements"];
207         if (is_array($el_list)) {
208                 foreach ($el_list as $ref_el) {
209                         unset($_SESSION[$this->mSessionCell . "_" .$ref_el["name"]]);
210                 }
211         }
212         unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
213     }
214     
215     function isSaved() {
216         return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);
217     }
218 }