ff7b3b28ae942eb4ccfbe0fadf6bf8ecd9ccd714
[timetracker.git] / WEB-INF / lib / form / FormElement.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 // FromElement is the base class for controls on forms.
30 class FormElement {
31   var $id = '';         // Control id.
32   var $name;            // Control name.
33   var $form_name = '';  // Form name the control is in.
34   var $value = '';      // Value of the control.
35   var $size = '';       // Control size.
36   var $max_length = ''; // Max length of text in control.
37   var $on_change = '';  // What happens when value of control changes.
38   var $on_click = '';   // What happens when the control is clicked.
39   var $label = '';      // Optional label for control.
40   var $style = '';      // Control style.
41   var $enabled = true;  // Whether the control is enabled.
42   var $class = 'FormElement'; // PHP class name for the element.
43   var $cssClass = null; // CSS class name for the element.
44
45   function __construct() {
46   }
47
48   function getName() { return $this->name; }
49   function getClass() { return $this->class; }
50   function getCssClass() { return $this->cssClass; }
51   function setCssClass($cssClass) { $this->cssClass = $cssClass; }
52
53   function setFormName($name) { $this->form_name = $name; }
54   function getFormName() { return $this->form_name; }
55
56   function setValue($value) { $this->value = $value; }
57   function getValue() { return $this->value; }
58
59   // Safe function variations are used to store/read values in/from user session for further reuse.
60   // They may convert data in derived classes to some standard form. For example, floats are stored
61   // with a dot delimiter (not comma), and dates are stored in DB_DATEFORMAT.
62   // This allows to reuse data in session even when user changes the deliminter or date format.
63   function setValueSafe($value) { $this->value = $value;}
64   function getValueSafe() { return $this->value; }
65
66   function setId($id) { $this->id = $id; }
67   function getId() { return $this->id; }
68
69   function setSize($value) { $this->size = $value; }
70   function getSize() { return $this->size; }
71
72   function setLabel($label) { $this->label = $label; }
73   function getLabel() { return $this->label; }
74
75   function setMaxLength($value) { $this->max_length = $value; }
76   function getMaxLength() { return $this->max_length; }
77
78   function setStyle($value) { $this->style = $value; }
79   function getStyle() { return $this->style; }
80
81   function setEnabled($flag) { $this->enabled = $flag; }
82   function isEnabled() { return $this->enabled; }
83
84   function setOnChange($str) { $this->on_change = $str; }
85   function setOnClick($str) { $this->on_click = $str; }
86
87   function localize($i18n) {} // Localization occurs in derived classes and is dependent on control type.
88                               // For example, in calendar control we need to localize day and month names.
89
90   // getHtml returns HTML for the element.
91   function getHtml() { return ''; }
92
93   // getLabelHtml returns HTML code for element label.
94   function getLabelHtml() { return '<label for="'.$this->id.'">'.$this->label.'</label>'; }
95
96   function toArray() {
97     return array(
98       'label'=>$this->getLabelHtml(),
99       'control'=>$this->getHtml());
100   }
101 }