Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updates for Moderation feature and renamed factory to userFactory in …

…all User classes and all calling methods
  • Loading branch information...
commit aa0a50197845d39389697e77a447e96158d239d3 1 parent e4b1553
@snytkine authored
Showing with 362 additions and 210 deletions.
  1. +4 −10 lib/Lampcms/Answer.php
  2. +2 −2 lib/Lampcms/AnswerParser.php
  3. +0 −2  lib/Lampcms/Answers.php
  4. +1 −3 lib/Lampcms/Api/Api.php
  5. +0 −2  lib/Lampcms/Api/ApiUser.php
  6. +11 −0 lib/Lampcms/Cache/Cache.php
  7. +5 −8 lib/Lampcms/Category/Renderer.php
  8. +29 −26 lib/Lampcms/Controllers/Accept.php
  9. +4 −6 lib/Lampcms/Controllers/Activate.php
  10. +2 −2 lib/Lampcms/Controllers/Delete.php
  11. +1 −2  lib/Lampcms/Controllers/Edit.php
  12. +1 −1  lib/Lampcms/Controllers/Editprofile.php
  13. +3 −7 lib/Lampcms/Controllers/Logingoogle.php
  14. +2 −8 lib/Lampcms/Controllers/Loginlinkedin.php
  15. +2 −9 lib/Lampcms/Controllers/Logintwitter.php
  16. +1 −1  lib/Lampcms/Controllers/Register.php
  17. +12 −4 lib/Lampcms/Controllers/Tagged.php
  18. +16 −11 lib/Lampcms/Controllers/Unanswered.php
  19. +1 −7 lib/Lampcms/Controllers/Userinfo.php
  20. +2 −3 lib/Lampcms/Controllers/Viewcategories.php
  21. +14 −7 lib/Lampcms/Controllers/Viewcategory.php
  22. +0 −5 lib/Lampcms/Controllers/Viewquestions.php
  23. +1 −1  lib/Lampcms/Controllers/Vote.php
  24. +5 −13 lib/Lampcms/CookieAuth.php
  25. +2 −5 lib/Lampcms/FollowManager.php
  26. +156 −2 lib/Lampcms/Moderator.php
  27. +3 −5 lib/Lampcms/Modules/Facebook/Client.php
  28. +1 −1  lib/Lampcms/Mongo/Doc.php
  29. +0 −2  lib/Lampcms/Mongo/Schema/Answer.php
  30. +1 −3 lib/Lampcms/Mongo/Schema/Question.php
  31. +16 −0 lib/Lampcms/Mongo/Schema/Resource.php
  32. +35 −42 lib/Lampcms/Question.php
  33. +4 −4 lib/Lampcms/QuestionParser.php
  34. +23 −1 lib/Lampcms/User.php
  35. +0 −3  lib/Lampcms/UserFacebook.php
  36. +2 −2 upgrade/v0.2.txt
View
14 lib/Lampcms/Answer.php
@@ -119,7 +119,6 @@ public function unsetAccepted()
*/
public function getResourceId()
{
-
return $this->offsetGet(Schema::PRIMARY);
}
@@ -128,11 +127,10 @@ public function getResourceId()
* (non-PHPdoc)
*
* @see LampcmsResourceInterface::getDeletedTime()
- * @return int|\Lampcms\int (0 if this answer is not deleted)
+ * @return int (0 if this answer is not deleted)
*/
public function getDeletedTime()
{
-
return $this->offsetGet(Schema::DELETED_TIMESTAMP);
}
@@ -168,7 +166,7 @@ public function setDeleted(User $user, $reason = null)
$reason = \strip_tags((string)$reason);
}
- parent::offsetSet('i_del_ts', time());
+ parent::offsetSet(Schema::DELETED_TIMESTAMP, time());
parent::offsetSet(Schema::RESOURCE_STATUS_ID, Schema::DELETED);
parent::offsetSet('a_deleted',
array(
@@ -251,7 +249,6 @@ public function setApprovedStatus(\Lampcms\User $Moderator)
*/
public function getOwnerId()
{
-
return $this->offsetGet(Schema::POSTER_ID);
}
@@ -264,7 +261,6 @@ public function getOwnerId()
*/
public function getLastModified()
{
-
return $this->offsetGet(Schema::LAST_MODIFIED_TIMESTAMP);
}
@@ -377,7 +373,6 @@ public function getVotesArray()
*/
public function getScore()
{
-
return $this->offsetGet(Schema::VOTES_SCORE);
}
@@ -539,10 +534,10 @@ public function increaseCommentsCount($count = 1)
/**
* Must use parent::offsetSet because
* $this->offsetSet will point back to this
- * method and enter infinite loop untill
+ * method and enter infinite loop until
* we run out of memory
*/
- parent::offsetSet('i_comments', ($commentsCount + $count));
+ parent::offsetSet(Schema::COMMENTS_COUNT, ($commentsCount + $count));
return $this;
}
@@ -607,7 +602,6 @@ public function getCategoryId()
*/
public function getComments()
{
-
return $this->offsetGet(Schema::COMMENTS_ARRAY);
}
View
4 lib/Lampcms/AnswerParser.php
@@ -164,8 +164,6 @@ public function parse(SubmittedAnswer $o, Question $q = null)
$this->Question = (null !== $q) ? $q : $this->getQuestion();
$this->makeAnswer();
$this->followQuestion();
- $this->updateQuestion();
- $this->updateCategory();
return $this->Answer;
}
@@ -301,6 +299,8 @@ protected function makeAnswer()
* post onCategoryUpdate only if status NOT PENDING
*/
if ($resourceStatus !== Schema::PENDING) {
+ $this->updateCategory();
+ $this->updateQuestion();
$this->Registry->Dispatcher->post($this->Question, 'onCategoryUpdate');
$this->Registry->Dispatcher->post($this->Answer, 'onNewAnswer', array('question' => $this->Question));
} elseif ($resourceStatus === Schema::PENDING) {
View
2  lib/Lampcms/Answers.php
@@ -107,7 +107,6 @@ public function __construct(Registry $Registry)
*/
public function getAnswers(Question $Question, $result = 'html')
{
-
$qid = $Question[Schema::PRIMARY];
$url = $Question['url'];
$pageID = $this->Registry->Router->getPageID();
@@ -211,6 +210,5 @@ public function getAnswers(Question $Question, $result = 'html')
$answers = \tplAnswer::loop($cursor, true, $func) . $pagerLinks;
return $answers;
-
}
}
View
4 lib/Lampcms/Api/Api.php
@@ -365,7 +365,7 @@ protected function initClientUser()
$this->initBasicAuthUser($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
} else {
d('No user credentials in request. Using basic quest user');
- $this->Registry->Viewer = ApiUser::factory($this->Registry);
+ $this->Registry->Viewer = ApiUser::userFactory($this->Registry);
}
d('Viewer id: ' . $this->Registry->Viewer->getUid());
@@ -382,8 +382,6 @@ protected function initClientUser()
*/
protected function initBasicAuthUser($username, $pwd)
{
-
-
try {
$UserAuth = new UserAuth($this->Registry);
$User = $UserAuth->validateLogin($username, $pwd, '\Lampcms\\Api\\UserBasicAuth');
View
2  lib/Lampcms/Api/ApiUser.php
@@ -69,6 +69,4 @@
*/
class ApiUser extends User
{
-
-
}
View
11 lib/Lampcms/Cache/Cache.php
@@ -563,6 +563,15 @@ public function __unset($key)
* Handle events
* (non-PHPdoc)
*
+ * @todo should write extra function that will
+ * register the shutdown function to unset specific key
+ * This way the unsetting of key will be done after
+ * other shutdown functions have completed
+ * The reason is that some functions are run via runLater()
+ * so it's possible that we unset the key before all the shutdown
+ * functions have finished running. This will result in
+ * unsetting cache keys before the new values are available
+ *
* @see Lampcms.Observer::main()
*/
protected function main()
@@ -571,12 +580,14 @@ protected function main()
case 'onNewQuestions':
case 'onNewQuestion':
case 'onResourceDelete':
+ case 'onApprovedQuestion':
$this->__unset('qunanswered');
$this->__unset('qrecent');
break;
case 'onNewAnswer':
case 'onAcceptAnswer':
+ case 'onApprovedAnswer':
$this->__unset('qunanswered');
break;
View
13 lib/Lampcms/Category/Renderer.php
@@ -202,7 +202,7 @@ public function getNormalizedCategory($id)
public function getByFilter($func)
{
- $a = array_filter($this->aCategories, $func);
+ $a = \array_filter($this->aCategories, $func);
return $a;
}
@@ -396,7 +396,7 @@ public function getSelectOption(array $category, $level = 0)
* has been deleted but the item not removed
* from a_sub array (normally this does not happend, but just in case must check)
*/
- if (array_key_exists($id, $this->aCategories)) {
+ if (\array_key_exists($id, $this->aCategories)) {
$ret .= $this->getSelectOption($this->aCategories[$id], ($level + 1));
}
}
@@ -424,9 +424,9 @@ public function getSubCategoriesOf($id, $activeOnly = true)
{
if (!empty($this->aCategories[$id]) && !empty($this->aCategories[$id]['a_sub'])) {
- $subKeys = array_flip($this->aCategories[$id]['a_sub']);
+ $subKeys = \array_flip($this->aCategories[$id]['a_sub']);
- $ret = array_intersect_key($this->aCategories, $subKeys);
+ $ret = \array_intersect_key($this->aCategories, $subKeys);
if ($activeOnly) {
return array_filter($ret, function($var)
{
@@ -443,14 +443,13 @@ public function getSubCategoriesOf($id, $activeOnly = true)
public function _getNestedDivs(array $categories = null)
{
-
$categories = ($categories) ? $categories : $this->aCategories;
$ret = '<div class="cats2">';
foreach ($categories as $c) {
if ($c['b_active']) {
if (!empty($c['a_sub'])) {
- $subs = \array_intersect_key($this->aCategories, array_flip($c['a_sub']));
+ $subs = \array_intersect_key($this->aCategories, \array_flip($c['a_sub']));
$ret .= $this->getNestedDivs($subs);
} else {
$ret .= \tplCategoryDiv::parse($c);
@@ -478,7 +477,6 @@ public function _getNestedDivs(array $categories = null)
*/
public function getNestedDivs(array $categories = null, $parentId = 0, $level = 0)
{
-
$categories = ($categories) ? $categories : $this->aCategories;
$ret = '<div class="cats_w fl cb" id="parent_' . $parentId . '">';
@@ -501,7 +499,6 @@ public function getNestedDivs(array $categories = null, $parentId = 0, $level =
$ret .= $tpl::parse($c, true);
-
}
}
View
55 lib/Lampcms/Controllers/Accept.php
@@ -55,13 +55,13 @@
use \Lampcms\Request;
use \Lampcms\Responder;
use \Lampcms\Question;
-
+use \Lampcms\Mongo\Schema\Question as Schema;
/**
* Controller for processing the 'Accept as best answer'
* vote
*
- * @todo require POST method for this
+ * @todo require POST method for this
*
* @author Dmitri Snytkine
*
@@ -76,6 +76,7 @@ class Accept extends WebPage
/**
*
* Question object
+ *
* @var Object Question for which answer is accepted
*/
protected $Question;
@@ -138,6 +139,7 @@ protected function postEvent()
* post onBeforeAcceptAnswer event
* and if notification is cancelled by observer
* then throw Exception
+ *
* @throws \Lampcms\Exception
* @return object $this
*/
@@ -175,14 +177,13 @@ protected function postOnBefore()
*/
protected function checkVoteHack()
{
-
if (!$this->Registry->Viewer->isModerator()) {
$timeOffset = time() - 172800; // 2 days
$cur = $this->Registry->Mongo->VOTE_HACKS->find(array('i_ts' => array('$gt' => $timeOffset)));
if ($cur && $cur->count(true) > 0) {
- $ip = Request::getIP();
+ $ip = Request::getIP();
$uid = $this->Registry->Viewer->getUid();
foreach ($cur as $aRec) {
@@ -208,9 +209,9 @@ protected function checkVoteHack()
*/
protected function getAnswer()
{
- $id = $this->Router->getNumber(1);
- $aAnswer = $this->Registry->Mongo->ANSWERS->findOne(array('_id' => $id));
- d('$aAnswer: ' . \print_r($aAnswer, 1));
+ $id = $this->Router->getNumber(1);
+ $aAnswer = $this->Registry->Mongo->ANSWERS->findOne(array(Schema::PRIMARY => $id));
+ d('$aAnswer: ' . \json_encode($aAnswer));
if (empty($aAnswer)) {
throw new \Lampcms\Exception('Answer not found by id: ' . $id);
@@ -238,10 +239,10 @@ protected function getAnswer()
*/
protected function getQuestion()
{
- $id = $this->Router->getNumber(1);
- $aQuestion = $this->Registry->Mongo->QUESTIONS->findOne(array('_id' => $this->Answer->getQuestionId()));
+ $id = $this->Router->getNumber(1);
+ $aQuestion = $this->Registry->Mongo->QUESTIONS->findOne(array(Schema::PRIMARY => $this->Answer->getQuestionId()));
- d('$aQuestion: ' . print_r($aQuestion, 1));
+ d('$aQuestion: ' . \json_encode($aQuestion));
if (empty($aQuestion)) {
throw new \Lampcms\Exception('Question not found for this answer: ' . $id);
@@ -300,7 +301,7 @@ protected function checkViewer()
*/
protected function updateQuestion()
{
- $ansID = $this->Question['i_sel_ans'];
+ $ansID = $this->Question[Schema::SELECTED_ANSWER_ID];
d('$ansID: ' . $ansID);
if (!empty($ansID)) {
@@ -343,8 +344,11 @@ protected function updateQuestion()
*/
protected function getOldAnswer()
{
- $this->aOldAnswer = $this->Registry->Mongo->ANSWERS->findOne(array('_id' => $this->Question['i_sel_ans']));
- d('old answer: ' . print_r($this->aOldAnswer, 1));
+ $this->aOldAnswer = $this->Registry->Mongo->ANSWERS->findOne(array(
+ Schema::PRIMARY => $this->Question[Schema::SELECTED_ANSWER_ID]
+ )
+ );
+ d('old answer: ' . \json_encode($this->aOldAnswer));
return $this;
}
@@ -371,20 +375,20 @@ protected function updateOldAnswer()
* owns the old answer
*
* @todo check current score and make sure
- * it will not become negative after we deduct
- * some points
+ * it will not become negative after we deduct
+ * some points
*
* @return object $this
*/
protected function updateOldAnswerer()
{
if (!empty($this->aOldAnswer)) {
- $uid = $this->aOldAnswer['i_uid'];
+ $uid = $this->aOldAnswer[Schema::POSTER_ID];
if (!empty($uid)) {
try {
- \Lampcms\User::factory($this->Registry)->by_id($uid)->setReputation((0 - $this->Registry->Ini->POINTS->BEST_ANSWER))->save();
+ \Lampcms\User::userFactory($this->Registry)->by_id($uid)->setReputation((0 - $this->Registry->Ini->POINTS->BEST_ANSWER))->save();
- } catch (\MongoException $e) {
+ } catch ( \MongoException $e ) {
e('unable to update reputation for old answerer ' . $e->getMessage());
}
}
@@ -408,7 +412,7 @@ protected function updateUser()
$uid = $this->Answer->getOwnerId();
d('$this->Answer->getOwnerId():. ' . $uid);
- if (!empty($uid) && ($this->Question['i_uid'] == $uid)) {
+ if (!empty($uid) && ($this->Question[Schema::POSTER_ID] == $uid)) {
d('Answered own question, this does not count towards reputation');
return $this;
@@ -416,7 +420,7 @@ protected function updateUser()
try {
$this->Registry->Mongo->USERS->update(array('_id' => $uid), array('$inc' => array("i_rep" => $this->Registry->Ini->POINTS->BEST_ANSWER)));
- } catch (\MongoException $e) {
+ } catch ( \MongoException $e ) {
e('unable to increase reputation for answerer ' . $e->getMessage());
}
@@ -452,17 +456,17 @@ protected function rewardViewer()
* Insert record into VOTE_HACKS collection
*
* @todo move this to external class and make
- * this method static, accepting only Registry
+ * this method static, accepting only Registry
* @return \Lampcms\Controllers\Accept
*/
protected function recordVoteHack()
{
$coll = $this->Registry->Mongo->VOTE_HACKS;
- $coll->ensureIndex(array('i_ts' => 1));
+ $coll->ensureIndex(array(Schema::CREATED_TIMESTAMP => 1));
$aData = array(
- 'i_uid' => $this->Registry->Viewer->getUid(),
- 'i_ts' => time(),
- 'ip' => Request::getIP());
+ Schema::POSTER_ID => $this->Registry->Viewer->getUid(),
+ Schema::CREATED_TIMESTAMP => time(),
+ Schema::IP_ADDRESS => Request::getIP());
$coll->save($aData);
@@ -479,7 +483,6 @@ protected function recordVoteHack()
*/
protected function redirect()
{
-
Responder::redirectToPage($this->Question->getUrl());
}
}
View
10 lib/Lampcms/Controllers/Activate.php
@@ -87,7 +87,6 @@ class Activate extends WebPage
protected function main()
{
-
$this->timeLimit = $this->Registry->Ini->VALIDATION_CODE_EXPIRATION;
$this->getEmailRecord()
@@ -150,7 +149,6 @@ protected function validateExpiration()
*/
protected function activateUser()
{
-
$aUser = $this->Registry->Mongo->USERS->findOne(array(Schema::PRIMARY => (int)$this->aEmail['i_uid']));
if (empty($aUser)) {
@@ -158,8 +156,7 @@ protected function activateUser()
throw new \Lampcms\Exception('@@Unable to find user, please create a new account@@');
}
-
- $this->oActivatedUser = User::factory($this->Registry, $aUser);
+ $this->oActivatedUser = User::userFactory($this->Registry, $aUser);
$role = $this->oActivatedUser->getRoleId();
/**
* If User's role is NOT 'unactivated' then
@@ -196,9 +193,10 @@ protected function activateUser()
*/
protected function setReturn()
{
-
$this->aPageVars['title'] = '@@Account activation complete@@';
-
+ /**
+ * @todo translate string
+ */
$this->aPageVars['body'] = '<div id="tools" class="larger">@@Account activation complete@@<br/>The account <b>' . $this->oActivatedUser->username . '</b> now has all the privileges<br/>
of a registered user on our website.<br/>
<br/>If you not already logged in, please login using<br/>
View
4 lib/Lampcms/Controllers/Delete.php
@@ -184,7 +184,7 @@ protected function getUserData()
$uid = $this->Resource['i_uid'];
d('uid: ' . $uid);
- $User = \Lampcms\User::factory($this->Registry)->by_id($uid);
+ $User = \Lampcms\User::userFactory($this->Registry)->by_id($uid);
$q = $this->Registry->Mongo->QUESTIONS->find(array('i_uid' => $uid, 'i_del_ts' => null));
$a = $this->Registry->Mongo->ANSWERS->find(array('i_uid' => $uid, 'i_del_ts' => null));
@@ -246,7 +246,7 @@ protected function banUser()
d('ban: ' . $ban);
if (!empty($ban) && $this->checkAccessPermission('ban_user')) {
- $User = User::factory($this->Registry)->by_id($this->Resource->getOwnerId());
+ $User = User::userFactory($this->Registry)->by_id($this->Resource->getOwnerId());
$User->setRoleId('suspended');
$User->save();
View
3  lib/Lampcms/Controllers/Edit.php
@@ -108,7 +108,6 @@ class Edit extends WebPage
protected function main()
{
-
$this->getResource()
->configureEditor()
->checkPermission()
@@ -213,7 +212,7 @@ protected function setForm()
* Create object of type Question or Answer
*
* @throws \Lampcms\Exception
- * @return \Lampcms\Controllers\object $this
+ * @return object $this
*/
protected function getResource()
{
View
2  lib/Lampcms/Controllers/Editprofile.php
@@ -142,7 +142,7 @@ protected function getUser()
* check Viewer permission here
*/
$this->checkAccessPermission('edit_any_profile');
- $this->User = \Lampcms\User::factory($this->Registry)->by_id($uid);
+ $this->User = \Lampcms\User::userFactory($this->Registry)->by_id($uid);
} else {
$this->User = $this->Registry->Viewer;
}
View
10 lib/Lampcms/Controllers/Logingoogle.php
@@ -177,7 +177,6 @@ class Logingoogle extends Register
*/
protected function main()
{
-
if (is_array($_GET) && !empty($_GET['error'])) {
d('Received error response from Google API: ' . $_GET['error']);
@@ -272,7 +271,6 @@ protected function getToken()
}
return $this;
-
}
@@ -326,7 +324,7 @@ protected function createOrUpdate()
d('found user id by email address. uid: ' . $res['i_uid']);
$aUser = $this->Registry->Mongo->USERS->findOne(array(Schema::PRIMARY => $res['i_uid']));
- $User = User::factory($this->Registry, $aUser);
+ $User = User::userFactory($this->Registry, $aUser);
$this->updateUser($User);
}
@@ -334,7 +332,7 @@ protected function createOrUpdate()
$a = $this->Registry->Mongo->USERS->findOne(array(Schema::EMAIL => $this->email));
if (!empty($a)) {
d('found user id by email address. uid: ' . $a['_id']);
- $User = User::factory($this->Registry, $a);
+ $User = User::userFactory($this->Registry, $a);
$this->updateUser($User);
}
}
@@ -379,7 +377,6 @@ protected function createOrUpdate()
*/
protected function updateUser(\Lampcms\User $User)
{
-
$User['google_id'] = (string)$this->userInfo['id'];
/**
@@ -424,7 +421,6 @@ protected function updateUser(\Lampcms\User $User)
*/
protected function createUser()
{
-
$sid = (false === ($sid = Cookie::getSidCookie())) ? String::makeSid() : $sid;
if (false !== $tzn = Cookie::get('tzn')) {
@@ -486,7 +482,7 @@ protected function createUser()
d('creating new googlge aUser: ' . \json_encode($aUser));
- $User = User::factory($this->Registry, $aUser);
+ $User = User::userFactory($this->Registry, $aUser);
$User->save();
d('new user _id: ' . $User['_id']);
View
10 lib/Lampcms/Controllers/Loginlinkedin.php
@@ -131,7 +131,6 @@ class Loginlinkedin extends WebPage
*/
protected function main()
{
-
$routerCallback = $this->Registry->Router->getCallback();
$this->callback = $routerCallback($this->callback);
d('$this->callback' . $this->callback);
@@ -195,7 +194,6 @@ protected function main()
*/
protected function step1()
{
-
$requestUrl = self::REQUEST_TOKEN_URL . $this->callback;
try {
@@ -306,7 +304,6 @@ protected function step2()
protected function createOrUpdate()
{
-
$aUser = $this->getUserByLinkedInId($this->aData['linkedin_id']);
if (!empty($this->bConnect)) {
@@ -316,7 +313,7 @@ protected function createOrUpdate()
$this->updateUser();
} elseif (!empty($aUser)) {
- $this->User = \Lampcms\UserLinkedin::factory($this->Registry, $aUser);
+ $this->User = \Lampcms\UserLinkedin::userFactory($this->Registry, $aUser);
$this->updateUser(); // only update token, secret, linkedin url
} else {
$this->isNewAccount = true;
@@ -350,7 +347,6 @@ protected function createOrUpdate()
*/
protected function createNewUser()
{
-
d('$this->aData: ' . print_r($this->aData, 1));
if (false !== $tzn = Cookie::get('tzn')) {
@@ -384,7 +380,7 @@ protected function createNewUser()
$this->aData = array_merge($this->Registry->Geo->Location->data, $this->aData);
}
- $this->User = \Lampcms\UserLinkedin::factory($this->Registry, $this->aData);
+ $this->User = \Lampcms\UserLinkedin::userFactory($this->Registry, $this->aData);
/**
* This will mark this userobject is new user
@@ -597,8 +593,6 @@ protected function getUserByLinkedInId($lid)
*/
protected function closeWindow(array $a = array())
{
- //exit;
-
d('cp a: ' . print_r($a, 1));
$js = '';
View
11 lib/Lampcms/Controllers/Logintwitter.php
@@ -384,7 +384,7 @@ protected function createOrUpdate()
$this->connect($tid);
} elseif (!empty($aUser)) {
- $this->User = $User = \Lampcms\UserTwitter::factory($this->Registry, $aUser);
+ $this->User = $User = \Lampcms\UserTwitter::userFactory($this->Registry, $aUser);
$this->updateUser();
} else {
$this->isNewAccount = true;
@@ -464,7 +464,6 @@ protected function connect($tid)
protected function createNewUser()
{
-
$aUser = array();
if(!empty($this->aUserData['utc_offset'])){
$timezone = \Lampcms\TimeZone::getTZbyoffset($this->aUserData['utc_offset']);
@@ -507,7 +506,7 @@ protected function createNewUser()
d('aUser: ' . print_r($aUser, 1));
- $this->User = \Lampcms\UserTwitter::factory($this->Registry, $aUser);
+ $this->User = \Lampcms\UserTwitter::userFactory($this->Registry, $aUser);
/**
* This will mark this userobject is new user
@@ -570,7 +569,6 @@ protected function updateUser($bUpdateAvatar = true)
$this->User->save();
return $this;
-
}
@@ -583,7 +581,6 @@ protected function updateUser($bUpdateAvatar = true)
*/
protected function postTweetStatus()
{
-
$sToFollow = $this->aTW['TWITTER_USERNAME'];
$follow = (!empty($sToFollow)) ? ' #follow @' . $sToFollow : '';
$siteName = $this->Registry->Ini->SITE_TITLE;
@@ -624,7 +621,6 @@ protected function postTweetStatus()
*/
protected function updateTwitterUserRecord()
{
-
$this->Registry->Mongo->USERS_TWITTER->save($this->aUserData);
return $this;
@@ -641,7 +637,6 @@ protected function updateTwitterUserRecord()
*/
protected function getUserByTid($tid)
{
-
$coll = $this->Registry->Mongo->USERS;
$coll->ensureIndex(array('twitter_uid' => 1));
@@ -720,14 +715,12 @@ protected function closeWindow(array $a = array())
*/
protected function makeUsername()
{
-
$res = $this->Registry->Mongo->USERS->findOne(array('username_lc' => \mb_strtolower($this->aUserData['screen_name'])));
$ret = (empty($res)) ? $this->aUserData['screen_name'] : '@' . $this->aUserData['screen_name'];
d('ret: ' . $ret);
return $ret;
-
}
}
View
2  lib/Lampcms/Controllers/Register.php
@@ -237,7 +237,7 @@ protected function createNewUser()
d('aUser: ' . \json_encode($aUser));
- $User = \Lampcms\User::factory($this->Registry, $aUser);
+ $User = \Lampcms\User::userFactory($this->Registry, $aUser);
$User->save();
d('new user _id: ' . $User['_id']);
View
16 lib/Lampcms/Controllers/Tagged.php
@@ -53,7 +53,7 @@
use \Lampcms\RedirectException;
use \Lampcms\Relatedtags;
-
+use \Lampcms\Mongo\Schema\Question as Schema;
/**
* Controller for rendering page
@@ -127,10 +127,18 @@ protected function getCursor()
* meaning most recent should be on top
*
*/
- $sort = array('i_ts' => -1);
+ $sort = array(Schema::CREATED_TIMESTAMP => -1);
+
+ $where = array(Schema::TAGS_ARRAY => array('$all' => $this->aTags));
+ /**
+ * Exclude deleted items
+ */
+ if ($this->Registry->Viewer->isModerator()) {
+ $where[Schema::RESOURCE_STATUS_ID] = array('$lt' => Schema::DELETED);
+ } else {
+ $where[Schema::RESOURCE_STATUS_ID] = Schema::POSTED;
+ }
- $where = array('a_tags' => array('$all' => $this->aTags));
- $where['i_del_ts'] = null;
$replaced = array(
'tags' => \str_replace(' ', ' + ', $this->tags),
'text' => '@@Tagged@@'
View
27 lib/Lampcms/Controllers/Unanswered.php
@@ -59,6 +59,7 @@
use Lampcms\Template\Urhere;
use Lampcms\Utf8String;
use Lampcms\TagsTagsTokenizer;
+use Lampcms\Mongo\Schema\Question as Schema;
/**
* Controller to view unanswered questions
@@ -144,7 +145,7 @@ protected function getCursor()
* meaning most recent should be on top
*
*/
- $sort = array('i_ts' => -1);
+ $sort = array(Schema::CREATED_TIMESTAMP => -1);
$this->title = '@@Questions with no accepted answer@@';
@@ -163,13 +164,13 @@ protected function getCursor()
case $urlParts['COND_NOANSWERS']:
$this->title = '@@Questions with no answers@@';
$this->pagerPath .= '/{_COND_NOANSWERS_}';
- $where = array('i_ans' => 0);
+ $where = array(Schema::NUM_ANSWERS => 0);
$this->typeDiv = Urhere::factory($this->Registry)->get('tplQuntypes', 'noanswer');
break;
case $urlParts['COND_TAGGED']:
- $where = array('i_sel_ans' => null,
- 'a_tags' => array('$all' => $this->getTags()));
+ $where = array(Schema::SELECTED_ANSWER_ID => null,
+ Schema::TAGS_ARRAY => array('$all' => $this->getTags()));
$this->pagerPath .= '/{_COND_TAGGED_}' . $this->rawTags;
$this->typeDiv = Urhere::factory($this->Registry)->get('tplQuntypes', 'newest');
$this->makeFollowTagButton();
@@ -188,14 +189,18 @@ protected function getCursor()
*/
default:
$this->title = '@@Questions with no accepted answer@@';
- $where = array('i_sel_ans' => null);
+ $where = array(Schema::SELECTED_ANSWER_ID => null);
$this->typeDiv = Urhere::factory($this->Registry)->get('tplQuntypes', 'newest');
}
/**
* Exclude deleted items
*/
- $where['i_del_ts'] = null;
+ if ($this->Registry->Viewer->isModerator()) {
+ $where[Schema::RESOURCE_STATUS_ID] = array('$lt' => Schema::DELETED);
+ } else {
+ $where[Schema::RESOURCE_STATUS_ID] = Schema::POSTED;
+ }
$this->Cursor = $this->Registry->Mongo->QUESTIONS->find($where, $this->aFields);
$this->count = $this->Cursor->count(true);
@@ -256,8 +261,8 @@ protected function getTags()
* $r is something like this: /tagged/tag%2B%2B/
*/
$r = $_SERVER['REQUEST_URI'];
- $m = \preg_match('/\/'.$cname.'\/([^\/]+)([\/]{0,1})/i', $r, $matches);
- d('matches: ' . print_r($matches, 1));
+ $m = \preg_match('/\/' . $cname . '\/([^\/]+)([\/]{0,1})/i', $r, $matches);
+ d('matches: ' . \json_encode($matches));
if ($matches && !empty($matches[1])) {
$tags = $matches[1];
@@ -289,8 +294,8 @@ protected function getTags()
*/
$this->tags = \str_replace(array('<', '>'), array('&lt;', '&gt;'), $this->tags);
- // $this->rawTags = $tags; //TagsTokenizer::factory($Utf8Tags)->getArrayCopy();
- $this->title = $this->tags;
+ // $this->rawTags = $tags; //TagsTokenizer::factory($Utf8Tags)->getArrayCopy();
+ $this->title = $this->tags;
if (empty($this->tags)) {
return array();
@@ -312,7 +317,7 @@ protected function getTags()
*/
$this->aTags = TagsTokenizer::factory($Utf8Tags)->getArrayCopy();
- d('aTags: ' . \print_r($this->aTags, 1));
+ d('aTags: ' . \json_encode($this->aTags));
}
return $this->aTags;
View
8 lib/Lampcms/Controllers/Userinfo.php
@@ -132,7 +132,7 @@ protected function getUser()
throw new \Lampcms\Lampcms404Exception('@@User not found@@');
}
- $this->User = User::factory($this->Registry, $a);
+ $this->User = User::userFactory($this->Registry, $a);
$this->aPageVars['title'] = $this->User->getDisplayName();
return $this;
@@ -192,9 +192,7 @@ protected function addProfile()
*/
protected function addQuestions()
{
-
/**
- *
* html of parsed questions and pagination links
* at the bottom all wrapped inside <div class="user_tags">
*
@@ -233,7 +231,6 @@ protected function addQuestions()
*/
protected function addAnswers()
{
-
/**
*
* html of parsed answers and pagination links
@@ -287,7 +284,6 @@ protected function addVotes()
*/
protected function addTags()
{
-
$this->aPageVars['body'] .= UserTagsBlock::get($this->Registry, $this->User);
return $this;
@@ -303,10 +299,8 @@ protected function addTags()
*/
protected function addFollowedTags()
{
-
$this->aPageVars['body'] .= UserFollowedTags::get($this->Registry, $this->User);
-
return $this;
}
View
5 lib/Lampcms/Controllers/Viewcategories.php
@@ -80,8 +80,7 @@ class Viewcategories extends Viewquestions
protected function main()
{
-
- $this->aPageVars['title'] = $this->_('Categories');
+ $this->aPageVars['title'] = '@@Categories@@';
$this->makeTopTabs()
->makeQlistHeader()
@@ -94,7 +93,7 @@ protected function main()
protected function makeQlistBody()
{
- $this->title = $this->_('Categories');
+ $this->title = '@@Categories@@';
$Renderer = new Renderer($this->Registry);
$this->aPageVars['body'] = $Renderer->getNestedDivs();
View
21 lib/Lampcms/Controllers/Viewcategory.php
@@ -56,6 +56,8 @@
use Lampcms\Paginator;
use Lampcms\Template\Urhere;
+use Lampcms\Mongo\Schema\Question as QuestionSchema;
+
/**
* Generate page to view
* questions that belong to one
@@ -84,14 +86,14 @@ protected function main()
$this->slug = $this->Registry->Router->getSegment(1, 's');
$this->pageID = (int)$this->Request->getPageID();
$this->pagerPath = '{_viewcategory_}/' . $this->slug;
- $this->counterTaggedText = $this->_('Questions in this category');
+ $this->counterTaggedText = '@@Questions in this category@@';
$this->getCategory()
->getCursor()
->paginate()
->sendCacheHeaders();
- $this->aPageVars['title'] = $this->_('Category') . ' :: ' . $this->aCategory['title'];
+ $this->aPageVars['title'] = '@@Category@@ :: ' . $this->aCategory['title'];
$this->makeTopTabs()
->makeQlistHeader()
->makeCounterBlock()
@@ -118,16 +120,20 @@ protected function getCategory()
return $this;
}
+
protected function getCursor()
{
+ $sort = array(QuestionSchema::CREATED_TIMESTAMP => -1);
+ $where = array(QuestionSchema::CATEGORY_ID => $this->aCategory['id']);
- $sort = array('i_ts' => -1);
-
- $where = array('i_cat' => $this->aCategory['id']);
/**
* Exclude deleted items
*/
- $where['i_del_ts'] = null;
+ if ($this->Registry->Viewer->isModerator()) {
+ $where[QuestionSchema::RESOURCE_STATUS_ID] = array('$lt' => QuestionSchema::DELETED);
+ } else {
+ $where[QuestionSchema::RESOURCE_STATUS_ID] = QuestionSchema::POSTED;
+ }
$this->Cursor = $this->Registry->Mongo->QUESTIONS->find($where, $this->aFields);
$this->count = $this->Cursor->count(true);
@@ -136,6 +142,7 @@ protected function getCursor()
return $this;
}
+
/**
* (non-PHPdoc)
* @see Lampcms\Controllers.Viewquestions::makeQlistHeader()
@@ -149,7 +156,7 @@ protected function makeQlistHeader()
$subs = '';
$subCategories = $Renderer->getSubCategoriesOf($this->aCategory['id']);
if (!empty($subCategories)) {
- $subs = \tplSubcategories::parse(array($this->_('Sub categories'), \tplSubcategory::loop($subCategories)), false);
+ $subs = \tplSubcategories::parse(array('@@Sub categories@@', \tplSubcategory::loop($subCategories)), false);
}
$this->aPageVars['qheader'] = $breadCrumb . $subs;
View
5 lib/Lampcms/Controllers/Viewquestions.php
@@ -242,11 +242,6 @@ protected function getCursor()
}
/**
- * Exclude deleted items
- */
- //$where['i_del_ts'] = null;
-
- /**
* Important!
* Starting from version 0.2 no longer using i_del_ts to indicate
* deleted items, instead using the RESOURCE_STATUS_ID ('i_status' field)
View
2  lib/Lampcms/Controllers/Vote.php
@@ -302,7 +302,7 @@ protected function setOwnerReputation()
*
*/
try {
- \Lampcms\User::factory($this->Registry)->by_id($uid)
+ \Lampcms\User::userFactory($this->Registry)->by_id($uid)
->setReputation($this->calculatePoints())
->save();
} catch ( \Exception $e ) {
View
18 lib/Lampcms/CookieAuth.php
@@ -78,14 +78,10 @@ class CookieAuth extends UserAuth
* Value of sid cookie
* @var string
*/
-
protected $sid;
public function authByCookie()
{
-
- //d('$_COOKIE: '.print_r($_COOKIE, 1));
-
$User = $this->checkRequiredCookies()
->validateCookieSalt()
->checkSidFormat()
@@ -112,7 +108,6 @@ public function authByCookie()
*/
protected function getSidUser()
{
-
$arrResult = $this->Registry->Mongo->USERS->findOne(array('_id' => $this->uid));
if (empty($arrResult)) {
@@ -122,7 +117,7 @@ protected function getSidUser()
throw new CookieAuthException('no user by uid cookie');
}
- return User::factory($this->Registry, $arrResult);
+ return User::userFactory($this->Registry, $arrResult);
}
@@ -132,11 +127,10 @@ protected function getSidUser()
* are present in request
*
* @throws CookieAuthException
- * @return \Lampcms\object $this
+ * @return object $this
*/
protected function checkRequiredCookies()
{
-
if (!isset($_COOKIE) || empty($_COOKIE['uid']) || empty($_COOKIE['sid'])) {
throw new CookieAuthException('No uid or sid cookie');
@@ -209,7 +203,7 @@ protected function validateCookieSalt()
* banned for hack attempts
*
* @throws CookieAuthException
- * @return \Lampcms\object $this
+ * @return object $this
*/
protected function checkForBannedIP()
{
@@ -246,7 +240,7 @@ protected function checkForBannedIP()
* 'sid' cookie is a valid hex number
*
* @throws CookieAuthException
- * @return \Lampcms\object $this
+ * @return object $this
*/
protected function checkSidFormat()
{
@@ -257,7 +251,6 @@ protected function checkSidFormat()
throw new CookieAuthException('"sid" cookie is not 48 chars long');
}
-
/**
* Check value of sid cookie
* it can contain only letters and numbers and a dot
@@ -282,7 +275,7 @@ protected function checkSidFormat()
* in the last 60 minutes
*
* @throws CookieAuthException
- * @return \Lampcms\object $this
+ * @return object $this
*/
protected function checkMultipleSidLoginErrors()
{
@@ -338,7 +331,6 @@ protected function checkMultipleSidLoginErrors()
*/
protected function compareSids($stored)
{
-
return ($stored === $this->sid);
}
}
View
7 lib/Lampcms/FollowManager.php
@@ -131,7 +131,6 @@ public function followQuestion(User $User, $Question)
d('qid: ' . $qid . ' $uid: ' . $uid);
$this->Registry->Dispatcher->post($User, 'onBeforeQuestionFollow', array('qid' => $qid));
-
if (is_object($Question)) {
$Question->addFollower($uid);
} else {
@@ -213,7 +212,7 @@ public function unfollowQuestion(User $User, $Question)
* @throws Exception
* @internal param int $id
*
- * @return \Lampcms\object $this
+ * @return object $this
*/
protected function checkQuestionExists($qid)
{
@@ -319,7 +318,7 @@ public function unfollowTag(User $User, $tag)
* @param int $userid id user being followed (followee)
*
* @throws \InvalidArgumentException
- * @return \Lampcms\object $this
+ * @return object $this
*/
public function followUser(User $User, $userid)
{
@@ -348,7 +347,6 @@ public function followUser(User $User, $userid)
$this->Registry->Mongo->USERS->update(array('_id' => $userid), array('$inc' => array('i_flwrs' => 1)));
return $this;
-
}
@@ -363,7 +361,6 @@ public function followUser(User $User, $userid)
*/
public function unfollowUser(User $User, $userid)
{
-
if (!is_int($userid)) {
throw new \InvalidArgumentException('$userid must be an integer');
}
View
158 lib/Lampcms/Moderator.php
@@ -53,6 +53,7 @@
namespace Lampcms;
use \Lampcms\IndexerFactory;
+use \Lampcms\Category\Updator;
use \Lampcms\Mongo\Schema\Resource as ResourceSchema;
use \Lampcms\Mongo\Schema\User as UserSchema;
@@ -126,14 +127,22 @@ public function approveResource($id, $type = 'q')
}
if ('q' === $type) {
- $this->Resource = new Question($this->Registry, $a);
+ $this->Question = $this->Resource = new Question($this->Registry, $a);
} else {
$this->Resource = new Answer($this->Registry, $a);
+ $this->Question = $this->getQuestion($this->Resource->getQuestionId());
}
if (true === $res = $this->Resource->setApprovedStatus($this->Registry->Viewer)) {
+
$this->updatePoster();
+ $this->updateCategory($type);
+
if ('q' === $type) {
+ $this->addTags()
+ ->addUnansweredTags()
+ ->addRelatedTags();
+
try {
IndexerFactory::factory($this->Registry)->indexQuestion($this->Resource);
} catch ( \Exception $e ) {
@@ -142,7 +151,14 @@ public function approveResource($id, $type = 'q')
}
$this->Registry->Dispatcher->post($this->Resource, 'onApprovedQuestion');
} else {
- $this->Registry->Dispatcher->post($this->Resource, 'onApprovedAnswer', array('question' => $this->getQuestion($this->Resource->getQuestionId())));
+ /**
+ * Need update etag of Question otherwise
+ * upon page reload the browser will show cached
+ * version of question with this answer still showing
+ * with the "pending" notice
+ */
+ $this->updateQuestion();
+ $this->Registry->Dispatcher->post($this->Resource, 'onApprovedAnswer', array('question' => $this->Question));
}
d('Approval complete for resource type ' . $type . ' id: ' . $id);
@@ -203,4 +219,142 @@ protected function updatePoster()
return $this;
}
+
+ /**
+ * Update count of answers in a category
+ *
+ * @param string $type 'q' means to update questions count, 'a' to update
+ * answers count in category
+ *
+ * @return \Lampcms\QuestionParser
+ */
+ protected function updateCategory($type = 'q')
+ {
+ $Updator = new Updator($this->Registry->Mongo);
+ if ('q' === $type) {
+ $Updator->addQuestion($this->Resource);
+ } else {
+ $Updator->addAnswer($this->Resource);
+ }
+
+ $this->Registry->Cache->__unset('categories');
+
+ return $this;
+ }
+
+ /**
+ * Increase answer count
+ * for question.
+ * Also set Last Answerer details
+ * and add Answerer User to list
+ * of Question contributors
+ * (this is for the dot-folders feature)
+ *
+ * The increaseAnswerCount will also update
+ * the last modified timestamp for question
+ *
+ * @return object $this
+ */
+ protected function updateQuestion()
+ {
+ d('cp');
+ $uid = $this->Resource->getOwnerId();
+ d('uid of answer: '.$uid);
+ $User = \Lampcms\User::userFactory($this->Registry)->by_id($uid);
+ d('$User id: '.$User->getUid());
+ $this->Question->updateAnswerCount();
+ $this->Question->addContributor($User);
+ $this->Question->setLatestAnswer($User, $this->Resource);
+ $this->Question->touch();
+ $this->Question->save();
+
+ return $this;
+ }
+
+
+ /**
+ * Update QUESTION_TAGS tags counter
+ *
+ * @return object $this
+ */
+ protected function addTags()
+ {
+ $o = Qtagscounter::factory($this->Registry);
+ $Question = $this->Question;
+ if (count($Question['a_tags']) > 0) {
+ $callable = function() use($o, $Question)
+ {
+ try {
+ $o->parse($Question);
+ } catch ( \Exception $e ) {
+
+ if (function_exists('d')) {
+ d('Error: Unable to add tags: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on ' . $e->getLine());
+ }
+
+ }
+ };
+ d('cp');
+ runLater($callable);
+ }
+
+ return $this;
+ }
+
+
+ /**
+ * Calculates related tags
+ * via shutdown function
+ *
+ * @return object $this
+ */
+ protected function addRelatedTags()
+ {
+ $Related = Relatedtags::factory($this->Registry);
+ $Question = $this->Question;
+ if (count($Question['a_tags']) > 0) {
+ d('cp');
+ $callable = function() use ($Related, $Question)
+ {
+ try {
+ $Related->addTags($Question);
+ } catch ( \Exception $e ) {
+ // cannot do much here, only error_log may be
+ // safe to use
+ }
+ };
+ runLater($callable);
+ }
+ d('cp');
+
+ return $this;
+ }
+
+
+ /**
+ * Skip if $this->Question['status'] is accptd
+ * which would be the case when question came from API
+ * and is already answered
+ *
+ * @return object $this
+ */
+ protected function addUnansweredTags()
+ {
+ if ('accptd' !== $this->Question[ResourceSchema::STATUS]) {
+ if (count($this->Question['a_tags']) > 0) {
+ $o = new UnansweredTags($this->Registry);
+ $Question = $this->Question;
+ $callable = function() use ($o, $Question)
+ {
+ $o->set($Question);
+ };
+ d('cp');
+ runLater($callable);
+ }
+ d('cp');
+ }
+
+ return $this;
+ }
+
}
View
8 lib/Lampcms/Modules/Facebook/Client.php
@@ -169,7 +169,6 @@ class Client
public function __construct(\Lampcms\Registry $Registry)
{
-
$this->Registry = $Registry;
$this->aFBConfig = $Registry->Ini->getSection('FACEBOOK');
$this->appId = $this->aFBConfig['APP_ID'];
@@ -201,7 +200,7 @@ public function getFacebookUser()
$aUser = $this->getUserArray($this->fbId);
if (!empty($aUser)) {
- $this->User = UserFacebook::factory($this->Registry, $aUser);
+ $this->User = UserFacebook::userFactory($this->Registry, $aUser);
d('existing user $this->User: ' . \json_encode($this->User->getArrayCopy()));
$this->updateUser();
d('cp');
@@ -270,7 +269,7 @@ public function getFacebookUser()
* to "active" FB user by adding valid FB token to User object
*/
if (!empty($aUser)) {
- $this->User = UserFacebook::factory($this->Registry, $aUser);
+ $this->User = UserFacebook::userFactory($this->Registry, $aUser);
d('existing user $this->User: ' . print_r($this->User->getArrayCopy(), 1));
$this->updateUser();
@@ -564,7 +563,7 @@ protected function createNewUser()
d('aUser: ' . print_r($aUser, 1));
- $this->User = UserFacebook::factory($this->Registry, $aUser);
+ $this->User = UserFacebook::userFactory($this->Registry, $aUser);
$this->User->insert();
d('$this->User after insert: ' . print_r($this->User->getArrayCopy(), 1));
@@ -684,7 +683,6 @@ protected function checkUniqueAccount(array $aUser = null)
*/
public function postUpdate(\Lampcms\Interfaces\FacebookUser $User, $aData)
{
-
if (!is_string($aData) && !is_array($aData)) {
throw new \InvalidArgumentException('Invalid data type of $aData: ' . \gettype($aData));
}
View
2  lib/Lampcms/Mongo/Doc.php
@@ -369,7 +369,7 @@ public function setCollectionName($name)
*/
public function __call($method, $arguments)
{
- if ('by' !== substr(strtolower($method), 0, 2)) {
+ if ('by' !== \substr(\strtolower($method), 0, 2)) {
throw new \InvalidArgumentException('Unknown method: ' . $method);
}
View
2  lib/Lampcms/Mongo/Schema/Answer.php
@@ -72,8 +72,6 @@ class Answer extends Resource
const TITLE = 'title';
- const COMMENTS_ARRAY = 'a_comments';
-
const COMMENTS_COUNT = 'i_comments';
/**
View
4 lib/Lampcms/Mongo/Schema/Question.php
@@ -63,8 +63,6 @@ class Question extends Answer
const TITLE_ARRAY = 'a_title';
- const STATUS = 'status';
-
const TAGS_HTML = 'tags_html';
const CREDITS = 'credits';
@@ -77,7 +75,7 @@ class Question extends Answer
const NUM_ANSWERS = 'i_ans';
- const NUM_FOLLOWERS = 'i_flwrs';
+ const SELECTED_ANSWER_ID = 'i_sel_ans';
const APP_ID = 'app_id';
View
16 lib/Lampcms/Mongo/Schema/Resource.php
@@ -67,6 +67,8 @@ class Resource
const IP_ADDRESS = 'ip';
+ const STATUS = 'status';
+
/**
* This field is a Status id of Question or Answer
* This indicates POSTED, PENDING or DELETED status
@@ -94,6 +96,20 @@ class Resource
const DELETED_TIMESTAMP = 'i_del_ts';
/**
+ * Array of followers
+ * this is array of integers (user ids)
+ */
+ const FOLLOWERS = 'a_flwrs';
+
+
+ const COMMENTS_ARRAY = 'a_comments';
+
+ /**
+ * count of followers
+ */
+ const NUM_FOLLOWERS = 'i_flwrs';
+
+ /**
* Status of normal Question or answer
*/
const POSTED = 1;
View
77 lib/Lampcms/Question.php
@@ -81,7 +81,6 @@ class Question extends \Lampcms\Mongo\Doc implements Interfaces\Question, Interf
public function __construct(Registry $Registry, array $a = null)
{
-
$a = ($a) ? $a : array();
parent::__construct($Registry, 'QUESTIONS', $a);
}
@@ -107,7 +106,6 @@ public function getResourceTypeId()
*/
public function getResourceId()
{
-
return $this->offsetGet(Schema::PRIMARY);
}
@@ -137,7 +135,6 @@ public function getQuestionId()
*/
public function getDeletedTime()
{
-
return $this->offsetGet(Schema::DELETED_TIMESTAMP);
}
@@ -150,7 +147,6 @@ public function getDeletedTime()
*/
public function getOwnerId()
{
-
return (int)$this->offsetGet(Schema::POSTER_ID);
}
@@ -163,7 +159,6 @@ public function getOwnerId()
*/
public function getLastModified()
{
-
return $this->offsetGet(Schema::LAST_MODIFIED_TIMESTAMP);
}
@@ -215,7 +210,7 @@ public function getUrl($short = false)
*/
public function getBody()
{
- return $this->offsetGet('b');
+ return $this->offsetGet(Schema::BODY);
}
@@ -227,7 +222,7 @@ public function getBody()
*/
public function getTitle()
{
- return $this->offsetGet('title');
+ return $this->offsetGet(Schema::TITLE);
}
/**
@@ -235,7 +230,7 @@ public function getTitle()
*/
public function getCategoryId()
{
- return $this->offsetGet('i_cat');
+ return $this->offsetGet(Schema::CATEGORY_ID);
}
@@ -273,8 +268,7 @@ public function isClosed()
*/
public function getAnswerCount()
{
-
- return $this->offsetGet('i_ans');
+ return $this->offsetGet(Schema::NUM_ANSWERS);
}
@@ -325,14 +319,13 @@ public function setClosed(User $closer, $reason = null)
*/
public function setDeleted(User $user, $reason = null)
{
-
if (0 === $this->getDeletedTime()) {
if ($reason) {
$reason = \strip_tags((string)$reason);
}
- parent::offsetSet('i_del_ts', time());
+ parent::offsetSet(Schema::DELETED_TIMESTAMP, time());
parent::offsetSet(Schema::RESOURCE_STATUS_ID, Schema::DELETED);
parent::offsetSet('a_deleted',
array(
@@ -360,7 +353,6 @@ public function setDeleted(User $user, $reason = null)
*/
public function setEdited(User $user, $reason = '')
{
-
if (!empty($reason)) {
$reason = \strip_tags((string)$reason);
}
@@ -399,17 +391,16 @@ public function setEdited(User $user, $reason = '')
*/
public function retag(User $user, array $tags)
{
-
- parent::offsetSet('a_tags', $tags);
+ parent::offsetSet(Schema::TAGS_ARRAY, $tags);
parent::offsetSet('tags_html', \tplQtags::loop($tags, false));
- $b = $this->offsetGet('b');
+ $b = $this->offsetGet(Schema::BODY);
d('b: ' . $b);
$oHtmlParser = \Lampcms\String\HTMLStringParser::stringFactory(Utf8String::stringFactory($b, 'utf-8', true));
$body = $oHtmlParser->unhilight()->hilightWords($tags)->valueOf();
- $this->offsetSet('b', $body);
+ $this->offsetSet(Schema::BODY, $body);
$this->setEdited($user, 'Retagged')->touch();
@@ -451,11 +442,11 @@ public function setBestAnswer(Answer $Answer)
* the count of unanswered tags, which
* is done via UnansweredTags object
*/
- if ('accptd' !== $this->offsetGet('status')) {
+ if ('accptd' !== $this->offsetGet(Schema::STATUS)) {
UnansweredTags::factory($this->Registry)->remove($this);
}
- parent::offsetSet('status', 'accptd');
+ parent::offsetSet(Schema::STATUS, 'accptd');
d('setting status to accptd');
$this->touch(false);
@@ -484,7 +475,7 @@ public function updateAnswerCount($inc = 1)
throw new \InvalidArgumentException('Param $inc must be an integer. was: ' . gettype($inc));
}
- $iAns = $this->offsetGet('i_ans');
+ $iAns = $this->offsetGet(Schema::NUM_ANSWERS);
d('$iAns ' . $iAns);
/**
@@ -498,7 +489,7 @@ public function updateAnswerCount($inc = 1)
$newCount = max(0, ($iAns + $inc));
d('$newCount: ' . $newCount);
- parent::offsetSet('i_ans', $newCount);
+ parent::offsetSet(Schema::NUM_ANSWERS, $newCount);
/**
* Change the status to answrd
@@ -508,9 +499,9 @@ public function updateAnswerCount($inc = 1)
* make the question 'answered'
*/
if ($newCount < 1) {
- parent::offsetSet('status', 'unans');
+ parent::offsetSet(Schema::STATUS, 'unans');
} elseif ('unans' === $this->offsetGet('status')) {
- parent::offsetSet('status', 'answrd');
+ parent::offsetSet(Schema::STATUS, 'answrd');
}
/**
@@ -719,9 +710,9 @@ public function getVotesArray()
{
$a = array(
- 'up' => $this->offsetGet('i_up'),
- 'down' => $this->offsetGet('i_down'),
- 'score' => $this->offsetGet('i_votes'));
+ 'up' => $this->offsetGet(Schema::UPVOTES_COUNT),
+ 'down' => $this->offsetGet(Schema::DOWNVOTES_COUNT),
+ 'score' => $this->offsetGet(Schema::VOTES_SCORE));
return $a;
}
@@ -735,7 +726,7 @@ public function getVotesArray()
*/
public function getScore()
{
- return $this->offsetGet('i_votes');
+ return $this->offsetGet(Schema::VOTES_SCORE);
}
@@ -772,14 +763,14 @@ public function addComment(CommentParser $Comment)
);
$aComments = $this->getComments();
- d('aComments: ' . print_r($aComments, 1));
+ d('aComments: ' . \json_encode($aComments));
/**
* Only keep the keys that we need
* get rid of keys like hash, i_res
* because we don't need them here
*/
$aComment = $Comment->getArrayCopy();
- $aComment = array_intersect_key($aComment, array_flip($aKeys));
+ $aComment = \array_intersect_key($aComment, \array_flip($aKeys));
$aComments[] = $aComment;
@@ -830,7 +821,7 @@ public function increaseCommentsCount($count = 1)
$commentsCount = $this->getCommentsCount();
d('$commentsCount ' . $commentsCount);
- parent::offsetSet('i_comments', ($commentsCount + $count));
+ parent::offsetSet(Schema::COMMENTS_COUNT, ($commentsCount + $count));
return $this;
}
@@ -872,12 +863,12 @@ public function deleteComment($id)
$newCount = count($aComments);
if (0 === $newCount) {
- $this->offsetUnset('a_comments');
+ $this->offsetUnset(Schema::COMMENTS_ARRAY);
} else {
$this->setComments($aComments);
}
- parent::offsetSet('i_comments', $newCount);
+ parent::offsetSet(Schema::COMMENTS_COUNT, $newCount);
return $this;
}
@@ -980,10 +971,10 @@ public function addFollower($User)
$uid = (is_int($User)) ? $User : $User->getUid();
- $aFollowers = $this->offsetGet('a_flwrs');
+ $aFollowers = $this->offsetGet(Schema::FOLLOWERS);
if (!in_array($uid, $aFollowers)) {
$aFollowers[] = $uid;
- $this->offsetSet('a_flwrs', $aFollowers);
+ $this->offsetSet(Schema::FOLLOWERS, $aFollowers);
$this->save();
}
@@ -1010,11 +1001,11 @@ public function removeFollower($User)
$uid = (is_int($User)) ? $User : $User->getUid();
- $aFollowers = $this->offsetGet('a_flwrs');
+ $aFollowers = $this->offsetGet(Schema::FOLLOWERS);
if (false !== $key = array_search($uid, $aFollowers)) {
d('cp unsetting key: ' . $key);
array_splice($aFollowers, $key, 1);
- $this->offsetSet('a_flwrs', $aFollowers);
+ $this->offsetSet(Schema::FOLLOWERS, $aFollowers);
$this->save();
}
@@ -1103,10 +1094,10 @@ public function removeAnswer(Answer $Answer)
* this question is technically unanswered again
*/
if ((true === $Answer['accepted']) &&
- ($id === $this->offsetGet('i_sel_ans'))
+ ($id === $this->offsetGet(Schema::SELECTED_ANSWER_ID))
) {
- parent::offsetSet('status', 'answrd');
- $this->offsetUnset('i_sel_ans');
+ parent::offsetSet(Schema::STATUS, 'answrd');
+ $this->offsetUnset(Schema::SELECTED_ANSWER_ID);