Advertisement
stixlink

model yii Shops

Nov 19th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.85 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * This is the model class for table "{{shops}}".
  5.  *
  6.  * The followings are the available columns in table '{{shops}}':
  7.  *
  8.  * @property integer $id
  9.  * @property integer $customer_id
  10.  * @property string  $ip
  11.  * @property integer $active
  12.  * @property string  $style
  13.  * @property string  $name
  14.  * @property string  $date_create
  15.  * @property string  $API_KEY
  16.  * @property string  $url
  17.  */
  18. class Shops extends CActiveRecord {
  19.     private $_oldTags;
  20.     public $category_name;
  21.  
  22.     static public $labelActive = array(
  23.         1 => 'Active',
  24.         0 => 'Not active'
  25.     );
  26.  
  27.     /**
  28.      * @return string the associated database table name
  29.      */
  30.     public function tableName() {
  31.  
  32.         return '{{shops}}';
  33.     }
  34.  
  35.     /**
  36.      * @return array validation rules for model attributes.
  37.      */
  38.     public function rules() {
  39.  
  40.         // NOTE: you should only define rules for those attributes that
  41.         // will receive user inputs.
  42.         return array(
  43.             array('customer_id, ip, API_KEY, url, name, category_id', 'required'),
  44.             array('API_KEY', 'unique'),
  45.             array('customer_id, active', 'numerical', 'integerOnly' => true),
  46.             array('ip', 'length', 'max' => 60),
  47.             array('style', 'length', 'max' => 45),
  48.             array('API_KEY', 'length', 'max' => 250),
  49.             array('url', 'length', 'max' => 600),
  50.             array('url', 'url', 'defaultScheme' => 'http'),
  51.             array('tags', 'safe'),
  52.             array('tags', 'match', 'pattern' => '/^[\w\s,]+$/', 'message' => 'Tags can only contain word characters.'),
  53.             array('url', 'url'),
  54.             array('tags', 'normalizeTags'),
  55.             array('API_KEY, date_create, customer_id', 'unsafe', 'on' => 'getshop'),
  56.             array('id, customer_id, url, ip, active, category_id, style, date_create, API_KEY', 'safe', 'on' => 'search'),
  57.             array('tags', 'safe', 'on' => 'filters'),
  58.         );
  59.     }
  60.  
  61.     /**
  62.      * @return array relational rules.
  63.      */
  64.     public function relations() {
  65.  
  66.         return array(
  67.  
  68.             'category' => array(self::BELONGS_TO, 'CategoryShop', 'category_id'),
  69.         );
  70.     }
  71.  
  72.     /**
  73.      * @return array customized attribute labels (name=>label)
  74.      */
  75.     public function attributeLabels() {
  76.  
  77.         return array(
  78.             'id' => 'ID',
  79.             'name' => 'Name',
  80.             'customer_id' => 'Customer',
  81.             'ip' => 'Ip',
  82.             'active' => 'Active',
  83.             'style' => 'Style',
  84.             'date_create' => 'Date Create',
  85.             'API_KEY' => 'Api Key',
  86.             'category_id' => 'Category',
  87.             'tags' => 'Tags',
  88.             'url' => 'Url'
  89.         );
  90.     }
  91.  
  92.     public function getListForCustomer($customer_id = null, $emptyItem = false) {
  93.  
  94.         $where = ' WHERE customer_id=' . (int)$customer_id;
  95.         if ($customer_id === null) {
  96.             $where = '';
  97.         }
  98.         $sql = 'SELECT id, `name` FROM '
  99.                . self::model()->tableName()
  100.                . $where . ' ORDER BY `name` ASC';
  101.         $rows = self::model()->getDbConnection()->createCommand($sql)->queryAll();
  102.  
  103.         if ($emptyItem === true) {
  104.             $result = array(null => '---');
  105.         } elseif (is_string($emptyItem)) {
  106.             $result = array(null => $emptyItem);
  107.         } else {
  108.             $result = array();
  109.         }
  110.  
  111.         foreach ($rows as $row) {
  112.             $result[$row['id']] = $row['name'];
  113.         }
  114.  
  115.         return $result;
  116.     }
  117.  
  118.     /**
  119.      * @param array $tags
  120.      *
  121.      * @return array(0=>conditions,1=>array(:params=>params))
  122.      */
  123.     public function proceedConditionTags(Array $tags, $aliasTable = 't') {
  124.  
  125.         $conditions[0] = '';
  126.         if (count($tags)) {
  127.             foreach ($tags as $tag) {
  128.                 $tagParam = substr(md5($tag), 0, rand(3, 6));
  129.                 $conditions[0] .= ($aliasTable ? $aliasTable . '.' : '') . "`tags` LIKE :$tagParam or ";
  130.                 $conditions[1][":$tagParam"] = "%$tag%";
  131.             }
  132.             if ($conditions[0] != '') {
  133.  
  134.                 $conditions[0] = chop($conditions[0], ' or ');
  135.             }
  136.         }
  137.  
  138.         return $conditions;
  139.     }
  140.  
  141.     /**
  142.      * @param array $categories
  143.      *
  144.      * @return string conditions
  145.      */
  146.     public function proceedConditionCategories(Array $categories, $aliasTable = 'category') {
  147.  
  148.         $ids = $conditions = '';
  149.         foreach ($categories as $category) {
  150.             $ids .= (int)$category . ',';
  151.         }
  152.         if ($ids != '') {
  153.             $ids = chop($ids, ',');
  154.         }
  155.  
  156.         if ($ids != '') {
  157.             $conditions = ($aliasTable ? $aliasTable . '.' : '') . "id in($ids)";
  158.         }
  159.  
  160.         return $conditions;
  161.     }
  162.  
  163.     public function beforeValidate() {
  164.  
  165.  
  166.         if ($this->getScenario() == 'getshop' && $this->isNewRecord) {
  167.  
  168.             $this->API_KEY = $this->getNewApiKey();
  169.             $this->date_create = date('Y-m-d H:i:s');
  170.             $this->customer_id = Yii::app()->user->id;
  171.  
  172.         }
  173.         if (!is_array($this->tags)) {
  174.             $this->tags = explode(',', $this->tags);
  175.             $trimTag = '';
  176.             foreach ($this->tags as $tag) {
  177.                 $trimTag .= trim($tag) . ',';
  178.             }
  179.             $this->tags = chop($trimTag, ',');
  180.         }
  181.  
  182.         return parent::beforeValidate();
  183.     }
  184.  
  185.     /**
  186.      * This is invoked after the record is saved.
  187.      */
  188.     protected function afterSave() {
  189.  
  190.         parent::afterSave();
  191.         Tag::model()->updateFrequency($this->_oldTags, $this->tags);
  192.     }
  193.  
  194.     /**
  195.      * @return array a list of links that point to the post list filtered by every tag of this post
  196.      */
  197.     public function getTagLinks() {
  198.  
  199.         $links = array();
  200.         foreach (Tag::string2array($this->tags) as $tag) {
  201.             $links[] = CHtml::link(CHtml::encode($tag), array('shop/index', 'tag' => $tag));
  202.         }
  203.  
  204.         return $links;
  205.     }
  206.  
  207.     /**
  208.      * Normalizes the user-entered tags.
  209.      */
  210.     public function normalizeTags($attribute, $params) {
  211.  
  212.         $this->tags = Tag::array2string(array_unique(Tag::string2array($this->tags)));
  213.     }
  214.  
  215.  
  216.     /**
  217.      * Проверка уникальности apikey
  218.      *
  219.      * @param $apikey
  220.      *
  221.      * @return bool
  222.      */
  223.     static public function isUniqueApikey($apikey) {
  224.  
  225.         $table = self::model()->tableName();
  226.         $query = "SELECT `API_KEY`, `id` FROM {$table} WHERE `API_KEY`=:apikey ";
  227.         $customer = Yii::app()->db
  228.             ->createCommand($query)
  229.             ->query(array(':apikey' => $apikey))
  230.             ->read();
  231.  
  232.         if (!empty($customer)) {
  233.             return false;
  234.         }
  235.  
  236.         return true;
  237.     }
  238.  
  239.  
  240.     /**
  241.      * Возвращает уникальный apikey
  242.      */
  243.     public function getNewApiKey() {
  244.  
  245.         $pass = $this->generateP();
  246.         while (!self::isUniqueApikey($pass)) {
  247.  
  248.             $pass = $this->generateP();
  249.         }
  250.  
  251.         return $pass;
  252.     }
  253.  
  254.     public function generateP() {
  255.  
  256.         $pass = '';
  257.         for ($i = 0; $i < 10; $i++) {
  258.             $pass .= chr(rand(40, 90));
  259.         }
  260.         $password = md5($pass);
  261.  
  262.         return $password;
  263.     }
  264.  
  265.     /**
  266.      * Проверка ip и API_KEY
  267.      *
  268.      * @param string $ip
  269.      * @param string $api_key
  270.      *
  271.      * @return bool
  272.      */
  273.     static public function checkIP($ip, $api_key) {
  274.  
  275.         $data = self::model()->find("ip=:ip and API_KEY=:apikey and active=1",
  276.             array(':ip' => $ip,
  277.                   ':apikey' => $api_key,
  278.             ));
  279.         if ($data) {
  280.             return true;
  281.         }
  282.  
  283.         return false;
  284.     }
  285.  
  286.     /**
  287.      * @param CDbCriteria $criteria
  288.      *
  289.      * Retrieves a list of models based on the current search/filter conditions.
  290.      *
  291.      * @return CActiveDataProvider the data provider that can return the models
  292.      * based on the search/filter conditions.
  293.      */
  294.     public function search() {
  295.  
  296.         $criteria = new CDbCriteria;
  297.  
  298.         $criteria->compare('id', $this->id);
  299.         $criteria->compare('customer_id', (int)Yii::app()->user->id);
  300.         $criteria->compare('category_id', $this->category_id);
  301.         $criteria->compare('name', $this->name, true);
  302.         $criteria->compare('ip', $this->ip, true);
  303.         $criteria->compare('active', $this->active);
  304.         $criteria->compare('style', $this->style, true);
  305.         $criteria->compare('tags', $this->tags, true);
  306.         $criteria->compare('date_create', $this->date_create, true);
  307.         $criteria->compare('API_KEY', $this->API_KEY, true);
  308.         $criteria->compare('url', $this->url, true);
  309.  
  310.         $criteria->order = 'id DESC';
  311.  
  312.         return new CActiveDataProvider($this, array(
  313.             'criteria' => $criteria,
  314.         ));
  315.     }
  316.  
  317.     public function filters(CDbCriteria $criteria) {
  318.  
  319.         $criteria->with = array('category');
  320.         $criteria->compare('active', 1);
  321.         $criteria->compare('tags', $this->tags, true);
  322.  
  323.         return new CActiveDataProvider($this, array(
  324.             'criteria' => $criteria,
  325.         ));
  326.     }
  327.  
  328.     /**
  329.      * Finds a single active record by apikey shop.
  330.      *
  331.      * @param string $apiKey
  332.      *
  333.      * @return Shops or NULL
  334.      */
  335.     public function findByApiKey($apiKey) {
  336.  
  337.         /** @var  $shop Shops */
  338.  
  339.         $shop = self::model()->find('API_KEY=:apikey', array(':apikey' => $apiKey));
  340.  
  341.         return $shop;
  342.     }
  343.  
  344.     /**
  345.      * Returns the static model of the specified AR class.
  346.      * Please note that you should have this exact method in all your CActiveRecord descendants!
  347.      *
  348.      * @param string $className active record class name.
  349.      *
  350.      * @return Shops the static model class
  351.      */
  352.     public static function model($className = __CLASS__) {
  353.  
  354.         return parent::model($className);
  355.     }
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement