PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/Plugin/TwitterBootstrap/View/Helper/BootstrapFormHelper.php

https://bitbucket.org/vishallogiciel/admin-bootstrap
PHP | 456 lines | 302 code | 19 blank | 135 comment | 54 complexity | 44061d57c9616ca6239b25575828e44e MD5 | raw file
  1. <?php
  2. App::uses("FormHelper", "View/Helper");
  3. class BootstrapFormHelper extends FormHelper {
  4. /**
  5. * Options used internally. Don't send any of these options along to FormHelper
  6. *
  7. * @var array
  8. * @access private
  9. */
  10. private $__dontSendToFormHelper = array(
  11. 'help_inline',
  12. 'help_block',
  13. 'label',
  14. 'div',
  15. 'error',
  16. 'checkbox_label',
  17. 'append',
  18. 'prepend',
  19. 'field'
  20. );
  21. /**
  22. * basic_input
  23. *
  24. * @param mixed $field
  25. * @param array $options
  26. * @access public
  27. * @return void
  28. */
  29. public function basicInput($field, $options = array()) {
  30. $options = $this->_parseInputOptions($field, $options);
  31. if (!isset($options["field"])) { return ""; }
  32. $options["label"] = $this->_constructLabel($options);
  33. $options["input"] = $this->_constructInput($options);
  34. return $options["label"] . $options["input"];
  35. }
  36. /**
  37. * _parse_input_options
  38. *
  39. * @param mixed $field
  40. * @param array $options
  41. * @access public
  42. * @return void
  43. */
  44. public function _parseInputOptions($field, $options = array()) {
  45. if (is_array($field)) {
  46. $options = $field;
  47. } else {
  48. $options["field"] = $field;
  49. }
  50. $defaults = array(
  51. "type" => "",
  52. "help_inline" => "",
  53. "help_block" => "",
  54. "label" => "",
  55. "append" => false,
  56. "prepend" => false,
  57. "state" => false
  58. );
  59. return array_merge($defaults, $options);
  60. }
  61. /**
  62. * _construct_label
  63. *
  64. * @param mixed $options
  65. * @access public
  66. * @return void
  67. */
  68. public function _constructLabel($options, $basic = true) {
  69. if ($options["label"] === false) { return ""; }
  70. if (in_array($options["type"], array("checkbox"))) {
  71. $opt = $options;
  72. $opt["type"] = "";
  73. $input = $this->_constructInput($opt);
  74. $options["label"] = parent::label(
  75. $options["field"],
  76. $input . $options["label"],
  77. "checkbox"
  78. );
  79. } else {
  80. $class = (!$basic) ? "control-label" : null;
  81. if (!empty($options["label"])) {
  82. $options["label"] = parent::label(
  83. $options["field"],
  84. $options["label"],
  85. array("class" => $class)
  86. );
  87. } else {
  88. $options["label"] = parent::label(
  89. $options["field"],
  90. null,
  91. array("class" => $class)
  92. );
  93. }
  94. }
  95. return $options["label"];
  96. }
  97. /**
  98. * _construct_input
  99. *
  100. * @param mixed $options
  101. * @access public
  102. * @return void
  103. */
  104. public function _constructInput($options) {
  105. if (in_array($options["type"], array("checkbox"))) {
  106. $options["input"] = "";
  107. }
  108. if (isset($options["input"])) { return $options["input"]; }
  109. $options["input"] = parent::input($options["field"], array(
  110. "div" => false,
  111. "label" => false
  112. ));
  113. return $options["input"];
  114. }
  115. /**
  116. * _constuct_input_and_addon
  117. *
  118. * @param mixed $options
  119. * @access public
  120. * @return void
  121. */
  122. public function _constuctInputAndAddon($options) {
  123. if (isset($options["input"])) { return $options["input"]; }
  124. $options["input"] = $this->_constructInput($options);
  125. $options["input"] = $this->_handleInputAddon($options);
  126. return $options["input"];
  127. }
  128. /**
  129. * _handle_input_addon
  130. *
  131. * @param mixed $options
  132. * @access public
  133. * @return void
  134. */
  135. public function _handleInputAddon($options) {
  136. $input = $options["input"];
  137. if ($options["append"]) {
  138. $input = $this->inputAddon($options["append"], $input, "append");
  139. } elseif ($options["prepend"]) {
  140. $input = $this->inputAddon($options["prepend"], $input, "prepend");
  141. }
  142. return $input;
  143. }
  144. /**
  145. * input_addon
  146. *
  147. * @param mixed $content
  148. * @param mixed $input
  149. * @param string $type
  150. * @access public
  151. * @return void
  152. */
  153. public function inputAddon($content, $input, $type = "append") {
  154. $tag = (strpos("input", $content) !== false) ? "label" : "span";
  155. $addon = $this->Html->tag($tag, $content, array("class" => "add-on"));
  156. return $this->Html->tag(
  157. "div",
  158. $input . $content,
  159. array("class" => "input-{$type}")
  160. );
  161. }
  162. /**
  163. * search
  164. *
  165. * @param mixed $name
  166. * @param array $options
  167. * @access public
  168. * @return void
  169. */
  170. public function search($name = null, $options = array()) {
  171. $class = "search-query";
  172. if (!$name) {
  173. $name = "search";
  174. }
  175. if (isset($options["class"])) {
  176. $options["class"] .= " {$class}";
  177. } else {
  178. $options["class"] = $class;
  179. }
  180. return $this->text($name, $options);
  181. }
  182. /**
  183. * Takes an array of options to output markup that works with
  184. * twitter bootstrap forms.
  185. *
  186. * @param array $options
  187. * @access public
  188. * @return string
  189. */
  190. public function input($field, $options = array()) {
  191. $options = $this->_parseInputOptions($field, $options);
  192. if (!isset($options['field'])) { return ''; }
  193. $out = $help_inline = $help_block = '';
  194. /*$model = $this->defaultModel;
  195. if (strpos(".", $options["field"]) !== false) {
  196. $split = explode(".", $options["field"]);
  197. $model = $split[0];
  198. } else {
  199. $options["field"] = "{$model}.{$options["field"]}";
  200. }*/
  201. if ($options['label'] === false) {
  202. $options['label'] = '';
  203. } else if (!empty($options['label'])) {
  204. $options['label'] = $this->label(
  205. $options['field'],
  206. $options['label'],
  207. "control-label"
  208. );
  209. } else {
  210. $options['label'] = $this->label(
  211. $options['field'],
  212. null,
  213. "control-label"
  214. );
  215. }
  216. list($help_inline, $help_block) = $this->_helpMarkup($options);
  217. if ($this->error($options['field'])) {
  218. $options['state'] = 'error';
  219. $help_block = $this->Html->tag(
  220. "span",
  221. $this->error($options['field']),
  222. array("class" => "help-block")
  223. );
  224. }
  225. $options["input"] = $this->_combineInput($options);
  226. $input = $this->Html->tag(
  227. "div",
  228. $options['input'].$help_inline.$help_block,
  229. array("class" => "controls")
  230. );
  231. $wrap_class = "control-group";
  232. if ($options["state"] !== false) {
  233. $wrap_class = "{$wrap_class} {$options["state"]}";
  234. }
  235. return $this->Html->tag(
  236. "div",
  237. $options['label'].$input,
  238. array("class" => $wrap_class)
  239. );
  240. }
  241. /**
  242. * Takes the array of options and will apply the append or prepend bits
  243. * from the options and returns the input string.
  244. *
  245. * @param mixed $input
  246. * @param string $type
  247. * @access public
  248. * @return string
  249. */
  250. public function _combineInput($options) {
  251. $combine_markup = array("append" => "", "prepend" => "");
  252. $input = "";
  253. if (isset($options["input"])) {
  254. $input = $options["input"];
  255. } else {
  256. $opt = array("div" => false, "label" => false, "error" => false);
  257. foreach ($options as $key => $value) {
  258. if (!in_array($key, $this->__dontSendToFormHelper)) {
  259. if ($key !== 'type' || !empty($value)) $opt[$key] = $value;
  260. }
  261. }
  262. $input = parent::input($options["field"], $opt);
  263. if (isset($options["checkbox_label"])) {
  264. $input = $this->label($options["field"], $input.' '.$options["checkbox_label"], array('class' => 'checkbox'));
  265. }
  266. }
  267. foreach (array_keys($combine_markup) as $combine) {
  268. if (isset($options[$combine]) && !empty($options[$combine])) {
  269. $_tag = "span";
  270. if (strpos($options[$combine], "button") !== false) {
  271. $_tag = false;
  272. }
  273. if (strpos($options[$combine], "input") !== false) {
  274. $_tag = "label";
  275. }
  276. if ($_tag) {
  277. $combine_markup[$combine] = $this->Html->tag(
  278. $_tag,
  279. $options[$combine],
  280. array("class" => "add-on")
  281. );
  282. } else {
  283. $combine_markup[$combine] = $options[$combine];
  284. }
  285. }
  286. }
  287. $_class = "";
  288. if (!empty($combine_markup["append"])) {
  289. $input = $input . $combine_markup["append"];
  290. $_class .= " input-append";
  291. }
  292. if (!empty($combine_markup["prepend"])) {
  293. $input = $combine_markup["prepend"] . $input;
  294. $_class .= " input-prepend";
  295. }
  296. if (!empty($combine_markup["append"]) || !empty($combine_markup["prepend"])) {
  297. $input = $this->Html->tag(
  298. "div",
  299. $input,
  300. array("class" => trim($_class))
  301. );
  302. }
  303. return $input;
  304. }
  305. /**
  306. * Takes the options from the input method and returns an array of the
  307. * inline help and inline block content wrapped in the appropriate markup.
  308. *
  309. * @param mixed $options
  310. * @access public
  311. * @return string
  312. */
  313. public function _helpMarkup($options) {
  314. $help_markup = array("help_inline" => "", "help_block" => "");
  315. foreach (array_keys($help_markup) as $help) {
  316. if (isset($options[$help]) && !empty($options[$help])) {
  317. $help_class = str_replace("_", "-", $help);
  318. $help_markup[$help] = $this->Html->tag(
  319. "span",
  320. $options[$help],
  321. array("class" => $help_class)
  322. );
  323. }
  324. }
  325. return array_values($help_markup);
  326. }
  327. /**
  328. * Outputs a list of radio form elements with the proper
  329. * markup for twitter bootstrap styles
  330. *
  331. * @param array $options
  332. * @access public
  333. * @return string
  334. */
  335. public function radio($field, $options = array()) {
  336. if (is_array($field)) {
  337. $options = $field;
  338. } else {
  339. $options["field"] = $field;
  340. }
  341. if (!isset($options["options"]) || !isset($options["field"])) {
  342. return "";
  343. }
  344. $opt = $options["options"];
  345. unset($options["options"]);
  346. $inputs = "";
  347. $hiddenField = (isset($options['hiddenField']) && $options['hiddenField']);
  348. foreach ($opt as $key => $val) {
  349. $input = parent::radio(
  350. $options["field"],
  351. array($key => $val),
  352. array("label" => false, 'hiddenField' => $hiddenField)
  353. );
  354. $id = array();
  355. preg_match_all("/id=\"[a-zA-Z0-9_-]*\"/", $input, $id);
  356. if (!empty($id[0])) {
  357. $id = end($id[0]);
  358. $id = substr($id, 4);
  359. $id = substr($id, 0, -1);
  360. $input = $this->Html->tag(
  361. "label",
  362. $input,
  363. array("class" => "radio", "for" => $id)
  364. );
  365. }
  366. $inputs .= $input;
  367. }
  368. $options["input"] = $inputs;
  369. return $this->input($options);
  370. }
  371. /**
  372. * Wraps the form button method and just applies the Bootstrap classes to
  373. * the button before passing the options on to the FormHelper button method.
  374. *
  375. * @param string $value
  376. * @param array $options
  377. * @access public
  378. * @return string
  379. */
  380. public function button($value = "Submit", $options = array()) {
  381. $options = $this->buttonOptions($options);
  382. return parent::button($value, $options);
  383. }
  384. /**
  385. * Wraps the postLink method to create post links that use the bootstrap
  386. * button styles.
  387. *
  388. * @param mixed $title
  389. * @param mixed $url
  390. * @param array $options
  391. * @param mixed $confirm
  392. * @access public
  393. * @return string
  394. */
  395. public function buttonForm($title, $url, $opt = array(), $confirm = false) {
  396. $opt = $this->buttonOptions($opt);
  397. return $this->postLink($title, $url, $opt, $confirm);
  398. }
  399. /**
  400. * Takes the array of options from $this->button or $this->button_link
  401. * and returns the modified array with the bootstrap classes
  402. *
  403. * @param mixed $options
  404. * @access public
  405. * @return string
  406. */
  407. public function buttonOptions($options) {
  408. $valid_styles = array(
  409. "danger", "info", "primary",
  410. "warning", "success", "inverse"
  411. );
  412. $valid_sizes = array("mini", "small", "large");
  413. $style = isset($options["style"]) ? $options["style"] : "";
  414. $size = isset($options["size"]) ? $options["size"] : "";
  415. $disabled = false;
  416. if (isset($options["disabled"])) {
  417. $disabled = (bool)$options["disabled"];
  418. }
  419. $class = "btn";
  420. if (!empty($style) && in_array($style, $valid_styles)) {
  421. $class .= " btn-{$style}";
  422. }
  423. if (!empty($size) && in_array($size, $valid_sizes)) {
  424. $class .= " btn-{$size}";
  425. }
  426. if ($disabled) { $class .= " disabled"; }
  427. unset($options["style"]);
  428. unset($options["size"]);
  429. unset($options["disabled"]);
  430. if (isset($options["class"])) {
  431. $options["class"] = $options["class"] . " " . $class;
  432. } else {
  433. $options["class"] = $class;
  434. }
  435. return $options;
  436. }
  437. }