Advertisement
stixlink

model framework yii

Sep 1st, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.69 KB | None | 0 0
  1. <?php
  2.  
  3. class UserArticles extends CActiveRecord {
  4.  
  5.    const STATUS_ACTIVE = 1;  //статья в работе
  6.    const STATUS_COMPLETED = 2; //статья выполнена
  7.    const STATUS_PAID = 3;  //статья оплачена
  8.    const STATUS_REPLACEMENT_AUTHOR = 0; //Отказ от автора
  9.    const STATUS_FAILURE = -1; //Отказ от статьи
  10.    const STATUS_EXPIRED = -2; //Просрочен
  11.    const PAGINATION_lIMIT_ARTICLES_AUTHOR = 10;
  12.  
  13.    public static function model($className = __CLASS__) {
  14.       return parent::model($className);
  15.    }
  16.  
  17.    public function tableName() {
  18.  
  19.       return '{{user_articles}}';
  20.    }
  21.  
  22.    /**
  23.     * вовращает массив текстовых представлений статуса
  24.     * @return string
  25.     */
  26.    static public function getStatusList() {
  27.  
  28.       return array(
  29.           self::STATUS_ACTIVE => 'В работе',
  30.           self::STATUS_COMPLETED => 'Написано',
  31.           self::STATUS_PAID => 'Оплачено',
  32.           self::STATUS_REPLACEMENT_AUTHOR => 'Отказ от автора',
  33.           self::STATUS_FAILURE => 'Отказ от статьи',
  34.           self::STATUS_EXPIRED => 'Просрочен',
  35.       );
  36.    }
  37.  
  38.    /**
  39.     * возвращает текстовое представление статуса
  40.     * @return string
  41.     */
  42.    public function getStatus() {
  43.  
  44.       $data = $this->getStatusList();
  45.  
  46.       return array_key_exists($this->status, $data) ? $data[$this->status] : '*неизвестно*';
  47.    }
  48.  
  49.    public function attributeLabels() {
  50.  
  51.       return array(
  52.           'article_title' => 'Заголовок',
  53.           'article_introtext' => 'Описание',
  54.           'article_fulltext' => 'Текст',
  55.       );
  56.    }
  57.  
  58.    public function relations() {
  59.  
  60.       return array(
  61.           'orders' => array(self::BELONGS_TO, 'Order', 'order_id'),
  62.           'userdata' => array(self::BELONGS_TO, 'User', 'user_id'),
  63.           'editor' => array(self::BELONGS_TO, 'User', 'editor_id'),
  64.           'orderarticles' => array(self::BELONGS_TO, 'OrderArticles', 'article_id'),
  65.       );
  66.    }
  67.  
  68.    public function rules() {
  69.  
  70.       return array(
  71.           array('article_title, article_introtext, article_fulltext, auto, symbols, unique_tester', 'safe'),
  72.       );
  73.    }
  74.  
  75.    /**
  76.     * Проверка на принадленость статья клиенту
  77.     * @param type $client_id
  78.     * @return boolean if articles belongs to the client
  79.     */
  80.    public function belongsToClient($id_articles, $client_id = NULL) {
  81.  
  82.       if ($client_id === NULL)
  83.          $client_id = Yii::app()->user->id;
  84.  
  85.       $com = Yii::app()->db->createCommand();
  86.       $com->select('COUNT(us.id), us.id, ua.id')
  87.               ->from('{{user_articles}} as ua')
  88.               ->leftJoin('{{orders}} as or', ' ua.order_id=or.id')
  89.               ->leftJoin('{{users}} as us', ' us.id=or.id_client')
  90.               ->where('ua.id=:ua_id AND us.id=:us_id', array(':ua_id' => $id_articles, ':us_id' => $client_id));
  91.  
  92.       $rslt = $com->queryScalar();
  93.       if ($rslt)
  94.          return true;
  95.       else
  96.          return false;
  97.    }
  98.  
  99.    public function beforeSave() {
  100.  
  101.       if ($this->isNewRecord) {
  102.  
  103.          $this->user_id = Yii::app()->user->id;
  104.          $this->status = self::STATUS_ACTIVE;
  105.          $this->start_date = time();
  106.       }
  107.  
  108.       return parent::beforeSave();
  109.    }
  110.  
  111.    /**
  112.     * получение списка статей которые связаны с автором
  113.     * @param type $author_id
  114.     * @param boolean $isPagination value: true or false
  115.     * @return boolean
  116.     */
  117.    public function getAllArticlesAuthor($author_id = NULL, $isPagination = false, $conditions = array(0 => array('AND'), 1 => array()), $paginationOffset = false) {
  118.       if ($author_id === NULL && Yii::app()->user->id) {
  119.  
  120.          if (Yii::app()->user->id) {
  121.  
  122.             $author_id = (int) Yii::app()->user->id;
  123.          } else {
  124.             return false;
  125.          }
  126.       } elseif ($author_id !== NULL) {
  127.  
  128.          $author_id = (int) $author_id;
  129.       } else {
  130.  
  131.          return false;
  132.       }
  133.  
  134.       $conditions[0][] = 'u.user_id=:user_id';
  135.       $conditions[1][':user_id'] = (int) $author_id;
  136.  
  137.       $articlesListCommand = Yii::app()->db->createCommand()
  138.               ->from(self::tableName() . ' AS u ')
  139.               ->where($conditions[0], $conditions[1]);
  140.  
  141.  
  142.  
  143.       if ((bool) $isPagination) {
  144.          $articlesListCommand->select('count(u.id)')
  145.                  ->leftJoin(Order::model()->tableName() . ' as o', 'u.order_id=o.id')
  146.                  ->leftJoin(OrderArticles::model()->tableName() . ' as oa', 'u.article_id=oa.id')
  147.                  ->leftJoin('{{types_thematics}} as th', 'th.id=o.id_thematics')
  148.                  ->leftJoin(User::model()->tableName() . ' as uc', 'uc.id=o.id_client');
  149.       } else {
  150.          $articlesListCommand->select('u.id as u_id, '
  151.                  . 'u.order_id, '
  152.                  . 'u.article_id, '
  153.                  . 'u.user_id,'
  154.                  . 'u.symbols as symbols, '
  155.                  . 'u.start_date as start_date, '
  156.                  . 'u.end_date as end_date,  '
  157.                  . 'u.status as status, '
  158.                  . 'u.done_date as done_date, '
  159.                  . 'u.unique_tester as unique_tester,'
  160.                  . 'o.id as o_id,'
  161.                  . 'o.cost as cost,'
  162.                  . 'o.id_thematics,'
  163.                  . 'o.id_type,'
  164.                  . 'oa.order_symbols_max as order_symbols_max,'
  165.                  . 'oa.reserve as reserve,'
  166.                  . 'th.title as thematics,'
  167.                  . 'o.id_client as id_client,'
  168.                  . 'uc.firstname as firstname,'
  169.                  . 'uc.lastname as lastname,'
  170.                  . 'o.hours_done as hours_done,'
  171.                  . 'oa.id as oa_id,'
  172.                  . 'oa.status as oa_status,'
  173.                  . 'tt.title as type_task');
  174.  
  175.          $articlesListCommand->limit(self::PAGINATION_lIMIT_ARTICLES_AUTHOR, $paginationOffset);
  176.       }
  177.  
  178.       if ((bool) $isPagination) {
  179.          $articlesList = (int) $articlesListCommand->queryScalar();
  180.       } else {
  181.  
  182.          $articlesList = $articlesListCommand
  183.                  ->leftJoin(Order::model()->tableName() . ' as o', 'u.order_id=o.id')
  184.                  ->leftJoin(OrderArticles::model()->tableName() . ' as oa', 'u.article_id=oa.id')
  185.                  ->leftJoin('{{types_thematics}} as th', 'th.id=o.id_thematics')
  186.                  ->leftJoin(User::model()->tableName() . ' as uc', 'uc.id=o.id_client')
  187.                  ->leftJoin(TypeTask::model()->tableName() . ' as tt', 'tt.id=o.id_type')
  188.                  ->group("u.article_id")
  189.                  ->order('u.id DESC')
  190.                  ->queryAll();
  191.       }
  192.  
  193.       return $articlesList;
  194.    }
  195.  
  196.    /**
  197.     *
  198.     * @param type $post
  199.     * @return type
  200.     */
  201.    public function myProjectConditions($post = NULL) {
  202.  
  203.       $conditions = array();
  204.       $conditions[0] = array('AND');
  205.       $conditions[1] = array();
  206.  
  207.       if ($post === NULL)
  208.          return $conditions;
  209.  
  210.       if ((int) @$post['them'] && @$post['stat'] != 'them') {
  211.  
  212.          $conditions[0][] = " o.id_thematics = :id_thematics";
  213.          $conditions[1][':id_thematics'] = (int) $post['them'];
  214.       }
  215.  
  216.       if ((int) @$post['stat'] && @$post['stat'] != 'any') {
  217.  
  218.          $conditions[0][] = " u.status = :u_status";
  219.          $conditions[1][':u_status'] = (int) $post['stat'];
  220.       }
  221.  
  222.       if ((int) @$post['type'] && @$post['type'] != 'any') {
  223.  
  224.          $conditions[0][] = " o.id_type = :id_type";
  225.          $conditions[1][':id_type'] = (int) $post['type'];
  226.       }
  227.  
  228.       $flagCondition = '';
  229.       if ((int) @$post['auto']) {
  230.  
  231.          $flagCondition = ' (u.status="' . UserArticles::STATUS_PAID . '" AND oa.status="' . OrderArticles::STATUS_COMPLETED_AUTO . '") ';
  232.       }
  233.  
  234.       if ((int) @$post['expi']) {
  235.  
  236.          if (trim($flagCondition) != '')
  237.             $flagCondition .= " OR ";
  238.  
  239.          $flagCondition .= ' (u.status="' . UserArticles::STATUS_EXPIRED . '")';
  240.       }
  241.  
  242.       if ((int) @$post['canc']) {
  243.  
  244.          if (trim($flagCondition) != '')
  245.             $flagCondition .= " OR ";
  246.  
  247.          $flagCondition .= ' (u.status="' . UserArticles::STATUS_FAILURE . '"  OR u.status="' . UserArticles::STATUS_REPLACEMENT_AUTHOR . '") ';
  248.       }
  249.  
  250.       if (trim($flagCondition) != '')
  251.          $conditions[0][] = $flagCondition;
  252.  
  253.       return $conditions;
  254.    }
  255.  
  256.    /**
  257.     * @param type $count
  258.     * @return \CPagination
  259.     */
  260.    public function getPagination($count) {
  261.  
  262.       $pagination = new CPagination((int) $count);
  263.       $pagination->pageSize = self::PAGINATION_lIMIT_ARTICLES_AUTHOR;
  264.  
  265.       return $pagination;
  266.    }
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement