Exemplo n.º 1
0
 /**
  * checks deadline of record
  * 
  * @param Timetracker_Model_Timesheet $_record
  * @param boolean $_throwException
  * @return void
  * @throws Timetracker_Exception_Deadline
  */
 protected function _checkDeadline(Timetracker_Model_Timesheet $_record, $_throwException = TRUE)
 {
     // get timeaccount
     $timeaccount = Timetracker_Controller_Timeaccount::getInstance()->get($_record->timeaccount_id);
     if (isset($timeaccount->deadline) && $timeaccount->deadline == Timetracker_Model_Timeaccount::DEADLINE_LASTWEEK) {
         if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
             Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Check if deadline is exceeded for timeaccount ' . $timeaccount->title);
         }
         // it is only on monday allowed to add timesheets for last week
         $date = new Tinebase_DateTime();
         $date->setTime(0, 0, 0);
         $dayOfWeek = $date->get('w');
         if ($dayOfWeek >= 2) {
             // only allow to add ts for this week
             $date->sub($dayOfWeek, Tinebase_DateTime::MODIFIER_DAY);
         } else {
             // only allow to add ts for last week
             $date->sub($dayOfWeek + 7, Tinebase_DateTime::MODIFIER_DAY);
         }
         // convert start date to Tinebase_DateTime
         $startDate = new Tinebase_DateTime($_record->start_date);
         if ($date->compare($startDate) >= 0) {
             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                 Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Deadline exceeded: ' . $startDate . ' < ' . $date);
             }
             if ($this->checkRight(Timetracker_Acl_Rights::MANAGE_TIMEACCOUNTS, FALSE) || Timetracker_Model_TimeaccountGrants::hasGrant($_record->timeaccount_id, Tinebase_Model_Grants::GRANT_ADMIN)) {
                 if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
                     Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' User with admin / manage all rights is allowed to save Timesheet even if it exceeds the deadline.');
                 }
             } else {
                 if ($_throwException) {
                     throw new Timetracker_Exception_Deadline();
                 }
             }
         } else {
             if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                 Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Valid date: ' . $startDate . ' >= ' . $date);
             }
         }
     }
 }
 /**
  * returns a Tinebase_Record_Diff record with differences to the given record
  * 
  * @param Tinebase_Record_Interface $_record record for comparison
  * @param array $omitFields omit fields (for example modlog fields)
  * @return Tinebase_Record_Diff|NULL
  */
 public function diff($_record, $omitFields = array())
 {
     if (!$_record instanceof Tinebase_Record_Abstract) {
         return $_record;
     }
     $result = new Tinebase_Record_Diff(array('id' => $this->getId(), 'model' => get_class($_record)));
     $diff = array();
     foreach (array_keys($this->_validators) as $fieldName) {
         if (in_array($fieldName, $omitFields)) {
             continue;
         }
         $ownField = $this->__get($fieldName);
         $recordField = $_record->{$fieldName};
         if ($fieldName == 'customfields' && is_array($ownField) && is_array($recordField)) {
             // special handling for customfields, remove empty customfields from array
             foreach (array_keys($recordField, '', true) as $key) {
                 unset($recordField[$key]);
             }
             foreach (array_keys($ownField, '', true) as $key) {
                 unset($ownField[$key]);
             }
         }
         if (in_array($fieldName, $this->_datetimeFields)) {
             if ($ownField instanceof DateTime && $recordField instanceof DateTime) {
                 /** @var Tinebase_DateTime $recordField */
                 if (!$ownField instanceof Tinebase_DateTime) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) {
                         Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Convert ' . $fieldName . ' to Tinebase_DateTime to make sure we have the compare() method');
                     }
                     $ownField = new Tinebase_DateTime($ownField);
                 }
                 if ($ownField->compare($recordField) === 0) {
                     continue;
                 } else {
                     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                         Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' datetime for field ' . $fieldName . ' is not equal: ' . $ownField->getIso() . ' != ' . $recordField->getIso());
                     }
                 }
             } else {
                 if (!$recordField instanceof DateTime && $ownField == $recordField) {
                     continue;
                 }
             }
         } else {
             if ($fieldName == $this->_identifier && $this->getId() == $_record->getId()) {
                 continue;
             } else {
                 if ($ownField instanceof Tinebase_Record_Abstract || $ownField instanceof Tinebase_Record_RecordSet) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
                         Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Doing subdiff for field ' . $fieldName);
                     }
                     $subdiff = $ownField->diff($recordField);
                     if (is_object($subdiff) && !$subdiff->isEmpty()) {
                         $diff[$fieldName] = $subdiff;
                     }
                     continue;
                 } else {
                     if ($recordField instanceof Tinebase_Record_Abstract && is_scalar($ownField)) {
                         // maybe we have the id of the record -> just compare the id
                         if ($recordField->getId() == $ownField) {
                             continue;
                         } else {
                             $recordField = $recordField->getId();
                         }
                     } else {
                         if ($ownField == $recordField) {
                             continue;
                         } else {
                             if (empty($ownField) && empty($recordField)) {
                                 continue;
                             } else {
                                 if (empty($ownField) && $recordField instanceof Tinebase_Record_RecordSet && count($recordField) == 0 || empty($recordField) && $ownField instanceof Tinebase_Record_RecordSet && count($ownField) == 0) {
                                     continue;
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' Found diff for ' . $fieldName . '(this/other):' . print_r($ownField, true) . '/' . print_r($recordField, true));
         }
         $diff[$fieldName] = $recordField;
     }
     $result->diff = $diff;
     return $result;
 }