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         $form->setRequest($request);
 
  42                 $this->setForm($form);
 
  43                 //if ($request) $this->initAttributes($request);
 
  44                 $this->initAttributes($request);
 
  47     function setForm(&$form) {
 
  49         $elements = $form->getElements();
 
  50         if (is_array($elements))
 
  51                 $this->setVariablesNames(array_keys($elements));
 
  54     function &getFormElement($name) {
 
  55         if ($this->mForm!=null) {
 
  56                         return  $this->mForm->mElements[$name];
 
  65     function setName($name) {
 
  67                 $this->mSessionCell = "formbean_".$this->mName;
 
  71      * init parameters and form
 
  73      * @param object $request
 
  75     function initAttributes(&$request) {
 
  76         //$submit_flag = $this->isSubmit();
 
  77         $submit_flag = (is_object($request) && ($request->isPost()));
 
  80                 // fill ActionForm and Form from Request
 
  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();
 
  89                 // fill ActionForm from Session
 
  93         // fill Form by ActionForm
 
  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]);
 
 101                 $this->mInitForm = true;
 
 106      * Init custom variables
 
 108      * @param unknown $request
 
 109      * @param unknown $respons
 
 111     function initVariables(&$request, &$respons) {
 
 115     function setVariablesNames($namelist) {
 
 116         $this->mVariables = $namelist;
 
 119     function setAttribute($name,$value) {
 
 122         $this->mValues[$name] = $value;
 
 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);
 
 129                         $this->mForm->mElements[$name]->setValueSafe($value);
 
 134     function getAttribute($name) {
 
 135         return @$this->mValues[$name];
 
 138         function getAttributes() {
 
 139         return $this->mValues;
 
 142     function validate(&$actionMapping, &$request) {
 
 146         function setAttributes($value) {
 
 149         $this->mValues = $value;
 
 150         if (is_array($this->mValues))
 
 151         foreach ($this->mValues as $name=>$value) {
 
 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);
 
 158                                 $this->mForm->mElements[$name]->setValueSafe($value);
 
 165         print_r($this->mValues);
 
 168     function isSubmit() {
 
 170         if (is_object($this->mForm)) {
 
 171             $res = $this->mForm->isSubmit();
 
 176     function saveBean() {
 
 178                 $elements = $this->mForm->getElements();
 
 180                 foreach ($elements as $el) {
 
 181                         $el_list[] = array("name"=>$el->getName(),"class"=>$el->getClass());
 
 183                                 $_SESSION[$this->mSessionCell . "_" . $el->getName()] = $el->getValueSafe();
 
 185                 $_SESSION[$this->mSessionCell . "session_store_elements"] = $el_list;
 
 187         //print_r($_SESSION);
 
 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) {
 
 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()]);
 
 202                                 if ($this->mForm && !isset($this->mForm->mElements[$ref_el["name"]])) {
 
 203                                         $this->mForm->mElements[$ref_el["name"]] = &$el;
 
 205                         $this->mValues[$el->getName()] = $el->getValue();
 
 208                 //print_r($_SESSION);
 
 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"]]);
 
 218         unset($_SESSION[$this->mSessionCell . "session_store_elements"]);
 
 222         return (isset($_SESSION[$this->mSessionCell . "session_store_elements"]) ? true : false);