/zan/helpers/helper.forms.php

https://github.com/Eyenrique/ZanPHP · PHP · 429 lines · 426 code · 0 blank · 3 comment · 1 complexity · 6c8c4f8ded75ce053b715ea99b5430bb 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 === "events") {
  109. $attrs .= ' '. $value .' ';
  110. } elseif($attribute !== "type" and $attribute !== "p" and $attribute !== "field") {
  111. $attrs .= ' '. strtolower($attribute) .'="'. encode($value) .'"';
  112. } else {
  113. $$attribute = $value;
  114. }
  115. }
  116. if(isset($type)) {
  117. if($type === "text") {
  118. $HTML = '<input'. $attrs .' type="text" /> ' . "\n";
  119. } elseif($type === "password") {
  120. $HTML = '<input'. $attrs .' type="password" /> ' . "\n";
  121. } elseif($type === "submit") {
  122. $HTML = '<input'. $attrs .' type="submit" /> ' . "\n";
  123. } elseif($type === "button") {
  124. $HTML = '<input'. $attrs .' type="button" /> ' . "\n";
  125. } elseif($type === "checkbox") {
  126. $HTML = '<input'. $attrs .' type="checkbox" /> ' . "\n";
  127. } elseif($type === "radio") {
  128. $HTML = '<input'. $attrs .' type="radio" /> ' . "\n";
  129. } elseif($type === "file") {
  130. $HTML = '<input'. $attrs .' type="file" /> ' . "\n";
  131. } elseif($type === "hidden") {
  132. $HTML = '<input'. $attrs .' type="hidden" /> ' . "\n";
  133. } elseif($type === "image") {
  134. $HTML = '<input'. $attrs .' type="image" /> ' . "\n";
  135. } elseif($type === "reset") {
  136. $HTML = '<input'. $attrs .' type="reset" /> ' . "\n";
  137. } else {
  138. $HTML = '<input'. $attrs .' type="text" /> ' . "\n";
  139. }
  140. } else {
  141. $HTML = '<input'. $attrs .' type="text" /> ' . "\n";
  142. }
  143. if(isset($p) and $p and isset($field)) {
  144. $HTML = ' <p>
  145. <span class="field">&raquo; '. $field .'</span><br />
  146. '. $HTML .'
  147. </p>';
  148. } elseif(isset($p) and $p) {
  149. $HTML = ' <p>
  150. '. $HTML .'
  151. </p>';
  152. } elseif(isset($field)) {
  153. $HTML = '<span class="field">&raquo; '. $field .'</span><br />'. $HTML .'';
  154. }
  155. return $HTML;
  156. } elseif($attributes) {
  157. return '<input name="'. $attributes .'" type="text" />' . "\n";
  158. } else {
  159. return NULL;
  160. }
  161. }
  162. /**
  163. * formLabel
  164. *
  165. * Sets a <label> tag.
  166. *
  167. * @param string $for
  168. * @param string $value
  169. * @param boolean $br = TRUE
  170. * @returns string $HTML
  171. */
  172. function formLabel($for, $text, $br = TRUE) {
  173. $HTML = "<label for=\"$for\">$text: </label>";
  174. if($br == TRUE) {
  175. $HTML .= "<br />" . "\n";
  176. }
  177. return $HTML;
  178. }
  179. /**
  180. * formOpen
  181. *
  182. * Sets and Opens a basic form structure
  183. *
  184. * @param string $ID = NULL
  185. * @param string $text = NULL
  186. * @param string $action
  187. * @param string $class = "forms"
  188. * @param string $method = "post"
  189. * @param string $enctype = "multipart/form-data"
  190. * @returns string $HTML
  191. */
  192. function formOpen($action = NULL, $class = "forms", $ID = NULL, $legend = NULL, $method = "post", $enctype = "multipart/form-data") {
  193. $ID = (isset($ID)) ? ' id="'. $ID .'"' : NULL;
  194. $legend = (isset($legend)) ? "<legend>$legend</legend>" . "\n" : NULL;
  195. $action = (strstr($action, "http://")) ? $action : _webBase . _sh . $action;
  196. $HTML = '<form'. $ID .' action="'. $action .'" method="'. $method .'" class="'. $class .'" enctype="'. $enctype .'">' . "\n\t";
  197. $HTML .= '<fieldset>' . "\n\t\t";
  198. $HTML .= $legend . "\n";
  199. return $HTML;
  200. }
  201. /**
  202. * formRadio
  203. *
  204. * Sets a <input /> type Radio tag and its attributes
  205. *
  206. * @param string $text = NULL
  207. * @param string $position = "Right"
  208. * @param string $name
  209. * @param string $value
  210. * @param string $ID = NULL
  211. * @param boolean $checked = FALSE
  212. * @param string $events = NULL
  213. * @param boolean $disable = FALSE
  214. * @returns string value
  215. */
  216. function formRadio($attributes, $options = FALSE) {
  217. if(isset($attributes) and is_array($attributes)) {
  218. $attrs = NULL;
  219. foreach($attributes as $attr => $value) {
  220. if($attr !== "position" and $attr !== "text" and $attr !== "type" and $attr !== "p" and $attr !== "field" and $attr !== "checked") {
  221. $attrs .= ' '. strtolower($attr) .'="'. encode($value) .'"';
  222. } else {
  223. $$attr = $value;
  224. }
  225. }
  226. if(is_array($options)) {
  227. $HTML = NULL;
  228. foreach($options as $option) {
  229. if(is_array($option)) {
  230. foreach($option as $attribute) {
  231. if($attribute["default"]) {
  232. $check = ' checked="checked"';
  233. } else {
  234. $check = NULL;
  235. }
  236. $HTML .= ' <input '. $attrs .' value="'. $attribute["name"] .'" type="radio"'. $check .' />'. $attribute["value"];
  237. }
  238. }
  239. }
  240. } else {
  241. if(isset($checked) and $checked) {
  242. $check = ' checked="checked"';
  243. } else {
  244. $check = NULL;
  245. }
  246. if(isset($position) and $position === "left" and isset($text)) {
  247. $HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
  248. } elseif(isset($position) and $position === "right" and isset($text)) {
  249. $HTML = '<input'. $attrs .' type="radio"'. $check .' /> '. $text;
  250. } elseif(isset($text)) {
  251. $HTML = $text . ' <input'. $attrs .' type="radio"'. $check .' />';
  252. } else {
  253. $HTML = '<input'. $attrs .' type="radio"'. $check .' />';
  254. }
  255. }
  256. if(isset($p) and isset($field)) {
  257. $HTML = ' <p>
  258. <span class="field">&raquo; '. $field .'</span><br />
  259. '. $HTML .'
  260. </p>';
  261. }
  262. return $HTML;
  263. } else {
  264. return NULL;
  265. }
  266. }
  267. /**
  268. * formSelect
  269. *
  270. * Sets a <select> tag and its attributes
  271. *
  272. * @param boolean $p = TRUE
  273. * @param string $text
  274. * @param string $name
  275. * @param mixed $options
  276. * @param string $class = "Select"
  277. * @param string $selected = NULL
  278. * @param string $ID = NULL
  279. * @param string $size = "1"
  280. * @param boolean $raquo = TRUE
  281. * @returns string $HTML
  282. */
  283. function formSelect($attributes = FALSE, $options = FALSE, $select = FALSE) {
  284. if(isset($attributes) and is_array($attributes)) {
  285. $attrs = NULL;
  286. foreach($attributes as $attribute => $value) {
  287. if($attribute !== "p" and $attribute !== "field") {
  288. $attrs .= ' '. strtolower($attribute) .'="'. encode($value) .'"';
  289. } else {
  290. $$attribute = $value;
  291. }
  292. }
  293. $HTML = "\t" . '<select'. $attrs .' size="1">'. "\n";
  294. if(is_array($options)) {
  295. foreach($options as $option) {
  296. if($select) {
  297. $HTML .= $select;
  298. $select = FALSE;
  299. }
  300. if(is_string($option)) {
  301. $HTML .= "\t\t" . '<option>'. $option .'</option>' . "\n";
  302. } else {
  303. $selected = (isset($option["selected"]) and $option["selected"]) ? ' selected="selected"' : NULL;
  304. $value = (isset($option["value"])) ? $option["value"] : NULL;
  305. $text = (isset($option["option"])) ? $option["option"] : NULL;
  306. $HTML .= "\t\t" . '<option value="' . $value . '"' . $selected . '>' . $text . '</option>' . "\n";
  307. }
  308. }
  309. }
  310. $HTML .= "\t" . '</select>' . "\n";
  311. unset($options);
  312. if(isset($p) and isset($field)) {
  313. $HTML = ' <p>
  314. <span class="field">&raquo; '. $field .'</span><br />
  315. '. $HTML .'
  316. </p>';
  317. }
  318. return $HTML;
  319. } else {
  320. return NULL;
  321. }
  322. }
  323. /**
  324. * formTextarea
  325. *
  326. * Sets a <textarea> tag and its attributes
  327. *
  328. * @param boolean $p = TRUE
  329. * @param string $text
  330. * @param string $name
  331. * @param string $value = NULL
  332. * @param string $class = "textarea"
  333. * @param string $selected = NULL
  334. * @param string $ID = NULL
  335. * @param int $rows = 25
  336. * @param int $cols = 90
  337. * @param boolean $raquo = TRUE
  338. * @returns string $HTML
  339. */
  340. function formTextarea($attributes = FALSE) {
  341. if(isset($attributes) and is_array($attributes)) {
  342. $attrs = NULL;
  343. foreach($attributes as $attribute => $val) {
  344. if($attribute !== "type" and $attribute !== "value" and $attribute !== "p" and $attribute !== "field") {
  345. $attrs .= ' '. strtolower($attribute) .'="'. encode($val) .'"';
  346. } else {
  347. $$attribute = $val;
  348. }
  349. }
  350. $value = isset($value) ? $value : NULL;
  351. $HTML = '<textarea'. $attrs .'>'. $value .'</textarea>';
  352. if(isset($p) and isset($field)) {
  353. $HTML = ' <p>
  354. <span class="field">&raquo; '. $field .'</span><br />
  355. '. $HTML .'
  356. </p>';
  357. }
  358. return $HTML;
  359. } else {
  360. return NULL;
  361. }
  362. }
  363. function formSave($action = NULL) {
  364. $HTML = '
  365. <p class="save-cancel">
  366. <input id="'. $action .'" name="'. $action .'" value="'. __(ucfirst($action)) .'" type="submit" class="submit save" />
  367. <input id="cancel" name="cancel" value="'. __("Cancel") .'" type="submit" class="submit cancel" />
  368. </p>';
  369. return $HTML;
  370. }
  371. function formUploadFrame($value, $events = NULL) {
  372. $HTML = '<input type="file" name="'. $value .'File" /> ';
  373. $HTML .= '<input type="submit" class="small-submit" name="'. $value .'Upload" value="'. __("Upload") .'" '. $events .' /><br />';
  374. $HTML .= '<iframe name="'. $value .'Upload" class="no-display"></iframe>';
  375. return $HTML;
  376. }