Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This is the model class for table "{{shops}}".
- *
- * The followings are the available columns in table '{{shops}}':
- *
- * @property integer $id
- * @property integer $customer_id
- * @property string $ip
- * @property integer $active
- * @property string $style
- * @property string $name
- * @property string $date_create
- * @property string $API_KEY
- * @property string $url
- */
- class Shops extends CActiveRecord {
- private $_oldTags;
- public $category_name;
- static public $labelActive = array(
- 1 => 'Active',
- 0 => 'Not active'
- );
- /**
- * @return string the associated database table name
- */
- public function tableName() {
- return '{{shops}}';
- }
- /**
- * @return array validation rules for model attributes.
- */
- public function rules() {
- // NOTE: you should only define rules for those attributes that
- // will receive user inputs.
- return array(
- array('customer_id, ip, API_KEY, url, name, category_id', 'required'),
- array('API_KEY', 'unique'),
- array('customer_id, active', 'numerical', 'integerOnly' => true),
- array('ip', 'length', 'max' => 60),
- array('style', 'length', 'max' => 45),
- array('API_KEY', 'length', 'max' => 250),
- array('url', 'length', 'max' => 600),
- array('url', 'url', 'defaultScheme' => 'http'),
- array('tags', 'safe'),
- array('tags', 'match', 'pattern' => '/^[\w\s,]+$/', 'message' => 'Tags can only contain word characters.'),
- array('url', 'url'),
- array('tags', 'normalizeTags'),
- array('API_KEY, date_create, customer_id', 'unsafe', 'on' => 'getshop'),
- array('id, customer_id, url, ip, active, category_id, style, date_create, API_KEY', 'safe', 'on' => 'search'),
- array('tags', 'safe', 'on' => 'filters'),
- );
- }
- /**
- * @return array relational rules.
- */
- public function relations() {
- return array(
- 'category' => array(self::BELONGS_TO, 'CategoryShop', 'category_id'),
- );
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public function attributeLabels() {
- return array(
- 'id' => 'ID',
- 'name' => 'Name',
- 'customer_id' => 'Customer',
- 'ip' => 'Ip',
- 'active' => 'Active',
- 'style' => 'Style',
- 'date_create' => 'Date Create',
- 'API_KEY' => 'Api Key',
- 'category_id' => 'Category',
- 'tags' => 'Tags',
- 'url' => 'Url'
- );
- }
- public function getListForCustomer($customer_id = null, $emptyItem = false) {
- $where = ' WHERE customer_id=' . (int)$customer_id;
- if ($customer_id === null) {
- $where = '';
- }
- $sql = 'SELECT id, `name` FROM '
- . self::model()->tableName()
- . $where . ' ORDER BY `name` ASC';
- $rows = self::model()->getDbConnection()->createCommand($sql)->queryAll();
- if ($emptyItem === true) {
- $result = array(null => '---');
- } elseif (is_string($emptyItem)) {
- $result = array(null => $emptyItem);
- } else {
- $result = array();
- }
- foreach ($rows as $row) {
- $result[$row['id']] = $row['name'];
- }
- return $result;
- }
- /**
- * @param array $tags
- *
- * @return array(0=>conditions,1=>array(:params=>params))
- */
- public function proceedConditionTags(Array $tags, $aliasTable = 't') {
- $conditions[0] = '';
- if (count($tags)) {
- foreach ($tags as $tag) {
- $tagParam = substr(md5($tag), 0, rand(3, 6));
- $conditions[0] .= ($aliasTable ? $aliasTable . '.' : '') . "`tags` LIKE :$tagParam or ";
- $conditions[1][":$tagParam"] = "%$tag%";
- }
- if ($conditions[0] != '') {
- $conditions[0] = chop($conditions[0], ' or ');
- }
- }
- return $conditions;
- }
- /**
- * @param array $categories
- *
- * @return string conditions
- */
- public function proceedConditionCategories(Array $categories, $aliasTable = 'category') {
- $ids = $conditions = '';
- foreach ($categories as $category) {
- $ids .= (int)$category . ',';
- }
- if ($ids != '') {
- $ids = chop($ids, ',');
- }
- if ($ids != '') {
- $conditions = ($aliasTable ? $aliasTable . '.' : '') . "id in($ids)";
- }
- return $conditions;
- }
- public function beforeValidate() {
- if ($this->getScenario() == 'getshop' && $this->isNewRecord) {
- $this->API_KEY = $this->getNewApiKey();
- $this->date_create = date('Y-m-d H:i:s');
- $this->customer_id = Yii::app()->user->id;
- }
- if (!is_array($this->tags)) {
- $this->tags = explode(',', $this->tags);
- $trimTag = '';
- foreach ($this->tags as $tag) {
- $trimTag .= trim($tag) . ',';
- }
- $this->tags = chop($trimTag, ',');
- }
- return parent::beforeValidate();
- }
- /**
- * This is invoked after the record is saved.
- */
- protected function afterSave() {
- parent::afterSave();
- Tag::model()->updateFrequency($this->_oldTags, $this->tags);
- }
- /**
- * @return array a list of links that point to the post list filtered by every tag of this post
- */
- public function getTagLinks() {
- $links = array();
- foreach (Tag::string2array($this->tags) as $tag) {
- $links[] = CHtml::link(CHtml::encode($tag), array('shop/index', 'tag' => $tag));
- }
- return $links;
- }
- /**
- * Normalizes the user-entered tags.
- */
- public function normalizeTags($attribute, $params) {
- $this->tags = Tag::array2string(array_unique(Tag::string2array($this->tags)));
- }
- /**
- * Проверка уникальности apikey
- *
- * @param $apikey
- *
- * @return bool
- */
- static public function isUniqueApikey($apikey) {
- $table = self::model()->tableName();
- $query = "SELECT `API_KEY`, `id` FROM {$table} WHERE `API_KEY`=:apikey ";
- $customer = Yii::app()->db
- ->createCommand($query)
- ->query(array(':apikey' => $apikey))
- ->read();
- if (!empty($customer)) {
- return false;
- }
- return true;
- }
- /**
- * Возвращает уникальный apikey
- */
- public function getNewApiKey() {
- $pass = $this->generateP();
- while (!self::isUniqueApikey($pass)) {
- $pass = $this->generateP();
- }
- return $pass;
- }
- public function generateP() {
- $pass = '';
- for ($i = 0; $i < 10; $i++) {
- $pass .= chr(rand(40, 90));
- }
- $password = md5($pass);
- return $password;
- }
- /**
- * Проверка ip и API_KEY
- *
- * @param string $ip
- * @param string $api_key
- *
- * @return bool
- */
- static public function checkIP($ip, $api_key) {
- $data = self::model()->find("ip=:ip and API_KEY=:apikey and active=1",
- array(':ip' => $ip,
- ':apikey' => $api_key,
- ));
- if ($data) {
- return true;
- }
- return false;
- }
- /**
- * @param CDbCriteria $criteria
- *
- * Retrieves a list of models based on the current search/filter conditions.
- *
- * @return CActiveDataProvider the data provider that can return the models
- * based on the search/filter conditions.
- */
- public function search() {
- $criteria = new CDbCriteria;
- $criteria->compare('id', $this->id);
- $criteria->compare('customer_id', (int)Yii::app()->user->id);
- $criteria->compare('category_id', $this->category_id);
- $criteria->compare('name', $this->name, true);
- $criteria->compare('ip', $this->ip, true);
- $criteria->compare('active', $this->active);
- $criteria->compare('style', $this->style, true);
- $criteria->compare('tags', $this->tags, true);
- $criteria->compare('date_create', $this->date_create, true);
- $criteria->compare('API_KEY', $this->API_KEY, true);
- $criteria->compare('url', $this->url, true);
- $criteria->order = 'id DESC';
- return new CActiveDataProvider($this, array(
- 'criteria' => $criteria,
- ));
- }
- public function filters(CDbCriteria $criteria) {
- $criteria->with = array('category');
- $criteria->compare('active', 1);
- $criteria->compare('tags', $this->tags, true);
- return new CActiveDataProvider($this, array(
- 'criteria' => $criteria,
- ));
- }
- /**
- * Finds a single active record by apikey shop.
- *
- * @param string $apiKey
- *
- * @return Shops or NULL
- */
- public function findByApiKey($apiKey) {
- /** @var $shop Shops */
- $shop = self::model()->find('API_KEY=:apikey', array(':apikey' => $apiKey));
- return $shop;
- }
- /**
- * 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 Shops the static model class
- */
- public static function model($className = __CLASS__) {
- return parent::model($className);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement