PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/bootstrap/widgets/input/TbInput.php

https://bitbucket.org/crisvandommelen/yii-onlinehelp
PHP | 470 lines | 242 code | 62 blank | 166 comment | 31 complexity | 4a991775b3ac329eb3f94a575826ba67 MD5 | raw file
  1. <?php
  2. /**
  3. * TbInput class file.
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; Christoffer Niska 2011-
  6. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  7. * @package bootstrap.widgets.input
  8. */
  9. /**
  10. * Bootstrap input widget.
  11. * Used for rendering inputs according to Bootstrap standards.
  12. */
  13. abstract class TbInput extends CInputWidget
  14. {
  15. // The different input types.
  16. const TYPE_CHECKBOX = 'checkbox';
  17. const TYPE_CHECKBOXLIST = 'checkboxlist';
  18. const TYPE_CHECKBOXLIST_INLINE = 'checkboxlist_inline';
  19. const TYPE_DROPDOWN = 'dropdownlist';
  20. const TYPE_FILE = 'filefield';
  21. const TYPE_PASSWORD = 'password';
  22. const TYPE_RADIO = 'radiobutton';
  23. const TYPE_RADIOLIST = 'radiobuttonlist';
  24. const TYPE_RADIOLIST_INLINE = 'radiobuttonlist_inline';
  25. const TYPE_TEXTAREA = 'textarea';
  26. const TYPE_TEXT = 'textfield';
  27. const TYPE_CAPTCHA = 'captcha';
  28. const TYPE_UNEDITABLE = 'uneditable';
  29. /**
  30. * @var TbActiveForm the associated form widget.
  31. */
  32. public $form;
  33. /**
  34. * @var string the input label text.
  35. */
  36. public $label;
  37. /**
  38. * @var string the input type.
  39. * Following types are supported: checkbox, checkboxlist, dropdownlist, filefield, password,
  40. * radiobutton, radiobuttonlist, textarea, textfield, captcha and uneditable.
  41. */
  42. public $type;
  43. /**
  44. * @var array the data for list inputs.
  45. */
  46. public $data = array();
  47. /**
  48. * @var string text to prepend.
  49. */
  50. public $prependText;
  51. /**
  52. * @var string text to append.
  53. */
  54. public $appendText;
  55. /**
  56. * @var string the hint text.
  57. */
  58. public $hintText;
  59. /**
  60. * @var array label html attributes.
  61. */
  62. public $labelOptions = array();
  63. /**
  64. * @var array prepend html attributes.
  65. */
  66. public $prependOptions = array();
  67. /**
  68. * @var array append html attributes.
  69. */
  70. public $appendOptions = array();
  71. /**
  72. * @var array hint html attributes.
  73. */
  74. public $hintOptions = array();
  75. /**
  76. * @var array error html attributes.
  77. */
  78. public $errorOptions = array();
  79. /**
  80. * @var array captcha html attributes.
  81. */
  82. public $captchaOptions = array();
  83. /**
  84. * Initializes the widget.
  85. * @throws CException if the widget could not be initialized.
  86. */
  87. public function init()
  88. {
  89. if (!isset($this->form))
  90. throw new CException(__CLASS__.': Failed to initialize widget! Form is not set.');
  91. if (!isset($this->model))
  92. throw new CException(__CLASS__.': Failed to initialize widget! Model is not set.');
  93. if (!isset($this->type))
  94. throw new CException(__CLASS__.': Failed to initialize widget! Input type is not set.');
  95. if ($this->type === self::TYPE_UNEDITABLE)
  96. {
  97. if (isset($this->htmlOptions['class']))
  98. $this->htmlOptions['class'] .= ' uneditable-input';
  99. else
  100. $this->htmlOptions['class'] = 'uneditable-input';
  101. }
  102. $this->processHtmlOptions();
  103. }
  104. /**
  105. * Processes the html options.
  106. */
  107. protected function processHtmlOptions()
  108. {
  109. if (isset($this->htmlOptions['prepend']))
  110. {
  111. $this->prependText = $this->htmlOptions['prepend'];
  112. unset($this->htmlOptions['prepend']);
  113. }
  114. if (isset($this->htmlOptions['append']))
  115. {
  116. $this->appendText = $this->htmlOptions['append'];
  117. unset($this->htmlOptions['append']);
  118. }
  119. if (isset($this->htmlOptions['hint']))
  120. {
  121. $this->hintText = $this->htmlOptions['hint'];
  122. unset($this->htmlOptions['hint']);
  123. }
  124. if (isset($this->htmlOptions['labelOptions']))
  125. {
  126. $this->labelOptions = $this->htmlOptions['labelOptions'];
  127. unset($this->htmlOptions['labelOptions']);
  128. }
  129. if (isset($this->htmlOptions['prependOptions']))
  130. {
  131. $this->prependOptions = $this->htmlOptions['prependOptions'];
  132. unset($this->htmlOptions['prependOptions']);
  133. }
  134. if (isset($this->htmlOptions['appendOptions']))
  135. {
  136. $this->appendOptions = $this->htmlOptions['appendOptions'];
  137. unset($this->htmlOptions['appendOptions']);
  138. }
  139. if (isset($this->htmlOptions['hintOptions']))
  140. {
  141. $this->hintOptions = $this->htmlOptions['hintOptions'];
  142. unset($this->htmlOptions['hintOptions']);
  143. }
  144. if (isset($this->htmlOptions['errorOptions']))
  145. {
  146. $this->errorOptions = $this->htmlOptions['errorOptions'];
  147. unset($this->htmlOptions['errorOptions']);
  148. }
  149. if (isset($this->htmlOptions['captchaOptions']))
  150. {
  151. $this->captchaOptions = $this->htmlOptions['captchaOptions'];
  152. unset($this->htmlOptions['captchaOptions']);
  153. }
  154. }
  155. /**
  156. * Runs the widget.
  157. * @throws CException if the widget type is invalid.
  158. */
  159. public function run()
  160. {
  161. switch ($this->type)
  162. {
  163. case self::TYPE_CHECKBOX:
  164. $this->checkBox();
  165. break;
  166. case self::TYPE_CHECKBOXLIST:
  167. $this->checkBoxList();
  168. break;
  169. case self::TYPE_CHECKBOXLIST_INLINE:
  170. $this->checkBoxListInline();
  171. break;
  172. case self::TYPE_DROPDOWN:
  173. $this->dropDownList();
  174. break;
  175. case self::TYPE_FILE:
  176. $this->fileField();
  177. break;
  178. case self::TYPE_PASSWORD:
  179. $this->passwordField();
  180. break;
  181. case self::TYPE_RADIO:
  182. $this->radioButton();
  183. break;
  184. case self::TYPE_RADIOLIST:
  185. $this->radioButtonList();
  186. break;
  187. case self::TYPE_RADIOLIST_INLINE:
  188. $this->radioButtonListInline();
  189. break;
  190. case self::TYPE_TEXTAREA:
  191. $this->textArea();
  192. break;
  193. case self::TYPE_TEXT:
  194. $this->textField();
  195. break;
  196. case self::TYPE_CAPTCHA:
  197. $this->captcha();
  198. break;
  199. case self::TYPE_UNEDITABLE:
  200. $this->uneditableField();
  201. break;
  202. default:
  203. throw new CException(__CLASS__.': Failed to run widget! Type is invalid.');
  204. }
  205. }
  206. /**
  207. * Returns the label for the input.
  208. * @return string the label
  209. */
  210. protected function getLabel()
  211. {
  212. if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel())
  213. return $this->form->labelEx($this->model, $this->attribute, $this->labelOptions);
  214. else if ($this->label !== null)
  215. return $this->label;
  216. else
  217. return '';
  218. }
  219. /**
  220. * Returns the prepend element for the input.
  221. * @return string the element
  222. */
  223. protected function getPrepend()
  224. {
  225. if ($this->hasAddOn())
  226. {
  227. $htmlOptions = $this->prependOptions;
  228. if (isset($htmlOptions['class']))
  229. $htmlOptions['class'] .= ' add-on';
  230. else
  231. $htmlOptions['class'] = 'add-on';
  232. ob_start();
  233. echo '<div class="'.$this->getAddonCssClass().'">';
  234. if (isset($this->prependText))
  235. echo CHtml::tag('span', $htmlOptions, $this->prependText);
  236. return ob_get_clean();
  237. }
  238. else
  239. return '';
  240. }
  241. /**
  242. * Returns the append element for the input.
  243. * @return string the element
  244. */
  245. protected function getAppend()
  246. {
  247. if ($this->hasAddOn())
  248. {
  249. $htmlOptions = $this->appendOptions;
  250. if (isset($htmlOptions['class']))
  251. $htmlOptions['class'] .= ' add-on';
  252. else
  253. $htmlOptions['class'] = 'add-on';
  254. ob_start();
  255. if (isset($this->appendText))
  256. echo CHtml::tag('span', $htmlOptions, $this->appendText);
  257. echo '</div>';
  258. return ob_get_clean();
  259. }
  260. else
  261. return '';
  262. }
  263. /**
  264. * Returns the id that should be used for the specified attribute
  265. * @param string $attribute the attribute
  266. * @return string the id
  267. */
  268. protected function getAttributeId($attribute)
  269. {
  270. return isset($this->htmlOptions['id'])
  271. ? $this->htmlOptions['id']
  272. : CHtml::getIdByName(CHtml::resolveName($this->model, $attribute));
  273. }
  274. /**
  275. * Returns the error text for the input.
  276. * @return string the error text
  277. */
  278. protected function getError()
  279. {
  280. return $this->form->error($this->model, $this->attribute, $this->errorOptions);
  281. }
  282. /**
  283. * Returns the hint text for the input.
  284. * @return string the hint text
  285. */
  286. protected function getHint()
  287. {
  288. if (isset($this->hintText))
  289. {
  290. $htmlOptions = $this->hintOptions;
  291. if (isset($htmlOptions['class']))
  292. $htmlOptions['class'] .= ' help-block';
  293. else
  294. $htmlOptions['class'] = 'help-block';
  295. return CHtml::tag('p', $htmlOptions, $this->hintText);
  296. }
  297. else
  298. return '';
  299. }
  300. /**
  301. * Returns the container CSS class for the input.
  302. * @return string the CSS class
  303. */
  304. protected function getContainerCssClass()
  305. {
  306. $attribute = $this->attribute;
  307. return $this->model->hasErrors(CHtml::resolveName($this->model, $attribute)) ? CHtml::$errorCss : '';
  308. }
  309. /**
  310. * Returns the input container CSS classes.
  311. * @return string the CSS class
  312. */
  313. protected function getAddonCssClass()
  314. {
  315. $classes = array();
  316. if (isset($this->prependText))
  317. $classes[] = 'input-prepend';
  318. if (isset($this->appendText))
  319. $classes[] = 'input-append';
  320. return implode(' ', $classes);
  321. }
  322. /**
  323. * Returns whether the input has an add-on (prepend and/or append).
  324. * @return boolean the result
  325. */
  326. protected function hasAddOn()
  327. {
  328. return isset($this->prependText) || isset($this->appendText);
  329. }
  330. /**
  331. * Renders a checkbox.
  332. * @return string the rendered content
  333. * @abstract
  334. */
  335. abstract protected function checkBox();
  336. /**
  337. * Renders a list of checkboxes.
  338. * @return string the rendered content
  339. * @abstract
  340. */
  341. abstract protected function checkBoxList();
  342. /**
  343. * Renders a list of inline checkboxes.
  344. * @return string the rendered content
  345. * @abstract
  346. */
  347. abstract protected function checkBoxListInline();
  348. /**
  349. * Renders a drop down list (select).
  350. * @return string the rendered content
  351. * @abstract
  352. */
  353. abstract protected function dropDownList();
  354. /**
  355. * Renders a file field.
  356. * @return string the rendered content
  357. * @abstract
  358. */
  359. abstract protected function fileField();
  360. /**
  361. * Renders a password field.
  362. * @return string the rendered content
  363. * @abstract
  364. */
  365. abstract protected function passwordField();
  366. /**
  367. * Renders a radio button.
  368. * @return string the rendered content
  369. * @abstract
  370. */
  371. abstract protected function radioButton();
  372. /**
  373. * Renders a list of radio buttons.
  374. * @return string the rendered content
  375. * @abstract
  376. */
  377. abstract protected function radioButtonList();
  378. /**
  379. * Renders a list of inline radio buttons.
  380. * @return string the rendered content
  381. * @abstract
  382. */
  383. abstract protected function radioButtonListInline();
  384. /**
  385. * Renders a textarea.
  386. * @return string the rendered content
  387. * @abstract
  388. */
  389. abstract protected function textArea();
  390. /**
  391. * Renders a text field.
  392. * @return string the rendered content
  393. * @abstract
  394. */
  395. abstract protected function textField();
  396. /**
  397. * Renders a CAPTCHA.
  398. * @return string the rendered content
  399. * @abstract
  400. */
  401. abstract protected function captcha();
  402. /**
  403. * Renders an uneditable field.
  404. * @return string the rendered content
  405. * @abstract
  406. */
  407. abstract protected function uneditableField();
  408. }