<?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 AnModelComment extends AnDomainModelAbstract
{
static public function describe($model)
{
$model->setTable('socialengine_comments AS comment');
$model->property('id', array('unique' => true, 'field'=>'comment.socialengine_comment_id'));
$model->property('body', array('required'=> true, 'field'=>'comment.body', 'filter'=>'string'));
$model->property('component', array('required'=> true, 'field'=>'comment.component'));
$model->property('creationTime', array('required'=> true, 'class'=>'lib.anahita.type.date', 'initialize_with'=>array('date'=>'comment.created_on')));
$model->belongsTo('node', array('required' => true,
'polymorphic' => true,
'type_field' => 'comment.node_type',
'child_key' => 'comment.node_id' ));
$model->belongsTo('author', array('required'=> true, 'model'=>'lib.anahita.model.actor' , 'child_key'=>'comment.created_by'));
$model->setRepository('lib.anahita.model.comment.repository');
}
/**
* Initialize a new comment
* @return
*/
public function awakeFromInsert()
{
if ( is_null($this->component) ) {
$pkg = $this->getIdentifier()->package;
if ( $pkg == 'anahita' ) $pkg = 'socialengine';
$this->component = 'com_'.$pkg;
}
}
/**
* Comment can be inserted if it's node is commentable
* @return
*/
public function validateForInsert()
{
return $this->node->getAcl()->canAddComment($this);
}
/**
* Comment by default can't be updated
* @return
*/
public function validateForUpdate()
{
return $this->node->getAcl()->canEditComment($this);
}
/**
* Comment is deletable if the author is the same as viewer
* @return
*/
public function validateForDelete()
{
return $this->node->getAcl()->canDeleteComment($this);
}
/**
* Notify the node that this comment has been deleted
* @return
*/
protected function _afterDelete()
{
$this->node->_notifyCommentDeleted($this);
}
/*
protected function _beforeInsert(KCommandContext $context)
{
$context['data']['comment.owner_type'] = (string) $this->node->getIdentifier();
}*/
protected function _afterInsert()
{
$this->node->_notifyCommentInserted($this);
}
protected function _afterUpdate()
{
$this->node->_notifyCommentUpdated($this);
}
}