1: <?php
2:
3: /**
4: * form.php
5: *
6: * PHP version 8
7: *
8: * LICENSE: This program is free software: you can redistribute it and/or modify
9: * it under the terms of the GNU Affero General Public License as
10: * published by the Free Software Foundation, either version 3 of the
11: * License, or (at your option) any later version.
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU Affero General Public License for more details.
16: * You should have received a copy of the GNU Affero General Public License
17: * along with this program. If not, see <http://www.gnu.org/licenses/>.
18: *
19: * @category Login
20: * @package Form
21: * @author Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
22: * @author Henri Schumacher <henri.hulski@gazeta.pl>
23: * @copyright 2007-2014 Henri Schumacher
24: * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License v3
25: * @version 1.0
26: * @link https://research.openhomeo.info/download/OpenHomeopath_1.0.2.tar.gz
27: * @see login.php
28: */
29:
30: /**
31: * The Form class is meant to simplify the task of keeping
32: * track of errors in user submitted forms and the form
33: * field values that were entered correctly.
34: *
35: * @category Login
36: * @package Form
37: * @author Jpmaster77 a.k.a. The Grandmaster of C++ (GMC)
38: * @author Henri Schumacher <henri.hulski@gazeta.pl>
39: * @copyright 2007-2014 Henri Schumacher
40: * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License v3
41: */
42:
43: class Form {
44:
45: /**
46: * Holds submitted form field values
47: * @var array
48: * @access private
49: */
50: private $values = array();
51:
52: /**
53: * Holds submitted form error messages
54: * @var array
55: * @access private
56: */
57: private $errors = array();
58:
59: /**
60: * The number of errors in submitted form
61: * @var integer
62: * @access public
63: */
64: public $num_errors;
65:
66: /**
67: * Class constructor
68: *
69: * Get form value and error arrays, used when there
70: * is an error with a user-submitted form.
71: *
72: * @return Form
73: * @access public
74: */
75: function __construct(){
76: if(isset($_SESSION['value_array']) && isset($_SESSION['error_array'])){
77: $this->values = $_SESSION['value_array'];
78: $this->errors = $_SESSION['error_array'];
79: $this->num_errors = count($this->errors);
80:
81: unset($_SESSION['value_array']);
82: unset($_SESSION['error_array']);
83: }
84: else{
85: $this->num_errors = 0;
86: }
87: }
88:
89: /**
90: * setError - Records new form error given the form
91: * field name and the error message attached to it.
92: *
93: * @param string $field form field
94: * @param string $errmsg error message
95: * @return void
96: * @access public
97: */
98: function setError($field, $errmsg){
99: $this->errors[$field] = $errmsg;
100: $this->num_errors = count($this->errors);
101: }
102:
103: /**
104: * value - Returns the value attached to the given
105: * field, if none exists, the empty string is returned.
106: *
107: * @param string $field form field
108: * @return string field value
109: * @access public
110: */
111: function value($field){
112: if(array_key_exists($field,$this->values)){
113: return htmlspecialchars(stripslashes($this->values[$field]));
114: }else{
115: return "";
116: }
117: }
118:
119: /**
120: * error - Returns the error message attached to the
121: * given field, if none exists, the empty string is returned.
122: *
123: * @param string $field form field
124: * @return string error message
125: * @access public
126: */
127: function error($field){
128: if(array_key_exists($field,$this->errors)){
129: return "<span class='error_message'>".$this->errors[$field]."</span>";
130: }else{
131: return "";
132: }
133: }
134:
135: /**
136: * getErrorArray - Returns the array of error messages
137: *
138: * @return array error messages
139: * @access public
140: */
141: function getErrorArray(){
142: return $this->errors;
143: }
144: }
145: