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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 import("DateAndTime");
34 var $mValues = array(); // values without localisation
35 var $mVariables = array();
37 var $mInitForm = false;
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);
46 function setForm(&$form) {
48 $elements = $form->getElements();
49 if (is_array($elements))
50 $this->setVariablesNames(array_keys($elements));
53 function &getFormElement($name) {
54 if ($this->mForm!=null) {
55 return $this->mForm->elements[$name];
64 function setName($name) {
66 $this->mSessionCell = "formbean_".$this->name;
70 * init parameters and form
72 * @param object $request
74 function initAttributes(&$request) {
75 $submit_flag = (is_object($request) && ($request->isPost()));
78 // fill ActionForm and Form from Request
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();
87 // fill ActionForm from Session
91 // fill Form by ActionForm
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]);
99 $this->mInitForm = true;
103 function setVariablesNames($namelist) {
104 $this->mVariables = $namelist;
107 function setAttribute($name,$value) {
110 $this->mValues[$name] = $value;
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);
117 $this->mForm->elements[$name]->setValueSafe($value);
122 function getAttribute($name) {
123 return @$this->mValues[$name];
126 function getAttributes() {
127 return $this->mValues;
130 function validate(&$actionMapping, &$request) {
134 function setAttributes($value) {
137 $this->mValues = $value;
138 if (is_array($this->mValues))
139 foreach ($this->mValues as $name=>$value) {
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);
146 $this->mForm->elements[$name]->setValueSafe($value);
153 print_r($this->mValues);
156 function saveBean() {
158 $elements = $this->mForm->getElements();
160 foreach ($elements as $el) {
161 $el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
163 $_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
165 $_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
167 //print_r($_SESSION);
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;
180 function getDetachedAttribute($name) {
181 return $_SESSION[$this->mSessionCell.'_'.$name];
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) {
189 // restore form elements
190 import('form.'.$ref_el["class"]);
191 $class_name = $ref_el["class"];
192 $el = new $class_name($ref_el["name"]);
194 $el->setValueSafe(@$_SESSION[$this->mSessionCell . "_" .$el->getName()]);
196 if ($this->mForm && !isset($this->mForm->elements[$ref_el["name"]])) {
197 $this->mForm->elements[$ref_el["name"]] = &$el;
199 $this->mValues[$el->getName()] = $el->getValue();
202 //print_r($_SESSION);
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"]]);
212 unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
216 return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);