Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class UserArticles extends CActiveRecord {
- const STATUS_ACTIVE = 1; //статья в работе
- const STATUS_COMPLETED = 2; //статья выполнена
- const STATUS_PAID = 3; //статья оплачена
- const STATUS_REPLACEMENT_AUTHOR = 0; //Отказ от автора
- const STATUS_FAILURE = -1; //Отказ от статьи
- const STATUS_EXPIRED = -2; //Просрочен
- const PAGINATION_lIMIT_ARTICLES_AUTHOR = 10;
- public static function model($className = __CLASS__) {
- return parent::model($className);
- }
- public function tableName() {
- return '{{user_articles}}';
- }
- /**
- * вовращает массив текстовых представлений статуса
- * @return string
- */
- static public function getStatusList() {
- return array(
- self::STATUS_ACTIVE => 'В работе',
- self::STATUS_COMPLETED => 'Написано',
- self::STATUS_PAID => 'Оплачено',
- self::STATUS_REPLACEMENT_AUTHOR => 'Отказ от автора',
- self::STATUS_FAILURE => 'Отказ от статьи',
- self::STATUS_EXPIRED => 'Просрочен',
- );
- }
- /**
- * возвращает текстовое представление статуса
- * @return string
- */
- public function getStatus() {
- $data = $this->getStatusList();
- return array_key_exists($this->status, $data) ? $data[$this->status] : '*неизвестно*';
- }
- public function attributeLabels() {
- return array(
- 'article_title' => 'Заголовок',
- 'article_introtext' => 'Описание',
- 'article_fulltext' => 'Текст',
- );
- }
- public function relations() {
- return array(
- 'orders' => array(self::BELONGS_TO, 'Order', 'order_id'),
- 'userdata' => array(self::BELONGS_TO, 'User', 'user_id'),
- 'editor' => array(self::BELONGS_TO, 'User', 'editor_id'),
- 'orderarticles' => array(self::BELONGS_TO, 'OrderArticles', 'article_id'),
- );
- }
- public function rules() {
- return array(
- array('article_title, article_introtext, article_fulltext, auto, symbols, unique_tester', 'safe'),
- );
- }
- /**
- * Проверка на принадленость статья клиенту
- * @param type $client_id
- * @return boolean if articles belongs to the client
- */
- public function belongsToClient($id_articles, $client_id = NULL) {
- if ($client_id === NULL)
- $client_id = Yii::app()->user->id;
- $com = Yii::app()->db->createCommand();
- $com->select('COUNT(us.id), us.id, ua.id')
- ->from('{{user_articles}} as ua')
- ->leftJoin('{{orders}} as or', ' ua.order_id=or.id')
- ->leftJoin('{{users}} as us', ' us.id=or.id_client')
- ->where('ua.id=:ua_id AND us.id=:us_id', array(':ua_id' => $id_articles, ':us_id' => $client_id));
- $rslt = $com->queryScalar();
- if ($rslt)
- return true;
- else
- return false;
- }
- public function beforeSave() {
- if ($this->isNewRecord) {
- $this->user_id = Yii::app()->user->id;
- $this->status = self::STATUS_ACTIVE;
- $this->start_date = time();
- }
- return parent::beforeSave();
- }
- /**
- * получение списка статей которые связаны с автором
- * @param type $author_id
- * @param boolean $isPagination value: true or false
- * @return boolean
- */
- public function getAllArticlesAuthor($author_id = NULL, $isPagination = false, $conditions = array(0 => array('AND'), 1 => array()), $paginationOffset = false) {
- if ($author_id === NULL && Yii::app()->user->id) {
- if (Yii::app()->user->id) {
- $author_id = (int) Yii::app()->user->id;
- } else {
- return false;
- }
- } elseif ($author_id !== NULL) {
- $author_id = (int) $author_id;
- } else {
- return false;
- }
- $conditions[0][] = 'u.user_id=:user_id';
- $conditions[1][':user_id'] = (int) $author_id;
- $articlesListCommand = Yii::app()->db->createCommand()
- ->from(self::tableName() . ' AS u ')
- ->where($conditions[0], $conditions[1]);
- if ((bool) $isPagination) {
- $articlesListCommand->select('count(u.id)')
- ->leftJoin(Order::model()->tableName() . ' as o', 'u.order_id=o.id')
- ->leftJoin(OrderArticles::model()->tableName() . ' as oa', 'u.article_id=oa.id')
- ->leftJoin('{{types_thematics}} as th', 'th.id=o.id_thematics')
- ->leftJoin(User::model()->tableName() . ' as uc', 'uc.id=o.id_client');
- } else {
- $articlesListCommand->select('u.id as u_id, '
- . 'u.order_id, '
- . 'u.article_id, '
- . 'u.user_id,'
- . 'u.symbols as symbols, '
- . 'u.start_date as start_date, '
- . 'u.end_date as end_date, '
- . 'u.status as status, '
- . 'u.done_date as done_date, '
- . 'u.unique_tester as unique_tester,'
- . 'o.id as o_id,'
- . 'o.cost as cost,'
- . 'o.id_thematics,'
- . 'o.id_type,'
- . 'oa.order_symbols_max as order_symbols_max,'
- . 'oa.reserve as reserve,'
- . 'th.title as thematics,'
- . 'o.id_client as id_client,'
- . 'uc.firstname as firstname,'
- . 'uc.lastname as lastname,'
- . 'o.hours_done as hours_done,'
- . 'oa.id as oa_id,'
- . 'oa.status as oa_status,'
- . 'tt.title as type_task');
- $articlesListCommand->limit(self::PAGINATION_lIMIT_ARTICLES_AUTHOR, $paginationOffset);
- }
- if ((bool) $isPagination) {
- $articlesList = (int) $articlesListCommand->queryScalar();
- } else {
- $articlesList = $articlesListCommand
- ->leftJoin(Order::model()->tableName() . ' as o', 'u.order_id=o.id')
- ->leftJoin(OrderArticles::model()->tableName() . ' as oa', 'u.article_id=oa.id')
- ->leftJoin('{{types_thematics}} as th', 'th.id=o.id_thematics')
- ->leftJoin(User::model()->tableName() . ' as uc', 'uc.id=o.id_client')
- ->leftJoin(TypeTask::model()->tableName() . ' as tt', 'tt.id=o.id_type')
- ->group("u.article_id")
- ->order('u.id DESC')
- ->queryAll();
- }
- return $articlesList;
- }
- /**
- *
- * @param type $post
- * @return type
- */
- public function myProjectConditions($post = NULL) {
- $conditions = array();
- $conditions[0] = array('AND');
- $conditions[1] = array();
- if ($post === NULL)
- return $conditions;
- if ((int) @$post['them'] && @$post['stat'] != 'them') {
- $conditions[0][] = " o.id_thematics = :id_thematics";
- $conditions[1][':id_thematics'] = (int) $post['them'];
- }
- if ((int) @$post['stat'] && @$post['stat'] != 'any') {
- $conditions[0][] = " u.status = :u_status";
- $conditions[1][':u_status'] = (int) $post['stat'];
- }
- if ((int) @$post['type'] && @$post['type'] != 'any') {
- $conditions[0][] = " o.id_type = :id_type";
- $conditions[1][':id_type'] = (int) $post['type'];
- }
- $flagCondition = '';
- if ((int) @$post['auto']) {
- $flagCondition = ' (u.status="' . UserArticles::STATUS_PAID . '" AND oa.status="' . OrderArticles::STATUS_COMPLETED_AUTO . '") ';
- }
- if ((int) @$post['expi']) {
- if (trim($flagCondition) != '')
- $flagCondition .= " OR ";
- $flagCondition .= ' (u.status="' . UserArticles::STATUS_EXPIRED . '")';
- }
- if ((int) @$post['canc']) {
- if (trim($flagCondition) != '')
- $flagCondition .= " OR ";
- $flagCondition .= ' (u.status="' . UserArticles::STATUS_FAILURE . '" OR u.status="' . UserArticles::STATUS_REPLACEMENT_AUTHOR . '") ';
- }
- if (trim($flagCondition) != '')
- $conditions[0][] = $flagCondition;
- return $conditions;
- }
- /**
- * @param type $count
- * @return \CPagination
- */
- public function getPagination($count) {
- $pagination = new CPagination((int) $count);
- $pagination->pageSize = self::PAGINATION_lIMIT_ARTICLES_AUTHOR;
- return $pagination;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement