/** * Creates a new version of the passed in object * * @param MappedObject $object * @return ObjectVersion */ public function createVersion(MappedObject $object, $label = '') { // make sure that there's a versioning table available for this object $versionType = get_class($object) . 'Version'; if (!class_exists($versionType)) { throw new Exception("Attempting to create a version for a non-versionable object"); } if (!$object) { throw new Exception("Cannot create a version of an empty object"); } // see if there's a previous object version, if so load it // so we have an appropriate 'from' time $lastVersion = $this->getMostRecentVersion($object); $from = date('Y-m-d H:i:s'); if ($lastVersion) { $from = date('Y-m-d H:i:s', strtotime($lastVersion->versioncreated) + 1); } $newVersion = new $versionType(); // when creating a version, we want to load the existing state from // the DB so that we're being accurate. $current = $this->dbService->getById($object->me()->id, get_class($object)); $properties = $current->unBind(); unset($properties['id']); /*unset($properties['created']); unset($properties['creator']);*/ $newVersion->bind($properties); $newVersion->recordid = $object->id; $newVersion->versioncreated = date('Y-m-d H:i:s'); $newVersion->validfrom = $from; $newVersion->label = $label; return $this->dbService->saveObject($newVersion); }
/** * Get a feed * * @param int $id * @return Feed */ public function getFeed($url) { $feed = $this->dbService->getByField(array('url' => $url), 'Feed'); $update = $feed == null || strtotime($feed->updated) < time() - 60 * 15; if ($feed == null) { $feed = new Feed(); } if ($update) { $this->log->debug("Loading feed {$url}"); // make the request! // $feed->content = $content = null; try { $client = Zend_Feed::getHttpClient(); $client->setUri($url); $response = $client->request('GET'); if ($response->getStatus() !== 200) { throw new Zend_Feed_Exception('Feed failed to load, got response code ' . $response->getStatus()); } $content = $response->getBody(); } catch (Exception $zfe) { $this->log->err("Failed loading feed from {$url}"); return $content; } $feed->content = $content; $feed->url = $url; $this->dbService->saveObject($feed); } return $feed; }
public function favouritepaneAction() { $favourite = new PanelFavourite(); $favourite->bind($this->_getAllParams()); $out = $this->dbService->saveObject($favourite); echo $this->ajaxResponse($out); }
/** * Save a contact against a given client object * * * @param array|Contact $params * @return Contact */ public function saveContact($params) { $contact = null; try { $this->dbService->beginTransaction(); // Get an existing one $contact = $this->dbService->saveObject($params, 'Contact'); // check for a user object to also update if ($contact->id) { $user = $this->userService->getUserByField('contactid', $contact->id); if ($user) { $params = array('firstname' => $contact->firstname, 'lastname' => $contact->lastname, 'email' => $contact->email); $this->userService->updateUser($user, $params); } } $this->trackerService->track('update-contact', $contact->id, null, null, print_r($params, true)); $this->dbService->commit(); } catch (Exception $e) { $this->dbService->rollback(); $this->log->err("Failed creating contact for params " . print_r($params, true) . ": " . $e->getMessage()); $contact = null; throw $e; } return $contact; }
/** * Lock a timesheet and all of the timesheet entries recorded * from its start -> finish period. If there's an entry * recorded during that period that belongs to another timesheet, * it won't be included. * * @param Timesheet $timesheet the timesheet that is to be locked */ public function lockTimesheet(Timesheet $timesheet) { $clientid = $timesheet->clientid; if ($timesheet->projectid) { $clientid = null; } $start = date('Y-m-d 00:00:00', strtotime($timesheet->from)); $end = date('Y-m-d 23:59:59', strtotime($timesheet->to)); $cats = array(); if (is_array($timesheet->tasktype)) { $cats = $timesheet->tasktype; } $records = $this->getDetailedTimesheet(null, null, $timesheet->projectid, $clientid, null, $start, $end, $cats); try { $this->dbService->beginTransaction(); foreach ($records as $record) { if ($record->timesheetid > 0) { za()->log("Record #{$record->id} is already part of timesheet #{$record->timesheet}"); continue; } $record->timesheetid = $timesheet->id; $this->dbService->saveObject($record); } $timesheet->locked = 1; $this->saveTimesheet($timesheet); $this->dbService->commit(); } catch (Exception $e) { $this->dbService->rollback(); throw $e; } }
/** * Set the status of an expense. */ public function setExpenseStatus(Expense $expense, $status) { $expense->status = $status; $expense->approver = za()->getUser()->getUsername(); $this->dbService->beginTransaction(); $this->dbService->saveObject($expense); $this->trackerService->track('expense-status-changed', $expense->approver . ' set status to ' . $status); $this->dbService->commit(); }
/** * Grants access to a particular item * * @param object $item the item to remove access from * @param String $user The user to remove access for * @param String $role The role to remove (optional) */ public function grantAccess($item, $user, $role) { $fields = array('itemid' => $item->id, 'itemtype' => get_class($item), 'authority' => $user->getUsername(), 'role' => $role); $existing = $this->dbService->getByField($fields, 'UserRole'); if ($existing != null) { return $existing; } $userrole = new UserRole(); $userrole->itemid = $item->id; $userrole->itemtype = get_class($item); $userrole->authority = $user->getUsername(); $userrole->role = $role; $this->dbService->saveObject($userrole); }
/** * Save tags for a given item * * @param object $item * @param string $tags */ public function saveTags($item, $tags) { $type = mb_strtolower(get_class($item)); $id = (int) $item->id; if (!$id || !mb_strlen($type)) { throw new Exception("Cannot delete tags for {$item}"); } // delete all tags for an object $this->deleteTags($item, za()->getUser()); // okay, now for everything in tags, we need to create a new one $tags = mb_split(",", $tags); foreach ($tags as $tag) { $tag = $this->normaliseTag($tag); if (mb_strlen($tag) <= 2) { continue; } $params = array(); $params['uid'] = za()->getUser()->getId(); $params['itemtype'] = $type; $params['itemid'] = $id; $params['tag'] = $tag; $this->dbService->saveObject($params, 'Tag'); } }
/** * Creates a watch between a user and object * * @param CrmUser $user * @param int $id * @param string $type */ public function createWatch($user, $id, $type = null) { // If the $id param is actually an object if ($type == null) { $type = get_class($id); $id = $id->id; } $existing = $this->getWatch($user, $id, $type); if ($existing) { // don't create another one return; } $params = array('itemid' => $id, 'itemtype' => $type, 'userid' => $user->getUsername()); $this->dbService->saveObject($params, 'ItemWatch'); }
/** * Create a task from a feature * * @param array $params * @return Task */ public function createTaskFromFeature($params, $createFrom = null) { // If called directly, we don't need to worry about // using the createFrom object. if ($params instanceof Feature) { $createFrom = $params; } /* @var $createFrom Feature */ $data = array(); $data['title'] = $createFrom->title; $data['description'] = $createFrom->description; $data['estimated'] = $createFrom->estimated * za()->getConfig('day_length', 8); $data['userid'] = array(za()->getUser()->getUsername()); $data['projectid'] = $createFrom->projectid; return $this->dbService->saveObject($data, 'Task'); }
/** * Set the leave status */ public function setLeaveStatus(LeaveApplication $leaveApplication, $status, $daysAffected = 0) { $leaveApplication->status = $status; $leaveApplication->approver = za()->getUser()->getUsername(); if ($daysAffected) { $leaveApplication->days = $daysAffected; } $this->dbService->beginTransaction(); if ($status == LeaveApplication::LEAVE_DENIED) { $leaveApplication->days = 0; } $this->dbService->saveObject($leaveApplication); if ($status == LeaveApplication::LEAVE_APPROVED) { // if it's leave approved, need to create a task in the relevant project milestone // and make sure the user has time added for it } $this->applyTimeForLeave($leaveApplication); $this->trackerService->track('leave-updated', "Leave application for {$leaveApplication->username} set to {$status}"); $this->dbService->commit(); // send a message to the user $msg = new TemplatedMessage('leave-updated.php', array('model' => $leaveApplication)); $this->notificationService->notifyUser('Leave Application Updated', $leaveApplication->username, $msg); }
/** * Saves an issue to the database * * @param array|Issue $params */ public function saveIssue($params, $doNotify = true) { $issue = null; $sendNotification = false; if (is_array($params)) { $existingId = ifset($params, 'id'); } else { $existingId = $params->id; } if (!$existingId) { $sendNotification = true; } $oldIssue = null; // If there's an existing one, save it in the history if ($existingId) { $oldIssue = $this->getIssue($existingId); $this->versioningService->createVersion($oldIssue); } // If saving a new one, we want to $issue = $this->dbService->saveObject($params, 'Issue'); // Okay, now check if it's on a project or not $project = null; if (!$issue->projectid) { // Get the project $client = $this->clientService->getClient($issue->clientid); if (!$client) { throw new Exception("No client exists with ID {$issue->clientid}"); } $project = $this->clientService->getClientSupportProject($client); if (!$project) { throw new Exception("Missing client details for request {$issue->title}"); } $issue->projectid = $project->id; $this->dbService->updateObject($issue); } else { $project = $this->projectService->getProject($issue->projectid); } // make sure it's assigned to someone if (!mb_strlen($issue->userid)) { if (!$project) { $project = $this->projectService->getProject($issue->projectid); } $issue->userid = $project->manager; $this->dbService->updateObject($issue); } // Check to see if the assignee has a watch, if not, add one $assignedTo = $this->userService->getByName($issue->userid); if ($assignedTo) { $existing = $this->notificationService->getWatch($assignedTo, $issue->id, 'Issue'); if (!$existing) { $this->notificationService->createWatch($assignedTo, $issue->id, 'Issue'); } } $this->log->debug("Saving request, notify {$issue->userid} = " . $sendNotification); $this->trackerService->track('create-issue', $issue->id); // now send a notification to those users in the // group assigned to the project the issue was just saved against. if ($sendNotification && $doNotify) { // create and assign a task to whoever was assigned to the issue // by default if ($assignedTo && false) { // Create the task $task = new Task(); $task->projectid = $issue->projectid; $task->userid = array($assignedTo->username); $task->title = 'Respond to request "' . $issue->title . '"'; $task->description = "Please investigate this request:\r\n\r\n" . $issue->description; $task->category = 'Support'; $task->due = date('Y-m-d H:i:s', strtotime('+2 days')); // $task = $this->projectService->saveTask($task, false, true); // $this->itemLinkService->linkItems($issue, $task); } $this->log->debug("Notifying users about new request {$issue->title}"); $this->notifyOfNewIssue($issue); // Add a watch for the current user. Note that it might be null if // this issue is created from an email $user = za()->getUser(); if ($user) { $this->notificationService->createWatch($user, $issue->id, 'Issue'); } } if ($issue->status == Issue::STATUS_CLOSED) { // remove notifications $subscribers = $this->notificationService->getSubscribers($issue); foreach ($subscribers as $username => $item) { $user = $this->userService->getUserByField('username', $username); if ($user) { $this->notificationService->removeWatch($user, $issue->id, 'Issue'); } } } // See if the status has changed and notify the creator if ($oldIssue != null && $oldIssue->status != $issue->status && $doNotify) { try { $this->notifyOfUpdatedIssue($oldIssue, $issue); } catch (Exception $e) { $this->log->warn("Failed to notify of request update"); } } return $issue; }
/** * Saves an existing feature * * @param Feature $feature */ public function saveFeature(Feature $feature) { $this->trackerService->track('save-feature', $feature->id); $this->dbService->saveObject($feature); }
/** * Save a file object * * @param File $file */ public function saveFile(File $file) { $file->path = $this->normalizePath($file->path); $this->dbService->saveObject($file); }
/** * Saves an object. is declared as a protected method to allow * subclasses to override how parameters are saved if needbe. */ protected function saveObject($params, $modelType) { return $this->dbService->saveObject($params, $modelType); }