/**
  * @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]);
 }
Example #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]);
    }
Example #3
0
 /**
  * Normalizing phone number
  *
  * @version 1.0.12 2015-09-25
  * @author Dmitry Fedorov <*****@*****.**>
  * @param string $phone
  * @param string $countryCode
  * @return string
  */
 public static function normalizePhone($phone, $countryCode)
 {
     if (empty(self::$_callingCodes)) {
         $countryRepository = new Country();
         self::$_callingCodes = $countryRepository->getCallingCodes();
     }
     if (!$countryCode) {
         $countryCode = 'JP';
     }
     $newPhone = static::PhoneUtil()->normalizeDigitsOnly($phone);
     $callingCode = self::$_callingCodes[strtoupper($countryCode)];
     if (!$newPhone || $newPhone == $callingCode) {
         return '';
     }
     if (strpos($newPhone, $callingCode) !== 0) {
         $newPhone = $callingCode . $newPhone;
     }
     return '+' . $newPhone;
 }
Example #4
0
    public function run()
    {
        $data = $this->_countryRepository->findAll();
        $items = [];
        foreach ($data as $code => $lang) {
            if ($this->countries === [] || in_array(strtolower($code), $this->countries)) {
                $locales[$code] = FlagIcon::flag($code);
                $items[$code] = ['label' => Html::a($locales[$code] . ' ' . $lang->name['english']['common'] . ' (' . reset($lang->name['native'])['common'] . ')', '#', ['class' => 'changePhoneMask', 'data-region' => $code])];
            }
        }
        $this->dropdown = array_merge($this->dropdown, ['items' => $items, 'encodeLabels' => false]);
        $callingCodes = $this->_countryRepository->getCallingCodes();
        $codes = Json::encode($callingCodes);
        $this->_js[] = <<<JS

var dialCodes = {$codes};
\$(".changePhoneMask").on("click", function () {

    var region = \$(this).data("region"),
        data = dialCodes[region],
        list =  \$(this).closest("ul"),
        container = list.parent(),
        widgetContainer = \$(this).closest(".{$this->s['class']['widgetContainer']}"),
        span = container ? container.prev() : false,
        input = \$(".{$this->s['class']['inputMask']}", widgetContainer);

    if(undefined !== data && !data.startsWith("+")) {
        data = "+" + data.replace(/(.)/g,"\\\\\$1");
    } else {
        data = "";
    }

    list.siblings("button").html(\$(this).html() + " <span class='caret'></span>");
    list.dropdown("toggle");

    input
        .prop("disabled", false)
        .inputmask({"mask": data + "9{3,15}"})
        .val("")
        .focus();

    return false;
});

\$(".changePhoneMask").click(function(event) {
    var widgetContainer = \$(this).closest(".{$this->s["class"]["widgetContainer"]}");
    \$("#" + widgetContainer.data("country-code-id")).val( \$(this).data("region") );
});

\$(".{$this->s["class"]["widgetContainer"]}").each(function(index) {
  var widgetContainer = \$(this),
    countryCode = \$("#" + widgetContainer.data("country-code-id")).val();

  if (countryCode) {
    var object = \$('.changePhoneMask[data-region=' + countryCode + ']', widgetContainer);
      object.closest('ul').siblings('button').html(object.html() + " <span class='caret'></span>");
  }
});
JS;
        $this->getView()->registerJs(join("\r", $this->_js));
        return $this->render('index', ['widget' => $this, 's' => $this->s]);
    }