/** * 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 just the author of the answer * exclude the ViewerID * * @param \Lampcms\Answer $Answer * @param int $excludeUid * * @return object $this */ protected function notifyAnswerAuthor(\Lampcms\Answer $Answer, $excludeUid = 0) { $routerCallback = $this->Registry->Router->getCallback(); $commentorID = (int) $this->aInfo['i_uid']; $answerOwnerId = $Answer->getOwnerId(); $siteUrl = $this->Registry->Ini->SITE_URL; d('$siteUrl: ' . $siteUrl); $url = $siteUrl . '{_WEB_ROOT_}/{_viewquestion_}/{_QID_PREFIX_}' . $this->aInfo['i_qid'] . '/#c' . $this->aInfo['_id']; $commUrl = $routerCallback($url); d('commUrl: ' . $commUrl); $ansID = $Answer->getResourceId(); d('ansID: ' . $ansID); d('$answerOwnerId: ' . $answerOwnerId); $coll = $this->collUsers; $varsBody = array('{username}' => $this->aInfo['username'], '{link}' => $commUrl, '{site_title}' => $this->Registry->Ini->SITE_NAME, '{title}' => $Answer['title'], '{body}' => \strip_tags($this->aInfo['b']), '{home}' => $this->Registry->Router->getHomePageUrl()); $subj = new TranslatableSubject('email.subject.answer_author', array('{username}' => $this->aInfo['username'])); $body = new TranslatableBody('email.body.answer_author', $varsBody); $locale = LAMPCMS_DEFAULT_LOCALE; d('subj: ' . (string) $subj); $Mailer = $this->Registry->Mailer; $Mailer->setCache($this->Registry->Cache); /** * Don not notify if comment made * by the same user who is answer author * * This update is sent to only one user - answer owner * so we use findOne instead of find() * and send use mail() instead of mailFromCursor() on Mailer */ if ($commentorID !== $answerOwnerId && $commentorID != $excludeUid) { d('setting up callable function'); $callable = function () use($answerOwnerId, $coll, $subj, $body, $Mailer, $locale) { $aUser = $coll->findOne(array('_id' => $answerOwnerId, 'ne_fa' => array('$ne' => true)), array('email' => true, 'locale' => true)); if (!empty($aUser) && !empty($aUser['email'])) { if (!empty($aUser['locale'])) { $locale = $aUser['locale']; } $subj = $Mailer->translate($locale, $subj); $body = $Mailer->translate($locale, $body); $Mailer->mail($aUser['email'], $subj, $body, null, false); } }; d('adding callable to runLater'); \Lampcms\runLater($callable); } return $this; }
/** * Removes one element from a_latest array * that represents answer passed in param. * * If that array had only one element * then also unset the whole 'a_latest' key * from this object * * @param \Lampcms\Answer|object $Answer object of type Answer * * @return object $this */ public function removeAnswer(Answer $Answer) { $id = $Answer->getResourceId(); $aLatest = $this->offsetGet('a_latest'); for ($i = 0; $i < count($aLatest); $i += 1) { if (!empty($aLatest[$i]) && $id === $aLatest[$i]['id']) { \array_splice($aLatest, $i, 1); break; } } if (0 === count($aLatest)) { $this->offsetUnset('a_latest'); } else { parent::offsetSet('a_latest', $aLatest); } /** * If removed Answer was also a "accepted" answer * then change status to just "answrd" here * * The updateAnswerCount(-1) method * may then change the status to "unans" * if it's determined that this was * the only answer * * Also need to add this question to * UNANSWERED_TAGS again because now * this question is technically unanswered again */ if (true === $Answer['accepted'] && $id === $this->offsetGet(Schema::SELECTED_ANSWER_ID)) { parent::offsetSet(Schema::STATUS, 'answrd'); $this->offsetUnset(Schema::SELECTED_ANSWER_ID); $this->offsetUnset('i_sel_uid'); UnansweredTags::factory($this->Registry)->set($this); } $this->updateAnswerCount(-1)->removeContributor($Answer->getOwnerId()); $this->touch(false); return $this; }
public function addAnswer(\Lampcms\Answer $A) { d('cp'); $id = $A->getCategoryId(); if ($id > 0) { $update = array('$inc' => array('i_acount' => 1), '$set' => array('i_ts' => time(), 'hts' => date('F j, Y, g:i a T'))); $this->Mongo->CATEGORY->update(array('id' => $id), $update, array("upsert" => true)); } return $this; }
/** * Notify just the author of the answer * exclude the ViewerID * * @return object $this */ protected function notifyAnswerAuthor(\Lampcms\Answer $Answer, $excludeUid = 0) { $commentorID = (int) $this->aInfo['i_uid']; $answerOwnerId = $Answer->getOwnerId(); $siteUrl = $this->Registry->Ini->SITE_URL; d('$siteUrl: ' . $siteUrl); $commUrl = $siteUrl . '/q' . $this->aInfo['i_qid'] . '/#c' . $this->aInfo['_id']; d('commUrl: ' . $commUrl); $ansID = $Answer->getResourceId(); d('ansID: ' . $ansID); d('$answerOwnerId: ' . $answerOwnerId); $coll = $this->collUsers; $subj = sprintf(static::$ANS_COMMENT_SUBJ, $this->aInfo['username']); $body = vsprintf(static::$ANS_COMMENT_BODY, array($this->aInfo['username'], $Answer['title'], \strip_tags($this->aInfo['b']), $commUrl, $siteUrl)); d('subj: ' . $subj); d('body: ' . $body); $oMailer = $this->Registry->Mailer; /** * Don not notify if comment made * by the same user who is answer author * * This update is sent to only one user - answer owner * so we use findOne instead of find() * and send use mail() instead of mailFromCursor() on Mailer */ if ($commentorID !== $answerOwnerId && $commentorID != $excludeUid) { $callable = function () use($commentorID, $answerOwnerId, $coll, $subj, $body, $oMailer, $excludeUid) { $aUser = $coll->findOne(array('_id' => $answerOwnerId, 'ne_fa' => array('$ne' => true)), array('email')); if (!empty($aUser) && !empty($aUser['email'])) { $oMailer->mail($aUser['email'], $subj, $body, null, false); } }; \Lampcms\runLater($callable); } return $this; }