예제 #1
0
 public function addnewstateAction()
 {
     Zend_Layout::getMvcInstance()->setLayoutPath(APPLICATION_PATH . "/layouts/scripts/popup/");
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
     }
     $msgarray = array();
     $controllername = 'states';
     $statestring = '';
     $id = $this->getRequest()->getParam('id');
     $selectedcountryid = $this->_request->getParam('selectcountryid', null);
     $statesform = new Default_Form_states();
     $statesmodel = new Default_Model_States();
     $countriesModel = new Default_Model_Countries();
     /* Changing the form */
     $statesform->setAction(BASE_URL . 'states/addnewstate/selectcountryid/' . $selectedcountryid);
     $statesform->removeElement('state');
     $statesform->addElement('text', 'state', array('label' => 'State', 'maxlength' => '20', 'required' => true, 'validators' => array(array('validator' => 'NotEmpty', 'options' => array('messages' => 'Please enter state name.')))));
     /* END */
     $countrieslistArr = $countriesModel->getTotalCountriesList('');
     if (sizeof($countrieslistArr) > 0) {
         $statesform->countryid->addMultiOption('', 'Select Country');
         foreach ($countrieslistArr as $countrieslistres) {
             if (isset($selectedcountryid)) {
                 if ($countrieslistres['id'] == $selectedcountryid) {
                     $statesform->countryid->addMultiOption($countrieslistres['id'], utf8_encode($countrieslistres['country_name']));
                     $statesform->setDefault('countryid', $selectedcountryid);
                 }
             } else {
                 $statesform->countryid->addMultiOption($countrieslistres['id'], utf8_encode($countrieslistres['country_name']));
             }
         }
     } else {
         $msgarray['countryid'] = 'Countries are not configured yet';
     }
     if ($this->getRequest()->getPost()) {
         $errorflag = "true";
         $msgarray = array();
         $id = $this->_request->getParam('id');
         $dbstate = '';
         $dbcountryid = '';
         $state = trim($this->_request->getParam('state'));
         $countryid = $this->_request->getParam('countryid');
         if (isset($countryid) && $countryid != '') {
             $isDuplicateStateNameArr = $statesmodel->getDuplicateStateName($state, $countryid);
             if ($isDuplicateStateNameArr[0]['count'] > 0) {
                 $errorflag = "false";
                 $msgarray['state'] = "State already exists.";
             }
         } else {
             $errorflag = "false";
             $msgarray['countryid'] = "Please select country.";
         }
         if ($statesform->isValid($this->_request->getPost()) && $errorflag == "true") {
             $menumodel = new Default_Model_Menu();
             $actionflag = '';
             $tableid = '';
             $NewStateId = $statesmodel->SaveMainStateData($countryid, $state);
             $tableid = $NewStateId;
             $actionflag = 1;
             $menuidArr = $menumodel->getMenuObjID('/states');
             $menuID = $menuidArr[0]['id'];
             $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $tableid);
             if (isset($selectedcountryid)) {
                 $stateData = $statesmodel->getStatesList($selectedcountryid, '');
             } else {
                 $stateData = array();
             }
             $opt = '';
             foreach ($stateData as $record) {
                 $opt .= sapp_Global::selectOptionBuilder($record['id'], $record['state_name']);
             }
             $this->view->stateData = $opt;
             $this->view->eventact = 'added';
             $close = 'close';
             $this->view->popup = $close;
         } else {
             $messages = $statesform->getMessages();
             foreach ($messages as $key => $val) {
                 foreach ($val as $key2 => $val2) {
                     $msgarray[$key] = $val2;
                     break;
                 }
             }
         }
     }
     $this->view->ermsg = '';
     $this->view->form = $statesform;
     $this->view->msgarray = $msgarray;
     $this->view->controllername = $controllername;
 }