/**
  * @inheritdoc
  */
 public function run()
 {
     FlagIconAsset::register($this->getView());
     $locales = [];
     $ids = [];
     $countryRepository = new Country();
     $data = $countryRepository->findAll();
     foreach ($this->currencies as $value) {
         $key = $value->id;
         if (!empty($value->country_flag)) {
             $locales[$key] = FlagIcon::flag($value->country_flag);
         } else {
             // iterate data (countries list) to find country code with defined ($value->code) currency
             foreach ($data as $code => $country_value) {
                 if (strcasecmp($country_value->currency['code'], $value->code) == 0) {
                     $locales[$key] = FlagIcon::flag($code);
                     break;
                 }
             }
         }
         $ids[$key] = $value->code;
     }
     $currencyFormat = 'function currencyFormat(state) {
         var locales = ' . Json::encode($locales) . ';
         if (!state.id) { return state.text; }
         return locales[state.id] + " " + state.text;
     }';
     $escape = new JsExpression('function(m) { return m; }');
     $this->getView()->registerJs($currencyFormat, View::POS_HEAD);
     $this->options = array_merge(['placeholder' => Yii::$app->translate->t('select currency')], $this->options);
     $pluginOptions = array_merge(['templateResult' => new JsExpression('currencyFormat'), 'templateSelection' => new JsExpression('currencyFormat'), 'escapeMarkup' => $escape], $this->pluginOptions);
     $pluginEvents = array_merge([], $this->pluginEvents);
     return Select2::widget(['model' => $this->model, 'attribute' => $this->attribute, 'name' => $this->name, 'value' => $this->value, 'options' => $this->options, 'data' => $ids, 'pluginOptions' => $pluginOptions, 'pluginEvents' => $pluginEvents]);
 }
Esempio n. 2
0
    public function run()
    {
        FlagIconAsset::register($this->getView());
        $locales = [];
        $languages = [];
        if (!empty($this->countries)) {
            array_walk($this->countries, function (&$data) {
                $data = strtolower($data);
            });
        }
        $countryRepository = new Country();
        $data = $countryRepository->findAll();
        foreach ($data as $code => $lang) {
            if (empty($this->countries) || in_array(strtolower($code), $this->countries)) {
                $locales[$code] = FlagIcon::flag($code);
                $languages[$code] = $lang->name['english']['common'] . ' (' . reset($lang->name['native'])['common'] . ')';
            }
        }
        $format = '
        function format(state) {
            var locales = ' . Json::encode($locales) . ';
            if (!state.id) { return state.text; }

            return locales[state.id] + " " + state.text;
        }';
        $escape = new JsExpression('function(m) { return m; }');
        $this->getView()->registerJs($format, View::POS_HEAD);
        $this->options = array_merge(['placeholder' => Yii::$app->translate->t('Choose country')], $this->options);
        $pluginOptions = array_merge(['templateResult' => new JsExpression('format'), 'templateSelection' => new JsExpression('format'), 'escapeMarkup' => $escape], $this->pluginOptions);
        $pluginEvents = array_merge([], $this->pluginEvents);
        return Select2::widget(['model' => $this->model, 'attribute' => $this->attribute, 'name' => $this->name, 'value' => $this->value, 'options' => $this->options, 'data' => $languages, 'pluginOptions' => $pluginOptions, 'pluginEvents' => $pluginEvents]);
    }
Esempio n. 3
0
 public function init()
 {
     parent::init();
     if (!$this->model) {
         throw new \Exception('model not found');
     }
     $view = $this->getView();
     if ($this->format === null) {
         $this->_showLocal = true;
     }
     FlagIconAsset::register($view);
 }
Esempio n. 4
0
 public function init()
 {
     FlagIconAsset::register($this->getView());
     if (isset($this->tabularFormRowIndex)) {
         if (!is_numeric($this->tabularFormRowIndex)) {
             throw new InvalidConfigException('`tabularFormRowIndex` must be numeric.');
         }
         $this->id .= "-{$this->tabularFormRowIndex}";
     }
     $this->s = ArrayHelper::merge(['class' => ['widgetContainer' => 'phone-input-container', 'col' => 'col-sm-12', 'inputMask' => 'input-mask']], $this->s);
     $this->_countryRepository = new Country();
     $callingCodes = $this->_countryRepository->getCallingCodes();
     if (!isset($this->buttonOptions['label'])) {
         $this->buttonOptions['label'] = Yii::$app->translate->t('choose country', 'unispot');
     }
     if (!isset($this->buttonOptions['containerOptions'])) {
         $this->buttonOptions['containerOptions'] = ['class' => 'input-group-btn'];
     }
     if (!isset($this->buttonOptions['options'])) {
         $this->buttonOptions['options'] = ['class' => 'btn-default'];
     }
     // Set Japan as defaule value
     $this->model->country_code = $this->model->country_code ?: 'JP';
     $inputId = Html::getInputId($this->model, 'country_code');
     if ($this->mask === null) {
         $this->mask = '9{3,15}';
         //  digital, length from 3 to 15
         if ($this->model->{$this->attribute} && $this->model->country_code) {
             $countryCode = strtoupper((string) $this->model->country_code);
             $this->mask = '+' . preg_replace('/(.)/i', "\\\\\$1", $callingCodes[$countryCode]) . $this->mask;
             if ($this->model->{$this->attribute} && strpos($this->model->{$this->attribute}, '+' . $callingCodes[$countryCode]) === 0) {
                 $this->model->{$this->attribute} = substr($this->model->{$this->attribute}, mb_strlen('+' . $callingCodes[$countryCode]));
             }
         }
     }
     if ($this->hint === null) {
         $this->hint = Yii::$app->translate->t('To choose your country enter your address below', 'unispot');
     }
     $this->options = array_merge(['id' => $this->id, 'class' => 'form-control', 'placeholder' => Yii::$app->translate->t('phone number...', 'unispot')], $this->options);
 }