/**
  * View to get the provinces for a given country
  */
 public function getprovinces()
 {
     $view = $this->getView();
     $request = $this->getPageRequest();
     $selected_country = $request->getParameter(0) ? $request->getParameter(0) : REMOTE_COUNTRY;
     $view->mode = View::MODE_AJAX;
     $view->contenttype = View::CTYPE_JSON;
     $provinces = GeoProvinceModel::Find(['country = ' . $selected_country]);
     // Convert the provinces to JSON data so the javascript
     // can use it as if it had loaded from the server.
     $provincejs = [];
     foreach ($provinces as $p) {
         /** @var GeoProvinceModel $p */
         $provincejs[] = $p->getAsArray();
     }
     $view->jsondata = $provincejs;
 }
 /**
  * Populate this model with data directly from BeansBooks
  *
  * @param $address
  *
  * @throws Exception
  */
 public function setFromBeansObject($address)
 {
     /** @noinspection PhpUndefinedNamespaceInspection This method is only available if the BeansBooks module is installed. */
     if (!($address instanceof \BeansBooks\Objects\CustomerAddress || $address instanceof \BeansBooks\Objects\VendorAddress)) {
         throw new Exception('Please only set an address from a BeansBooks CustomerAddress or VendorAddress.');
     }
     $keys = $this->getBeansKeys();
     foreach ($keys as $rk => $lk) {
         $rv = $address->get($rk);
         if ($lk == 'state') {
             if (strlen($rv) > 3) {
                 // They typed in the state name here?
                 // This needs to be the state code.
                 $province = GeoProvinceModel::Find(['country = ' . $address->get('country'), 'name = ' . $rv], 1);
                 if ($province) {
                     $rv = $province->get('code');
                 }
             }
         }
         $this->set($lk, $rv);
     }
 }
 public function render()
 {
     // Make sure that some defaults are set first.
     if (!$this->get('name')) {
         $this->set('name', 'address');
     }
     if ($this->_model && ($this->_model->exists() || $this->_model->changed())) {
         // There is a valid model set, I can pull all the values from that!
         // This should also be used if the model was created but doesn't exist in the database, but was changed.
         // ie: a user entered information on a new model, but had an error that kicked it back.
         // that model may not exist, but it has been changed with the user's data, and so needs to be preserved.
         $v = $this->_model->getAsArray();
     } else {
         // There is no model currently set, fine... I'll just use some defaults.
         $v = ['id' => '', 'label' => '', 'address1' => '', 'address2' => '', 'city' => REMOTE_CITY, 'province' => REMOTE_PROVINCE, 'postal' => '', 'country' => REMOTE_COUNTRY];
     }
     $id = $v['id'];
     $label = $v['label'];
     $address1 = $v['address1'];
     $address2 = $v['address2'];
     $city = $v['city'];
     $province = $v['province'];
     $postal = $v['postal'];
     $country = $v['country'];
     // Get the provinces for the given selected country, (to save an ajax call)
     $provinces = GeoProvinceModel::Find(['country = ' . $country]);
     $countries = GeoCountryModel::Find(null, null, 'name');
     // Convert the provinces to JSON data so the javascript
     // can use it as if it had loaded from the server.
     $provincejs = [];
     foreach ($provinces as $p) {
         /** @var GeoProvinceModel $p */
         $provincejs[] = $p->getAsArray();
     }
     $file = $this->getTemplateName();
     $tpl = \Core\Templates\Template::Factory($file);
     $tpl->assign('id', $id);
     $tpl->assign('use_label', $this->_attributes['use_label']);
     $tpl->assign('label', $label);
     $tpl->assign('address1', $address1);
     $tpl->assign('address2', $address2);
     $tpl->assign('city', $city);
     $tpl->assign('province', $province);
     $tpl->assign('postal', $postal);
     $tpl->assign('country', $country);
     $tpl->assign('provinces', $provinces);
     $tpl->assign('province_json', json_encode($provincejs));
     $tpl->assign('countries', $countries);
     $tpl->assign('element', $this);
     $tpl->assign('req', $this->get('required'));
     return $tpl->fetch();
 }