PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/library/plugins/form/class.form.php

https://github.com/jkroon/Webshop-admin
PHP | 653 lines | 365 code | 207 blank | 81 comment | 60 complexity | c44e8bf2edde4450c52caa2b30e13021 MD5 | raw file
  1. <?php
  2. class formPlugin extends plugin {
  3. protected $data;
  4. protected $validation;
  5. protected $fields;
  6. protected $currentTable = 'Form';
  7. protected $formAttributes;
  8. protected $attributes;
  9. protected $html;
  10. protected $order = false;
  11. protected $outputSent;
  12. protected $ignore;
  13. protected $useOnly;
  14. // De constructor wordt aangemaakt
  15. public function __construct() {
  16. parent::__construct();
  17. parent::loadExternalFile('script', 'form', 'script');
  18. }
  19. // De functie om een model te binden aan het object
  20. public function bindModel($model) {
  21. $this -> model = $model;
  22. }
  23. // De functie voor het meegeven van de post data
  24. public function setData($data) {
  25. $this -> post = $data;
  26. }
  27. // Het aanmaken van het formulier
  28. public function create($table, $attributes=array()) {
  29. if (!isset($attributes['action'])) {
  30. $attributes['action'] = '';
  31. }
  32. if (!isset($attributes['method'])) {
  33. $attributes['method'] = 'post';
  34. }
  35. $this -> currentTable = ucfirst(strtolower(convert_table_name($table, 'join')));
  36. $this -> formAttributes['start'] = $attributes;
  37. }
  38. // Het aanmaken van een input
  39. private function input($name, $attributes='', $table='') {
  40. $table = (empty($table) ? $this -> currentTable : ucfirst(strtolower(convert_table_name($table, 'join'))));
  41. $name = strtolower($name);
  42. $this -> data[$table]['input'][] = $name;
  43. $this -> attributes[$table][$name] = $attributes;
  44. $this -> fields[$table][] = $name;
  45. }
  46. // Het aanmaken van een tekstveld
  47. public function text($name, $attributes='', $table='') {
  48. // Het type attribuut wordt aangemaakt
  49. $attributes['type'] = 'text';
  50. // Er wordt een input van het veld gemaakt
  51. self::input($name, $attributes, $table);
  52. }
  53. // Het aanmaken van een textarea
  54. public function textarea($name, $attributes='', $table='') {
  55. $table = (empty($table) ? $this -> currentTable : ucfirst(strtolower(convert_table_name($table, 'join'))));
  56. $name = strtolower($name);
  57. $this -> data[$table]['textarea'][] = $name;
  58. $this -> attributes[$table][$name] = $attributes;
  59. $this -> fields[$table][] = $name;
  60. }
  61. // Het aanmaken van een hidden field
  62. public function hidden($name, $attributes='', $table='') {
  63. // Het type attribuut wordt aangemaakt
  64. $attributes['type'] = 'hidden';
  65. // Er wordt een input veld aangemaakt
  66. self::input($name, $attributes, $table);
  67. }
  68. // Het aanmaken van een checkbox
  69. public function checkbox($name, $attributes='', $table='') {
  70. // Het type attribuut wordt aangemaakt
  71. $attributes['type'] = 'checkbox';
  72. // Er wordt bekeken of de checkbox standaard op true moet staan
  73. if (isset($attributes['defaultChecked']) && !$this -> post) {
  74. $attributes['checked'] = 'checked';
  75. unset($attributes['defaultChecked']);
  76. }
  77. // Er wordt een veld aangemaakt
  78. self::input($name, $attributes, $table);
  79. }
  80. // Het aanmaken van een selectieformulier
  81. public function select($name, $attributes='', $table='') {
  82. $table = (empty($table) ? $this -> currentTable : ucfirst(strtolower(convert_table_name($table, 'join'))));
  83. $name = strtolower($name);
  84. // De velden worden aan de array toegevoegd
  85. $this -> data[$table]['select'][] = $name;
  86. $this -> attributes[$table][$name] = $attributes;
  87. $this -> fields[$table][] = $name;
  88. }
  89. // De automatische genereer functie voor een tabel
  90. public function auto_generate($table) {
  91. // De tabelnaam wordt naar enkelvoud geparsed
  92. $table = safe(convert_table_name($table, 'outer_join'));
  93. // De current tabel wordt ingesteld
  94. $this -> currentTable = ucfirst(strtolower(convert_table_name($table, 'join')));
  95. // De query wordt uitgevoerd
  96. $result = parent::query("DESCRIBE `". $table ."`");
  97. // Alle velden worden door de check heen gehaald
  98. while($var = mysql_fetch_assoc($result)) {
  99. // De waarden worden in een variable geplaatst
  100. $field_name = $var['Field'];
  101. $data_type = $var['Type'];
  102. // Het data_type wordt bepaald
  103. $length = preg_match("/\((.*)\)/", $data_type, $matches);
  104. if ($length) {
  105. $data_type = preg_replace("/\((.*)\)/", "", $data_type);
  106. $length = $matches[1];
  107. }
  108. // De array voor de atributen wordt leeg gemaakt
  109. $attributes = array();
  110. // Alle velden zoals smallint, mediumint, bigint en int worden 1 gemaakt
  111. if (preg_match("/^(tinyint|smallint|mediumint|bigint|int)$/", $data_type)) {
  112. $data_type = 'int';
  113. }
  114. // Er wordt gekeken of er een associatie aanwezig is
  115. if (preg_match("/_id$/", $field_name)) {
  116. $explode = explode('_', $field_name);
  117. $table_name = convert_table_name($explode[0], 'outer_join');
  118. // Er wordt gecontroleerd of de tabel bestaat
  119. $query = "SELECT * FROM `". safe($table_name) ."`";
  120. $result1 = plugin::query($query, false);
  121. if ($result1) {
  122. // Er wordt bekeken welke velden moeten worden opgehaald
  123. $query = "DESCRIBE `". safe($table_name) ."`";
  124. $result1 = plugin::query($query, false);
  125. while($var1 = mysql_fetch_assoc($result1)) {
  126. if (preg_match('/varchar/', $var1['Type'])) {
  127. $name_field = $var1['Field'];
  128. break;
  129. }
  130. }
  131. $data_type = 'select';
  132. $attributes['auto_select'] = array($table_name, 'id', $name_field);
  133. }
  134. }
  135. // Primaire sleutels worden genegeerd
  136. if ($var['Key'] == 'PRI') {
  137. $data_type = '';
  138. }
  139. // Aan de hand van het datatype wordt het uiteindelijke input veld bepaald
  140. switch($data_type) {
  141. case "varchar":
  142. $attributes['maxlength'] = $length;
  143. self::text($field_name, $attributes, $table);
  144. break;
  145. case "int":
  146. $attributes['maxlength'] = $length;
  147. $attributes['validate'] = 'isDigit';
  148. self::text($field_name, $attributes, $table);
  149. break;
  150. case "decimal":
  151. $attributes['validate'] = $var['Type'];
  152. self::text($field_name, $attributes, $table);
  153. break;
  154. case "select":
  155. self::select($field_name, $attributes, $table);
  156. break;
  157. }
  158. }
  159. }
  160. public function end($attributes) {
  161. if (!is_array($attributes)) {
  162. $value = $attributes;
  163. $attributes = array();
  164. $attributes['type'] = 'submit';
  165. $attributes['value'] = $value;
  166. }
  167. // De verschillende attributen worden geladen
  168. if (!isset($attributes['type'])) {
  169. $attributes['type'] = 'submit';
  170. $attributes['value'] = 'Verzenden';
  171. }
  172. $this -> formAttributes['end'] = $attributes;
  173. }
  174. public function createOrder($array) {
  175. foreach($array as $table=>$fields) {
  176. $table = ucfirst(strtolower(convert_table_name($table, 'join')));
  177. $this -> order[$table] = $fields;
  178. }
  179. }
  180. public function prepare() {
  181. // Het formulier wordt gegenereerd
  182. if (is_array($this -> data)) {
  183. foreach($this -> data as $table=>$table_content) {
  184. foreach($table_content as $type=>$array) {
  185. // Er wordt bekeken of er enkel opgegeven velden mogen worden gebruikt
  186. if (isset($this -> useOnly[$table]) && count($this -> useOnly[$table]) > 0) {
  187. $use_fields = $this -> useOnly[$table];
  188. $all_fields = $this -> fields[$table];
  189. foreach($all_fields as $all_field) {
  190. if (!in_array($all_field, $use_fields)) {
  191. $this -> ignore[$table][] = $all_field;
  192. }
  193. }
  194. }
  195. // Er wordt gekeken of er een POST aanvraag is, zo ja dan worden de values geladen
  196. if ($this -> post) {
  197. if (array_key_exists($table, $this -> post)) {
  198. foreach($this -> post[$table] as $field=>$value) {
  199. $this -> attributes[$table][$field]['value'] = $value;
  200. }
  201. }
  202. }
  203. // Er wordt bekeken of er fouten aanwezig zijn
  204. if ($this -> model && $this -> model -> validateErrors) {
  205. $errors = $this -> model -> validateErrors;
  206. if (array_key_exists($table, $errors)) {
  207. foreach($errors[$table] as $field=>$error) {
  208. $this -> attributes[$table][$field]['error'] = $error;
  209. }
  210. }
  211. }
  212. // Er wordt bekeken of er velden genegeerd moeten worden
  213. if (isset($this -> ignore[$table])) {
  214. $ignore_fields = $this -> ignore[$table];
  215. foreach($ignore_fields as $ignore_field) {
  216. $array_key = array_search($ignore_field, $array);
  217. if ($array_key !== false) {
  218. unset($array[$array_key]);
  219. }
  220. }
  221. }
  222. // De INPUT velden worden ingeladen
  223. if ($type == 'input') {
  224. foreach($array as $field) {
  225. // De attributen worden in een string geplaatst
  226. $attributes = $this -> attributes[$table][$field];
  227. // Een eventuele foutmelding wordt gecreëerd
  228. if (array_key_exists('error', $attributes)) {
  229. $errorClass = ' formError';
  230. $errorMsg = $attributes['error'];
  231. } else {
  232. $errorClass = '';
  233. $errorMsg = false;
  234. }
  235. // De variable html wordt opnieuw aangemaakt
  236. $html = '<div id="div'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'" class="formDivPlaceholder'. $errorClass .'"><div class="formDivPlaceholderIn">';
  237. // Een eventueel label wordt aangemaakt
  238. if (array_key_exists('label', $attributes)) {
  239. $html .= '<label id="label'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'">' . $attributes['label'] . '</label>';
  240. unset($attributes['label']);
  241. }
  242. // Indien er sprake is van een checkbox, wordt deze gechecked indien nodig
  243. if ($attributes['type'] == 'checkbox' && $this -> post[$table]) {
  244. if (!empty($this -> post[$table][$field])) {
  245. $attributes['checked'] = 'checked';
  246. } else {
  247. if (isset($attributes['checked'])) {
  248. unset($attributes['checked']);
  249. }
  250. }
  251. }
  252. // Het inputveld wordt aangemaakt
  253. $html .= '<input name="post['. ucfirst(strtolower($table)) .']['. strtolower($field) .']"
  254. id="input'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'"';
  255. foreach($attributes as $name=>$value) {
  256. $html .= ' ' . $name . '="' . $value . '"';
  257. }
  258. $html .= ' />';
  259. // Een eventuele foutmelding wordt weergegeven
  260. if ($errorMsg) {
  261. $html .= '<span>'. $errorMsg .'</span>';
  262. }
  263. // De division (formDivPlaceholder) wordt afgesoten
  264. $html .= '</div></div>';
  265. // Het veld wordt in de centrate array geplaatst
  266. $this -> html[ucfirst(strtolower($table))][strtolower($field)] = $html;
  267. }
  268. }
  269. // De textarea velen worden gegenereerd
  270. if ($type == 'textarea') {
  271. foreach($array as $field) {
  272. // De data wordt aangemaakt
  273. //$table = ucfirst(strtolower($table));
  274. $attributes = $this -> attributes[$table][$field];
  275. // De variable html wordt opnieuw aangemaakt
  276. $html = '<div id="div'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'" class="formDivPlaceholder"><div class="formDivPlaceholderIn">';
  277. // Een eventueel label wordt aangemaakt
  278. if (array_key_exists('label', $attributes)) {
  279. $html .= '<label id="label'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'">' . $attributes['label'] . '</label>';
  280. unset($attributes['label']);
  281. }
  282. if (array_key_exists('value', $attributes)) {
  283. $content = $attributes['value'];
  284. unset($attributes['value']);
  285. } else {
  286. $content = '';
  287. }
  288. // De textarea wordt aangemaakt
  289. $html .= '<textarea name="post['. ucfirst(strtolower($table)) .']['. strtolower($field) .']" ';
  290. foreach($attributes as $key=>$value) {
  291. $html .= $key . '="'. $value .'" ';
  292. }
  293. $html .= '>';
  294. $html .= $content;
  295. $html .= '</textarea>';
  296. // De divisions worden gesloden
  297. $html .= '</div></div>';
  298. // De HTML code wordt in de HTML array gepushed
  299. $this -> html[ucfirst(strtolower($table))][strtolower($field)] = $html;
  300. }
  301. }
  302. // De SELECT velden worden gegenereerd
  303. if ($type == 'select') {
  304. foreach($array as $field) {
  305. // De tabel wordt klaargemaakt
  306. $table = ucfirst(strtolower($table));
  307. // De attributen worden in een string geplaatst
  308. $attributes = $this -> attributes[$table][$field];
  309. // De variable html wordt opnieuw aangemaakt
  310. $html = '<div id="div'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'" class="formDivPlaceholder"><div class="formDivPlaceholderIn">';
  311. // Een eventueel label wordt aangemaakt
  312. if (array_key_exists('label', $attributes)) {
  313. $html .= '<label id="label'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'">' . $attributes['label'] . '</label>';
  314. unset($attributes['label']);
  315. }
  316. // De attributen worden in een string gezet
  317. $attributes = $this -> attributes[$table][$field];
  318. // Het select element wordt aangemaakt
  319. $html .= '<select name="post['. ucfirst(strtolower($table)) .']['. strtolower($field) .']" id="input'. ucfirst(strtolower($table)) . ucfirst(strtolower($field)) .'" ';
  320. foreach($attributes as $attrName=>$attrVal) {
  321. $html .= $attrName . '="'. $attrVal .'" ';
  322. }
  323. $html .= '>';
  324. // Er wordt bekeken of auto_select van toepassing is
  325. if (array_key_exists('auto_select', $attributes)) {
  326. $info = $attributes['auto_select'];
  327. // De opties worden ingeladen
  328. $query = "SELECT `". safe($info[1]) ."`, `". safe($info[2]) ."` FROM `". safe($info[0]) ."`";
  329. $result = plugin::query($query);
  330. $options = '';
  331. while($option = mysql_fetch_assoc($result)) {
  332. $attributes['options'][$option[$info1]] = $option[$info[2]];
  333. }
  334. }
  335. if (array_key_exists('options', $attributes) && count($attributes['options']) > 0) {
  336. if ($this -> post && isset($this -> post[ucfirst(strtolower($table))][$field])) {
  337. $id = $this -> post[ucfirst(strtolower($table))][$field];
  338. } else {
  339. $id = false;
  340. }
  341. foreach($attributes['options'] as $optKey=>$optVal) {
  342. if ($id == $optKey) {
  343. $html .= '<option value="'. $optKey .'" selected>'. $optVal .'</option>';
  344. } else {
  345. $html .= '<option value="'. $optKey .'">'. $optVal .'</option>';
  346. }
  347. }
  348. }
  349. // Het selectiemenu wordt afgesloten
  350. $html .= '</select>';
  351. // Het division element wordt afgesloten
  352. $html .= '</div></div>';
  353. // De HTML code wordt in de HTML array gepushed
  354. $this -> html[ucfirst(strtolower($table))][strtolower($field)] = $html;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. }
  361. public function setAttribute($option, $array) {
  362. foreach($array as $table=>$fields) {
  363. // De tabelnaam wordt juist geconverteerd
  364. $table = ucfirst(strtolower(convert_table_name($table, 'join')));
  365. foreach($fields as $name=>$value) {
  366. $name = strtolower($name);
  367. if (isset($this -> attributes[$table][$name])) {
  368. $this -> attributes[$table][$name][$option] = $value;
  369. }
  370. }
  371. }
  372. }
  373. public function useOnly($array) {
  374. foreach($array as $table=>$fields) {
  375. $table = ucfirst(strtolower(convert_table_name($table, 'join')));
  376. $this -> useOnly[$table] = $fields;
  377. // De position tabel wordt aangemaakt indien deze niet bestaat
  378. if (!$this -> order | empty($this -> order)) {
  379. foreach($fields as $field) {
  380. $this -> order[$table][] = $field;
  381. }
  382. }
  383. }
  384. }
  385. public function ignore($array) {
  386. // De geselecteerde velden worden toegevoegd aan de ignore array
  387. foreach($array as $table=>$fields) {
  388. $table = ucfirst(strtolower(convert_table_name($table, 'join')));
  389. $this -> ignore[$table] = $fields;
  390. }
  391. }
  392. public function getOutput() {
  393. // De prive functie prepare wordt uitgevoerd om de array in HTML te converteren
  394. self::prepare();
  395. // De return variable wordt aangemaakt
  396. $return = '';
  397. // De formulier tag wordt geöpend
  398. if (isset($this -> formAttributes['start'])) {
  399. $return .= '<form';
  400. foreach($this -> formAttributes['start'] as $key=>$value) {
  401. $return .= ' ' . $key . '="'. $value .'"';
  402. }
  403. $return .= '><fieldset>';
  404. unset($this -> formAttributes['start']);
  405. }
  406. if (is_array($this -> order)) {
  407. foreach($this -> order as $table=>$fields) {
  408. foreach($fields as $field) {
  409. if (isset($this -> html[ucfirst(strtolower($table))][strtolower($field)])) {
  410. $return .= $this -> html[ucfirst(strtolower($table))][strtolower($field)];
  411. unset($this -> html[ucfirst(strtolower($table))][strtolower($field)]);
  412. }
  413. }
  414. }
  415. }
  416. // Indien er velden niet gepositioneerd stonden, worden deze weergegeven
  417. if (is_array($this -> html)) {
  418. foreach($this -> html as $table=>$fields) {
  419. foreach($fields as $field) {
  420. $return .= $field;
  421. }
  422. }
  423. }
  424. // Er wordt gekeken of het formulier al afgesloten moet worden
  425. if (isset($this -> formAttributes['end'])) {
  426. // Het formulier wordt afgesloten
  427. $return .= '<div id="divFormSubmit" class="formDivPlachHolder"><input ';
  428. foreach($this -> formAttributes['end'] as $key=>$value) {
  429. $return .= ' ' . $key . '="'. $value .'"';
  430. }
  431. $return .= ' /></div></fieldset></form>';
  432. }
  433. // De code wordt opgeschoond
  434. self::cleanUp();
  435. // De uiteindelijke data wordt geretouneerd
  436. return $return;
  437. }
  438. private function cleanUp() {
  439. // De variablen worden leeggemaakt
  440. $fields = array('html', 'data', 'fields', 'validation', 'attributes', 'order', 'ignore', 'useOnly');
  441. // Alle variablen worden leeggemaakt
  442. foreach($fields as $field) {
  443. $this -> {$field} = '';
  444. }
  445. }
  446. }