Esempio n. 1
0
 public function addnewcityAction()
 {
     Zend_Layout::getMvcInstance()->setLayoutPath(APPLICATION_PATH . "/layouts/scripts/popup/");
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
     }
     $id = $this->getRequest()->getParam('id');
     $selectedcountryid = $this->_request->getParam('selectcountryid');
     $selectedstateid = $this->_request->getParam('selectstateid');
     $msgarray = array();
     $setDefaultString = '';
     $citystring = '';
     $controllername = 'cities';
     $citiesform = new Default_Form_cities();
     $citiesmodel = new Default_Model_Cities();
     $countriesModel = new Default_Model_Countries();
     $statesmodel = new Default_Model_States();
     /* Changing the form */
     $citiesform->setAction(BASE_URL . 'cities/addnewcity/selectcountryid/' . $selectedcountryid . '/selectstateid/' . $selectedstateid);
     $citiesform->removeElement('city');
     $citiesform->addElement('text', 'city', array('label' => 'City', 'maxlength' => '20', 'required' => true, 'validators' => array(array('validator' => 'NotEmpty', 'options' => array('messages' => 'Please enter city name.')))));
     /* END */
     $countrieslistArr = $countriesModel->getTotalCountriesList('');
     if (sizeof($countrieslistArr) > 0) {
         $citiesform->countryid->addMultiOption('', 'Select Country');
         foreach ($countrieslistArr as $countrieslistres) {
             if ($selectedcountryid != '') {
                 if ($countrieslistres['id'] == $selectedcountryid) {
                     $citiesform->countryid->addMultiOption($countrieslistres['id'], utf8_encode($countrieslistres['country_name']));
                     $citiesform->setDefault('countryid', $selectedcountryid);
                 }
             } else {
                 $citiesform->countryid->addMultiOption($countrieslistres['id'], utf8_encode($countrieslistres['country_name']));
             }
             $citiesform->countryid->setAttrib('onchange', 'displayParticularState_normal(this,"","state","city")');
         }
     } else {
         $msgarray['countryid'] = 'Countries are not configured yet.';
     }
     $countryid = $this->_request->getParam('countryid');
     if (isset($selectedcountryid) && $selectedcountryid != '' || isset($countryid) && $countryid != '') {
         if ($countryid) {
             $statesmodeldata = $statesmodel->getStatesList($countryid, '');
         } else {
             $statesmodeldata = $statesmodel->getStatesList($selectedcountryid, '');
         }
         $citiesform->state->addMultiOption('', 'Select State');
         foreach ($statesmodeldata as $res) {
             $citiesform->state->addMultiOption($res['id'], utf8_encode($res['state_name']));
             if ($selectedstateid != '') {
                 if ($res['id'] == $selectedstateid) {
                     $setDefaultString = $res['id'];
                 }
             } else {
                 if ($countryid != '') {
                     if ($res['id'] == $countryid) {
                         $setDefaultString = $res['id'];
                     }
                 }
             }
         }
         $citiesform->setDefault('state', $setDefaultString);
     } else {
         $citiesform->state->addMultiOption('', 'Select State');
     }
     if ($this->getRequest()->getPost()) {
         $id = $this->_request->getParam('id');
         $errorflag = "true";
         $msgarray = array();
         $dbstate = '';
         $dbcountryid = '';
         $stateidstr = $this->_request->getParam('state');
         $stateid = intval($stateidstr);
         $city = $this->_request->getParam('city');
         if (isset($stateid)) {
             $isDuplicateCityNameArr = $citiesmodel->getDuplicateCityName($city, $stateid);
             if ($isDuplicateCityNameArr[0]['count'] > 0) {
                 $errorflag = "false";
                 $msgarray['city'] = "City already exists.";
             }
         } else {
             $errorflag = "false";
             $msgarray['state'] = "Please select state.";
         }
         if ($citiesform->isValid($this->_request->getPost()) && $errorflag == "true") {
             $menumodel = new Default_Model_Menu();
             $city = $this->_request->getParam('city');
             $actionflag = '';
             $tableid = '';
             $NewCityId = $citiesmodel->SaveMainCityData($stateid, $city);
             $actionflag = 1;
             $tableid = $NewCityId;
             $menuidArr = $menumodel->getMenuObjID('/cities');
             $menuID = $menuidArr[0]['id'];
             $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $tableid);
             if (isset($selectedstateid) && isset($selectedcountryid)) {
                 $cityData = $citiesmodel->getCitiesList($selectedstateid, 'city');
             } else {
                 $cityData = array();
             }
             $opt = '';
             foreach ($cityData as $record) {
                 $opt .= sapp_Global::selectOptionBuilder($record['id'], $record['city_name']);
             }
             $this->view->cityData = $opt;
             $this->view->eventact = 'added';
             $close = 'close';
             $this->view->popup = $close;
         } else {
             $messages = $citiesform->getMessages();
             foreach ($messages as $key => $val) {
                 foreach ($val as $key2 => $val2) {
                     $msgarray[$key] = $val2;
                     break;
                 }
             }
             if (isset($countryid) && $countryid != 0 && $countryid != '') {
                 $statesmodel = new Default_Model_States();
                 $statesmodeldata = $statesmodel->getBasicStatesList($countryid);
                 foreach ($statesmodeldata as $res) {
                     $citiesform->state->addMultiOption($res['state_id_org'] . '!@#' . utf8_encode($res['state']), utf8_encode($res['state']));
                 }
                 if (isset($stateidstr) && $stateidstr != 0 && $stateidstr != '') {
                     $citiesform->setDefault('state', $stateidstr);
                 }
             }
         }
     }
     $this->view->controllername = $controllername;
     $this->view->ermsg = '';
     $this->view->form = $citiesform;
     $this->view->msgarray = $msgarray;
 }