Ejemplo n.º 1
0
 /**
  * Creates or updates a clocking.
  *
  * @param int $id
  * @param array $data
  * @return int The clocking ID
  */
 public function do_update($id, $data)
 {
     $con = Propel::getConnection();
     if (!$con->beginTransaction()) {
         throw new Exception('Could not start transaction.');
     }
     $clocking = null;
     try {
         $authUser = $this->requireUser();
         // Validate input data
         $validator = new KickstartValidator();
         $locale = Localizer::getInstance();
         // Cut off seconds to get time in full minutes
         if (isset($data['Start']) and is_numeric($data['Start'])) {
             $data['Start'] -= date('s', $data['Start']);
         }
         if (isset($data['End']) and is_numeric($data['End'])) {
             $data['End'] -= date('s', $data['End']);
         }
         $warnings = $validator->filterErrors($data, $this->initFilter($this->filter_basic, $locale));
         if ($warnings) {
             return array('result' => false, 'warnings' => $warnings);
         }
         if ((string) $id === '') {
             $event = 'create';
             $clocking = new Clocking();
         } else {
             $event = 'update';
             $clocking = $this->getClockingById($id, $con);
             if ($clocking->getBooked() or $clocking->getFrozen()) {
                 throw new Exception('Cannot change clocking entry #' . $id . ' because it already has bookings or is locked for booking.');
             }
         }
         $isAdmin = $authUser->getIsAdmin();
         $allowedColumns = array('TypeId' => true, 'Start' => true, 'End' => true, 'Breaktime' => true, 'Comment' => true);
         if ($isAdmin) {
             $allowedColumns['ApprovalStatus'] = true;
         }
         $clocking->fromArray(array_intersect_key($data, array('UserId' => true) + $allowedColumns));
         $clockingUser = $clocking->getUserRelatedByUserId($con);
         $clockingUserId = $clocking->getUserId();
         $authUserAccountId = $authUser->getAccountId();
         // Check if authenticated user may access clocking's user
         if ($clockingUser === null or (string) $clockingUser->getAccountId() !== (string) $authUserAccountId or !$isAdmin and $clockingUser !== $authUser) {
             throw new Exception('Invalid user #' . $clockingUserId . ' specified for clocking or no permission to access that user\'s data.');
         }
         $type = $clocking->getClockingType($con);
         if ($type === null) {
             throw new Exception('Clocking #' . $id . ' has no clocking type assigned.');
         }
         $account = $authUser->getAccount($con);
         if ($account === null) {
             throw new Exception('Could not load account of user #' . $authUser->getId() . ' "' . $authUser->getFQN($con) . '".');
         }
         // Check hard time limit for non-admin users
         if (!$isAdmin) {
             $this->validateTimeLimits($account, $authUser, $clocking, $con);
         }
         $isNew = $clocking->isNew();
         // Save first to obtain an ID which may be referenced by a plugin
         $clocking->save($con);
         $clockingData = EntityArray::from($clocking, $con) + array('IsNew' => $isNew, 'Type' => EntityArray::from($type, $con));
         if (!$isAdmin and ($type->getApprovalRequired() or $this->pastGraceTimeExceeded($type, min((int) $clocking->getStart('U'), (int) $clocking->getEnd('U'))))) {
             $clocking->setApprovalStatus(ClockingPeer::APPROVAL_STATUS_REQUIRED);
         }
         $clocking->fromArray(array_intersect_key(PluginPeer::fireEvent($clockingUser, 'clocking', $event, $clockingData, $con), $allowedColumns));
         $type = $clocking->getClockingType($con);
         // Plugins may have changed this
         if ($type === null or (string) $type->getAccountId() !== (string) $authUserAccountId) {
             throw new Exception('Clocking #' . $id . ' has an invalid or unknown clocking type #' . $clocking->getTypeId() . ' assigned.');
         }
         $start = (int) $clocking->getStart('U');
         $end = (int) $clocking->getEnd('U');
         if ($start > $end) {
             throw new APIException(self::ERROR_INTERVAL, 'Start time (' . $clocking->getStart('Y-m-d H:i:s') . ') must be before end time (' . $clocking->getEnd('Y-m-d H:i:s') . ').', array('start' => $start, 'end' => $end));
         } elseif ($type->getWholeDay()) {
             // Set time of day for start and end to 00:00:00
             $clocking->setStart(strtotime(date('Y-m-d 00:00:00', $start)));
             $clocking->setEnd(strtotime(date('Y-m-d 00:00:00', $end)));
             // Set break time to 0
             $clocking->setBreaktime(0);
         } elseif ($start === $end) {
             // Create an open clocking entry (i.e. sign on for work).
             // Fail if there are other open entries.
             if (($openClocking = $this->getOpenClocking($authUser, $clockingUser, $clocking, $con)) !== null) {
                 $openComment = $openClocking->getComment();
                 throw new APIException(self::ERROR_OPEN, 'Clocking #' . $openClocking->getId() . ((string) $openComment === '' ? '' : ' "' . $openComment . '"') . ' from ' . $openClocking->getStart('r') . ' to ' . $openClocking->getEnd('r') . ' is already open. Please close that entry first.' . $openClocking->getId() . ' ' . $clocking->getId(), $openClocking);
             }
         } elseif ($clocking->getTime() < $clocking->getBreaktime()) {
             throw new APIException(self::ERROR_BREAK, 'Break (' . $clocking->getBreaktime() / 60 . ' minutes) must be less than the specified work time (' . $clocking->getTime() . ' = ' . $clocking->getStart('Y-m-d H:i:s') . ' - ' . $clocking->getEnd('Y-m-d H:i:s') . ').');
         }
         $futureGraceTime = $type->getFutureGraceTime();
         if ($futureGraceTime !== null and $end > time() + $futureGraceTime) {
             throw new APIException(self::ERROR_FUTURE, 'Clocking type "' . $type->getIdentifier() . '" #' . $type->getId() . ' does not allow entries in the future (' . $clocking->getStart('Y-m-d H:i:s') . ' - ' . $clocking->getEnd('Y-m-d H:i:s') . ').');
         }
         $clocking->save($con);
         $clocking->reload(false, $con);
         if ($clocking->getFrozen()) {
             throw new APIException(self::ERROR_LOCKED, 'The clocking #' . $clocking->getId() . ' is currently locked for booking.');
         }
         // Check for other non-whole-day clockings with overlapping time
         if (!$type->getWholeDay()) {
             $firstConflict = self::createClockingQuery($authUser, $con)->filterById($clocking->getId(), Criteria::NOT_EQUAL)->filterByUserId($clockingUserId)->add(ClockingTypePeer::WHOLE_DAY, 0, Criteria::EQUAL)->filterByStart($end, Criteria::LESS_THAN)->filterByEnd($start, Criteria::GREATER_THAN)->filterByDeleted(0, Criteria::EQUAL)->findOne($con);
             if ($firstConflict !== null) {
                 throw new APIException(self::ERROR_OVERLAP, $clocking->__toString() . ' overlaps with ' . $firstConflict->__toString() . '.', $firstConflict);
             }
         }
         SystemLogPeer::add('clocking.' . $event, $clocking, SystemLogPeer::CODE_SUCCESSFUL, null, $authUser, array('clocking' => $clocking->toArray()), $con);
     } catch (Exception $e) {
         $con->rollBack();
         SystemLogPeer::add('clocking.' . $event, $clocking, SystemLogPeer::CODE_FAILED, $e->getMessage(), $authUser, array('exception' => $e->__toString(), 'clocking' => $clocking->toArray()), $con);
         throw $e;
     }
     if (!$con->commit()) {
         throw new Exception('Could not commit transaction.');
     }
     return $clocking->getId();
 }