public function save($appraisalratingsform)
 {
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
         $loginRole = $auth->getStorage()->read()->emprole;
         $loginGroup = $auth->getStorage()->read()->group_id;
         $businessUnit = $auth->getStorage()->read()->businessunit_id;
         $department = $auth->getStorage()->read()->department_id;
     }
     $appraisalratingsmodel = new Default_Model_Appraisalratings();
     $app_init_model = new Default_Model_Appraisalinit();
     if ($loginRole != SUPERADMINROLE && $loginGroup != MANAGEMENT_GROUP) {
         $businessUnit = $businessUnit;
         $department = $department;
     } else {
         $businessUnit = $this->_request->getParam('businessunit');
         $department = $this->_request->getParam('departmentid');
     }
     //$res = $appraisalratingsmodel->checkAccessAddratings($businessUnit,$department);
     $res = $app_init_model->check_per_implmentation($businessUnit, $department);
     $implementation = $res['performance_app_flag'];
     $rating_type = $res['appraisal_ratings'];
     $configured_id = $res['id'];
     $msgarray = array();
     $resultArr = array();
     $errorflag = "true";
     $id = $this->_request->getParam('id');
     $appraisalid = $this->_request->getParam('appraisalid');
     // $update_id = $this->_request->getParam('update_id');
     $appraisal_ratings = $rating_type == 1 ? 5 : 10;
     $db = Zend_Db_Table::getDefaultAdapter();
     $db->beginTransaction();
     $actionflag = '';
     try {
         if ($id != '') {
             for ($i = 0; $i < $appraisal_ratings; $i++) {
                 $rating_text = $this->_request->getParam('rating_text_' . ($i + 1));
                 $update_id = $this->_request->getParam('update_id_' . ($i + 1));
                 /* for Update record  */
                 $where = array('id=?' => $update_id);
                 $actionflag = 2;
                 $menumodel = new Default_Model_Menu();
                 $tableid = '';
                 $data = array('rating_text' => $rating_text, 'modifiedby_role' => $loginRole, 'modifiedby_group' => $loginGroup, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
                 //$where = 'isactive=1';
                 $Id = $appraisalratingsmodel->SaveorUpdateAppraisalRatingsData($data, $where);
             }
         } else {
             for ($i = 0; $i < $appraisal_ratings; $i++) {
                 $rating_text = $this->_request->getParam('rating_text_' . $i);
                 $menumodel = new Default_Model_Menu();
                 $tableid = '';
                 $data = array('pa_configured_id' => $configured_id, 'pa_initialization_id' => $appraisalid, 'rating_type' => $rating_type, 'rating_value' => $i + 1, 'rating_text' => $rating_text, 'createdby_role' => $loginRole, 'createdby_group' => $loginGroup, 'modifiedby_role' => $loginRole, 'modifiedby_group' => $loginGroup, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"), 'createdby' => $loginUserId, 'createddate' => gmdate("Y-m-d H:i:s"), 'isactive' => 1);
                 $where = '';
                 $actionflag = 1;
                 $Id = $appraisalratingsmodel->SaveorUpdateAppraisalRatingsData($data, $where);
             }
         }
         if ($Id == 'update') {
             $tableid = $id;
             $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Ratings updated successfully."));
         } else {
             $tableid = $Id;
             $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Ratings added successfully."));
         }
         $menuidArr = $menumodel->getMenuObjID('/appraisalratings');
         $menuID = $menuidArr[0]['id'];
         $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $tableid);
         /** Start
          * Sending Mails to employees
          */
         $appraisalconfigmodel = new Default_Model_Appraisalconfig();
         if ($implementation == 0) {
             $employeeDetailsArr = $appraisalconfigmodel->getUserDetailsByID($businessUnit, $department);
         } else {
             $employeeDetailsArr = $appraisalconfigmodel->getUserDetailsByID($businessUnit, '');
         }
         $msg_add_update = $Id == 'update' ? "updated" : "added";
         //Sending mail to Super admin
         $options['subject'] = APPLICATION_NAME . ': Performance Appraisal Ratings ' . ucfirst($msg_add_update);
         $options['header'] = 'Performance Appraisal Configuration';
         $options['toEmail'] = SUPERADMIN_EMAIL;
         $options['toName'] = 'Super Admin';
         $options['message'] = 'Dear Super Admin, performance appraisal configuration ' . $msg_add_update;
         // $mail_id =  sapp_Global::_sendEmail($options);
         // Sending mail to others
         if (!empty($employeeDetailsArr)) {
             foreach ($employeeDetailsArr as $emp) {
                 $options['subject'] = APPLICATION_NAME . ': Performance Appraisal Settings Added.';
                 $options['header'] = 'Performance Appraisal Configuration';
                 $options['toEmail'] = $emp['emailaddress'];
                 $options['toName'] = $emp['userfullname'];
                 $options['message'] = 'Dear ' . $emp['userfullname'] . ', performance appraisal configuration ' . $msg_add_update;
                 //  $mail_id =  sapp_Global::_sendEmail($options);
             }
         }
         /**
          * End
          */
         $db->commit();
         $this->_redirect('appraisalratings');
         //throw new Exception("Some error message");
     } catch (Exception $e) {
         $db->rollBack();
         echo $e->getMessage();
         echo $e->getTraceAsString();
         return $msgarray;
     }
 }
 public function save($appraisalratingsform)
 {
     $auth = Zend_Auth::getInstance();
     if ($auth->hasIdentity()) {
         $loginUserId = $auth->getStorage()->read()->id;
         $loginRole = $auth->getStorage()->read()->emprole;
         $loginGroup = $auth->getStorage()->read()->group_id;
         $businessUnit = $auth->getStorage()->read()->businessunit_id;
         $department = $auth->getStorage()->read()->department_id;
         $loginUserfullname = $auth->getStorage()->read()->userfullname;
         $loginUserEmpId = $auth->getStorage()->read()->employeeId;
     }
     $appraisalratingsmodel = new Default_Model_Appraisalratings();
     $app_init_model = new Default_Model_Appraisalinit();
     if ($loginRole != SUPERADMINROLE && $loginGroup != MANAGEMENT_GROUP) {
         $businessUnit = $businessUnit;
         $department = $department;
     } else {
         $businessUnit = $this->_request->getParam('businessunit');
         $department = $this->_request->getParam('departmentid');
     }
     $res = $app_init_model->checkAppraisalExists($businessUnit, $department);
     $implementation = isset($res[0]['performance_app_flag']) ? $res[0]['performance_app_flag'] : NULL;
     $rating_type = !empty($res[0]['appraisal_ratings']) ? $res[0]['appraisal_ratings'] : NULL;
     $configured_id = 0;
     $msgarray = array();
     $resultArr = array();
     $errorflag = "true";
     $id = $this->_request->getParam('id');
     $dept_id = $this->_request->getParam('departmentid');
     $implementation = is_numeric($dept_id) && $dept_id > 0 ? 0 : 1;
     $appraisal_ratings = $this->_request->getParam('appraisal_rating');
     $appraisalid = $this->_request->getParam('appraisalid');
     $db = Zend_Db_Table::getDefaultAdapter();
     $db->beginTransaction();
     $actionflag = '';
     try {
         if ($id != '') {
             for ($i = 0; $i < $appraisal_ratings; $i++) {
                 $rating_text = $this->_request->getParam('rating_text_' . ($i + 1));
                 $update_id = $this->_request->getParam('update_id_' . ($i + 1));
                 /* for Update record  */
                 $where = array('id=?' => $update_id);
                 $actionflag = 2;
                 $tableid = '';
                 $data = array('rating_text' => $rating_text, 'modifiedby_role' => $loginRole, 'modifiedby_group' => $loginGroup, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"));
                 $Id = $appraisalratingsmodel->SaveorUpdateAppraisalRatingsData($data, $where);
             }
         } else {
             for ($i = 0; $i < $appraisal_ratings; $i++) {
                 $rating_text = $this->_request->getParam('rating_text_' . $i);
                 $tableid = '';
                 $data = array('pa_configured_id' => $configured_id, 'pa_initialization_id' => $appraisalid, 'rating_type' => $rating_type, 'rating_value' => $i + 1, 'rating_text' => $rating_text, 'createdby_role' => $loginRole, 'createdby_group' => $loginGroup, 'modifiedby_role' => $loginRole, 'modifiedby_group' => $loginGroup, 'modifiedby' => $loginUserId, 'modifieddate' => gmdate("Y-m-d H:i:s"), 'createdby' => $loginUserId, 'createddate' => gmdate("Y-m-d H:i:s"), 'isactive' => 1);
                 $where = '';
                 $actionflag = 1;
                 $Id = $appraisalratingsmodel->SaveorUpdateAppraisalRatingsData($data, $where);
             }
         }
         if ($Id == 'update') {
             $tableid = $id;
             $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Ratings updated successfully."));
         } else {
             $tableid = $Id;
             $this->_helper->getHelper("FlashMessenger")->addMessage(array("success" => "Ratings added successfully."));
         }
         /*
          *   Logs Storing
          */
         $menuID = APPRAISALRATINGS;
         $result = sapp_Global::logManager($menuID, $actionflag, $loginUserId, $tableid);
         /*
          *  Logs storing ends
          */
         //to get initialization details using appraisal Id for Business Unit,Department,To Year
         $appraisal_details = $appraisalratingsmodel->getappdata($appraisalid);
         if (!empty($appraisal_details)) {
             $bunit = $appraisal_details['unitname'];
             $dept = $appraisal_details['deptname'];
             $to_year = $appraisal_details['to_year'];
         }
         /** Start
          * Sending Mails to employees
          */
         $appraisalconfigmodel = new Default_Model_Appraisalconfig();
         if ($implementation == 0) {
             $employeeDetailsArr = $appraisalconfigmodel->getUserDetailsByID($businessUnit, $department);
         } else {
             $employeeDetailsArr = $appraisalconfigmodel->getUserDetailsByID($businessUnit, '');
         }
         $msg_add_update = $Id == 'update' ? "updated" : "configured";
         $dept_str = $dept == '' ? " " : "and department <b>{$dept}</b> ";
         $emp_id_str = $loginRole == SUPERADMINROLE ? " " : "({$loginUserEmpId})";
         //Preparing Employee array for Bcc
         $empArr = array();
         if (!empty($employeeDetailsArr)) {
             $empArrList = '';
             foreach ($employeeDetailsArr as $emp) {
                 array_push($empArr, $emp['emailaddress']);
             }
         }
         //echo "<pre>";print_r($empArr);die();
         $options['subject'] = APPLICATION_NAME . ': Performance Appraisal Ratings ' . ucfirst($msg_add_update);
         $options['header'] = 'Performance Appraisal : Ratings';
         $options['bcc'] = $empArr;
         $options['toEmail'] = SUPERADMIN_EMAIL;
         $options['toName'] = 'Super Admin';
         $options['message'] = "<div style='padding: 0; text-align: left; font-size:14px; font-family:Arial, Helvetica, sans-serif;'>\t\t\t\t\n\t\t\t<span style='color:#3b3b3b;'>Hi,</span><br />\n\t\t\t<div style='padding:20px 0 0 0;color:#3b3b3b;'>Performance appraisal ratings have been {$msg_add_update} for the year <b>{$to_year}</b> for business unit <b>{$bunit}</b> {$dept_str} by " . $loginUserfullname . $emp_id_str . " </div>\n\t\t\t<div style='padding:20px 0 10px 0;'>Please <a href=" . BASE_URL . " target='_blank' style='color:#b3512f;'>click here</a> to login  to your <b>" . APPLICATION_NAME . "</b> account.</div>\n\t\t\t</div> ";
         $mail_id = sapp_Global::_sendEmail($options);
         /**
          * End mails sending
          */
         $db->commit();
         $this->_redirect('appraisalratings');
     } catch (Exception $e) {
         $db->rollBack();
         echo $e->getMessage();
         echo $e->getTraceAsString();
         return $msgarray;
     }
 }