public function editAction()
 {
     $orgInfoModel = new Default_Model_Organisationinfo();
     $getorgData = $orgInfoModel->getorgrecords();
     $sitepreferencemodel = new Default_Model_Sitepreference();
     $activerecordArr = $sitepreferencemodel->getActiveRecord();
     $timezoneid = !empty($activerecordArr[0]['timezoneid']) ? $activerecordArr[0]['timezoneid'] : '';
     $popConfigPermission = array();
     $organisationHead = array();
     if (!empty($getorgData)) {
         $orgdata = '';
         $auth = Zend_Auth::getInstance();
         if ($auth->hasIdentity()) {
             $loginUserId = $auth->getStorage()->read()->id;
             $loginUserRole = $auth->getStorage()->read()->emprole;
             $loginUserGroup = $auth->getStorage()->read()->group_id;
         }
         if (sapp_Global::_checkprivileges(TIMEZONE, $loginUserGroup, $loginUserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'timezone');
         }
         if (sapp_Global::_checkprivileges(COUNTRIES, $loginUserGroup, $loginUserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'country');
         }
         if (sapp_Global::_checkprivileges(STATES, $loginUserGroup, $loginUserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'state');
         }
         if (sapp_Global::_checkprivileges(CITIES, $loginUserGroup, $loginUserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'city');
         }
         if (sapp_Global::_checkprivileges(EMPLOYEE, $loginUserGroup, $loginUserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'employee');
         }
         $msgarray = array();
         $flag = 'true';
         $id = $this->getRequest()->getParam('id');
         $callval = $this->getRequest()->getParam('call');
         $deptModel = new Default_Model_Departments();
         $deptform = new Default_Form_departments();
         $statesmodel = new Default_Model_States();
         $citiesmodel = new Default_Model_Cities();
         $countriesModel = new Default_Model_Countries();
         $statesmodel = new Default_Model_States();
         $citiesmodel = new Default_Model_Cities();
         $timezonemodel = new Default_Model_Timezone();
         $businessunitsmodel = new Default_Model_Businessunits();
         $orgInfoModel = new Default_Model_Organisationinfo();
         $employeeModal = new Default_Model_Employee();
         $allTimezoneData = $timezonemodel->fetchAll('isactive=1', 'timezone')->toArray();
         $allCountriesData = $countriesModel->fetchAll('isactive=1', 'country')->toArray();
         $allStatesData = $statesmodel->fetchAll('isactive=1', 'state')->toArray();
         $allCitiesData = $citiesmodel->fetchAll('isactive=1', 'city')->toArray();
         $allBusinessunitsData = $businessunitsmodel->fetchAll('isactive=1', 'unitname')->toArray();
         $deptData = array();
         $deptform->setAttrib('action', BASE_URL . 'departments/edit');
         $country = $getorgData[0]['country'];
         if (isset($_POST['country'])) {
             $country = $_POST['country'];
         }
         $state = $getorgData[0]['state'];
         if (isset($_POST['state'])) {
             $state = $_POST['state'];
         }
         $city = $getorgData[0]['city'];
         if (isset($_POST['city'])) {
             $city = $_POST['city'];
         }
         $deptform->setDefault('timezone', $timezoneid);
         $address = $getorgData[0]['address1'];
         $organisationHead = $employeeModal->getCurrentOrgHead();
         if (isset($country) && $country != 0 && $country != '') {
             $deptform->setDefault('country', $country);
             $statesData = $statesmodel->getBasicStatesList($country);
             foreach ($statesData as $res) {
                 $deptform->state->addMultiOption($res['state_id_org'], utf8_encode($res['state']));
             }
             if (isset($state) && $state != 0 && $state != '') {
                 $deptform->setDefault('state', $state);
             }
         }
         if (isset($state) && $state != 0 && $state != '') {
             $citiesData = $citiesmodel->getBasicCitiesList($state);
             foreach ($citiesData as $res) {
                 $deptform->city->addMultiOption($res['city_org_id'], utf8_encode($res['city']));
             }
             if (isset($city) && $city != 0 && $city != '') {
                 $deptform->setDefault('city', $city);
             }
         }
         if (isset($address) && $address != '') {
             $deptform->address1->setValue($address);
         }
         if (is_numeric($id) && $id > 0) {
             $data = $deptModel->getSingleDepartmentData($id);
             if (!empty($data)) {
                 $deptform->setAttrib('action', BASE_URL . 'departments/edit/id/' . $id);
                 $managementUsersData = $deptModel->getDepartmenttHead($data['depthead']);
                 foreach ($managementUsersData as $mgmtdata) {
                     $deptform->depthead->addMultiOption($mgmtdata['user_id'], $mgmtdata['userfullname']);
                 }
                 $deptform->populate($data);
                 $deptform->setDefault('depthead', $data['depthead']);
                 $deptform->submit->setLabel('Update');
                 $deptform->state->clearMultiOptions();
                 $deptform->city->clearMultiOptions();
                 $deptform->state->addMultiOption('', utf8_encode("Select State"));
                 $deptform->city->addMultiOption('', utf8_encode("Select City"));
                 $countryId = $data['country'];
                 if (isset($_POST['country'])) {
                     $countryId = $_POST['country'];
                 }
                 $stateId = $data['state'];
                 if (isset($_POST['state'])) {
                     $stateId = $_POST['state'];
                 }
                 $cityId = $data['city'];
                 if (isset($_POST['city'])) {
                     $cityId = $_POST['city'];
                 }
                 if ($countryId != '') {
                     $statesData = $statesmodel->getBasicStatesList($countryId);
                     foreach ($statesData as $res) {
                         $deptform->state->addMultiOption($res['state_id_org'], utf8_encode($res['state']));
                     }
                     $deptform->setDefault('country', $countryId);
                     $deptform->setDefault('state', $stateId);
                 }
                 if ($stateId != '') {
                     $citiesData = $citiesmodel->getBasicCitiesList($stateId);
                     foreach ($citiesData as $res) {
                         $deptform->city->addMultiOption($res['city_org_id'], utf8_encode($res['city']));
                     }
                     $deptform->setDefault('city', $cityId);
                 }
                 if ($data["startdate"] != '') {
                     $st_date = sapp_Global::change_date($data["startdate"], 'view');
                     $deptform->setDefault('start_date', $st_date);
                 }
                 $this->view->ermsg = '';
                 $this->view->datarr = $data;
             } else {
                 $this->view->ermsg = 'nodata';
             }
         } else {
             if ($id != '') {
                 $this->view->ermsg = 'nodata';
             } else {
                 $managementUsersData = $deptModel->getDepartmenttHead('');
                 foreach ($managementUsersData as $mgmtdata) {
                     $deptform->depthead->addMultiOption($mgmtdata['user_id'], $mgmtdata['userfullname']);
                 }
             }
         }
         $this->view->deptData = sizeof($deptData);
         $this->view->form = $deptform;
         if (!empty($allBusinessunitsData) && !empty($allCountriesData) && !empty($allStatesData) && !empty($allCitiesData) && !empty($allTimezoneData)) {
             $this->view->configuremsg = '';
         } else {
             $this->view->configuremsg = 'notconfigurable';
         }
         if (empty($allCountriesData)) {
             $msgarray['country'] = 'Countries are not configured yet.';
             $flag = 'false';
         }
         if (empty($allStatesData)) {
             $msgarray['state'] = 'States are not configured yet.';
             $flag = 'false';
         }
         if (empty($allCitiesData)) {
             $msgarray['city'] = 'Cities are not configured yet.';
             $flag = 'false';
         }
         if (empty($allBusinessunitsData)) {
             $msgarray['unitid'] = 'Business units are not added yet.';
             $flag = 'false';
         }
         if (empty($allTimezoneData)) {
             $msgarray['timezone'] = 'Time zones are not configured yet.';
             $flag = 'false';
         }
         $this->view->msgarray = $msgarray;
         if ($this->getRequest()->getPost()) {
             $deptname = trim($this->_request->getParam('deptname'));
             $unitid = $this->_request->getParam('unitid');
             if ($deptname != '' && $unitid != '') {
                 if (!preg_match('/^[a-zA-Z.\\- ?]+$/', $deptname)) {
                     $msgarray['deptname'] = "Please enter valid department name.";
                     $flag = 'false';
                 } else {
                     $checkExists = $deptModel->checkExistance($deptname, $unitid, $id);
                     if ($checkExists != 0) {
                         $msgarray['deptname'] = "Department name already exists.";
                         $flag = 'false';
                     }
                 }
             } else {
                 $flag = 'false';
             }
             $start_date = $this->_request->getParam('start_date', null);
             $start_date = sapp_Global::change_date($start_date, 'database');
             if ($deptform->isValid($this->_request->getPost()) && $flag == 'true') {
                 $deptname = $this->_request->getParam('deptname');
                 $deptcode = $this->_request->getParam('deptcode');
                 $description = $this->_request->getParam('description');
                 $country = $this->_request->getParam('country');
                 $state = intval($this->_request->getParam('state'));
                 $city = $this->_request->getParam('city');
                 $address1 = $this->_request->getParam('address1');
                 $address2 = $this->_request->getParam('address2');
                 $address3 = $this->_request->getParam('address3');
                 $unitid = $this->_request->getParam('unitid');
                 $timezone = $this->_request->getParam('timezone');
                 $depthead = $this->_request->getParam('depthead');
                 $deptcodeExistance = $deptModel->checkCodeDuplicates($deptcode, $id);
                 if (!$deptcodeExistance) {
                     $date = new Zend_Date();
                     $actionflag = '';
                     $tableid = '';
                     $data = array('deptname' => trim($deptname), 'deptcode' => trim($deptcode), 'description' => trim($description), 'startdate' => $start_date != '' ? $start_date : NULL, 'country' => trim($country), 'state' => trim($state), 'city' => trim($city), 'address1' => trim($address1), 'address2' => trim($address2), 'address3' => trim($address3), 'timezone' => trim($timezone), 'unitid' => $unitid, 'depthead' => trim($depthead), 'modifiedby' => $loginUserId, 'modifieddate' => $date->get('yyyy-MM-dd HH:mm:ss'));
                     if ($id != '') {
                         $where = array('id=?' => $id);
                         $actionflag = 2;
                     } else {
                         $data['createdby'] = $loginUserId;
                         $data['createddate'] = $date->get('yyyy-MM-dd HH:mm:ss');
                         $data['isactive'] = 1;
                         $where = '';
                         $actionflag = 1;
                     }
                     $Id = $deptModel->SaveorUpdateDepartmentsUnits($data, $where);
                     /* Updating business unit and department for org head*/
                     $emp_data = array('businessunit_id' => $unitid, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
                     $emp_where = array('user_id=?' => $depthead);
                     if ($Id == 'update') {
                         $tableid = $id;
                         $emp_data['department_id'] = $id;
                         $this->_helper->getHelper("FlashMessenger")->addMessage("Department updated successfully.");
                     } else {
                         $tableid = $Id;
                         $emp_data['department_id'] = $Id;
                         $this->_helper->getHelper("FlashMessenger")->addMessage("Department added successfully.");
                     }
                     $employeeModal->SaveorUpdateEmployeeData($emp_data, $emp_where);
                     $menuID = DEPARTMENTS;
                     $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $tableid);
                     $this->_redirect('departments');
                 } else {
                     $msgarray['message'] = 'A Department, with the given code, already exists.';
                     $msgarray['msgtype'] = 'error';
                     $this->view->messages = $msgarray;
                 }
             } else {
                 $messages = $deptform->getMessages();
                 foreach ($messages as $key => $val) {
                     foreach ($val as $key2 => $val2) {
                         $msgarray[$key] = $val2;
                         break;
                     }
                     if (empty($allCountriesData)) {
                         $msgarray['country'] = 'Countries are not configured yet.';
                     }
                     if (empty($allStatesData)) {
                         $msgarray['state'] = 'States are not configured yet.';
                     }
                     if (empty($allCitiesData)) {
                         $msgarray['city'] = 'Cities are not configured yet.';
                     }
                     if (empty($allBusinessunitsData)) {
                         $msgarray['unitid'] = 'Business units are not added yet.';
                     }
                     if (empty($allTimezoneData)) {
                         $msgarray['timezone'] = 'Time zones are not configured yet.';
                     }
                 }
                 $this->view->msgarray = $msgarray;
             }
         }
     } else {
         $orgdata = 'noorgdata';
         $this->view->orgdata = $orgdata;
     }
     $this->view->popConfigPermission = $popConfigPermission;
     $this->view->organisationHead = $organisationHead;
 }