Advertisement
oborudko

Untitled

Apr 8th, 2021
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\widgets;
  4.  
  5. use app\assets\DropdownDatePickerAsset;
  6. use app\models\plugins\DropdownDatePicker;
  7. use yii\widgets\InputWidget;
  8. use yii\helpers\Html;
  9. use yii\helpers\Json;
  10.  
  11. class DateDropdownInput extends InputWidget
  12. {
  13.     public $defaultDate;
  14.  
  15.     public $minDate = '1921-01-01';
  16.  
  17.     public $maxDate = '';
  18.  
  19.     public $pluginName = 'dropdownDatepicker';
  20.  
  21.     protected $pluginOptions;
  22.  
  23.     public function __construct(DropdownDatePicker $pluginOptions, $config = [])
  24.     {
  25.         $this->pluginOptions = $pluginOptions;
  26.  
  27.         if (!empty($config)) {
  28.             \Yii::configure($this, $config);
  29.         }
  30.         $this->init();
  31.     }
  32.  
  33.     public function init()
  34.     {
  35.         DropdownDatePickerAsset::register($this->view);
  36.         parent::init();
  37.  
  38.         $pluginOptions = $this->pluginOptions;
  39.         foreach ($pluginOptions->attributes() as $attribute) {
  40.             if ($this->hasProperty($attribute) && !empty($this->{$attribute})) {
  41.                 $pluginOptions->{$attribute} = $this->{$attribute};
  42.             }
  43.         }
  44.     }
  45.  
  46.  
  47.     public function run(): Void
  48.     {
  49.  
  50.         $view = $this->view;
  51.         $language = Json::htmlEncode(\Yii::$app->language);
  52.         $pluginName = $this->pluginName;
  53.         $pluginOptons = Json::htmlEncode($this->pluginOptions);
  54.         $inputId = $this->options['id'];
  55.  
  56.         echo Html::activehiddenInput($this->model, $this->attribute);
  57.  
  58.         $js = <<<JS
  59.         (function($) {
  60.             const localeData = moment.localeData($language);
  61.             $('#{$inputId}').{$pluginName}(${pluginOptons});
  62.         })(jQuery);
  63. JS;
  64.  
  65.         $view->registerJs($js);
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement