PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/mu-plugins/ivycat_plugin/lib/form.class.php

https://bitbucket.org/anneivycat/ebcookhouse
PHP | 294 lines | 237 code | 27 blank | 30 comment | 15 complexity | 976f4fbe9c4f86d03558221a2d7f5b6b MD5 | raw file
  1. <?php
  2. require_once 'validate.class.php';
  3. /**
  4. * Validation "controller" of sorts. Almost a Factory class with some common functions. This is the front door for any controller to
  5. * access a set of validation rules and methods for a particular form.
  6. **/
  7. class FormCreate{
  8. protected $form_data;
  9. protected $inputs;
  10. protected $post_status;
  11. protected $validate;
  12. /**
  13. * Main constructor method: requires the library to be called (string)
  14. **/
  15. public function __construct(){
  16. $this->validate = new ValidateForm();
  17. $this->post_status = ( $_SERVER['REQUEST_METHOD'] == "POST" ) ? true : false;
  18. }
  19. public function new_field( $type, $name, $atts ){
  20. $field = 'add_' . $type . '_field';
  21. $this->inputs[] = self::$field( $name, $atts );
  22. }
  23. public function new_fields( array $fields ){
  24. foreach( $fields as $field ){
  25. self::new_field( $field['type'], $field['name'], $field['atts'] );
  26. }
  27. return true;
  28. }
  29. public function process_form( $data ){
  30. if( !$this->post_status ) return;
  31. $this->inputs = $this->validate->validate_array( $this->inputs );
  32. foreach( $this->inputs as $key=>$value ){
  33. if( isset( $value['error'] ) ){
  34. $this->inputs[$key]['output'] .= '<div class="error">'.$value['error'].'</div>';
  35. }
  36. }
  37. return $this->inputs;
  38. }
  39. /**
  40. * Returns the scrubbed data back - essentially in the form of the POST data.
  41. * (note: form elements not in the validation array will not be passed along this is an exclusive method )
  42. **/
  43. public function get_data(){
  44. $data = array();
  45. foreach($this->inputs as $k=>$v){
  46. if( is_array( $v ) ) $data[$k] = $v['value'];
  47. }
  48. //fprint_r( $this->inputs );
  49. return $data;
  50. }
  51. /**
  52. * Returns the formdata (this has labels, rules and data)
  53. **/
  54. public function all_formdata(){
  55. return $this->inputs;
  56. }
  57. /**
  58. * returns one record
  59. **/
  60. public function get_field($key){
  61. return $this->data[$key];
  62. }
  63. /**
  64. * A test to see if there are errors. TRUE means there is an error!
  65. **/
  66. public function has_errors(){
  67. return $this->validate->has_errors();
  68. }
  69. /**
  70. * Shows the errors for a given field
  71. **/
  72. public function show_errors( $field, $label = false ){
  73. if($error = $this->form_data[$field]['error']){
  74. return '<p class="error">'.$error.'</p>';
  75. }
  76. return ( $label ) ? $label : "";
  77. }
  78. /**
  79. * set the value of an element in form_data
  80. **/
  81. public function set_value_post($field){
  82. return $this->form_data[$field]['value'];
  83. }
  84. /**
  85. * Adds a validation rule only.
  86. */
  87. public function add_validation_rule( $name, $label, $rules='', $scrub='', $value='' ){
  88. $this->inputs[$name] = array(
  89. 'label' => $label,
  90. 'rules' => $rules,
  91. 'scrub' => $scrub,
  92. 'value' => ( isset( $_POST[$name] ) ) ? $_POST[$name] : $value
  93. );
  94. }
  95. public function add_validation_rules( array $rules ){
  96. foreach( $rules as $rule ){
  97. self::add_validation_rule( $rule['name'], $rule['label'], $rule['rules'], $rule['scrub'], $rule['value'] );
  98. }
  99. }
  100. public function output_field( $name ){
  101. return $this->inputs[$name]['output'];
  102. }
  103. protected function add_text_field( $name, $atts ){
  104. $value = ( $atts['default'] ) ? $atts['default'] : ' ';
  105. $value = ( isset( $_POST[$name] ) ) ? $_POST[$name] : $value;
  106. $label = isset( $atts['label'] ) ? trim($atts['label']) : trim( ucwords( str_replace( '-', ' ', $name ) ) );
  107. $labelclass = ( isset( $atts['rules'] ) && preg_match( '~required~', $atts['rules'] ) ) ? ' class="required"' : '';
  108. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  109. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  110. $output = '<label for="'.$id.'"'.$labelclass.'>'.$label.'</label>
  111. <input id="'.$id.'"'.$class.' type="text" name="'.trim($name).'" value="'.trim($value).'"/>';
  112. self::set_input( $name, $label, $value, $atts, $output );
  113. }
  114. protected function add_textarea_field( $name, $atts ){
  115. $value = ( isset( $_POST[$name] ) ) ? trim($_POST[$name]) : ' ';
  116. $label = isset( $atts['label'] ) ? trim($atts['label']) : trim( ucwords( str_replace( '-', ' ', $name ) ) );
  117. $labelclass = ( isset( $atts['rules'] ) && preg_match( '~required~', $atts['rules'] ) ) ? ' class="required"' : '';
  118. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  119. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  120. $output = '<label for="'.$id.'"'.$labelclass.'>'.$label.'</label>
  121. <textarea id="'.$id.'"'.$class.' type="text" name="'.trim($name).'">'.trim($value).'</textarea>';
  122. self::set_input( $name, $label, $value, $atts, $output );
  123. }
  124. protected function add_editor_field( $name, $atts ){
  125. }
  126. protected function add_checkbox_field( $name, $atts ){
  127. $value = ( array ) ( $atts['default'] ) ? $atts['default'] : ' ';
  128. $value = ( isset( $_POST[$name] ) ) ? (array)$_POST[$name] : $value;
  129. $label = isset( $atts['label'] ) ? trim($atts['label']) : trim( ucwords( str_replace( '-', ' ', $name ) ) );
  130. $labelclass = ( isset( $atts['rules'] ) && preg_match( '~required~', $atts['rules'] ) ) ? ' class="required"' : '';
  131. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  132. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  133. $itr = 1;
  134. $output = "<ul class='multiple-select'>";
  135. $output .= ( !$atts['hide-label'] ) ? $atts['label'] : '';
  136. foreach( $atts['options'] as $kval => $kname ):
  137. $val = ( is_numeric( $kval ) ) ? $kval : trim( $kval );
  138. if( is_array( $value ) )$checked = ( in_array( $kval, $value ) ) ? ' checked="checked"' : '';
  139. $output .= '<li><input id="'.$id.'-'.$itr.'"'.$class.' type="checkbox" name="'.trim($name).'[]" value="'.$val.'"'.$checked.'/>
  140. <label for="'.$id.'-'.$itr.'"'.$labelclass.'>'.$kname.'</label></li>';
  141. $itr++;
  142. endforeach;
  143. $output .= "</ul>";
  144. self::set_input( $name, $label, $value, $atts, $output );
  145. }
  146. protected function add_radio_field( $name, $atts ){
  147. $default = ( $atts['default'] ) ? $atts['default'] : ' ';
  148. $value = ( isset( $_POST[$name] ) ) ? trim( $_POST[$name] ) : $default;
  149. $label = isset( $atts['label'] ) ? trim($atts['label']) : trim( ucwords( str_replace( '-', ' ', $name ) ) );
  150. $labelclass = ( isset( $atts['rules'] ) && preg_match( '~required~', $atts['rules'] ) ) ? ' class="required"' : '';
  151. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  152. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  153. $itr = 1;
  154. $output = "<ul>";
  155. $output .= ( !$atts['hide-label'] ) ? '<li>'.$atts['label'].'</li>' : '';
  156. foreach( $atts['options'] as $kval => $kname ):
  157. $val = ( is_numeric( $kval ) ) ? $kval : trim( $kval );
  158. $checked = ( $value == $val ) ? ' checked="checked"' : '';
  159. $output .= '<li><input id="'.$id.'-'.$itr.'"'.$class.' type="radio" name="'.trim($name).'" value="'.$val.'"'.$checked.'/>
  160. <label for="'.$id.'-'.$itr.'"'.$labelclass.'>'.$kname.'</label></li>';
  161. $itr++;
  162. endforeach;
  163. $output .= "</ul>";
  164. self::set_input( $name, $label, $value, $atts, $output );
  165. }
  166. protected function add_dropdown_field( $name, $atts ){
  167. $value = ( isset( $_POST[$name] ) ) ? trim($_POST[$name] ) : ' ';
  168. $label = isset( $atts['label'] ) ? trim($atts['label']) : trim( ucwords( str_replace( '-', ' ', $name ) ) );
  169. $labelclass = ( isset( $atts['rules'] ) && preg_match( '~required~', $atts['rules'] ) ) ? ' class="required"' : '';
  170. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  171. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  172. $output = '<label for="'.$id.'"'.$labelclass.'>'.$label.'</label>';
  173. $output .= '<select id="'.$id.'"'.$class.' type="text" name="'.trim($name).'" value="'.trim($value).'">';
  174. foreach( $atts['options'] as $kval=>$kname ):
  175. $selected = ( $value == $kval ) ? ' selected="selected"' : '';
  176. $output .= '<option value="'.$abbr.'"'.$selected.'>'.$state.'</option>';
  177. endforeach;
  178. $output .= '</select>';
  179. self::set_input( $name, $label, $value, $atts, $output );
  180. }
  181. protected function add_hidden_field( $name, $atts ){
  182. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  183. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  184. $output = '<input id="'.$id.'"'.$class.' type="hidden" name="'.trim($name).'" value="'.trim( $atts['value'] ).'"/>';
  185. self::set_input( $name, $label, $atts['value'], $atts, $output );
  186. }
  187. protected function add_state_field( $name, $atts ){
  188. $value = ( isset( $_POST[$name] ) ) ? trim($_POST[$name]) : ' ';
  189. $label = isset( $atts['label'] ) ? trim($atts['label']) : trim( ucwords( str_replace( '-', ' ', $name ) ) );
  190. $labelclass = ( isset( $atts['rules'] ) && preg_match( '~required~', $atts['rules'] ) ) ? ' class="required"' : '';
  191. $id = ( $atts['id'] ) ? trim($atts['id']) : 'input-'.trim($name);
  192. $class = ( $atts['class'] ) ? ' class="'.$atts['class'].'"' : '';
  193. $output = '<label for="'.$id.'"'.$labelclass.'>'.$label.'</label>';
  194. $output .= '<select id="'.$id.'"'.$class.' type="text" name="'.trim($name).'" value="'.trim($value).'">';
  195. foreach( self::states() as $abbr=>$state ):
  196. $selected = ( $abbr == $value ) ? ' selected="selected"' : '';
  197. $output .= '<option value="'.$abbr.'"'.$selected.'>'.$state.'</option>';
  198. endforeach;
  199. $output .= '</select>';
  200. self::set_input( $name, $label, $value, $atts, $output );
  201. }
  202. protected function set_input( $name, $label, $value, $atts, $output ){
  203. $this->inputs[$name] = array(
  204. 'label' => $label,
  205. 'rules' => isset( $atts['rules'] ) ? $atts['rules'] : '',
  206. 'scrub' => isset( $atts['scrub'] ) ? $atts['scrub'] : '',
  207. 'value' => $value,
  208. 'atts' => $atts,
  209. 'output' => $output
  210. );
  211. }
  212. protected function states(){
  213. return array(
  214. "AL"=>"Alabama",
  215. "AK"=>"Alaska",
  216. "AZ"=>"Arizona",
  217. "AR"=>"Arkansas",
  218. "CA"=>"California",
  219. "CO"=>"Colorado",
  220. "CT"=>"Connecticut",
  221. "DE"=>"Deleware",
  222. "DC"=>"District of Columbia",
  223. "FL"=>"Florida",
  224. "GA"=>"Georgia",
  225. "HI"=>"Hawaii",
  226. "ID"=>"Idaho",
  227. "IL"=>"Illinois",
  228. "IN"=>"Indiana",
  229. "IA"=>"Iowa",
  230. "KS"=>"Kansas",
  231. "KY"=>"Kentuky",
  232. "LA"=>"Louisiana",
  233. "ME"=>"Maine",
  234. "MD"=>"Maryland",
  235. "MA"=>"Massachusetts",
  236. "MI"=>"Michigan",
  237. "MN"=>"Minnesota",
  238. "MS"=>"Mississippi",
  239. "MO"=>"Missouri",
  240. "MT"=>"Montana",
  241. "NE"=>"Nebraska",
  242. "NV"=>"Nevada",
  243. "NH"=>"New Hampshire",
  244. "NJ"=>"New Jersey",
  245. "NM"=>"New Mexico",
  246. "NY"=>"New York",
  247. "NC"=>"North Carolina",
  248. "ND"=>"North Dakota",
  249. "OH"=>"Ohio",
  250. "OK"=>"Oklahoma",
  251. "OR"=>"Oregon",
  252. "PA"=>"Pennsylvania",
  253. "RI"=>"Rhode Island",
  254. "SC"=>"South Carolina",
  255. "SD"=>"South Dakota",
  256. "TN"=>"Tennessee",
  257. "TX"=>"Texas",
  258. "UT"=>"Utah",
  259. "VT"=>"Vermont",
  260. "VA"=>"Virginia",
  261. "WA"=>"Washington",
  262. "WV"=>"West Virginia",
  263. "WI"=>"Wisconsin",
  264. "WY"=>"Wyoming"
  265. );
  266. }
  267. }