/** * Delete all answers made by user * Also update questions affected by * deletion of those answers * * @return object $this */ protected function deleteAnswers() { $coll = $this->Registry->Mongo->ANSWERS; $cur = $coll->find(array('i_uid' => $this->Request['uid'])); if ($cur && $cur->count() > 0) { foreach ($cur as $a) { $Question = new \Lampcms\Question($this->Registry); try { $Answer = new Answer($this->Registry, $a); $Question->by_id((int) $Answer->getQuestionId()); $Question->removeAnswer($Answer); $Question->save(); /** * setSaved() because we don't need auto-save feature * to save each answer * since all answers will be removed at end of this method */ $Answer->setSaved(); } catch (\MongoException $e) { d('Question not found by _id: ' . $a['i_qid']); } if (!empty($a['cc'])) { $this->aCountries[] = $a['cc']; } } $res = $coll->remove(array('i_uid' => $this->Request['uid']), array('safe' => true)); d('questions removed: ' . print_r($res, 1)); } return $this; }
/** * Notify all who follows the question * But exclude the Viewer - whoever just added * the new answer or whatever * * * and exclude all who follows the Viewer because all who * follows the Viewer will be notified via * the nofityUserFollowers * * @param int qid id of question * * @param int excludeUid UserID of user that should NOT * be notified. Usually this is in a special case of when * the answer or comment owner has already been notified * so now we just have to exclude the same user in case same user * is also the question author. * * @return object $this */ protected function notifyQuestionFollowers($qid = null, $excludeUid = 0) { $viewerID = $this->Registry->Viewer->getUid(); d('$viewerID: ' . $viewerID); $routerCallback = $this->Registry->Router->getCallback(); /** * $qid can be passed here * OR in can be extracted from $this->Question */ if ($qid) { $Question = new \Lampcms\Question($this->Registry); try { $Question->by_id((int) $qid); } catch (\Exception $e) { e('Unable to create Question by _id. Exception: ' . $e->getMessage() . ' in file: ' . $e->getFile() . ' on line: ' . $e->getLine()); $Question = null; } } else { $Question = $this->Question; } if (null === $Question) { d('no $Question'); return $this; } $subj = 'onNewAnswer' === $this->eventName || 'onApprovedAnswer' === $this->eventName ? 'email.subject.question_answer' : 'email.subject.question_comment'; $body = 'onNewAnswer' === $this->eventName || 'onApprovedAnswer' === $this->eventName ? 'email.body.question_answer' : 'email.body.question_comment'; $siteUrl = $this->Registry->Ini->SITE_URL; $updateType = 'onNewAnswer' === $this->eventName || 'onApprovedAnswer' === $this->eventName ? 'answer' : 'comment'; $username = '******' === $this->eventName || 'onApprovedAnswer' === $this->eventName ? $this->obj['username'] : $this->aInfo['username']; if ('onNewAnswer' === $this->eventName || 'onApprovedAnswer' === $this->eventName) { $url = $this->obj->getUrl(); } else { $url = $siteUrl . '{_WEB_ROOT_}/{_viewquestion_}/{_QID_PREFIX_}' . $this->aInfo['i_qid'] . '/#c' . $this->aInfo['_id']; $url = $routerCallback($url); } d('url: ' . $url); $content = 'onNewAnswer' !== $this->eventName ? "\n____\n" . \strip_tags($this->aInfo['b']) . "\n" : ''; $varsBody = array('{username}' => $username, '{title}' => $this->Question['title'], '{body}' => $content, '{link}' => $url, '{site_title}' => $this->Registry->Ini->SITE_NAME, '{home}' => $this->Registry->Router->getHomePageUrl()); $body = new TranslatableBody($body, $varsBody); $subj = new TranslatableSubject($subj); $Mailer = $this->Registry->Mailer; $Mailer->setCache($this->Registry->Cache); /** * MongoCollection USERS * * @var object MongoCollection */ $coll = $this->collUsers; d('before shutdown function for question followers'); /** * Get array of followers for this question */ $aFollowers = $Question['a_flwrs']; if (!empty($aFollowers)) { $func = function () use($updateType, $viewerID, $aFollowers, $subj, $body, $coll, $Mailer, $excludeUid) { /** * Remove $viewerID from aFollowers * Remove excludeID from aFollowers * Removing these userIDs from * the find $in condition is guaranteed to not * have these IDs in result and is much better * than adding extra $ne or $nin conditions * on these uids to find() * */ if (false !== ($key = array_search($viewerID, $aFollowers))) { array_splice($aFollowers, $key, 1); } if (!empty($excludeUid)) { if (false !== ($key = array_search($excludeUid, $aFollowers))) { array_splice($aFollowers, $key, 1); } } array_unique($aFollowers); /** * Find all users who follow this question * and * are not also following the Viewer * * In case of comment we should not exclude * Viewer followers because Viewer followers are NOT * notified on user comment * */ if ('comment' !== $updateType) { /** * Condition: Find all users with _id in array of $aFollowers * AND NOT FOLLOWING viewer (who is the author of this answer) * because they will also be notified of this answer * in a separate email that is sent to all followers of Viewer */ $cur = $coll->find(array('_id' => array('$in' => $aFollowers), 'a_f_u' => array('$nin' => array(0 => $viewerID)), 'ne_fq' => array('$ne' => true)), array('email' => true, 'locale' => true)); } else { $cur = $coll->find(array('_id' => array('$in' => $aFollowers), 'ne_fq' => array('$ne' => true)), array('email' => true, 'locale' => true)); } $count = $cur->count(); if ($count > 0) { $Mailer->mailFromCursor($cur, $subj, $body); } }; \Lampcms\runLater($func); } return $this; }
/** * Now update tags counter to decrease tags count * but ONLY if deleted item is Question * * @return object $this; */ protected function updateTags() { if (!$this->requested) { if ('QUESTIONS' === $this->collection) { $Question = $this->Resource; \Lampcms\Qtagscounter::factory($this->Registry)->removeTags($Question); if (0 === $this->Resource['i_sel_ans']) { d('going to remove to Unanswered tags'); \Lampcms\UnansweredTags::factory($this->Registry)->remove($Question); } } else { $Question = new \Lampcms\Question($this->Registry); $Question->by_id($this->Resource->getQuestionId()); d('tags: ' . print_r($Question['a_tags'], 1)); } /** * Must extract uid from $this->Resource because in case * the resource is an answer, then the * $Question has different owner, thus * will remove user tags for the wrong user * */ $uid = $this->Resource->getOwnerId(); \Lampcms\UserTags::factory($this->Registry)->removeTags($Question, $uid); } return $this; }
/** * Notify all who follows the question * But exclude the Viewer - whoever just added * the new answer or whatever * * * and exclude all who follows the Viewer because all who * follows the Viewer will be notified via * the nofityUserFollowers * * @param int qid id of question * * @param int excludeUid UserID of user that should NOT * be notified. Usually this is in a special case of when * the answer or comment owner has already been notified * so now we just have to exclude the same user in case same user * is also the question author. * * @return object $this */ protected function notifyQuestionFollowers($qid = null, $excludeUid = 0) { $viewerID = $this->Registry->Viewer->getUid(); d('$viewerID: ' . $viewerID); /** * * $qid can be passed here * OR in can be extracted from $this->Question * */ if ($qid) { $Question = new \Lampcms\Question($this->Registry); try { $Question->by_id((int) $qid); } catch (\Exception $e) { e($e->getMessage() . ' in file: ' . $e->getFile() . ' on line: ' . $e->getLine()); $Question = null; } } else { $Question = $this->Question; } if (null === $Question) { return $this; } $updateType = 'onNewAnswer' === $this->eventName ? 'answer' : 'comment'; $subj = sprintf(static::$QUESTION_FOLLOW_SUBJ, $updateType); d('cp'); $siteUrl = $this->Registry->Ini->SITE_URL; $username = '******' === $updateType ? $this->obj['username'] : $this->aInfo['username']; $url = 'answer' === $updateType ? $this->obj->getUrl() : $siteUrl . '/q' . $this->aInfo['i_qid'] . '/#c' . $this->aInfo['_id']; d('url: ' . $url); $content = 'comment' === $updateType ? "\n____\n" . \strip_tags($this->aInfo['b']) . "\n" : ''; $body = vsprintf(static::$QUESTION_FOLLOW_BODY, array($username, $updateType, $this->Question['title'], $url, $siteUrl, $content)); d('$body: ' . $body); $oMailer = $this->Registry->Mailer; d('cp'); /** * MongoCollection USERS * @var object MongoCollection */ $coll = $this->collUsers; d('before shutdown function for question followers'); /** * Get array of followers for this question */ $aFollowers = $Question['a_flwrs']; if (!empty($aFollowers)) { $func = function () use($updateType, $viewerID, $aFollowers, $updateType, $subj, $body, $coll, $oMailer, $excludeUid) { /** * Remove $viewerID from aFollowers * Remove excludeID from aFollowers * Removing these userIDs from * the find $in condition is guaranteed to not * have these IDs in result and is much better * than adding extra $ne or $nin conditions * on these uids to find() * */ if (false !== ($key = array_search($viewerID, $aFollowers))) { array_splice($aFollowers, $key, 1); } if (!empty($excludeUid)) { if (false !== ($key = array_search($excludeUid, $aFollowers))) { array_splice($aFollowers, $key, 1); } } array_unique($aFollowers); /** * Find all users who follow this question * and * are not themselves the viewer (a viewer may reply to * own question and we don't want to notify viewer of that) * * In case of comment we should not exclude * user followers because user followers are NOT * notified on user comment * */ if ('comment' !== $updateType) { $cur = $coll->find(array('_id' => array('$in' => $aFollowers), 'a_f_u' => array('$nin' => array(0 => $viewerID)), 'ne_fq' => array('$ne' => true)), array('email')); } else { $cur = $coll->find(array('_id' => array('$in' => $aFollowers), 'ne_fq' => array('$ne' => true)), array('email')); } $count = $cur->count(); if ($count > 0) { /** * Passing callback function * to exclude mailing to those who * opted out on Email On Followed Question */ $oMailer->mailFromCursor($cur, $subj, $body); /** * , function($a){ if(!empty($a['email']) && (!array_key_exists('e_fq', $a) || false !== $a['e_fq'])){ return $a['email']; } return null; } */ } }; \Lampcms\runLater($func); } return $this; }