public function executeAddEdit(sfWebRequest $request) { $op = $request->getParameter('op'); $contextId = $request->getParameter('id'); $contextName = trim($request->getParameter('name')); $newContext = null; if ($contextName && strpos($contextName, ' ') !== FALSE) { die("ERROR: " . __('ACCOUNT_ERROR_TAG_CANT_HAVE_SPACE')); } $existingContexts = PcUserPeer::getLoggedInUser()->getContextsArray(true); if (count($existingContexts)) { if (in_array(strtolower($contextName), $existingContexts)) { die("ERROR: " . __('ACCOUNT_ERROR_TAG_ALREADY_EXIST')); } } if ($op == 'delete' && $contextId) { $contextToDelete = PcUsersContextsPeer::retrieveByPk($contextId); PcUtils::checkLoggedInUserPermission(PcUserPeer::retrieveByPk($contextToDelete->getUserId())); $contextToDelete->delete(); } else { if ($op == 'edit' && $contextId && $contextName) { $contextToEdit = PcUsersContextsPeer::retrieveByPk($contextId); PcUtils::checkLoggedInUserPermission(PcUserPeer::retrieveByPk($contextToEdit->getUserId())); $contextToEdit->setContext($contextName)->save(); // {{{ // this lines to make sure the list details we sent back via AJAX // are the ones stored in the database $contextToEdit = PcUsersContextsPeer::retrieveByPk($contextId); // }}} } else { if ($op == 'add' && $contextName) { // getting max sortOrder $c = new Criteria(); $c->addDescendingOrderByColumn(PcUsersContextsPeer::SORT_ORDER); $maxSortOrder = PcUsersContextsPeer::doSelectOne($c)->getSortOrder(); $context = new PcUsersContexts(); $context->setContext($contextName)->setPcUser(PcUserPeer::getLoggedInUser())->setSortOrder($maxSortOrder + 1)->save(); // {{{ // this lines to make sure the list details we sent back via AJAX // are the ones stored in the database $newContext = PcUsersContextsPeer::retrieveByPk($context->getId()); // }}} } } } $tag = isset($contextToEdit) && $contextToEdit ? $contextToEdit : $newContext; if ($request->isXmlHttpRequest()) { if ($tag) { $ret = array('id' => $tag->getId(), 'name' => $tag->getContext()); return $this->renderJson($ret); } else { return $this->renderDefault(); } } }
/** * Register a new user * * @param string $email - the email address * @param string $password - the plain password (no encryption) * @param string $lang - if it is null or empty, the language will be detected from the header of the request * @param string $preferredLang - this should be a 2-char abbreviation of the lang the user wants, * among the ones in the main app.yml config file * @param string $tzLabel - a timezone label as in the PcTimezone db table * @param integer $dstOn (1 or 0) - whether or not the dst for the user is on * @param boolen $joinNewsletter(=false) - whether the user decided to join our newsletter * @param boolen $sendActivationEmail(=true) - whether to send the activation email * @return boolean|PcUser false is a user with that email already exists, the PcUser object otherwise */ public static function registerNewUser($email, $password, $lang, $preferredLang, $tzLabel, $dstOn, $joinNewsletter = false, $sendActivationEmail = true) { if (self::emailExist($email)) { return false; } $newUser = new PcUser(); $newUser->setEmail($email); $newUser->setPassword($password); $newUser->setAwaitingActivation(1); if ($joinNewsletter) { $newUser->setNewsletter(1); } $newUser->save(); // Dealing with timezone $newUser->setDstActive($dstOn); $c = new Criteria(); $c->add(PcTimezonePeer::LABEL, $tzLabel, Criteria::EQUAL); $timezone = PcTimezonePeer::doSelectOne($c); if (!is_object($timezone)) { // set to a default one $timezone = PcTimezonePeer::retrieveByPK(21); } $newUser->setTimezoneId($timezone->getId()); // Dealing with formats // _ time format: the countries with the majority of our users are using the // 12H format, thus we can leave the default // _ for the date format we check whether they are in USA // _ for the first day of the week, we check whether they are in USA $dateFormatId = 3; $weekStart = 1; // from Monday $tzOffset = $timezone->getOffset(); if ($tzOffset <= -300 && $tzOffset >= -450) { $dateFormatId = 4; $weekStart = 0; // from Sunday } $newUser->setDateFormat($dateFormatId); $newUser->setWeekStart($weekStart); if ($lang != null && $lang !== '') { $newUser->setLanguage($lang); } else { $newUser->setLanguage(PcUtils::getVisitorAcceptLanguage()); } $availableLangs = PcLanguagePeer::getAvailableLanguageAbbreviations(); if (in_array($preferredLang, $availableLangs)) { $newUser->setPreferredLanguage($preferredLang); } else { $newUser->setPreferredLanguage(SfConfig::get('app_site_defaultLang')); } $newUser->setIpAddress(PcUtils::getVisitorIPAddress()); if ($sessionEntryPoint = sfContext::getInstance()->getUser()->getAttribute('session_entry_point')) { $newUser->setSessionEntryPoint($sessionEntryPoint); } if ($sessionReferral = sfContext::getInstance()->getUser()->getAttribute('session_referral')) { $newUser->setSessionReferral($sessionReferral); } $newUser->save(); // Creating system lists $inboxList = new PcList(); $inboxList->setIsInbox(1)->setTitle(__('ACCOUNT_LISTS_INBOX'))->setCreator($newUser)->save(); $todoList = new PcList(); $todoList->setIsTodo(1)->setTitle(__('ACCOUNT_LISTS_TODO'))->setCreator($newUser)->save(); // Creating default contexts $context = new PcUsersContexts(); $context->setPcUser($newUser)->setContext(__('ACCOUNT_TAGS_DEFAULT_HOME'))->save(); $context = new PcUsersContexts(); $context->setPcUser($newUser)->setContext(__('ACCOUNT_TAGS_DEFAULT_ERRANDS'))->save(); $context = new PcUsersContexts(); $context->setPcUser($newUser)->setContext(__('ACCOUNT_TAGS_DEFAULT_COMPUTER'))->save(); // Creating some tasks for the inbox $newUser->addToInbox(__('ACCOUNT_MISC_WELCOME_TASK')); // creating Plancake email address $newUser->generateAndStorePlancakeEmailAddress(); // I need to use a token for the activation of their account $token = ''; $c = new Criteria(); $c->add(PcActivationTokenPeer::USER_ID, $newUser->getId(), Criteria::EQUAL); $tokenEntry = PcActivationTokenPeer::doSelectOne($c); if (is_object($tokenEntry)) { $token = $tokenEntry->getToken(); } else { $secret = sfConfig::get('app_registration_secret'); // token doesn't need to be 32-char long. It is better to keep it short // so there will be less chance the email client will break the link into 2 lines $token = substr(md5($newUser->getId() . $secret . time()), 0, 14); $tokenEntry = new PcActivationToken(); $tokenEntry->setUserId($newUser->getId()); $tokenEntry->setToken($token); $tokenEntry->save(); } // now we can send the email if ($sendActivationEmail) { $link = sfContext::getInstance()->getController()->genUrl('@activation?t=' . $token, true); $from = sfConfig::get('app_emailAddress_contact'); $subject = 'Plancake - ' . __('WEBSITE_REGISTRATION_EMAIL_SUBJECT'); $body = sprintf(__('WEBSITE_REGISTRATION_EMAIL_BODY'), $link); PcUtils::sendEmail($email, $subject, $body, $from); } $newUser->refreshLatestBlogAccess(); sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($newUser, 'user.sign_up', array('user' => $newUser, 'plainPassword' => $password))); return $newUser; }
/** * Declares an association between this object and a PcUsersContexts object. * * @param PcUsersContexts $v * @return PcTasksContexts The current object (for fluent API support) * @throws PropelException */ public function setPcUsersContexts(PcUsersContexts $v = null) { if ($v === null) { $this->setUsersContextsId(NULL); } else { $this->setUsersContextsId($v->getId()); } $this->aPcUsersContexts = $v; // Add binding for other direction of this n:n relationship. // If this object has already been added to the PcUsersContexts object, it will not be re-added. if ($v !== null) { $v->addPcTasksContexts($this); } return $this; }
public function import($xml) { $originalMaxExecutionTime = ini_get('max_execution_time'); ini_set('max_execution_time', 120); proc_nice(1); $tagLocalIds = array(); $listLocalIds = array(); $newTagIds = array(); $newListIds = array(); $newTaskIds = array(); $newNoteIds = array(); $tags = $xml->plancake_tasks->tags->tag; foreach ($tags as $tag) { $isNewTag = false; $tagId = (int) $tag->id > 0 ? $tag->id : null; $tagLocalId = (int) $tag->localId; $tagName = (string) $tag->name; $tagSortOrder = (int) $tag->sortOrder; $tagObj = null; if ($tagId && !in_array($tagId, $newTagIds)) { $tagObj = PcUsersContextsPeer::retrieveByPK($tagId); } if ($tagObj) { if ($tagObj->getUserId() != $this->user->getId()) { die("Hacking attempt."); } } else { // tags are unique by name, thus // we check whether the tag is already in the instance // we are importing the dump to. $c = new Criteria(); $c->add(PcUsersContextsPeer::CONTEXT, $tagName); $tagObj = PcUsersContextsPeer::doSelectOne($c); if (!is_object($tagObj)) { $tagObj = new PcUsersContexts(); $isNewTag = true; } } $tagObj->setUserId($this->user->getId())->setContext($tagName)->setSortOrder($tagSortOrder)->save(); $tagLocalIds[$tagLocalId] = $tagObj->getId(); if ($isNewTag) { $newTagIds[] = $tagObj->getId(); } } $lists = $xml->plancake_tasks->lists->list; foreach ($lists as $list) { $isNewList = false; $listId = (int) $list->id > 0 ? $list->id : null; $listLocalId = (int) $list->localId; $listName = (string) $list->name; $listSortOrder = (int) $list->sortOrder; $listIsInbox = (int) $list->isInbox == 1 ? true : false; $listIsTodo = (int) $list->isTodo == 1 ? true : false; $listIsHeader = (int) $list->isHeader == 1 ? true : false; $listObj = null; if ($listId && !in_array($listId, $newListIds)) { $listObj = PcListPeer::retrieveByPK($listId); } if ($listObj) { if ($listObj->getCreatorId() != $this->user->getId()) { die("Hacking attempt."); } } else { if ($listIsInbox) { $listObj = $this->user->getInbox(); } else { if ($listIsTodo) { $listObj = $this->user->getTodo(); } else { $listObj = new PcList(); $isNewList = true; } } } $listObj->setCreatorId($this->user->getId())->setTitle($listName)->setSortOrder($listSortOrder)->setIsInbox($listIsInbox)->setIsTodo($listIsTodo)->setIsHeader($listIsHeader)->save(); $listLocalIds[$listLocalId] = $listObj->getId(); if ($isNewList) { $newListIds[] = $listObj->getId(); } } $tasks = $xml->plancake_tasks->tasks->task; foreach ($tasks as $task) { $isNewTask = false; $taskId = (int) $task->id > 0 ? $task->id : 0; $taskListLocalId = (int) $task->listLocalId; $taskDescription = (string) $task->description; $taskSortOrder = (int) $task->sortOrder; $taskDueDate = (string) $task->dueDate; $taskDueTime = strlen($task->dueTime) > 0 ? (int) $task->dueTime : ''; $taskRepetitionId = (int) $task->repetitionId; $taskRepetitionParam = (int) $task->repetitionParam; $taskIsStarred = (int) $task->isStarred == 1 ? true : false; $taskIsCompleted = (int) $task->isCompleted == 1 ? true : false; $taskIsHeader = (int) $task->isHeader == 1 ? true : false; $taskIsFromSystem = (int) $task->isFromSystem == 1 ? true : false; $taskTagLocalIds = (string) $task->tagLocalIds; $taskNote = (string) $task->note; $taskListId = $listLocalIds[$taskListLocalId]; $taskTagIdsArray = array(); $taskTagLocalIdsArray = PcUtils::explodeWithEmptyInputDetection(',', $taskTagLocalIds); foreach ($taskTagLocalIdsArray as $id) { $taskTagIdsArray[] = $tagLocalIds[$id]; } $taskTagIds = ''; if (count($taskTagIdsArray)) { $taskTagIds = implode(',', $taskTagIdsArray); } $taskFromDb = null; if ($taskId && !in_array($taskId, $newTaskIds)) { $taskFromDb = PcTaskPeer::retrieveByPK($taskId); } if (!is_object($taskFromDb)) { // if the task doesn't exist (even if the dump contains a taskId) // we want to add it. $taskId = 0; $isNewTask = true; } $newTask = PcTaskPeer::createOrEdit($taskDescription, $taskListId, $taskId, $taskTagIds, $taskIsHeader, $taskNote, $taskDueDate, $taskDueTime, $taskIsStarred, $taskRepetitionId, $taskRepetitionParam, 0, '', 'd-m-Y', false); if ($taskIsCompleted) { $newTask->setIsCompleted(1); $newTask->setCompletedAt($task->completedAt); $newTask->save(); } if ($isNewTask) { $newTaskIds[] = $newTask->getId(); } if (!$taskId) { $newTask->deleteDirtyEntry(); } } $notes = $xml->plancake_notes->notes->note; foreach ($notes as $note) { $isNewNote = false; $noteId = (int) $note->id > 0 ? $note->id : null; $noteTitle = (string) $note->title; $noteContent = (string) $note->content; $noteObj = null; if ($noteId && !in_array($noteId, $newNoteIds)) { $noteObj = PcNotePeer::retrieveByPK($noteId); } if ($noteObj) { if ($noteObj->getCreatorId() != $this->user->getId()) { die("Hacking attempt."); } } else { $noteObj = new PcNote(); $isNewNote = true; } $noteObj->setCreatorId($this->user->getId())->setTitle($noteTitle)->setContent($noteContent)->save(); if ($isNewNote) { $newNoteIds[] = $noteObj->getId(); } } ini_set('max_execution_time', $originalMaxExecutionTime); }
/** * Adds an object to the instance pool. * * Propel keeps cached copies of objects in an instance pool when they are retrieved * from the database. In some cases -- especially when you override doSelect*() * methods in your stub classes -- you may need to explicitly add objects * to the cache in order to ensure that the same objects are always returned by doSelect*() * and retrieveByPK*() calls. * * @param PcUsersContexts $value A PcUsersContexts object. * @param string $key (optional) key to use for instance map (for performance boost if key was already calculated externally). */ public static function addInstanceToPool(PcUsersContexts $obj, $key = null) { if (Propel::isInstancePoolingEnabled()) { if ($key === null) { $key = (string) $obj->getId(); } // if key === null self::$instances[$key] = $obj; } }