public function save() { $flash = Flash::Instance(); $errors = array(); if (!$this->checkParams($this->modeltype)) { sendBack(); } if (parent::save($this->modeltype, '', $errors)) { if (strtolower($this->_data['saveform']) == 'save and post') { $this->_data['id'] = $this->_data[$this->modeltype]['id']; $this->post(); } else { sendTo($this->name, 'view', $this->_modules, array('id' => $this->saved_model->id)); } } else { if (!empty($this->_data[$this->modeltype]['id'])) { $this->_data['id'] = $this->_data[$this->modeltype]['id']; } $flash->addErrors($errors); $this->refresh(); } }
public function save() { $flash = Flash::Instance(); $errors = array(); if (!$this->checkParams($this->modeltype)) { sendBack(); } /* Get the Holiday Entitlement Id for the required request.*/ $holidayEntitlement = DataObjectFactory::Factory('Holidayentitlement'); $date = fix_date(date(DATE_FORMAT)); $holidayEntitlement->loadEntitlement($this->_data[$this->modeltype]['employee_id'], $date); $days_left = $holidayEntitlement->get_total_days_left($date, $this->_data[$this->modeltype]['employee_id']); //Compare the days left with the requested number of days. $holidayRequest = $this->_templateobject; $db = DB::Instance(); $cc = new ConstraintChain(); $cc->add(new Constraint('employee_id', '=', $this->_data[$this->modeltype]['employee_id'])); $cc->add(new Constraint('status', 'in', '(' . $db->qstr($holidayRequest->newRequest()) . ',' . $db->qstr($holidayRequest->authorise()) . ')')); if (!empty($this->_data[$this->modeltype]['id'])) { $holidayRequest->load($this->_data[$this->modeltype]['id']); $days_left += $holidayRequest->num_days; $cc->add(new Constraint('id', '!=', $holidayRequest->id)); } if (isset($this->_data[$this->modeltype]['start_date']) && empty($this->_data[$this->modeltype]['start_date'])) { $errors[] = 'You must enter a start date'; } if (isset($this->_data[$this->modeltype]['end_date']) && empty($this->_data[$this->modeltype]['end_date'])) { $errors[] = 'You must enter a start date'; } if (isset($this->_data[$this->modeltype]['start_date']) && isset($this->_data[$this->modeltype]['end_date']) && count($errors) == 0 && strtotime(fix_date($this->_data[$this->modeltype]['start_date'])) > strtotime(fix_date($this->_data['Holidayrequest']['end_date']))) { $errors[] = 'End Date cannot be before Start Date'; } if (count($errors) == 0) { $cc->add(new Constraint($db->qstr(fix_date($this->_data[$this->modeltype]['start_date'])), 'between', 'start_date and end_date')); $cc->add(new Constraint($db->qstr(fix_date($this->_data[$this->modeltype]['end_date'])), 'between', 'start_date and end_date')); } if ($holidayRequest->getCount($cc) > 0) { $errors[] = 'This request overlaps an existing request'; } if ($days_left < $this->_data[$this->modeltype]['num_days'] && !isset($this->_data['Holidayrequest']['special_circumstances'])) { $errors[] = 'You do not have any days left or your request is not within an entitlement period!'; } if (count($errors) > 0 || !parent::save($this->modeltype)) { $flash->addErrors($errors); $this->_data['employee_id'] = $this->_data[$this->modeltype]['employee_id']; $this->refresh(); } else { sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null); } }