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;
104 * Init custom variables
106 * @param unknown $request
107 * @param unknown $respons
109 function initVariables(&$request, &$respons) {
113 function setVariablesNames($namelist) {
114 $this->mVariables = $namelist;
117 function setAttribute($name,$value) {
120 $this->mValues[$name] = $value;
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);
127 $this->mForm->elements[$name]->setValueSafe($value);
132 function getAttribute($name) {
133 return @$this->mValues[$name];
136 function getAttributes() {
137 return $this->mValues;
140 function validate(&$actionMapping, &$request) {
144 function setAttributes($value) {
147 $this->mValues = $value;
148 if (is_array($this->mValues))
149 foreach ($this->mValues as $name=>$value) {
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);
156 $this->mForm->elements[$name]->setValueSafe($value);
163 print_r($this->mValues);
166 function saveBean() {
168 $elements = $this->mForm->getElements();
170 foreach ($elements as $el) {
171 $el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
173 $_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
175 $_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
177 //print_r($_SESSION);
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) {
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->localize($GLOBALS["I18N"]);
190 $el->setValueSafe(@$_SESSION[$this->mSessionCell . "_" .$el->getName()]);
192 if ($this->mForm && !isset($this->mForm->elements[$ref_el["name"]])) {
193 $this->mForm->elements[$ref_el["name"]] = &$el;
195 $this->mValues[$el->getName()] = $el->getValue();
198 //print_r($_SESSION);
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"]]);
208 unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
212 return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);