PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/haichau59/manga
PHP | 588 lines | 289 code | 83 blank | 216 comment | 31 complexity | 1785eebbae6212914eace03c09a0a2d9 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. const TYPE_DATEPICKER = 'datepicker';
  30. const TYPE_REDACTOR = 'redactor';
  31. const TYPE_MARKDOWNEDITOR = 'markdowneditor';
  32. const TYPE_HTML5EDITOR = 'wysihtml5';
  33. const TYPE_DATERANGEPICKER = 'daterangepicker';
  34. const TYPE_TOGGLEBUTTON = 'togglebutton';
  35. const TYPE_COLORPICKER = 'colorpicker';
  36. const TYPE_CKEDITOR = 'ckeditor';
  37. const TYPE_TIMEPICKER = 'timepicker';
  38. const TYPE_SELECT2 = 'select2';
  39. /**
  40. * @var TbActiveForm the associated form widget.
  41. */
  42. public $form;
  43. /**
  44. * @var string the input label text.
  45. */
  46. public $label;
  47. /**
  48. * @var string the input type.
  49. * Following types are supported: checkbox, checkboxlist, dropdownlist, filefield, password,
  50. * radiobutton, radiobuttonlist, textarea, textfield, captcha and uneditable.
  51. */
  52. public $type;
  53. /**
  54. * @var array the data for list inputs.
  55. */
  56. public $data = array();
  57. /**
  58. * @var string text to prepend.
  59. */
  60. public $prependText;
  61. /**
  62. * @var string text to append.
  63. */
  64. public $appendText;
  65. /**
  66. * @var string the hint text.
  67. */
  68. public $hintText;
  69. /**
  70. * @var array label html attributes.
  71. */
  72. public $labelOptions = array();
  73. /**
  74. * @var array prepend html attributes.
  75. */
  76. public $prependOptions = array();
  77. /**
  78. * @var array append html attributes.
  79. */
  80. public $appendOptions = array();
  81. /**
  82. * @var array hint html attributes.
  83. */
  84. public $hintOptions = array();
  85. /**
  86. * @var array error html attributes.
  87. */
  88. public $errorOptions = array();
  89. /**
  90. * @var array captcha html attributes.
  91. */
  92. public $captchaOptions = array();
  93. /**
  94. * Initializes the widget.
  95. * @throws CException if the widget could not be initialized.
  96. */
  97. public function init()
  98. {
  99. if (!isset($this->form))
  100. throw new CException(__CLASS__ . ': Failed to initialize widget! Form is not set.');
  101. if (!isset($this->model))
  102. throw new CException(__CLASS__ . ': Failed to initialize widget! Model is not set.');
  103. if (!isset($this->type))
  104. throw new CException(__CLASS__ . ': Failed to initialize widget! Input type is not set.');
  105. if ($this->type === self::TYPE_UNEDITABLE)
  106. {
  107. if (isset($this->htmlOptions['class']))
  108. $this->htmlOptions['class'] .= ' uneditable-input';
  109. else
  110. $this->htmlOptions['class'] = 'uneditable-input';
  111. }
  112. $this->processHtmlOptions();
  113. }
  114. /**
  115. * Processes the html options.
  116. */
  117. protected function processHtmlOptions()
  118. {
  119. if (isset($this->htmlOptions['prepend']))
  120. {
  121. $this->prependText = $this->htmlOptions['prepend'];
  122. unset($this->htmlOptions['prepend']);
  123. }
  124. if (isset($this->htmlOptions['append']))
  125. {
  126. $this->appendText = $this->htmlOptions['append'];
  127. unset($this->htmlOptions['append']);
  128. }
  129. if (isset($this->htmlOptions['hint']))
  130. {
  131. $this->hintText = $this->htmlOptions['hint'];
  132. unset($this->htmlOptions['hint']);
  133. }
  134. if (isset($this->htmlOptions['labelOptions']))
  135. {
  136. $this->labelOptions = $this->htmlOptions['labelOptions'];
  137. unset($this->htmlOptions['labelOptions']);
  138. }
  139. if (isset($this->htmlOptions['prependOptions']))
  140. {
  141. $this->prependOptions = $this->htmlOptions['prependOptions'];
  142. unset($this->htmlOptions['prependOptions']);
  143. }
  144. if (isset($this->htmlOptions['appendOptions']))
  145. {
  146. $this->appendOptions = $this->htmlOptions['appendOptions'];
  147. unset($this->htmlOptions['appendOptions']);
  148. }
  149. if (isset($this->htmlOptions['hintOptions']))
  150. {
  151. $this->hintOptions = $this->htmlOptions['hintOptions'];
  152. unset($this->htmlOptions['hintOptions']);
  153. }
  154. if (isset($this->htmlOptions['errorOptions']))
  155. {
  156. $this->errorOptions = $this->htmlOptions['errorOptions'];
  157. unset($this->htmlOptions['errorOptions']);
  158. }
  159. if (isset($this->htmlOptions['captchaOptions']))
  160. {
  161. $this->captchaOptions = $this->htmlOptions['captchaOptions'];
  162. unset($this->htmlOptions['captchaOptions']);
  163. }
  164. }
  165. /**
  166. * Runs the widget.
  167. * @throws CException if the widget type is invalid.
  168. */
  169. public function run()
  170. {
  171. switch ($this->type)
  172. {
  173. case self::TYPE_CHECKBOX:
  174. $this->checkBox();
  175. break;
  176. case self::TYPE_CHECKBOXLIST:
  177. $this->checkBoxList();
  178. break;
  179. case self::TYPE_CHECKBOXLIST_INLINE:
  180. $this->checkBoxListInline();
  181. break;
  182. case self::TYPE_DROPDOWN:
  183. $this->dropDownList();
  184. break;
  185. case self::TYPE_FILE:
  186. $this->fileField();
  187. break;
  188. case self::TYPE_PASSWORD:
  189. $this->passwordField();
  190. break;
  191. case self::TYPE_RADIO:
  192. $this->radioButton();
  193. break;
  194. case self::TYPE_RADIOLIST:
  195. $this->radioButtonList();
  196. break;
  197. case self::TYPE_RADIOLIST_INLINE:
  198. $this->radioButtonListInline();
  199. break;
  200. case self::TYPE_TEXTAREA:
  201. $this->textArea();
  202. break;
  203. case self::TYPE_TEXT:
  204. $this->textField();
  205. break;
  206. case self::TYPE_CAPTCHA:
  207. $this->captcha();
  208. break;
  209. case self::TYPE_UNEDITABLE:
  210. $this->uneditableField();
  211. break;
  212. case self::TYPE_DATEPICKER:
  213. $this->datepickerField();
  214. break;
  215. case self::TYPE_REDACTOR:
  216. $this->redactorJs();
  217. break;
  218. case self::TYPE_MARKDOWNEDITOR:
  219. $this->markdownEditorJs();
  220. break;
  221. case self::TYPE_HTML5EDITOR:
  222. $this->html5Editor();
  223. break;
  224. case self::TYPE_DATERANGEPICKER:
  225. $this->dateRangeField();
  226. break;
  227. case self::TYPE_TOGGLEBUTTON:
  228. $this->toggleButton();
  229. break;
  230. case self::TYPE_COLORPICKER:
  231. $this->colorpickerField();
  232. break;
  233. case self::TYPE_CKEDITOR:
  234. $this->ckEditor();
  235. break;
  236. // Adding timepicker (Sergii)
  237. case self::TYPE_TIMEPICKER:
  238. $this->timepickerField();
  239. break;
  240. case self::TYPE_SELECT2:
  241. $this->select2Field();
  242. break;
  243. default:
  244. throw new CException(__CLASS__ . ': Failed to run widget! Type is invalid.');
  245. }
  246. }
  247. /**
  248. * Returns the label for the input.
  249. * @return string the label
  250. */
  251. protected function getLabel()
  252. {
  253. if ($this->label !== false && !in_array($this->type, array('checkbox', 'radio')) && $this->hasModel())
  254. return $this->form->labelEx($this->model, $this->attribute, $this->labelOptions);
  255. else if ($this->label !== null)
  256. return $this->label;
  257. else
  258. return '';
  259. }
  260. /**
  261. * Returns the prepend element for the input.
  262. * @return string the element
  263. */
  264. protected function getPrepend()
  265. {
  266. if ($this->hasAddOn())
  267. {
  268. $htmlOptions = $this->prependOptions;
  269. if (isset($htmlOptions['class']))
  270. $htmlOptions['class'] .= ' add-on';
  271. else
  272. $htmlOptions['class'] = 'add-on';
  273. ob_start();
  274. echo '<div class="' . $this->getAddonCssClass() . '">';
  275. if (isset($this->prependText))
  276. echo CHtml::tag('span', $htmlOptions, $this->prependText);
  277. return ob_get_clean();
  278. } else
  279. return '';
  280. }
  281. /**
  282. * Returns the append element for the input.
  283. * @return string the element
  284. */
  285. protected function getAppend()
  286. {
  287. if ($this->hasAddOn())
  288. {
  289. $htmlOptions = $this->appendOptions;
  290. if (isset($htmlOptions['class']))
  291. $htmlOptions['class'] .= ' add-on';
  292. else
  293. $htmlOptions['class'] = 'add-on';
  294. ob_start();
  295. if (isset($this->appendText))
  296. echo CHtml::tag('span', $htmlOptions, $this->appendText);
  297. echo '</div>';
  298. return ob_get_clean();
  299. } else
  300. return '';
  301. }
  302. /**
  303. * Returns the id that should be used for the specified attribute
  304. * @param string $attribute the attribute
  305. * @return string the id
  306. */
  307. protected function getAttributeId($attribute)
  308. {
  309. return isset($this->htmlOptions['id'])
  310. ? $this->htmlOptions['id']
  311. : CHtml::getIdByName(CHtml::resolveName($this->model, $attribute));
  312. }
  313. /**
  314. * Returns the error text for the input.
  315. * @return string the error text
  316. */
  317. protected function getError()
  318. {
  319. return $this->form->error($this->model, $this->attribute, $this->errorOptions);
  320. }
  321. /**
  322. * Returns the hint text for the input.
  323. * @return string the hint text
  324. */
  325. protected function getHint()
  326. {
  327. if (isset($this->hintText))
  328. {
  329. $htmlOptions = $this->hintOptions;
  330. if (isset($htmlOptions['class']))
  331. $htmlOptions['class'] .= ' help-block';
  332. else
  333. $htmlOptions['class'] = 'help-block';
  334. return CHtml::tag('p', $htmlOptions, $this->hintText);
  335. } else
  336. return '';
  337. }
  338. /**
  339. * Returns the container CSS class for the input.
  340. * @return string the CSS class
  341. */
  342. protected function getContainerCssClass()
  343. {
  344. $attribute = $this->attribute;
  345. return $this->model->hasErrors(CHtml::resolveName($this->model, $attribute)) ? CHtml::$errorCss : '';
  346. }
  347. /**
  348. * Returns the input container CSS classes.
  349. * @return string the CSS class
  350. */
  351. protected function getAddonCssClass()
  352. {
  353. $classes = array();
  354. if (isset($this->prependText))
  355. $classes[] = 'input-prepend';
  356. if (isset($this->appendText))
  357. $classes[] = 'input-append';
  358. return implode(' ', $classes);
  359. }
  360. /**
  361. * Returns whether the input has an add-on (prepend and/or append).
  362. * @return boolean the result
  363. */
  364. protected function hasAddOn()
  365. {
  366. return isset($this->prependText) || isset($this->appendText);
  367. }
  368. /**
  369. * Renders a checkbox.
  370. * @return string the rendered content
  371. * @abstract
  372. */
  373. abstract protected function checkBox();
  374. /**
  375. * Renders a toggle button.
  376. * @return string the rendered content
  377. * @abstract
  378. */
  379. abstract protected function toggleButton();
  380. /**
  381. * Renders a list of checkboxes.
  382. * @return string the rendered content
  383. * @abstract
  384. */
  385. abstract protected function checkBoxList();
  386. /**
  387. * Renders a list of inline checkboxes.
  388. * @return string the rendered content
  389. * @abstract
  390. */
  391. abstract protected function checkBoxListInline();
  392. /**
  393. * Renders a drop down list (select).
  394. * @return string the rendered content
  395. * @abstract
  396. */
  397. abstract protected function dropDownList();
  398. /**
  399. * Renders a file field.
  400. * @return string the rendered content
  401. * @abstract
  402. */
  403. abstract protected function fileField();
  404. /**
  405. * Renders a password field.
  406. * @return string the rendered content
  407. * @abstract
  408. */
  409. abstract protected function passwordField();
  410. /**
  411. * Renders a radio button.
  412. * @return string the rendered content
  413. * @abstract
  414. */
  415. abstract protected function radioButton();
  416. /**
  417. * Renders a list of radio buttons.
  418. * @return string the rendered content
  419. * @abstract
  420. */
  421. abstract protected function radioButtonList();
  422. /**
  423. * Renders a list of inline radio buttons.
  424. * @return string the rendered content
  425. * @abstract
  426. */
  427. abstract protected function radioButtonListInline();
  428. /**
  429. * Renders a textarea.
  430. * @return string the rendered content
  431. * @abstract
  432. */
  433. abstract protected function textArea();
  434. /**
  435. * Renders a text field.
  436. * @return string the rendered content
  437. * @abstract
  438. */
  439. abstract protected function textField();
  440. /**
  441. * Renders a CAPTCHA.
  442. * @return string the rendered content
  443. * @abstract
  444. */
  445. abstract protected function captcha();
  446. /**
  447. * Renders an uneditable field.
  448. * @return string the rendered content
  449. * @abstract
  450. */
  451. abstract protected function uneditableField();
  452. /**
  453. * Renders a datepicker field.
  454. * @return string the rendered content
  455. * @abstract
  456. */
  457. abstract protected function datepickerField();
  458. /**
  459. * Renders a redactorJS wysiwyg field.
  460. * @abstract
  461. * @return mixed
  462. */
  463. abstract protected function redactorJs();
  464. /**
  465. * Renders a markdownEditorJS wysiwyg field.
  466. * @abstract
  467. * @return mixed
  468. */
  469. abstract protected function markdownEditorJs();
  470. /**
  471. * Renders a bootstrap CKEditor wysiwyg editor.
  472. * @abstract
  473. * @return mixed
  474. */
  475. abstract protected function ckEditor();
  476. /**
  477. * Renders a bootstrap wysihtml5 editor.
  478. * @abstract
  479. * @return mixed
  480. */
  481. abstract protected function html5Editor();
  482. /**
  483. * Renders a daterange picker field
  484. * @abstract
  485. * @return mixed
  486. */
  487. abstract protected function dateRangeField();
  488. /**
  489. * Renders a colorpicker field.
  490. * @return string the rendered content
  491. * @abstract
  492. */
  493. abstract protected function colorpickerField();
  494. /**
  495. * Renders a timepicker field.
  496. * @return string the rendered content
  497. * @abstract
  498. */
  499. abstract protected function timepickerField();
  500. /**
  501. * Renders a select2 field.
  502. * @return mixed
  503. */
  504. abstract protected function select2Field();
  505. }