public function executeEdit(sfWebRequest $request) { $allowedOps = array('delete', 'edit', 'add'); $op = $request->getParameter('op'); if (!in_array($op, $allowedOps)) { return false; } if ($op == 'add') { return $this->executeAdd($request); } $listId = $request->getParameter('listId'); $list = PcListPeer::retrieveByPk($listId); if ($list && $op == 'delete') { $creator = $list->getCreator(); PcUtils::checkLoggedInUserPermission($creator); if ($list->isSystem()) { die("ERROR: " . __('ACCOUNT_ERROR_CANT_EDIT_SYSTEM_LIST')); } $list->delete(); return $this->renderDefault(); } if ($op == 'edit') { $creator = $list->getCreator(); PcUtils::checkLoggedInUserPermission($creator); if ($list->isInbox()) { die("ERROR: " . __('ACCOUNT_ERROR_CANT_EDIT_SYSTEM_LIST')); } $newTitle = $request->getParameter('listTitle'); $isHeader = $request->getParameter('isHeader') ? $request->getParameter('isHeader') : 0; $isHeader = (bool) $isHeader; $list->setTitle($newTitle); $list->setIsHeader($isHeader); $list->save(); $ret = array('id' => $list->getId(), 'name' => $list->getTitle(), 'isHeader' => (int) $list->isHeader()); return $this->renderJson($ret); } return $this->renderDefault(); }
/** * Get the associated PcList object * * @param PropelPDO Optional Connection object. * @return PcList The associated PcList object. * @throws PropelException */ public function getPcList(PropelPDO $con = null) { if ($this->aPcList === null && $this->list_id !== null) { $this->aPcList = PcListPeer::retrieveByPk($this->list_id); /* The following can be used additionally to guarantee the related object contains a reference to this object. This level of coupling may, however, be undesirable since it could result in an only partially populated collection in the referenced object. $this->aPcList->addPcTasks($this); */ } return $this->aPcList; }
/** * Creates (or edits) a task * By default, it sets isFromSystem false. That is because a user may * edit a task from the system and make it meaningful to them. The problem * is the automatic cleaning up of system tasks would delete it. And this * method is used also when editing a task. * Repetition expression has got priority on dueDate * * @param string $description * @param integer $listId (if listId not >0, we will use the user's Inbox) * @param integer $taskId (if we are editing an existing task) * @param string $contexts (comma separated list of ids) * @param boolean $isHeader * @param string $note * @param string $dueDate (no GMT but user's local date) * @param string $dueTime - formatted as an integer, ie: 0753 (no GMT but user's local time) * @param integer $repetitionId * @param integer $repetitionParam * @param integer $taskAboveId - if ($taskAboveId = -100000), that means to put at the end of the list * @param string $callerContext - can be either 'ajax' or 'email' or 'gcal' * @param string $dueDateFormat - to specify a particular format for parsing the dueDate param * (otherwise the user's date format is used). * Uses the PHP date() format * @param boolean $setNextOccurrence (=true) * @return PcTask - the object that has been created */ public static function createOrEdit($description, $listId = 0, $taskId = 0, $contexts = '', $isHeader = false, $note = '', $dueDate = '', $dueTime = '', $isStarred = 0, $repetitionId = 0, $repetitionParam = 0, $taskAboveId = 0, $callerContext = '', $dueDateFormat = '', $setNextOccurrence = true) { $dueDate = strtolower($dueDate); $loggedInUser = PcUserPeer::getLoggedInUser(); if ($repetitionId < 0) { $repetitionId = 0; } if (!((int) $listId > 0)) { // Inserting into the default list: Inbox $listId = $loggedInUser->getInbox()->getId(); } $listCreator = PcListPeer::retrieveByPk($listId)->getCreator(); PcUtils::checkLoggedInUserPermission($listCreator); $mode = $taskId ? 'edit' : 'add'; $task = $mode == 'add' ? new PcTask() : PcTaskPeer::retrieveByPk($taskId); if ($mode == 'edit' && !$task->validateOwner($loggedInUser)) { throw new sfException('User ' . $loggedInUser->getId() . ' trying to access the task ' . $task->getId() . ' illegitimately'); } list($listIdFromShortcut, $contextIdsFromShortcut, $potentialDueDateExpressionsFromShortcut, $potentialDueTimeExpressionsFromShortcut) = $task->extractInfoFromTaskDescription($description); $listId = $listIdFromShortcut > 0 ? $listIdFromShortcut : $listId; // if we are here all the contexts (if any) are valid if (count($contextIdsFromShortcut)) { $task->setContexts(implode(',', $contextIdsFromShortcut)); } else { $userContexts = $loggedInUser->getContextsArray(true); $contextIdsFromInput = PcUtils::explodeWithEmptyInputDetection(',', $contexts); $validatedContexts = array(); foreach ($contextIdsFromInput as $cid) { $userContext = PcUsersContextsPeer::retrieveByPK($cid); if ($userContext) { $validatedContexts[] = array_search(strtolower($userContext->getContext()), $userContexts); } } $task->setContexts(implode(',', $validatedContexts)); } // {{{ START: looking for a contact $contactPrefix = 'cid'; if (strpos($description, $contactPrefix) !== FALSE) { if (preg_match('/' . $contactPrefix . '([0-9]+)/', $description, $contactArray)) { $description = str_replace($contactArray[0], '', $description); // removing the contact bit from the description if (is_numeric($contactArray[1])) { $task->setContactId($contactArray[1]); } } } // END: looking for a contact }}} $task->setDescription($description); // it is the actual content of the task if ($repetitionId == 34 && !($repetitionParam > 0)) { // $repetitionParam should have been > 0! $repetitionId = 0; } $oldRepetitionId = $task->getRepetitionId(); if ($repetitionId > 0) { $task->setRepetitionId($repetitionId); $task->setRepetitionParam($repetitionParam); } else { $task->setRepetitionId(NULL); $task->setRepetitionParam(0); } $changeInRepetitionId = false; if ($oldRepetitionId != $task->getRepetitionId()) { $changeInRepetitionId = true; } try { $task->setListId($listId); } catch (sfException $e) { if ($callerContext == 'ajax') { die('ERROR: You can\'t insert a task in a header.'); } } $task->setIsHeader($isHeader); if (!$isHeader) { $task->setNote($note); } else { $task->setDueDate(null); } $validDueDateExpression = false; $unrecognizedShortcuts = array(); if (count($potentialDueDateExpressionsFromShortcut) && !$isHeader) { foreach ($potentialDueDateExpressionsFromShortcut as $potentialDueDateExpressionFromShortcut) { $validDueDateExpression = $task->setDueDate($potentialDueDateExpressionFromShortcut); if (!$validDueDateExpression) { $unrecognizedShortcuts[] = $potentialDueDateExpressionFromShortcut; } else { $validDueDateExpression = true; break; } } } if (!$validDueDateExpression && $dueDate && !$isHeader) { $validDueDateExpression = $task->setDueDate($dueDate, $dueDateFormat); } if (!$task->getDueDate()) { $task->setDueDate(NULL); } if ($mode == 'add') { $list = $task->getPcList(); if (!$list) { // this should not happen, but just in case... $list = $loggedInUser->getInbox(); } if (!$taskAboveId) { $maxTasksSortOrder = self::getMaxTasksSortOrder($list); $task->setSortOrder($maxTasksSortOrder + 1); } else { if ($taskAboveId == -100000) { $minTasksSortOrder = self::getMinTasksSortOrder($list); $task->setSortOrder($minTasksSortOrder - 1); } else { $maxTasksSortOrder = self::getMaxTasksSortOrder($list); // they are inserting the new task below an old one // I need to insert the new task after the task whose id is beforeListId $allTasks = $list->getIncompletedTasks(); // the tasks are returned with sortOrder descending order // and the tasks are displayed with the greatest sortOrder on top $newSortOrder = $maxTasksSortOrder; foreach ($allTasks as $oneOfTheOtherTasks) { $delta = 1; if ($oneOfTheOtherTasks->getId() == $taskAboveId) { $task->setSortOrder($newSortOrder - 1); $delta++; } $oneOfTheOtherTasks->setSortOrder($newSortOrder); $oneOfTheOtherTasks->save(); $newSortOrder -= $delta; } } } } if (count($unrecognizedShortcuts)) { if ($callerContext == 'ajax') { die("ERROR: {$unrecognizedShortcuts[0]} is not a tag neither a valid due date/time expression."); } } if ($potentialDueTimeExpressionsFromShortcut && $callerContext == 'ajax') { if (!$potentialDueTimeExpressionsFromShortcut->isValid()) { die("ERROR: the due time shortcut is not valid."); } } if ($dueTime || $potentialDueTimeExpressionsFromShortcut) { // it doesn't make sense to set a time if a date is not specified if ($task->getDueDate() || $task->getRepetitionId()) { if ($dueTime) { $task->setDueTime($dueTime); } else { $task->setDueTime($potentialDueTimeExpressionsFromShortcut->getIntegerValue()); } } else { if ($callerContext == 'ajax') { die('ERROR: You can\'t set a due time without a due date.'); } else { // if the user tried to insert a due time shortcuts but we couldn't take // that value because there is no due date, we put the due time back in // the description if ($potentialDueTimeExpressionsFromShortcut) { $descriptionWithDueTimeEmbedded = $task->getDescription() . ' @' . $potentialDueTimeExpressionsFromShortcut->getHumanFriendlyTime($loggedInUser); $task->setDescription($descriptionWithDueTimeEmbedded); } } } } else { $task->setDueTime(null); } if (!$task->isHeader()) { $task->setStarred((bool) $isStarred); } $task->save(); if ($task->getRepetitionId() && $setNextOccurrence) { $task->setNextOccurrence(true); // N.B.: this saves the object!!!!!!! } // we need to do this operation here because we need the object // saved in the db to be able to refer to it via its ID $task->alignTasksContextsTable(); // {{{ // this is to fix a bug: // long task descriptions were cut (because of database schema contraint) // without a clear feedback on the user interface (Javascript was // inserting the whole string sent via AJAX) $task = PcTaskPeer::retrieveByPK($task->getId()); // }}} // This is VERY IMPRTANT because a user may // edit a task from the system and make it meaningful to them. The problem // is the automatic cleaning up of system tasks would delete it. And this // method is used also when editing a task. $task->setIsFromSystem(false); if ($loggedInUser->hasGoogleCalendarIntegrationActive()) { if ($callerContext != 'gcal') { $gcal = new GoogleCalendarInterface($loggedInUser); $gcal->init(); $gcal->createOrUpdateEvent($task); } } return $task; }
/** * Sets the list Id. * * @see parent::setListId() * @param integer $v * @return PcTask */ public function setListId($v) { if (PcListPeer::retrieveByPk($v)->isHeader()) { throw new sfException('A task can\'t belong to a header rather than a proper list.'); } return parent::setListId($v); }