PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/zan/helpers/forms.php

https://github.com/triartdesign/ZanPHP
PHP | 431 lines | 428 code | 0 blank | 3 comment | 1 complexity | edf670a7b5ccac2316ac4b99c7ab23a6 MD5 | raw file
  1. <?php
  2. /**
  3. * Access from index.php:
  4. */
  5. if(!defined("_access")) {
  6. die("Error: You don't have permission to access here...");
  7. }
  8. /**
  9. * Forms Helper
  10. *
  11. *
  12. *
  13. * @package ZanPHP
  14. * @subpackage Core
  15. * @category Helpers
  16. * @author MilkZoft
  17. * @link http://www.milkzoft.com
  18. */
  19. /**
  20. * formCheckbox
  21. *
  22. * Sets a specific <input /> type Checkbox tag and its attributes
  23. *
  24. * @param string $text = NULL
  25. * @param string $position = "Right"
  26. * @param string $name
  27. * @param string $value
  28. * @param string $ID = NULL
  29. * @param boolean $checked = FALSE
  30. * @param string $events = NULL
  31. * @param boolean $disabled = FALSE
  32. * @return string value
  33. */
  34. function formCheckbox($attributes = FALSE) {
  35. if(isset($attributes) and is_array($attributes)) {
  36. $attrs = NULL;
  37. foreach($attributes as $attribute => $value) {
  38. if($attribute !== "position" and $attribute !== "text" and $attribute !== "type" and $attribute !== "checked") {
  39. $attrs .= ' '. strtolower($attribute) .'="'. encode($value) .'"';
  40. } else {
  41. $$attribute = encode($value);
  42. }
  43. }
  44. if(isset($checked) and $checked) {
  45. $check = ' checked="checked"';
  46. } else {
  47. $check = NULL;
  48. }
  49. if(isset($position) and $position === "left" and isset($text)) {
  50. return $text .' <input'. $attrs .' type="checkbox"'. $check .' />';
  51. } elseif(isset($position) and $position === "right" and isset($text)) {
  52. return '<input'. $attrs .' type="checkbox"'. $check .' /> '. $text;
  53. } elseif(isset($text)) {
  54. return $text .' <input'. $attrs .' type="checkbox"'. $check .' />';
  55. } else {
  56. return '<input'. $attrs .' type="checkbox"'. $check .' />';
  57. }
  58. } else {
  59. return NULL;
  60. }
  61. }
  62. /**
  63. * formClose
  64. *
  65. * Closes a Basic Form structure
  66. *
  67. * @returns string $HTML
  68. */
  69. function formClose() {
  70. $HTML = "\t" . "</fieldset>" . "\n";
  71. $HTML .= "</form>";
  72. return $HTML;
  73. }
  74. function formField($a = NULL, $text, $raquo = TRUE) {
  75. $raquo = ($raquo === TRUE) ? "&raquo; " : "";
  76. if(!is_null($a)) {
  77. $HTML = '<p class="field">' . "\n";
  78. $HTML .= "\t" . '<a '. $a .'>'. $raquo . $text .'</a>' . "\n";
  79. $HTML .= '</p>' . "\n";
  80. } else {
  81. $HTML = '<p class="field">' . "\n";
  82. $HTML .= "\t" . $raquo . $text . "\n";
  83. $HTML .= '</p>' . "\n";
  84. }
  85. return $HTML;
  86. }
  87. /**
  88. * formInput
  89. *
  90. * Sets an <input /> tag with a custom attributes.
  91. *
  92. * @param mixed $p = "Yes"
  93. * @param string $text = NULL
  94. * @param string $name = NULL
  95. * @param string $value = NULL
  96. * @param string $class = "input"
  97. * @param string $type = "text"
  98. * @param string $ID = NULL
  99. * @param string $events = NULL
  100. * @param string $src = NULL
  101. * @param boolean $raquo = NULL
  102. * @returns string $HTML
  103. */
  104. function formInput($attributes = FALSE) {
  105. if(isset($attributes) and is_array($attributes)) {
  106. $attrs = NULL;
  107. foreach($attributes as $attribute => $value) {
  108. if($attribute === "required") {
  109. $attrs .= ' required ';
  110. } elseif($attribute === "events") {
  111. $attrs .= ' '. $value .' ';
  112. } elseif($attribute !== "type" and $attribute !== "p" and $attribute !== "field") {
  113. $attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
  114. } else {
  115. $$attribute = $value;
  116. }
  117. }
  118. if(isset($type)) {
  119. if($type === "text") {
  120. $HTML = '<input'. $attrs .' type="text" /> ' . "\n";
  121. } elseif($type === "password") {
  122. $HTML = '<input'. $attrs .' type="password" /> ' . "\n";
  123. } elseif($type === "submit") {
  124. $HTML = '<input'. $attrs .' type="submit" /> ' . "\n";
  125. } elseif($type === "button") {
  126. $HTML = '<input'. $attrs .' type="button" /> ' . "\n";
  127. } elseif($type === "checkbox") {
  128. $HTML = '<input'. $attrs .' type="checkbox" /> ' . "\n";
  129. } elseif($type === "radio") {
  130. $HTML = '<input'. $attrs .' type="radio" /> ' . "\n";
  131. } elseif($type === "file") {
  132. $HTML = '<input'. $attrs .' type="file" /> ' . "\n";
  133. } elseif($type === "hidden") {
  134. $HTML = '<input'. $attrs .' type="hidden" /> ' . "\n";
  135. } elseif($type === "image") {
  136. $HTML = '<input'. $attrs .' type="image" /> ' . "\n";
  137. } elseif($type === "reset") {
  138. $HTML = '<input'. $attrs .' type="reset" /> ' . "\n";
  139. } else {
  140. $HTML = '<input'. $attrs .' type="text" /> ' . "\n";
  141. }
  142. } else {
  143. $HTML = '<input'. $attrs .' type="text" /> ' . "\n";
  144. }
  145. if(isset($p) and $p and isset($field)) {
  146. $HTML = ' <p>
  147. <span class="field">&raquo; '. $field .'</span><br />
  148. '. $HTML .'
  149. </p>';
  150. } elseif(isset($p) and $p) {
  151. $HTML = ' <p>
  152. '. $HTML .'
  153. </p>';
  154. } elseif(isset($field)) {
  155. $HTML = '<span class="field">&raquo; '. $field .'</span><br />'. $HTML .'';
  156. }
  157. return $HTML;
  158. } elseif($attributes) {
  159. return '<input name="'. $attributes .'" type="text" />' . "\n";
  160. } else {
  161. return NULL;
  162. }
  163. }
  164. /**
  165. * formLabel
  166. *
  167. * Sets a <label> tag.
  168. *
  169. * @param string $for
  170. * @param string $value
  171. * @param boolean $br = TRUE
  172. * @returns string $HTML
  173. */
  174. function formLabel($for, $text, $br = TRUE) {
  175. $HTML = "<label for=\"$for\">$text: </label>";
  176. if($br == TRUE) {
  177. $HTML .= "<br />" . "\n";
  178. }
  179. return $HTML;
  180. }
  181. /**
  182. * formOpen
  183. *
  184. * Sets and Opens a basic form structure
  185. *
  186. * @param string $ID = NULL
  187. * @param string $text = NULL
  188. * @param string $action
  189. * @param string $class = "forms"
  190. * @param string $method = "post"
  191. * @param string $enctype = "multipart/form-data"
  192. * @returns string $HTML
  193. */
  194. function formOpen($action = NULL, $class = "forms", $ID = NULL, $legend = NULL, $method = "post", $enctype = "multipart/form-data") {
  195. $ID = (isset($ID)) ? ' id="'. $ID .'"' : NULL;
  196. $legend = (isset($legend)) ? "<legend>$legend</legend>" . "\n" : NULL;
  197. $action = (strstr($action, "http://")) ? $action : get("webBase") . "/" . $action;
  198. $HTML = '<form'. $ID .' action="'. $action .'" method="'. $method .'" class="'. $class .'" enctype="'. $enctype .'">' . "\n\t";
  199. $HTML .= '<fieldset>' . "\n\t\t";
  200. $HTML .= $legend . "\n";
  201. return $HTML;
  202. }
  203. /**
  204. * formRadio
  205. *
  206. * Sets a <input /> type Radio tag and its attributes
  207. *
  208. * @param string $text = NULL
  209. * @param string $position = "Right"
  210. * @param string $name
  211. * @param string $value
  212. * @param string $ID = NULL
  213. * @param boolean $checked = FALSE
  214. * @param string $events = NULL
  215. * @param boolean $disable = FALSE
  216. * @returns string value
  217. */
  218. function formRadio($attributes, $options = FALSE) {
  219. if(isset($attributes) and is_array($attributes)) {
  220. $attrs = NULL;
  221. foreach($attributes as $attr => $value) {
  222. if($attr !== "position" and $attr !== "text" and $attr !== "type" and $attr !== "p" and $attr !== "field" and $attr !== "checked") {
  223. $attrs .= ' '. strtolower($attr) .'="'. $value .'"';
  224. } else {
  225. $$attr = $value;
  226. }
  227. }
  228. if(is_array($options)) {
  229. $HTML = NULL;
  230. foreach($options as $option) {
  231. if(is_array($option)) {
  232. foreach($option as $attribute) {
  233. if($attribute["default"]) {
  234. $check = ' checked="checked"';
  235. } else {
  236. $check = NULL;
  237. }
  238. $HTML .= ' <input '. $attrs .' value="'. $attribute["name"] .'" type="radio"'. $check .' />'. $attribute["value"];
  239. }
  240. }
  241. }
  242. } else {
  243. if(isset($checked) and $checked) {
  244. $check = ' checked="checked"';
  245. } else {
  246. $check = NULL;
  247. }
  248. if(isset($position) and $position === "left" and isset($text)) {
  249. $HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
  250. } elseif(isset($position) and $position === "right" and isset($text)) {
  251. $HTML = '<input'. $attrs .' type="radio"'. $check .' /> '. $text;
  252. } elseif(isset($text)) {
  253. $HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
  254. } else {
  255. $HTML = '<input'. $attrs .' type="radio"'. $check .' />';
  256. }
  257. }
  258. if(isset($p) and isset($field)) {
  259. $HTML = ' <p>
  260. <span class="field">&raquo; '. $field .'</span><br />
  261. '. $HTML .'
  262. </p>';
  263. }
  264. return $HTML;
  265. } else {
  266. return NULL;
  267. }
  268. }
  269. /**
  270. * formSelect
  271. *
  272. * Sets a <select> tag and its attributes
  273. *
  274. * @param boolean $p = TRUE
  275. * @param string $text
  276. * @param string $name
  277. * @param mixed $options
  278. * @param string $class = "Select"
  279. * @param string $selected = NULL
  280. * @param string $ID = NULL
  281. * @param string $size = "1"
  282. * @param boolean $raquo = TRUE
  283. * @returns string $HTML
  284. */
  285. function formSelect($attributes = FALSE, $options = FALSE, $select = FALSE) {
  286. if(isset($attributes) and is_array($attributes)) {
  287. $attrs = NULL;
  288. foreach($attributes as $attribute => $value) {
  289. if($attribute !== "p" and $attribute !== "field") {
  290. $attrs .= ' '. strtolower($attribute) .'="'. $value .'"';
  291. } else {
  292. $$attribute = $value;
  293. }
  294. }
  295. $HTML = "\t" . '<select'. $attrs .' size="1">'. "\n";
  296. if(is_array($options)) {
  297. foreach($options as $option) {
  298. if($select) {
  299. $HTML .= $select;
  300. $select = FALSE;
  301. }
  302. if(is_string($option)) {
  303. $HTML .= "\t\t" . '<option>'. $option .'</option>' . "\n";
  304. } else {
  305. $selected = (isset($option["selected"]) and $option["selected"]) ? ' selected="selected"' : NULL;
  306. $value = (isset($option["value"])) ? $option["value"] : NULL;
  307. $text = (isset($option["option"])) ? $option["option"] : NULL;
  308. $HTML .= "\t\t" . '<option value="' . $value . '"' . $selected . '>' . $text . '</option>' . "\n";
  309. }
  310. }
  311. }
  312. $HTML .= "\t" . '</select>' . "\n";
  313. unset($options);
  314. if(isset($p) and isset($field)) {
  315. $HTML = ' <p>
  316. <span class="field">&raquo; '. $field .'</span><br />
  317. '. $HTML .'
  318. </p>';
  319. }
  320. return $HTML;
  321. } else {
  322. return NULL;
  323. }
  324. }
  325. /**
  326. * formTextarea
  327. *
  328. * Sets a <textarea> tag and its attributes
  329. *
  330. * @param boolean $p = TRUE
  331. * @param string $text
  332. * @param string $name
  333. * @param string $value = NULL
  334. * @param string $class = "textarea"
  335. * @param string $selected = NULL
  336. * @param string $ID = NULL
  337. * @param int $rows = 25
  338. * @param int $cols = 90
  339. * @param boolean $raquo = TRUE
  340. * @returns string $HTML
  341. */
  342. function formTextarea($attributes = FALSE) {
  343. if(isset($attributes) and is_array($attributes)) {
  344. $attrs = NULL;
  345. foreach($attributes as $attribute => $val) {
  346. if($attribute !== "type" and $attribute !== "value" and $attribute !== "p" and $attribute !== "field") {
  347. $attrs .= ' '. strtolower($attribute) .'="'. $val .'"';
  348. } else {
  349. $$attribute = $val;
  350. }
  351. }
  352. $value = isset($value) ? $value : NULL;
  353. $HTML = '<textarea'. $attrs .'>'. $value .'</textarea>';
  354. if(isset($p) and isset($field)) {
  355. $HTML = ' <p>
  356. <span class="field">&raquo; '. $field .'</span><br />
  357. '. $HTML .'
  358. </p>';
  359. }
  360. return $HTML;
  361. } else {
  362. return NULL;
  363. }
  364. }
  365. function formSave($action = NULL) {
  366. if($action === "save") {
  367. $href = path(segment(0, isLang()) ."/cpanel/add/");
  368. } else {
  369. $href = path(segment(0, isLang()) ."/cpanel/edit/". segment(3, isLang()));
  370. }
  371. $onclick = 'onclick="document.getElementById(\'form-add\').target=\'\'; document.getElementById(\'form-add\').action=\''. $href .'\'"';
  372. $HTML = '
  373. <p class="save-cancel">
  374. <input id="'. $action .'" name="'. $action .'" value="'. __(ucfirst($action)) .'" '. $onclick .' type="submit" class="btn btn-success">
  375. <input id="cancel" name="cancel" value="'. __("Cancel") .'" type="submit" class="btn btn-danger" />
  376. </p>';
  377. return $HTML;
  378. }