Skip to content

Commit

Permalink
Intermediate commit just to backup changes. This commit is untested
Browse files Browse the repository at this point in the history
and may be unstable. For stable version use previous commit or wait
for next one.
  • Loading branch information
snytkine committed Mar 14, 2011
1 parent 3392065 commit cd17cdc
Show file tree
Hide file tree
Showing 37 changed files with 5,521 additions and 209 deletions.
4 changes: 3 additions & 1 deletion acl.ini
Expand Up @@ -48,7 +48,7 @@ add_blog_post = A
post_by_email = A post_by_email = A
ask = A ask = A
answer = A answer = A
comment = A comment = D
vote = A vote = A
accept = A accept = A


Expand All @@ -68,7 +68,9 @@ edit_question = A
edit_answer = A edit_answer = A
delete_question = A delete_question = A
delete_answer = A delete_answer = A
delete_comment = A
retag = A retag = A
comment = A
ban_user = A ban_user = A
unban_user = A unban_user = A
close_question = A close_question = A
Expand Down
129 changes: 125 additions & 4 deletions lib/Lampcms/Answer.php
Expand Up @@ -61,7 +61,7 @@
* @author Dmitri Snytkine * @author Dmitri Snytkine
* *
*/ */
class Answer extends MongoDoc implements Interfaces\LampcmsResource, Interfaces\UpDownRatable class Answer extends MongoDoc implements Interfaces\LampcmsResource, Interfaces\UpDownRatable, Interfaces\CommentedResource
{ {


public function __construct(Registry $oRegistry, array $a = array()){ public function __construct(Registry $oRegistry, array $a = array()){
Expand Down Expand Up @@ -126,8 +126,8 @@ public function setDeleted(User $user, $reason = null){
'hts' => date('F j, Y g:i a T') 'hts' => date('F j, Y g:i a T')
) )
); );

$this->updateLastModified(); $this->touch();
} }


return $this; return $this;
Expand Down Expand Up @@ -159,7 +159,7 @@ public function setEdited(User $user, $reason = ''){


$this->offsetSet('a_edited', $aEdited); $this->offsetSet('a_edited', $aEdited);


$this->updateLastModified(); $this->touch();


return $this; return $this;
} }
Expand Down Expand Up @@ -196,6 +196,18 @@ public function updateLastModified(){
return $this; return $this;
} }


/**
* Updates last modified timestamp
* A replacement for updateLastModified() method
*
* @return object $this
*/
public function touch(){
$this->offsetSet('i_lm_ts', time());

return $this;
}



/** /**
* (non-PHPdoc) * (non-PHPdoc)
Expand Down Expand Up @@ -291,4 +303,113 @@ public function getUrl(){


return $this->oRegistry->Ini->SITE_URL.'/q'.$this->offsetGet('i_qid').'/#ans'.$this->offsetGet('_id'); return $this->oRegistry->Ini->SITE_URL.'/q'.$this->offsetGet('i_qid').'/#ans'.$this->offsetGet('_id');
} }

public function addComment(CommentParser $oComment){
$aKeys = array('_id', 'i_uid', 'i_prnt', 'username', 'b_owner', 'b', 't', 'ts');

$aComments = $this->getComments();
d('aComments: '.print_r($aComments, 1));
/**
* Only keep the keys that we need
* get rid of keys like hash, i_res
* because we don't need them here
*/
$aComment = $oComment->getArrayCopy();
$aComment = array_intersect_key($aComment, array_flip($aKeys));

$aComments[] = $aComment;

$this->offsetSet('comments', $aComments);
$this->increaseCommentsCount();

return $this;

}


public function getCommentsCount(){
$aComments = $this->getComments();

return count($aComments);
}


public function increaseCommentsCount(){
/**
* Now increase comments count
*/
$commentsCount = $this->getCommentsCount();
d('$commentsCount '.$commentsCount);

$this->offsetSet('i_comments', ($commentsCount + 1) );

return $this;
}


/**
* Remove one comment from array of comments
* then re-save the new array of comments
* the numerical keys of array will be reset
* Also i_comments value will be updated to the
* new count of comments
*
* (non-PHPdoc)
* @see Lampcms\Interfaces.CommentedResource::deleteComment()
*/
public function deleteComment($id){

if(!$this->checkOffset('comments')){
e('This question does not have any comments');

return $this;
}

$aComments = $this->offsetGet('comments');

for($i = 0; $i<count($aComments); $i+=1){
if($aComments[$i]['_id'] == $id){
d('unsetting comment: '.$i);
array_splice($aComments, $i, 1);
break;
}
}

$newCount = count($aComments);
if( 0 === $newCount){
$this->offsetUnset('comments');
} else {
$this->offsetSet('comments', $aComments);
}

$this->offsetSet('i_comments', $newCount );

return $this;
}


/**
* Getter for 'comments' element
* @return array of comments or empty array if
* 'comments' element not present in the object
*
*/
public function getComments(){
return $this->getFallback('comments', array());
}


/**
* Get uid of user who asked the question
* for which this is the answer
* This is useful during adding a comment
* to an asnwer where we need to know wheather of not
* the comment comes from the original asker.
*
*
*/
public function getQuestionOwnerId(){
return $this->offsetGet('i_quid');
}

} }
3 changes: 3 additions & 0 deletions lib/Lampcms/AnswerParser.php
Expand Up @@ -199,6 +199,7 @@ protected function makeAnswer(){
'_id' => $this->oRegistry->Resource->create('ANSWER'), '_id' => $this->oRegistry->Resource->create('ANSWER'),
'i_qid' => $qid, 'i_qid' => $qid,
'i_uid' => $uid, 'i_uid' => $uid,
'i_quid' => $this->oQuestion->getOwnerId(),
'title' => $this->oQuestion->title, 'title' => $this->oQuestion->title,
'hash' => $hash, 'hash' => $hash,
'uname' => $username, 'uname' => $username,
Expand Down Expand Up @@ -371,4 +372,6 @@ public function getQuestion(){


return $this->oQuestion; return $this->oQuestion;
} }

} }

0 comments on commit cd17cdc

Please sign in to comment.