public function addAction()
 {
     $emptyFlag = 0;
     $report_opt = array();
     $popConfigPermission = array();
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
         $loginuserRole = $auth->getStorage()->read()->emprole;
         $loginuserGroup = $auth->getStorage()->read()->group_id;
     }
     $employeeModel = new Default_Model_Employee();
     $currentOrgHead = $employeeModel->getCurrentOrgHead();
     if (empty($currentOrgHead)) {
         $this->addorganisationhead($loginUserId);
     } else {
         if (sapp_Global::_checkprivileges(PREFIX, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'prefix');
         }
         if (sapp_Global::_checkprivileges(IDENTITYCODES, $loginuserGroup, $loginuserRole, 'edit') == 'Yes') {
             array_push($popConfigPermission, 'identitycodes');
         }
         if (sapp_Global::_checkprivileges(EMPLOYMENTSTATUS, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'empstatus');
         }
         if (sapp_Global::_checkprivileges(JOBTITLES, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'jobtitles');
         }
         if (sapp_Global::_checkprivileges(POSITIONS, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
             array_push($popConfigPermission, 'position');
         }
         $this->view->popConfigPermission = $popConfigPermission;
         $employeeform = new Default_Form_employee();
         $usersModel = new Default_Model_Users();
         $employmentstatusModel = new Default_Model_Employmentstatus();
         $busineesUnitModel = new Default_Model_Businessunits();
         $user_model = new Default_Model_Usermanagement();
         $candidate_model = new Default_Model_Candidatedetails();
         $role_model = new Default_Model_Roles();
         $jobtitlesModel = new Default_Model_Jobtitles();
         $prefixModel = new Default_Model_Prefix();
         $msgarray = array();
         $identity_code_model = new Default_Model_Identitycodes();
         $identity_codes = $identity_code_model->getIdentitycodesRecord();
         $emp_identity_code = isset($identity_codes[0]) ? $identity_codes[0]['employee_code'] : "";
         if ($emp_identity_code != '') {
             $emp_id = $emp_identity_code . str_pad($user_model->getMaxEmpId($emp_identity_code), 4, '0', STR_PAD_LEFT);
         } else {
             $emp_id = '';
             $msgarray['employeeId'] = 'Identity codes are not configured yet.';
         }
         $employeeform->employeeId->setValue($emp_id);
         $employeeform->modeofentry->setValue('Direct');
         $roles_arr = $role_model->getRolesList_EMP();
         if (sizeof($roles_arr) > 0) {
             $employeeform->emprole->addMultiOptions(array('' => 'Select Role') + $roles_arr);
         } else {
             $employeeform->emprole->addMultiOptions(array('' => 'Select Role'));
             $msgarray['emprole'] = 'Roles are not configured yet.';
         }
         $candidate_options = $candidate_model->getCandidatesNamesForUsers();
         if (sizeof($candidate_options) > 0) {
             $employeeform->rccandidatename->addMultiOptions(array('' => 'Select Candidate') + $candidate_options);
         } else {
             $msgarray['rccandidatename'] = 'No candidates.';
         }
         $referedby_options = $user_model->getRefferedByForUsers();
         if (sizeof($referedby_options) > 0) {
             $employeeform->candidatereferredby->addMultiOptions(array('' => 'Select referred by') + $referedby_options);
         } else {
             $msgarray['candidatereferredby'] = 'Employees are not added yet.';
         }
         $employmentStatusData = $employmentstatusModel->getempstatusActivelist();
         $employeeform->emp_status_id->addMultiOption('', 'Select Employment Status');
         if (!empty($employmentStatusData)) {
             foreach ($employmentStatusData as $employmentStatusres) {
                 $employeeform->emp_status_id->addMultiOption($employmentStatusres['workcodename'], $employmentStatusres['statusname']);
             }
         } else {
             $msgarray['emp_status_id'] = 'Employment status is not configured yet.';
             $emptyFlag++;
         }
         $businessunitData = $busineesUnitModel->getDeparmentList();
         if (!empty($businessunitData)) {
             $employeeform->businessunit_id->addMultiOption('0', 'No Business Unit');
             foreach ($businessunitData as $businessunitres) {
                 $employeeform->businessunit_id->addMultiOption($businessunitres['id'], $businessunitres['unitname']);
             }
             $departmentsmodel = new Default_Model_Departments();
             $departmentlistArr = $departmentsmodel->getDepartmentList('0');
             $totalDeptList = $departmentsmodel->getTotalDepartmentList();
             $employeeform->department_id->clearMultiOptions();
             $employeeform->department_id->addMultiOption('', 'Select Department');
             if (count($departmentlistArr) > 0) {
                 foreach ($departmentlistArr as $departmentlistresult) {
                     $employeeform->department_id->addMultiOption($departmentlistresult['id'], utf8_encode($departmentlistresult['deptname']));
                 }
             }
             if (empty($totalDeptList)) {
                 $msgarray['department_id'] = 'Departments are not added yet.';
             }
             if (isset($department_id) && $department_id != 0 && $department_id != '') {
                 $employeeform->setDefault('department_id', $department_id);
             }
         } else {
             $msgarray['businessunit_id'] = 'Business units are not added yet.';
             $emptyFlag++;
         }
         $jobtitleData = $jobtitlesModel->getJobTitleList();
         $employeeform->jobtitle_id->addMultiOption('', 'Select Job Title');
         if (!empty($jobtitleData)) {
             foreach ($jobtitleData as $jobtitleres) {
                 $employeeform->jobtitle_id->addMultiOption($jobtitleres['id'], $jobtitleres['jobtitlename']);
             }
         } else {
             $msgarray['jobtitle_id'] = 'Job titles are not configured yet.';
             $msgarray['position_id'] = 'Positions are not configured yet.';
             $emptyFlag++;
         }
         $prefixData = $prefixModel->getPrefixList();
         $employeeform->prefix_id->addMultiOption('', 'Select Prefix');
         if (!empty($prefixData)) {
             foreach ($prefixData as $prefixres) {
                 $employeeform->prefix_id->addMultiOption($prefixres['id'], $prefixres['prefix']);
             }
         } else {
             $msgarray['prefix_id'] = 'Prefixes are not configured yet.';
             $emptyFlag++;
         }
         if (isset($_POST['emprole']) && isset($_POST['department_id']) && $_POST['emprole'] != '') {
             $role_split = preg_split('/_/', $_POST['emprole']);
             $reportingManagerData = $usersModel->getReportingManagerList_employees($_POST['department_id'], '', $role_split[1]);
             if (!empty($reportingManagerData)) {
                 $report_opt = $reportingManagerData;
                 if (isset($_POST['reporting_manager']) && $_POST['reporting_manager'] != '') {
                     $employeeform->setDefault('reporting_manager', $_POST['reporting_manager']);
                 }
             }
         }
         $employeeform->setAttrib('action', BASE_URL . 'employee/add');
         $this->view->form = $employeeform;
         $this->view->msgarray = $msgarray;
         $this->view->report_opt = $report_opt;
         $this->view->emptyFlag = $emptyFlag++;
         if ($this->getRequest()->getPost()) {
             $result = $this->save($employeeform);
             $this->view->msgarray = $result;
             $this->view->messages = $result;
         }
     }
 }
 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;
 }
 public function edit_oldAction()
 {
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
         $loginuserRole = $auth->getStorage()->read()->emprole;
         $loginuserGroup = $auth->getStorage()->read()->group_id;
     }
     $popConfigPermission = array();
     $empid = '';
     $orgheadsData = array();
     $oldOrgHead = '';
     $new_stateId = '';
     $actionpage = '';
     if (sapp_Global::_checkprivileges(PREFIX, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
         array_push($popConfigPermission, 'prefix');
     }
     if (sapp_Global::_checkprivileges(IDENTITYCODES, $loginuserGroup, $loginuserRole, 'edit') == 'Yes') {
         array_push($popConfigPermission, 'identitycodes');
     }
     if (sapp_Global::_checkprivileges(JOBTITLES, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
         array_push($popConfigPermission, 'jobtitles');
     }
     if (sapp_Global::_checkprivileges(POSITIONS, $loginuserGroup, $loginuserRole, 'add') == 'Yes') {
         array_push($popConfigPermission, 'position');
     }
     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, 'edit') == 'Yes') {
         array_push($popConfigPermission, 'employee');
     }
     $msgarray = array();
     $new_stateId = '';
     $new_cityId = '';
     $id = $this->getRequest()->getParam('id');
     $form = new Default_Form_Organisationinfo();
     $user_model = new Default_Model_Usermanagement();
     $orgInfoModel = new Default_Model_Organisationinfo();
     $countriesModel = new Default_Model_Countries();
     $statesmodel = new Default_Model_States();
     $citiesmodel = new Default_Model_Cities();
     $role_model = new Default_Model_Roles();
     $prefixModel = new Default_Model_Prefix();
     $identity_code_model = new Default_Model_Identitycodes();
     $jobtitlesModel = new Default_Model_Jobtitles();
     $employeeModal = new Default_Model_Employee();
     $positionsmodel = new Default_Model_Positions();
     $identity_codes = $identity_code_model->getIdentitycodesRecord();
     $role_data = $role_model->getRolesList_orginfo();
     $allCountriesData = $countriesModel->fetchAll('isactive=1', 'country')->toArray();
     $allStatesData = $statesmodel->fetchAll('isactive=1', 'state')->toArray();
     $allCitiesData = $citiesmodel->fetchAll('isactive=1', 'city')->toArray();
     $flag = 'true';
     $emp_identity_code = isset($identity_codes[0]) ? $identity_codes[0]['employee_code'] : "";
     if ($emp_identity_code != '') {
         $emp_id = $emp_identity_code . str_pad($user_model->getMaxEmpId($emp_identity_code), 4, '0', STR_PAD_LEFT);
     } else {
         $emp_id = '';
         $msgarray['employeeId'] = 'Identity codes are not configured yet.';
         $flag = 'false';
     }
     $form->employeeId->setValue($emp_id);
     $form->emprole->addMultiOptions(array('' => 'Select Role') + $role_data);
     if (empty($allCountriesData)) {
         $msgarray['country'] = 'Countries are not configured yet.';
         $flag = 'false';
     }
     if (empty($role_data)) {
         $msgarray['emprole'] = 'Roles are not added 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';
     }
     $prefixData = $prefixModel->getPrefixList();
     $form->prefix_id->addMultiOption('', 'Select Prefix');
     if (!empty($prefixData)) {
         foreach ($prefixData as $prefixres) {
             $form->prefix_id->addMultiOption($prefixres['id'], $prefixres['prefix']);
         }
     } else {
         $msgarray['prefix_id'] = 'Prefixes are not configured yet.';
     }
     $jobtitleData = $jobtitlesModel->getJobTitleList();
     if (!empty($jobtitleData)) {
         foreach ($jobtitleData as $jobtitleres) {
             $form->jobtitle_id->addMultiOption($jobtitleres['id'], $jobtitleres['jobtitlename']);
         }
     } else {
         $msgarray['jobtitle_id'] = 'Job titles are not configured yet.';
         $msgarray['position_id'] = 'Positions are not configured yet.';
     }
     $orgheadsData = $employeeModal->getEmployeesForOrgHead();
     $currentOrgHead = $employeeModal->getCurrentOrgHead();
     if (!empty($currentOrgHead)) {
         $oldOrgHead = $currentOrgHead[0]['user_id'];
     }
     if ($id) {
         $form->submit->setLabel('Update');
         try {
             $data = $orgInfoModel->getOrganisationData($id);
             $form->setAttrib('action', BASE_URL . 'organisationinfo/edit/id/' . $id);
             $data['org_startdate'] = sapp_Global::change_date($data['org_startdate'], 'view');
             $form->populate($data);
             $countryId = $data['country'];
             $stateId = $data['state'];
             $cityId = $data['city'];
             $actionpage = 'edit';
             if (count($_POST) > 0) {
                 $countryId = isset($_POST['country']) ? $_POST['country'] : "";
                 $stateId = isset($_POST['state']) ? $_POST['state'] : "";
                 $cityId = isset($_POST['city']) ? $_POST['city'] : "";
             }
             if ($countryId != '') {
                 $statesData = $statesmodel->getBasicStatesList((int) $countryId);
                 foreach ($statesData as $res) {
                     if ($stateId == $res['state_id_org']) {
                         $new_stateId = $res['state_id_org'] . '!@#' . utf8_encode($res['state']);
                     }
                     $form->state->addMultiOption($res['state_id_org'] . '!@#' . utf8_encode($res['state']), utf8_encode($res['state']));
                 }
                 if (count($_POST) == 0) {
                     $stateId = $new_stateId;
                 }
             }
             if ($stateId != '') {
                 $citiesData = $citiesmodel->getBasicCitiesList((int) $stateId);
                 foreach ($citiesData as $res) {
                     if ($cityId == $res['city_org_id']) {
                         $new_cityId = $res['city_org_id'] . '!@#' . utf8_encode($res['city']);
                     }
                     $form->city->addMultiOption($res['city_org_id'] . '!@#' . utf8_encode($res['city']), utf8_encode($res['city']));
                 }
                 if (count($_POST) == 0) {
                     $cityId = $new_cityId;
                 }
             }
             $emp_data = $employeeModal->fetchRow("is_orghead = 1");
             if (!empty($emp_data)) {
                 $user_data = $user_model->fetchRow("id = " . $emp_data->user_id);
                 if (!empty($user_data)) {
                     $form->setDefault('firstname_orghead', $user_data->firstname);
                     $form->setDefault('lastname_orghead', $user_data->lastname);
                     $form->setDefault('employeeId', $user_data->employeeId);
                     $form->setDefault('emprole', $user_data->emprole);
                     $form->setDefault('emailaddress', $user_data->emailaddress);
                     $form->setDefault('jobtitle_id', $user_data->jobtitle_id);
                     $form->setDefault('prefix_id', $emp_data->prefix_id);
                     $form->setDefault('date_of_joining', sapp_Global::change_date($emp_data->date_of_joining, 'view'));
                     $jobtitle_id = $emp_data->jobtitle_id;
                     if (isset($_POST['jobtitle_id'])) {
                         $jobtitle_id = $_POST['jobtitle_id'];
                     }
                     $form->position_id->addMultiOption('', 'Select a Position');
                     if ($jobtitle_id != '') {
                         $positionlistArr = $positionsmodel->getPositionList($jobtitle_id);
                         if (sizeof($positionlistArr) > 0) {
                             foreach ($positionlistArr as $positionlistres) {
                                 $form->position_id->addMultiOption($positionlistres['id'], $positionlistres['positionname']);
                             }
                         }
                     }
                     $form->setDefault('position_id', $emp_data->position_id);
                     $form->setDefault('orghead', $user_data->id);
                 }
                 $empid = $emp_data->user_id;
             } else {
                 $form->setDefault('orghead', '');
             }
             if (empty($orgheadsData)) {
                 $msgarray['orghead'] = 'Management employees are not added yet.';
             }
             $form->setDefault('country', $countryId);
             $form->setDefault('state', $stateId);
             $form->setDefault('city', $cityId);
             $this->view->domainValue = $data['domain'];
             $this->view->org_image = $data['org_image'];
             $this->view->ermsg = '';
             $this->view->datarr = $data;
             $this->view->user_id = $empid;
             $this->view->orgheadsData = $orgheadsData;
         } catch (Exception $e) {
             $this->view->ermsg = 'nodata';
         }
     } else {
         $actionpage = 'add';
         $activeOrgs = $orgInfoModel->getorgrecords();
         if (empty($activeOrgs)) {
             $form->setAttrib('action', BASE_URL . 'organisationinfo/edit');
             $country = $this->_request->getParam('country');
             $state = intVal($this->_request->getParam('state'));
             $city = intVal($this->_request->getParam('city'));
             if (isset($country) && $country != 0 && $country != '') {
                 $statesData = $statesmodel->getBasicStatesList($country);
                 foreach ($statesData as $res) {
                     $form->state->addMultiOption($res['state_id_org'] . '!@#' . utf8_encode($res['state']), utf8_encode($res['state']));
                 }
                 if (isset($state) && $state != 0 && $state != '') {
                     $form->setDefault('state', $state);
                 }
             }
             if (isset($state) && $state != 0 && $state != '') {
                 $citiesData = $citiesmodel->getBasicCitiesList($state);
                 foreach ($citiesData as $res) {
                     $form->city->addMultiOption($res['city_org_id'] . '!@#' . utf8_encode($res['city']), utf8_encode($res['city']));
                 }
                 if (isset($city) && $city != 0 && $city != '') {
                     $form->setDefault('city', $city);
                 }
             }
             $this->view->ermsg = '';
         } else {
             $this->view->ermsg = 'cannotadd';
         }
         $form->position_id->addMultiOption('', 'Select a Position');
         if (isset($_POST['jobtitle_id']) && $_POST['jobtitle_id'] != '') {
             $jobtitle_id = $_POST['jobtitle_id'];
             $positionlistArr = $positionsmodel->getPositionList($jobtitle_id);
             if (sizeof($positionlistArr) > 0) {
                 foreach ($positionlistArr as $positionlistres) {
                     $form->position_id->addMultiOption($positionlistres['id'], $positionlistres['positionname']);
                 }
             }
         }
         $emp_data = $employeeModal->fetchRow("is_orghead = 1");
         if (!empty($emp_data)) {
             $user_data = $user_model->fetchRow("id = " . $emp_data->user_id);
             if (!empty($user_data)) {
                 $form->setDefault('employeeId', $user_data->employeeId);
                 $form->setDefault('emprole', $user_data->emprole);
                 $form->setDefault('emailaddress', $user_data->emailaddress);
                 $form->setDefault('jobtitle_id', $user_data->jobtitle_id);
                 $form->setDefault('prefix_id', $emp_data->prefix_id);
                 $form->setDefault('date_of_joining', sapp_Global::change_date($emp_data->date_of_joining, 'view'));
                 $jobtitle_id = $emp_data->jobtitle_id;
                 if (isset($_POST['jobtitle_id'])) {
                     $jobtitle_id = $_POST['jobtitle_id'];
                 }
                 $form->position_id->addMultiOption('', 'Select a Position');
                 if ($jobtitle_id != '') {
                     $positionlistArr = $positionsmodel->getPositionList($jobtitle_id);
                     if (sizeof($positionlistArr) > 0) {
                         foreach ($positionlistArr as $positionlistres) {
                             $form->position_id->addMultiOption($positionlistres['id'], $positionlistres['positionname']);
                         }
                     }
                 }
                 $form->setDefault('position_id', $emp_data->position_id);
                 $form->setDefault('orghead', $user_data->id);
             }
             $empid = $emp_data->user_id;
         } else {
             $form->setDefault('orghead', '');
         }
         if (empty($orgheadsData)) {
             $msgarray['orghead'] = 'Management employees are not added yet.';
         }
         $this->view->user_id = $empid;
         $this->view->orgheadsData = $orgheadsData;
     }
     $this->view->form = $form;
     $this->view->currentOrgHead = $currentOrgHead;
     $this->view->actionpage = $actionpage;
     if (!empty($allCountriesData) && !empty($allStatesData) && !empty($allCitiesData)) {
         $this->view->configuremsg = '';
     } else {
         $this->view->configuremsg = 'notconfigurable';
     }
     if (isset($_POST['prevorgheadrm'])) {
         $prevorgheadrm = $this->_request->getParam('prevorgheadrm');
     } else {
         $prevorgheadrm = '';
     }
     $this->view->prevorgheadrmval = $prevorgheadrm;
     if (isset($_POST['rmflag'])) {
         $rmflag = $this->_request->getParam('rmflag');
     } else {
         $rmflag = '0';
     }
     $this->view->rmflag = $rmflag;
     if ($this->getRequest()->getPost()) {
         $imagerror = $this->_request->getParam('imgerr');
         $imagepath = $this->_request->getParam('org_image_value');
         $imgerrmsg = $this->_request->getParam('imgerrmsg');
         $pphnumber = $this->_request->getParam('phonenumber');
         $sphnumber = $this->_request->getParam('secondaryphone');
         $org_startdate = sapp_Global::change_date($this->_request->getParam('org_startdate'), 'database');
         $flag = 'true';
         if (isset($imagepath) && $imagepath != '') {
             $imageArr = explode('.', $imagepath);
             if (sizeof($imageArr) > 1) {
                 $imagename = $imageArr[0];
                 $imageext = $imageArr[1];
                 $extArr = array('gif', 'jpg', 'jpeg', 'png');
                 if (!in_array($imageext, $extArr)) {
                     $msgarray['org_image_value'] = 'Please upload an appropriate image file.';
                     $flag = 'false';
                 }
             } else {
                 $msgarray['org_image_value'] = 'Please upload an appropriate image file.';
                 $flag = 'false';
             }
         }
         if ($imagerror == 'error') {
             if ($imgerrmsg != '' && $imgerrmsg != 'undefined') {
                 $msgarray['org_image_value'] = $imgerrmsg;
             } else {
                 $msgarray['org_image_value'] = 'Please upload an appropriate image file.';
             }
             $flag = 'false';
         }
         if ($pphnumber == $sphnumber && $sphnumber != '' && $pphnumber != '') {
             $msgarray['secondaryphone'] = 'Please enter different phone number.';
             $flag = 'false';
         }
         if ($form->isValid($this->_request->getPost()) && $flag != 'false') {
             $domain = $this->_request->getParam('domain');
             $domain = implode(',', $domain);
             $prevorgheadrm = $this->_request->getParam('prevorgheadrm');
             $actionflag = '';
             $date = new Zend_Date();
             $newOrgHead = $this->_request->getParam('orghead');
             if ($newOrgHead != '') {
                 $neworgHeadData = $employeeModal->getsingleEmployeeData($newOrgHead);
             }
             if (!empty($neworgHeadData)) {
                 $headfullname = $neworgHeadData[0]['userfullname'];
             } else {
                 $headfullname = '';
             }
             $data = array('organisationname' => trim($this->_request->getParam('organisationname')), 'domain' => trim($domain), 'website' => trim($this->_request->getParam('website')), 'org_image' => $imagepath, 'orgdescription' => trim($this->_request->getParam('orgdescription')), 'totalemployees' => trim($this->_request->getParam('totalemployees')), 'org_startdate' => $org_startdate != '' ? $org_startdate : NULL, 'phonenumber' => trim($this->_request->getParam('phonenumber')), 'secondaryphone' => trim($this->_request->getParam('secondaryphone')), 'faxnumber' => trim($this->_request->getParam('faxnumber')), 'country' => trim((int) $this->_request->getParam('country')), 'state' => trim(intval($this->_request->getParam('state'))), 'city' => trim(intval($this->_request->getParam('city'))), 'address1' => trim($this->_request->getParam('address1')), 'address2' => trim($this->_request->getParam('address2')), 'address3' => trim($this->_request->getParam('address3')), 'description' => trim($this->_request->getParam('description')), 'orghead' => trim($this->_request->getParam('orghead')), 'designation' => trim($this->_request->getParam('jobtitle_id', null)), 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
             $db = Zend_Db_Table::getDefaultAdapter();
             $db->beginTransaction();
             try {
                 if ($oldOrgHead != $newOrgHead && $oldOrgHead != '' && $newOrgHead != '' && $prevorgheadrm) {
                     $orgInfoModel->changeOrgHead($oldOrgHead, $newOrgHead, $prevorgheadrm);
                 }
                 $path = IMAGE_UPLOAD_PATH;
                 $imagepath = $this->_request->getParam('org_image_value');
                 $filecopy = 'success';
                 if ($imagepath != '') {
                     $filecopy = 'error';
                     if (file_exists(USER_PREVIEW_UPLOAD_PATH . '//' . $imagepath)) {
                         try {
                             if (copy(USER_PREVIEW_UPLOAD_PATH . '//' . $imagepath, $path . '//' . $imagepath)) {
                                 $filecopy = 'success';
                             }
                             unlink(USER_PREVIEW_UPLOAD_PATH . '//' . $imagepath);
                         } catch (Exception $e) {
                             echo $msgarray['org_image_value'] = $e->getMessage();
                             exit;
                         }
                     }
                 }
                 $where = array('id=?' => $id);
                 if ($imagepath == '') {
                     unset($data['org_image']);
                 } else {
                     if ($filecopy == 'error') {
                         unset($data['org_image']);
                     }
                 }
                 if ($id != '') {
                     $where = array('id=?' => $id);
                     $actionflag = 2;
                 } else {
                     $data['createdby'] = $loginUserId;
                     $data['createddate'] = gmdate("Y-m-d H:i:s");
                     $data['isactive'] = 1;
                     $where = '';
                     $actionflag = 1;
                 }
                 $Id = $orgInfoModel->SaveorUpdateData($data, $where);
                 $jobtitle_id = $this->_request->getParam('jobtitle_id', null);
                 $position_id = $this->_request->getParam('position_id', null);
                 $date_of_joining = sapp_Global::change_date($this->_request->getParam('date_of_joining', null), 'database');
                 $employeeId = $this->_getParam('employeeId', null);
                 $emprole = $this->_getParam('emprole', null);
                 $emailaddress = $this->_getParam('emailaddress', null);
                 $emppassword = sapp_Global::generatePassword();
                 $first_name = trim($this->_request->getParam('firstname_orghead', null));
                 $last_name = trim($this->_request->getParam('lastname_orghead', null));
                 $userfullname = $first_name . ' ' . $last_name;
                 //$userfullname = $headfullname;
                 $prefix_id = $this->_getParam('prefix_id', null);
                 $user_id = $this->_getParam('user_id', null);
                 $user_data = array('emprole' => $emprole, 'firstname' => $first_name, 'lastname' => $last_name, 'userfullname' => $userfullname, 'emailaddress' => $emailaddress, 'jobtitle_id' => $jobtitle_id, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"), 'emppassword' => md5($emppassword), 'employeeId' => $employeeId, 'selecteddate' => $date_of_joining, 'userstatus' => 'old', 'modeofentry' => 'Direct');
                 $emp_data = array('user_id' => $newOrgHead, 'jobtitle_id' => $jobtitle_id, 'position_id' => $position_id, 'prefix_id' => $prefix_id, 'reporting_manager' => 0, 'date_of_joining' => $date_of_joining, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
                 if ($Id == 'update') {
                     $tableid = $id;
                     unset($user_data['emppassword']);
                     unset($user_data['modeofentry']);
                     unset($user_data['userstatus']);
                     if ($newOrgHead != '') {
                         $user_st = $user_model->SaveorUpdateUserData($user_data, " id = " . $newOrgHead);
                         $employeeModal->SaveorUpdateEmployeeData($emp_data, " user_id = " . $newOrgHead);
                     } else {
                         $user_data['userstatus'] = 'old';
                         $user_data['emppassword'] = md5($emppassword);
                         $user_data['createdby'] = $loginUserId;
                         $user_data['createddate'] = gmdate("Y-m-d H:i:s");
                         $user_data['isactive'] = 1;
                         if ($emp_identity_code != '') {
                             $emp_id = $emp_identity_code . str_pad($user_model->getMaxEmpId($emp_identity_code), 4, '0', STR_PAD_LEFT);
                         } else {
                             $emp_id = '';
                         }
                         $user_data['employeeId'] = $emp_id;
                         $user_id = $user_model->SaveorUpdateUserData($user_data, '');
                         $emp_data['user_id'] = $newOrgHead;
                         $emp_data['createdby'] = $loginUserId;
                         $emp_data['createddate'] = gmdate("Y-m-d H:i:s");
                         $emp_data['isactive'] = 1;
                         $emp_data['is_orghead'] = 1;
                         $employeeModal->SaveorUpdateEmployeeData($emp_data, '');
                     }
                     if ($filecopy == 'success') {
                         $this->_helper->getHelper("FlashMessenger")->addMessage("Organization information updated successfully.");
                     } else {
                         $this->_helper->getHelper("FlashMessenger")->addMessage("Organization information updated successfully but failed to upload the logo.");
                     }
                 } else {
                     //start of saving into employee table
                     $user_data['createdby'] = $loginUserId;
                     $user_data['createddate'] = gmdate("Y-m-d H:i:s");
                     $user_data['isactive'] = 1;
                     if ($emp_identity_code != '') {
                         $emp_id = $emp_identity_code . str_pad($user_model->getMaxEmpId($emp_identity_code), 4, '0', STR_PAD_LEFT);
                     } else {
                         $emp_id = '';
                     }
                     $user_data['employeeId'] = $emp_id;
                     $user_id = $user_model->SaveorUpdateUserData($user_data, '');
                     $emp_data['user_id'] = $user_id;
                     $emp_data['createdby'] = $loginUserId;
                     $emp_data['createddate'] = gmdate("Y-m-d H:i:s");
                     $emp_data['isactive'] = 1;
                     $emp_data['is_orghead'] = 1;
                     $employeeModal->SaveorUpdateEmployeeData($emp_data, '');
                     $tableid = $Id;
                     if ($filecopy == 'success') {
                         $this->_helper->getHelper("FlashMessenger")->addMessage("Organization information saved successfully.");
                     } else {
                         $this->_helper->getHelper("FlashMessenger")->addMessage("Organization information saved successfully but failed to upload the logo.");
                     }
                 }
                 $menuID = ORGANISATIONINFO;
                 try {
                     if ($Id != '' && $Id != 'update') {
                         $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $Id);
                     } else {
                         $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $id);
                     }
                 } catch (Exception $e) {
                     echo $e->getMessage();
                 }
                 /* Send Mail to the user */
                 if ($Id != 'update') {
                     $base_url = 'http://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBaseUrl();
                     $view = $this->getHelper('ViewRenderer')->view;
                     $this->view->emp_name = $userfullname;
                     $this->view->password = $emppassword;
                     $this->view->emp_id = $employeeId;
                     $this->view->base_url = $base_url;
                     $text = $view->render('mailtemplates/newpassword.phtml');
                     $options['subject'] = APPLICATION_NAME . ': login credentials';
                     $options['header'] = 'Greetings from Sentrifugo';
                     $options['toEmail'] = $emailaddress;
                     $options['toName'] = $this->view->emp_name;
                     $options['message'] = $text;
                     $result = sapp_Global::_sendEmail($options);
                 }
                 /* END */
                 $db->commit();
                 $this->_redirect('organisationinfo');
             } catch (Exception $e) {
                 $db->rollBack();
                 return 'failed';
             }
         } else {
             $messages = $form->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 (isset($this->msgarray['domain'])) {
                 $this->view->msMsg = 'multiselecterror';
             }
         }
     }
     $this->view->msgarray = $msgarray;
     $this->view->popConfigPermission = $popConfigPermission;
 }