/** * Updates a user * * @param int $intId The user ID * @param array $arrData The data array * @throws Exception * @return int The user ID */ public function do_update($intId = null, $arrData) { $user = null; $con = Propel::getConnection(); if (!$con->beginTransaction()) { throw new Exception('Could not start transaction.'); } try { $authUser = $this->requireUser(); $accountId = $authUser->getAccountId(); $validator = new KickstartValidator(); $locale = Localizer::getInstance(); if ($intId and (!isset($arrData['Password']) or $arrData['Password'] == '')) { unset($this->filter_basic['Password']); unset($arrData['Password']); unset($arrData['Password2']); } $warnings = $validator->filterErrors($arrData, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { return array('result' => false, 'warnings' => $warnings); } if ($intId) { $user = $authUser->getSubordinate($intId); } else { $user = new User(); $user->setAccountId($accountId)->setDomainId($authUser->getDomainId()); } if (isset($arrData['Password'])) { $user->setPassword($arrData['Password']); } $allowedFields = array('Name' => true, 'Firstname' => true, 'Lastname' => true, 'Phone' => true, 'Email' => true, 'Number' => true); if ($authUser->getIsAdmin()) { $allowedFields += array('DomainId' => true, 'ManagerOf' => true, 'IsAdmin' => true); } $user->fromArray(array_intersect_key($arrData, $allowedFields)); // Fail if domain does not belong to authenticated account $domain = $user->getDomain($con); if ($domain === null or $domain->getAccountId() !== $accountId) { throw new Exception('Invalid domain ID #' . $user->getDomainId()); } $user->save($con); if (!empty($arrData['Properties'])) { $user->setProperties($arrData['Properties'], $con); } } catch (Exception $e) { $con->rollBack(); throw $e; } if (!$con->commit()) { throw new Exception('Could not commit transaction.'); } return $user->getId(); }
/** * Updates a property * * @param int $id The property ID * @param array $data * @return int The property ID */ public function do_update($id, $data = null) { $user = $this->requireUser(); if (!$user->isAdmin()) { throw new Exception('Only administrators are allowed to edit properties.'); } // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($data, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { return array('result' => false, 'warnings' => $warnings); } $query = PropertyQuery::create()->filterByAccount($user->getAccount()); if ($id !== null) { $query->filterById($id, Criteria::NOT_EQUAL); $property = PropertyQuery::create()->filterByAccount($user->getAccount())->findOneById($id); if (!$property) { throw new Exception('Property not found; ID: ' . $id); } } else { $property = new Property(); } // Check for duplicates if (isset($data['Name']) and $query->findOneByName($data['Name'])) { throw new Exception($locale->insert('error.taken', array('value' => '"' . $data['Name'] . '"'))); } unset($data['Id']); $property->fromArray($data); $property->setAccount($user->getAccount()); $property->save(); return $property->getId(); }
/** * Updates a domain * * @param int $intId * @param array $arrData * @return int The domain ID */ public function do_update($intId, $arrData) { $domain = null; $con = Propel::getConnection(); if (!$con->beginTransaction()) { throw new Exception('Could not start transaction.'); } try { $user = $this->requireUser(); $account = $user->getAccount($con); // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($arrData, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { $con->rollBack(); return array('result' => false, 'warnings' => $warnings); } $query = DomainQuery::create()->filterByAccount($account); if ($intId !== null) { $domain = DomainQuery::create()->filterByAccount($account)->findOneById($intId, $con); if ($domain === null) { throw new Exception('Domain not found; ID: ' . $intId); } $query->filterById($intId, Criteria::NOT_EQUAL); } else { $domain = new Domain(); $domain->setAccount($account); } // Check for duplicates if ($query->findOneByName($arrData['Name'], $con)) { throw new Exception($locale->insert('error.taken', array('value' => '"' . $arrData['Name'] . '"'))); } $domain->fromArray(array_intersect_key($arrData, array('AddressId' => true, 'Name' => true, 'Description' => true, 'Number' => true))); $domain->save($con); if (!empty($arrData['Properties'])) { $domain->setProperties($arrData['Properties'], $con); } } catch (Exception $e) { $con->rollBack(); throw $e; } if (!$con->commit()) { throw new Exception('Could not commit transaction.'); } return $domain->getId(); }
/** * Updates a plugin * * @param int $id The vacation ID * @param array $data * @return int The vacation ID */ public function do_update($id, $data) { $user = $this->requireUser(); if (!$user->isAdmin()) { throw new Exception('Non-administrative user "' . $user->getFQN() . '" cannot modify plugins.'); } // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($data, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { return array('result' => false, 'warnings' => $warnings); } $query = PluginQuery::create()->filterByAccount($user->getAccount()); if ($id) { $query->filterById($id, Criteria::NOT_EQUAL); $plugin = PluginQuery::create()->filterByAccount($user->getAccount())->findOneById($id); if (!$plugin) { throw new Exception('Plugin not found; ID: ' . $id); } } else { $plugin = new Plugin(); } // Check for duplicates if ($query->findOneByIdentifier($data['Name'])) { throw new Exception($locale->insert('error.taken', array('value' => '"' . $data['Name'] . '"'))); } if (isset($data['Start'])) { $plugin->setStart(strtotime($data['Start'] . 'Z', 0)); unset($data['Start']); } $plugin->fromArray($data); $plugin->setAccount($user->getAccount()); $plugin->save(); return $plugin->getId(); }
/** * Updates an account. * * @param int $id * @param array $data * @return int The account ID */ public function do_update($id, $data) { $account = null; $con = Propel::getConnection(); if (!$con->beginTransaction()) { throw new Exception('Could not start transaction.'); } try { $user = $this->requireUser(); // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($data, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { $con->rollBack(); return array('result' => false, 'warnings' => $warnings); } if ($id === null) { $account = new Account(); } else { $account = AccountQuery::create()->findOneById($id, $con); if ($account === null or $account !== $user->getAccount($con) or !$user->getIsAdmin()) { throw new Exception('Account #' . $id . ' not found or no permission to update it.'); } // Check for duplicates if (isset($data['Name'])) { $otherAccount = AccountQuery::create()->filterById($account->getId(), Criteria::NOT_EQUAL)->findOneByName($data['Name'], $con); if ($otherAccount !== null) { throw new Exception($locale->insert('error.taken', array('value' => '"' . $data['Name'] . '"'))); } } } $account->fromArray(array_intersect_key($data, array('Name' => true))); $account->save($con); if (!empty($data['Address'])) { $address = $account->getAddress($con); if ($address === null) { $address = new Address(); $address->setAccount($account); } $address->fromArray(array_intersect_key($data['Address'], array('Company' => true, 'Firstname' => true, 'Lastname' => true, 'Address' => true, 'Zipcode' => true, 'City' => true, 'State' => true, 'Province' => true, 'Country' => true, 'Phone' => true, 'Fax' => true, 'Website' => true, 'Email' => true, 'Vatid' => true))); $address->save($con); } if (!empty($data['Properties'])) { $account->setProperties($data['Properties'], $con); } } catch (Exception $e) { $con->rollBack(); throw $e; } if (!$con->commit()) { throw new Exception('Could not commit transaction.'); } return $account->getId(); }
/** * Updates a holiday * * @param int $intId The holiday ID * @param array $arrData * @return int The holiday ID */ public function do_update($intId, $arrData) { $user = $this->requireUser(); // Validate input data $validator = new KickstartValidator(); $locale = Localizer::getInstance(); $warnings = $validator->filterErrors($arrData, $this->initFilter($this->filter_basic, $locale)); if ($warnings) { return array('result' => false, 'warnings' => $warnings); } if ($intId) { if (!($holiday = HolidayQuery::create()->findOneById($intId))) { throw new Exception('Holiday with ID ' . $intId . ' not found!'); } } else { $holiday = new Holiday(); } $con = Propel::getConnection(HolidayPeer::DATABASE_NAME); $con->beginTransaction(); try { $holiday->setName($arrData['Name'])->setDate($arrData['Date'])->setAccount($user->getAccount())->save($con); // Assign the domains if (!(isset($arrData['Domains']) && is_array($arrData['Domains']))) { $arrData['Domains'] = array(); } $sub = array(); foreach (HolidayDomainQuery::create()->filterByHoliday($holiday)->find() as $link) { if (in_array($link->getDomainId(), $arrData['Domains'])) { $sub[] = $link->getDomainId(); } else { $link->delete($con); } } $diff = array_diff($arrData['Domains'], $sub); if (sizeof($diff) > 0) { // Get the account's domains $domainFilter = DomainQuery::create()->filterByAccount($user->getAccount())->add(DomainPeer::ID, $arrData['Domains'], Criteria::IN)->find(); if (sizeof($domainFilter) != sizeof($arrData['Domains'])) { // Obviously there are some domains the user does not belong to } foreach (array_diff($arrData['Domains'], $sub) as $domainId) { $link = new HolidayDomain(); $link->setHoliday($holiday)->setDomainId($domainId)->save($con); } } $con->commit(); } catch (Exception $e) { $con->rollBack(); throw $e; } return array('result' => $holiday->getId(), 'test' => $diff); // return $holiday->getId(); }
/** * 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(); }