PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/concreteOLD/helpers/form.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 404 lines | 198 code | 60 blank | 146 comment | 54 complexity | ba4e779bc6a1b8c541b6d02ea1fb5752 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Helpers
  4. * @category Concrete
  5. * @author Andrew Embler <andrew@concrete5.org>
  6. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  7. * @license http://www.concrete5.org/license/ MIT License
  8. */
  9. /**
  10. * Helpful functions for working with forms. Includes HTML input tags and the like
  11. * @package Helpers
  12. * @category Concrete
  13. * @author Andrew Embler <andrew@concrete5.org>
  14. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  15. * @license http://www.concrete5.org/license/ MIT License
  16. */
  17. defined('C5_EXECUTE') or die("Access Denied.");
  18. class FormHelper {
  19. private $radioIndex = 1;
  20. private $selectIndex = 1;
  21. public function reset() {
  22. $this->radioIndex = 1;
  23. $this->selectIndex = 1;
  24. }
  25. public function __construct() {
  26. $this->th = Loader::helper("text");
  27. }
  28. /**
  29. * Returns an action suitable for including in a form action property.
  30. * @param string $action
  31. * @param string $task
  32. */
  33. public function action($action, $task = null) {
  34. return View::url($action, $task);
  35. }
  36. /**
  37. * Creates a submit button
  38. * @param string $name
  39. * @param string value
  40. * @param array $fields Additional fields appended at the end of the submit button
  41. * return string $html
  42. */
  43. public function submit($name, $value, $fields = array(), $additionalClasses='') {
  44. return '<input type="submit"' . $this->parseMiscFields('btn ccm-input-submit ' . $additionalClasses, $fields) . ' id="' . $name . '" name="' . $name . '" value="' . $value . '" />';
  45. }
  46. /**
  47. * Creates a button
  48. * @param string $name
  49. * @param string value
  50. * @param array $fields Additional fields appended at the end of the submit button
  51. * return string $html
  52. */
  53. public function button($name, $value, $fields = array(), $additionalClasses='') {
  54. return '<input type="button"' . $this->parseMiscFields('btn ccm-input-button ' . $additionalClasses, $fields) . ' id="' . $name . '" name="' . $name . '" value="' . $value . '" />';
  55. }
  56. /**
  57. * Creates a label tag
  58. * @param string $field
  59. * @param string $name
  60. * @return string $html
  61. */
  62. public function label($field, $name, $miscFields = array()) {
  63. $str = '<label for="' . $field . '"' . $this->parseMiscFields(null, $miscFields) . '>' . $name . '</label>';
  64. return $str;
  65. }
  66. /**
  67. * Creates a file input element
  68. * @param string $key
  69. */
  70. public function file($key) {
  71. $str = '<input type="file" id="' . $key . '" name="' . $key . '" value="" class="ccm-input-file" />';
  72. return $str;
  73. }
  74. /**
  75. * Creates a hidden form field.
  76. * @param string $key
  77. * @param string $value
  78. */
  79. public function hidden($key, $value = null) {
  80. $val = $this->getRequestValue($key);
  81. if ($val !== false && (!is_array($val))) {
  82. $value = $val;
  83. }
  84. $str = '<input type="hidden" name="' . $key . '" id="' . $key . '" value="' . $value . '" />';
  85. return $str;
  86. }
  87. /**
  88. * Generates a checkbox
  89. * @param string $key Checkbox's name and id. Should end with "[]" if it's to return an array on submit.
  90. * @param string $value String value sent to server, if checkbox is checked, on submit
  91. * @param string $isChecked "Checked" value (subject to be overridden by $_REQUEST). Checkbox is checked if value is true (string). Note that 'false' (string) evaluates to true (boolean)!
  92. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  93. * @return $html
  94. */
  95. public function checkbox($key, $value, $isChecked = false, $miscFields = array()) {
  96. $id = $key;
  97. $_field = $key;
  98. if ((strpos($key, '[]') + 2) == strlen($key)) {
  99. $_field = substr($key, 0, strpos($key, '[]'));
  100. $id = $_field . '_' . $value;
  101. }
  102. if ($isChecked && (!isset($_REQUEST[$_field])) && ($_SERVER['REQUEST_METHOD'] != 'POST')) {
  103. $checked = true;
  104. } else if ($this->getRequestValue($key) == $value) {
  105. $checked = true;
  106. } else if (is_array($this->getRequestValue($key)) && in_array($value, $this->getRequestValue($key))) {
  107. $checked = true;
  108. }
  109. if ($checked) {
  110. $checked = 'checked="checked" ';
  111. }
  112. return '<input type="checkbox" '. $this->parseMiscFields('ccm-input-checkbox', $miscFields) . ' name="' . $key . '" id="' . $id . '" value="' . $value . '" ' . $checked . ' />';
  113. }
  114. /**
  115. * Creates a textarea field. Second argument is either the value of the field (and if it's blank we check post) or a misc. array of fields
  116. * @param string $key
  117. * @return string $html
  118. */
  119. public function textarea($key) {
  120. $a = func_get_args();
  121. $str = '<textarea id="' . $key . '" name="' . $key . '" ';
  122. $rv = $this->getRequestValue($key);
  123. if (count($a) == 3) {
  124. $innerValue = ($rv !== false) ? $rv : $a[1];
  125. $miscFields = $a[2];
  126. } else {
  127. if (is_array($a[1])) {
  128. $innerValue = ($rv !== false) ? $rv : '';
  129. $miscFields = $a[1];
  130. } else {
  131. // we ignore this second value if a post is set with this guy in it
  132. $innerValue = ($rv !== false) ? $rv : $a[1];
  133. }
  134. }
  135. $str .= $this->parseMiscFields('ccm-input-textarea', $miscFields);
  136. $str .= '>' . $innerValue . '</textarea>';
  137. return $str;
  138. }
  139. /**
  140. * Generates a radio button
  141. * @param string $key
  142. * @param string $valueOfButton
  143. * @param string $valueOfSelectedOption
  144. */
  145. public function radio($key, $value, $valueOrArray = false, $miscFields = array()) {
  146. $str = '<input type="radio" name="' . $key . '" id="' . $key . $this->radioIndex . '" value="' . $value . '" ';
  147. if (is_array($valueOrArray)) {
  148. $miscFields = $valueOrArray;
  149. }
  150. $str .= $this->parseMiscFields('ccm-input-radio', $miscFields) . ' ';
  151. if ($valueOrArray == $value && !isset($_REQUEST[$key]) || (isset($_REQUEST[$key]) && $_REQUEST[$key] == $value)) {
  152. $str .= 'checked="checked" ';
  153. }
  154. $this->radioIndex++;
  155. $str .= ' />';
  156. return $str;
  157. }
  158. protected function processRequestValue($key, $type = "post") {
  159. $arr = ($type == 'post') ? $_POST : $_GET;
  160. if (strpos($key, '[') !== false) {
  161. // we've got something like 'akID[34]['value'] here, which we need to get data from
  162. /* @var $ah ArrayHelper */
  163. $ah = Loader::helper('array');
  164. $key = str_replace(']', '', $key);
  165. $key = explode('[', trim($key, '['));
  166. $v2 = $ah->get($arr, $key);
  167. if (isset($v2)) {
  168. // if the type if GET, we make sure to strip it of any nasties
  169. // POST we will let stay unfiltered
  170. if (is_string($v2)) {
  171. return $this->th->entities($v2);
  172. } else {
  173. return $v2;
  174. }
  175. }
  176. } else if (isset($arr[$key])) {
  177. return $this->th->entities($arr[$key]);
  178. }
  179. return false;
  180. }
  181. // checks the request based on the key passed. Does things like turn the key into arrays if the key has text versions of [ and ] in it, etc..
  182. public function getRequestValue($key) {
  183. $val = $this->processRequestValue($key, 'post');
  184. if ($val !== false) {
  185. return $val;
  186. } else {
  187. return $this->processRequestValue($key, 'get');
  188. }
  189. }
  190. /**
  191. * Internal function that creates an <input> element of type $type. Handles the messiness of evaluating $valueOrArray. Assigns a default class of ccm-input-$type
  192. * @param string $key Input element's name and id
  193. * @param string $type Accepted value for HTML attribute "type"
  194. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  195. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  196. * @return $html
  197. */
  198. protected function inputType($key, $type, $valueOrArray, $miscFields) {
  199. $val = $this->getRequestValue($key);
  200. if (is_array($valueOrArray)) {
  201. //valueOrArray is, in fact, the miscFields array
  202. $miscFields = $valueOrArray;
  203. } else {
  204. //valueOrArray is either empty or the default field value; miscFields is either empty or the miscFields
  205. $val = ($val !== false) ? $val : $valueOrArray;
  206. }
  207. $val = str_replace('"', '&#34;', $val);
  208. return "<input id=\"$key\" type=\"$type\" name=\"$key\" value=\"$val\" " . $this->parseMiscFields("ccm-input-$type", $miscFields) . ' />';
  209. }
  210. /**
  211. * Renders a text input field.
  212. * @param string $key Input element's name and id
  213. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  214. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  215. * @return $html
  216. */
  217. public function text($key, $valueOrArray = false, $miscFields = array()) {
  218. return $this->inputType($key, 'text', $valueOrArray, $miscFields);
  219. }
  220. /**
  221. * Renders an email input field.
  222. * @param string $key Input element's name and id
  223. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  224. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  225. * @return $html
  226. */
  227. public function email($key, $valueOrArray = false, $miscFields = array()) {
  228. return $this->inputType($key, 'email', $valueOrArray, $miscFields);
  229. }
  230. /**
  231. * Renders a telephone input field.
  232. * @param string $key Input element's name and id
  233. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  234. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  235. * @return $html
  236. */
  237. public function telephone($key, $valueOrArray = false, $miscFields = array()) {
  238. return $this->inputType($key, 'tel', $valueOrArray, $miscFields);
  239. }
  240. /**
  241. * Renders a URL input field.
  242. * @param string $key Input element's name and id
  243. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  244. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  245. * @return $html
  246. */
  247. public function url($key, $valueOrArray = false, $miscFields = array()) {
  248. return $this->inputType($key, 'url', $valueOrArray, $miscFields);
  249. }
  250. /**
  251. * Renders a search input field.
  252. * @param string $key Input element's name and id
  253. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  254. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  255. * @return $html
  256. */
  257. public function search($key, $valueOrArray = false, $miscFields = array()) {
  258. return $this->inputType($key, 'search', $valueOrArray, $miscFields);
  259. }
  260. /**
  261. * Renders a select field. First argument is the name of the field. Second is an associative array of key => display. Second argument is either the value of the field to be selected (and if it's blank we check post) or a misc. array of fields
  262. * @param string $key
  263. * @return $html
  264. */
  265. public function select($key, $optionValues, $valueOrArray = false, $miscFields = array()) {
  266. $val = $this->getRequestValue($key);
  267. if (is_array($val)) {
  268. $valueOrArray = $val[0];
  269. }
  270. if ((strpos($key, '[]') + 2) == strlen($key)) {
  271. $_key = substr($key, 0, strpos($key, '[]'));
  272. $id = $_key . $this->selectIndex;
  273. } else {
  274. $_key = $key;
  275. $id = $key;
  276. }
  277. if (is_array($valueOrArray)) {
  278. $miscFields = $valueOrArray;
  279. } else {
  280. $miscFields['ccm-passed-value'] = $valueOrArray;
  281. }
  282. $str = '<select name="' . $key . '" id="' . $id . '" ' . $this->parseMiscFields('ccm-input-select', $miscFields) . '>';
  283. foreach($optionValues as $k => $value) {
  284. $selected = "";
  285. if ($valueOrArray == $k && !isset($_REQUEST[$_key]) || ($val !== false && $val == $k) || (is_array($_REQUEST[$_key]) && (in_array($k, $_REQUEST[$_key])))) {
  286. $selected = 'selected="selected"';
  287. }
  288. $str .= '<option value="' . $k . '" ' . $selected . '>' . $value . '</option>';
  289. }
  290. $this->selectIndex++;
  291. $str .= '</select>';
  292. return $str;
  293. }
  294. /**
  295. * Renders a multiple select box
  296. * @param string $key Select's name and id
  297. * @param array $optionValues Hash array with name/value as the select's option value/text
  298. * @param array|string $defaultValues Default value(s) which match with the option values; overridden by $_REQUEST
  299. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  300. * @return $html
  301. */
  302. public function selectMultiple($key, $optionValues, $defaultValues = false, $miscFields = array()) {
  303. $val = $this->getRequestValue($key . '[]');
  304. $defaultValues = (array) $defaultValues;
  305. if ($val) {
  306. $defaultValues = $val;
  307. }
  308. $str = "<input type='hidden' class='ignore' name='{$key}' value='' />
  309. <select name=\"{$key}[]\" id=\"$key\" multiple=\"multiple\"" . $this->parseMiscFields('ccm-input-select', $miscFields) . ">";
  310. foreach ($optionValues as $val => $text) {
  311. $selected = in_array($val, $defaultValues) ? ' selected="selected"' : '';
  312. $str .= "<option value=\"$val\"$selected>$text</option>";
  313. }
  314. $str .= "</select>";
  315. return $str;
  316. }
  317. /**
  318. * Renders a password input field.
  319. * @param string $key Input element's name and id
  320. * @param string|array $valueOrArray Either the default value (subject to be overridden by $_REQUEST) or $miscFields (see below)
  321. * @param array $miscFields A hash array with html attributes as key/value pairs (possibly including "class")
  322. * @return $html
  323. */
  324. public function password($key, $valueOrArray = false, $miscFields = array()) {
  325. return $this->inputType($key, 'password', $valueOrArray, $miscFields);
  326. }
  327. /**
  328. * Create an HTML fragment of attribute values, merging any CSS class names as necessary
  329. * @param string $defaultClass Default CSS class name
  330. * @param array $attributes A hash array of attributes (name => value)
  331. * @return string a fragment of attributes suitable to put inside of an HTML tag
  332. */
  333. protected function parseMiscFields($defaultClass, $attributes) {
  334. $attr = '';
  335. if ($defaultClass) {
  336. $attributes['class'] = trim((isset($attributes['class']) ? $attributes['class'] : '') . ' ' . $defaultClass);
  337. }
  338. foreach ((array) $attributes as $k => $v) {
  339. $attr .= " $k=\"$v\"";
  340. }
  341. return $attr;
  342. }
  343. }