/**
  * @param string $language
  * @dataProvider languageDataProvider
  */
 public function testBundleHasFile2($language)
 {
     $assetManager = Yii::$app->getAssetManager();
     $assetManager->bundles[DatePickerLanguageAsset::className()] = ['language' => $language];
     $bundle = $assetManager->getBundle(DatePickerLanguageAsset::className());
     $this->assertInstanceOf('yii\\jui\\datepicker\\DatePickerLanguageAsset', $bundle);
     $this->assertArrayHasKey(0, $bundle->depends);
     $this->assertEquals('yii\\jui\\datepicker\\DatePickerAsset', $bundle->depends[0]);
     $this->assertArrayHasKey(0, $bundle->js);
     $this->assertFileExists($bundle->basePath . DIRECTORY_SEPARATOR . $bundle->js[0]);
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     $this->sourcePath = get_class_vars(DatePickerLanguageAsset::className())['sourcePath'];
     if (is_null($this->language)) {
         $this->language = Yii::$app->language;
         if (is_null($this->language)) {
             $this->language = 'en-US';
         }
     }
     if ($this->language != 'en-US' && $this->language != 'en') {
         $sourcePath = Yii::getAlias($this->sourcePath);
         $intlLoaded = extension_loaded('intl');
         $version = $intlLoaded ? 'intl-' . phpversion('intl') : 'fix';
         $jsFile = 'datepicker-' . $this->language . '-' . $version . '.js';
         if (!is_file($sourcePath . DIRECTORY_SEPARATOR . $jsFile)) {
             $clientOptions = ['monthNames' => [], 'monthNamesShort' => [], 'dayNames' => [], 'dayNamesShort' => [], 'dayNamesMin' => []];
             $formatter = Yii::$app->getFormatter();
             foreach (array_map(function ($month) {
                 return mktime(0, 0, 0, $month);
             }, range(1, 12)) as $timestamp) {
                 $clientOptions['monthNames'][] = $formatter->asDate($timestamp, 'MMMM');
                 $clientOptions['monthNamesShort'][] = $formatter->asDate($timestamp, 'MMM');
             }
             foreach (array_map(function ($day) {
                 return mktime(0, 0, 0, 11, $day, 2015);
             }, range(1, 7)) as $timestamp) {
                 $clientOptions['dayNames'][] = $formatter->asDate($timestamp, 'EEEE');
                 $clientOptions['dayNamesShort'][] = $formatter->asDate($timestamp, 'EEE');
                 if ($intlLoaded) {
                     $clientOptions['dayNamesMin'][] = $formatter->asDate($timestamp, 'EEEEEE');
                 }
             }
             $data = 'jQuery.datepicker.setDefaults(' . Json::htmlEncode($clientOptions) . ');';
             file_put_contents($sourcePath . DIRECTORY_SEPARATOR . $jsFile, $data);
         }
         $this->js[] = $jsFile;
     }
 }
 /**
  * @param int $mode
  * @param string $actual
  * @param string $expectedHtml
  * @param string $expectedJs
  */
 protected function checkExpected($mode, $actual, $expectedHtml, $expectedJs)
 {
     switch ($mode) {
         case static::MODE_MODEL_ATTRIBUTE:
         case static::MODE_MODEL_ATTRIBUTE_VALUE:
             $expectedHtml = '<div class="form-group field-testform-date">' . "\n" . $expectedHtml . "\n" . '</div>';
         case static::MODE_NAME_VALUE:
             $this->assertEquals($expectedHtml, $actual);
             $view = Yii::$app->getView();
             $this->assertArrayHasKey(DatePickerAsset::className(), $view->assetBundles);
             $this->assertArrayHasKey(DatePickerLanguageAsset::className(), $view->assetBundles);
             $this->assertArrayHasKey(DatePickerLanguageFixAsset::className(), $view->assetBundles);
             $this->assertArrayHasKey(View::POS_READY, $view->js);
             $jsKey = md5($expectedJs);
             $this->assertArrayHasKey($jsKey, $view->js[View::POS_READY]);
             $this->assertEquals($expectedJs, $view->js[View::POS_READY][$jsKey]);
             return;
         case static::MODE_NAME_VALUE_AJAX:
             $expectedHtml .= '<script>' . $expectedJs . '</script>';
             $this->assertEquals($expectedHtml, $actual);
             return;
         case static::MODE_MODEL_ATTRIBUTE_AJAX:
         case static::MODE_MODEL_ATTRIBUTE_VALUE_AJAX:
             $expectedHtml .= '<script>' . $expectedJs . '</script>';
             $expectedHtml = '<div class="form-group field-testform-date">' . "\n" . $expectedHtml . "\n" . '</div>';
             $this->assertEquals($expectedHtml, $actual);
             return;
     }
     throw new Exception();
 }
Пример #4
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     $hasModel = $this->hasModel();
     if ($hasModel) {
         if (array_key_exists('value', $this->options)) {
             $value = $this->options['value'];
         } else {
             $value = Html::getAttributeValue($this->model, $this->attribute);
         }
     } else {
         $value = $this->value;
     }
     if (is_int($value) || is_string($value) && strlen($value) || $value instanceof DateTime || $value instanceof DateTimeInterface) {
         $formatter = Yii::$app->getFormatter();
         try {
             $altValue = $formatter->asDate($value, $this->altDateFormat);
             $value = $formatter->asDate($value, $this->dateFormat);
         } catch (InvalidParamException $e) {
             $altValue = $value;
         }
     } else {
         $altValue = $value;
     }
     if ($hasModel) {
         $this->options = array_merge($this->options, ['name' => false, 'value' => $value]);
         $this->altOptions['value'] = $altValue;
         $output = Html::activeTextInput($this->model, $this->attribute, $this->options);
         $output .= Html::activeHiddenInput($this->model, $this->attribute, $this->altOptions);
     } else {
         $output = Html::textInput(false, $value, $this->options);
         $output .= Html::hiddenInput($this->name, $altValue, $this->altOptions);
     }
     $js = 'jQuery(\'#' . $this->options['id'] . '\').datepicker(' . Json::htmlEncode($this->clientOptions) . ');';
     if (Yii::$app->getRequest()->getIsAjax()) {
         $output .= Html::script($js);
     } else {
         $view = $this->getView();
         DatePickerAsset::register($view);
         DatePickerLanguageAsset::register($view);
         DatePickerLanguageFixAsset::register($view);
         $view->registerJs($js);
     }
     return $output;
 }