PageRenderTime 159ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbDatePicker.php

https://github.com/max-rautkin/yii-admincrud
PHP | 109 lines | 60 code | 15 blank | 34 comment | 13 complexity | dd2003aa868a719340b47a17faddab42 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /*## TbDatePicker widget class
  3. *
  4. * @author: antonio ramirez <antonio@clevertech.biz>
  5. * @copyright Copyright &copy; Clevertech 2012-
  6. * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
  7. * @package YiiBooster bootstrap.widgets
  8. */
  9. class TbDatePicker extends CInputWidget
  10. {
  11. /**
  12. * @var TbActiveForm when created via TbActiveForm.
  13. * This attribute is set to the form that renders the widget
  14. * @see TbActionForm->inputRow
  15. */
  16. public $form;
  17. /**
  18. * @var array the options for the Bootstrap JavaScript plugin.
  19. */
  20. public $options = array();
  21. /**
  22. * @var string[] the JavaScript event handlers.
  23. */
  24. public $events = array();
  25. /**
  26. *### .init()
  27. *
  28. * Initializes the widget.
  29. */
  30. public function init()
  31. {
  32. $this->htmlOptions['type'] = 'text';
  33. $this->htmlOptions['autocomplete'] = 'off';
  34. if (!isset($this->options['language'])) {
  35. $this->options['language'] = substr(Yii::app()->getLanguage(), 0, 2);
  36. }
  37. if (!isset($this->options['format'])) {
  38. $this->options['format'] = 'mm/dd/yyyy';
  39. }
  40. if (!isset($this->options['weekStart'])) {
  41. $this->options['weekStart'] = 0;
  42. } // Sunday
  43. }
  44. /**
  45. *### .run()
  46. *
  47. * Runs the widget.
  48. */
  49. public function run()
  50. {
  51. list($name, $id) = $this->resolveNameID();
  52. if ($this->hasModel()) {
  53. if ($this->form) {
  54. echo $this->form->textField($this->model, $this->attribute, $this->htmlOptions);
  55. } else {
  56. echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
  57. }
  58. } else {
  59. echo CHtml::textField($name, $this->value, $this->htmlOptions);
  60. }
  61. $this->registerClientScript();
  62. $this->registerLanguageScript();
  63. $options = !empty($this->options) ? CJavaScript::encode($this->options) : '';
  64. ob_start();
  65. echo "jQuery('#{$id}').datepicker({$options})";
  66. foreach ($this->events as $event => $handler) {
  67. echo ".on('{$event}', " . CJavaScript::encode($handler) . ")";
  68. }
  69. Yii::app()->getClientScript()->registerScript(__CLASS__ . '#' . $this->getId(), ob_get_clean() . ';');
  70. }
  71. /**
  72. *### .registerClientScript()
  73. *
  74. * Registers required client script for bootstrap datepicker. It is not used through bootstrap->registerPlugin
  75. * in order to attach events if any
  76. */
  77. public function registerClientScript()
  78. {
  79. Yii::app()->bootstrap->registerPackage('datepicker');
  80. }
  81. public function registerLanguageScript()
  82. {
  83. if (isset($this->options['language']) && $this->options['language'] != 'en') {
  84. $file = 'locales/bootstrap-datepicker.' . $this->options['language'] . '.js';
  85. if (@file_exists(Yii::getPathOfAlias('bootstrap.assets') . '/js/' . $file)) {
  86. if (Yii::app()->bootstrap->enableCdn) {
  87. Yii::app()->clientScript->registerScriptFile('//cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.0.2/js/locales/bootstrap-datepicker.' . $this->options['language'] . '.js', CClientScript::POS_END);
  88. } else {
  89. Yii::app()->bootstrap->registerAssetJs('locales/bootstrap-datepicker.' . $this->options['language'] . '.js');
  90. }
  91. }
  92. }
  93. }
  94. }