<?php
/**
* @version 1.0.0
* @category Anahita Social Engineâ¢
* @copyright Copyright (C) 2008 - 2010 rmdStudio Inc. and Peerglobe Technology Inc. All rights reserved.
* @license GNU GPLv2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
* @link http://www.anahitapolis.com
*/
class AnModelStory extends AnModelStoryAbstract implements AnModelCommentCommentable
{
static public function describe($story)
{
parent::describe($story);
$story->rawQuery()->where('story.story_type','=','story');
$story->property('numOfComments', array('default' => 0, 'field'=>'story.story_comment_count'));
$story->property('numOfComments', array('default' => 0, 'field'=>'story.story_comment_count'));
$story->property('bundleId', array('field'=>'story.story_bundle_id', 'default'=>null));
$story->belongsTo('bundledStory', array('model'=>'lib.anahita.model.story','child_key'=>'story.story_bundled_story_id'));
$story->belongsTo('lastCommenter', array('model'=>'lib.anahita.model.actor', 'child_key'=>'story.story_last_comment_by'));
$story->property('lastCommentTime', array('default' => 0, 'class'=>'lib.anahita.type.date', 'initialize_with'=>array('date'=>"story.story_last_comment_on")));
$story->has('many', 'comments', array('model'=>'lib.anahita.model.comment','child_key'=>'node'));
}
public function bundleWith($story)
{
srand((double)time()*1000000);
$id = rand(0, time());
$this->bundleId = $id;
$this->bundledStory = $story;
$story->bundleId = $id;
$story->bundledStory = $this;
}
/**
* @TODO any story is insertable
* @return
*/
public function validateForInsert()
{
return $this->owner->getAcl()->canAddStory($this);
}
/**
*
* @return
*/
protected function _registerHooks()
{
parent::_registerHooks();
$this->registerFunctionAfter('delete','__hookDeleteCommentsAfterDelete');
}
/**
* Hook called after a comment has been inserted for this story
* @param $comment AnmodelComment Object
*/
public function _notifyCommentInserted($comment)
{
$this->numOfComments = $this->comments->getTotal();
$this->lastCommenter = $comment->author;
$this->lastCommentTime = $comment->creationTime;
}
/**
* Hook called after a comment of this story has been deleted
* @return
* @param $comment AnmodelComment Object
*/
public function _notifyCommentDeleted($comment)
{
$this->numOfComments = $this->comments->getTotal();
}
/**
* Hook called after a comment of this story has been updated
* @param $comment AnmodelComment Object
*/
public function _notifyCommentUpdated($comment)
{
$this->lastCommenter = $comment->author;
$this->lastCommentTime = $comment->creationTime;
}
/**
* After delete Hook - delete all the comments of this story
* @return
*/
public function __hookDeleteCommentsAfterDelete()
{
KFactory::get('lib.koowa.database')->delete('socialengine_comments', 'WHERE node_id = '.$this->id);
}
}