View file vkclone-0.0.1/protected/models/TopicPost.php

File size: 1.77Kb
<?php
/**
 * This is the model class for table "{{forum_section_topic_posts}}".
 */
class TopicPost extends CActiveRecord {

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{forum_section_topic_posts}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		return array(
			array('author_id, topic_id, text', 'required'),
			array('author_id, topic_id', 'length', 'max' => 10),
			array('text', 'filter', 'filter' => 'trim'),
			// The following rule is used by search().
			array('id, created_at, author_id, topic_id, text, update_datetime', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		return array(
			'versions' => array(self::HAS_MANY, 'TopicPostVersion', 'post_id'),
			'author' => array(self::BELONGS_TO, 'User', 'author_id'),
			'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),
		);
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return TopicPost the static model class
	 */
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}

	/**
	 * Returns post text at specific version
	 */
	public function textByVersion(TopicPostVersion $neededVersion) {
		$versions = TopicPostVersion::model()->findAll(array(
			'condition' => 'post_id = :post_id',
			'order' => 'created_at, id',
			'params' => array(':post_id' => $this->id),
		));
		$text = null;
		foreach ($versions as $version) {
			$text = xdiff_string_patch($text, $version->diff);
			if ($version->equals($neededVersion))
				break;
		}
		return $text;
	}
}