Advertisement
stixlink

yii2 model

Oct 27th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5.  
  6. use app\models\queries\PrintGoodsRelationQuery;
  7. use yii;
  8. use app\models\behaviors\Avatar;
  9.  
  10. /**
  11.  * This is the model class for table "{{%clothing_prints}}".
  12.  *
  13.  * @property integer $id
  14.  * @property string  $name
  15.  * @property string  $descriptions
  16.  * @property integer $status
  17.  * @property integer $timer
  18.  * @property float   $price
  19.  * @property string  $image
  20.  *
  21.  * @property Goods[] $goods
  22.  */
  23. class Prints extends ExtendsActiveRecord {
  24.  
  25.     public $imageFile;
  26.     /**
  27.      * Список тэгов, закреплённых за постом.
  28.      *
  29.      * @var array
  30.      */
  31.     protected $goods = [];
  32.  
  33.     /**
  34.      * @param $goodsId
  35.      */
  36.     public function setGoods($goodsId) {
  37.  
  38.         $this->goods = (array)$goodsId;
  39.     }
  40.  
  41.     /**
  42.      * Возвращает массив идентификаторов одежды.
  43.      *
  44.      * @return array
  45.      */
  46.     public function getGoodsIds() {
  47.  
  48.         return yii\helpers\ArrayHelper::getColumn(
  49.             $this->getPrintGoods()->all(), 'goods_id'
  50.         );
  51.     }
  52.  
  53.     /**
  54.      * Возвращает массив идентификаторов одежды.
  55.      *
  56.      * @return yii\db\ActiveRecord[] | null
  57.      */
  58.     public function getGoods() {
  59.  
  60.         $ids = $this->getGoodsIds();
  61.         if (is_array($ids) && count($ids)) {
  62.             $result = Goods::findAll($ids);
  63.  
  64.             return $result;
  65.         }
  66.  
  67.         return [];
  68.  
  69.     }
  70.  
  71.     /**
  72.      * @param queries\GoodsQuery $query
  73.      *
  74.      * @return queries\Goods[]|array
  75.      */
  76.     public function getGoodsByCriteria(queries\GoodsQuery $query) {
  77.  
  78.         $query->rightJoin(PrintGoodsRelation::tableName() . ' `pgr`', 'pgr.goods_id=gds.id');
  79.  
  80.         $query->andFilterWhere(['pgr.print_id' => $this->id]);
  81.  
  82.         return $query->all();
  83.     }
  84.  
  85.     public function getPrintGoods() {
  86.  
  87.         return $this->hasMany(PrintGoodsRelation::className(), ['print_id' => 'id']);
  88.     }
  89.  
  90.     /**
  91.      * @inheritdoc
  92.      */
  93.     public static function tableName() {
  94.  
  95.         return '{{%clothing_prints}}';
  96.     }
  97.  
  98.     /**
  99.      * @inheritdoc
  100.      */
  101.     public function rules() {
  102.  
  103.         return [
  104.             [['name'], 'required'],
  105.             [['descriptions'], 'string'],
  106.             [['status', 'timer'], 'integer'],
  107.             [['name'], 'string', 'max' => 45],
  108.             [['price'], 'number', 'numberPattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'],
  109.             [['imageFile', 'goods'], 'safe'],
  110.             [['imageFile'], 'file', 'extensions' => 'png'],
  111.             [['image'], 'string', 'max' => 300]
  112.         ];
  113.     }
  114.  
  115.     /**
  116.      * @inheritdoc
  117.      */
  118.     public function attributeLabels() {
  119.  
  120.         return [
  121.             'id' => Yii::t('app', 'ID'),
  122.             'name' => Yii::t('app', 'Name'),
  123.             'descriptions' => Yii::t('app', 'Descriptions'),
  124.             'status' => Yii::t('app', 'Status'),
  125.             'price' => Yii::t('app', 'Price'),
  126.             'image' => Yii::t('app', 'Image'),
  127.             'timer' => Yii::t('app', 'Timer'),
  128.             'imageFile' => Yii::t('app', 'Image file'),
  129.             'goods' => Yii::t('app', 'Goods'),
  130.         ];
  131.     }
  132.  
  133.  
  134.     public static function find() {
  135.  
  136.         return new queries\PrintsQuery(get_called_class());
  137.     }
  138.  
  139.  
  140.     public function getPrice() {
  141.  
  142.         return $this->price;
  143.     }
  144.  
  145.     public function getTimer() {
  146.  
  147.         return date('d.m.Y H:i', $this->timer);
  148.     }
  149.  
  150.     /**
  151.      * Возвращает путь к картинке
  152.      *
  153.      * @return null|string
  154.      */
  155.     public function getImageUrl() {
  156.  
  157.         if ($this->image) {
  158.             return '/uploads/prints/' . $this->image;
  159.         }
  160.  
  161.         return null;
  162.     }
  163.  
  164.     /**
  165.      * Возвращает timer в unix формате
  166.      *
  167.      * @return int
  168.      */
  169.     public function getUnixTimer() {
  170.  
  171.         $a = preg_replace("/[0-9]/", '', $this->timer);
  172.  
  173.         if ($a != '') {
  174.             return strtotime($this->timer);
  175.         }
  176.  
  177.         return $this->timer;
  178.     }
  179.  
  180.     /**
  181.      * Возвращает путь сохранения принтов
  182.      *
  183.      * @return string
  184.      */
  185.     public function getSavePath() {
  186.  
  187.         return Yii::$app->basePath . '/public_html/uploads/prints/';
  188.     }
  189.  
  190.     public function behaviors() {
  191.  
  192.         return [
  193.             'prints' => [
  194.                 'class' => 'app\models\behaviors\Avatar',
  195.                 'inAttribute' => 'image',
  196.                 'inAttributeUploadImage' => 'imageFile'
  197.             ]
  198.         ];
  199.     }
  200.  
  201.     /**
  202.      * Возвращает актуален ли предзаказ принта
  203.      *
  204.      * @return bool
  205.      */
  206.     public function isActualPreOrder() {
  207.  
  208.         return ($this->getUnixTimer() > time()) ? true : false;
  209.     }
  210.  
  211.     public function beforeValidate() {
  212.  
  213.         $this->timer = $this->getUnixTimer();
  214.  
  215.         return parent::beforeValidate();
  216.     }
  217.  
  218.     public function afterSave($insert, $changedAttributes) {
  219.  
  220.         PrintGoodsRelation::deleteAll(['print_id' => $this->id]);
  221.         $values = [];
  222.         foreach ($this->goods as $id) {
  223.             $values[] = [$this->id, $id];
  224.         }
  225.         self::getDb()->createCommand()
  226.             ->batchInsert(PrintGoodsRelation::tableName(), ['print_id', 'goods_id'], $values)->execute();
  227.  
  228.         parent::afterSave($insert, $changedAttributes);
  229.     }
  230.  
  231.     public function afterDelete() {
  232.  
  233.         PrintGoodsRelation::deleteAll(['print_id' => $this->id]);
  234.  
  235.         parent::afterDelete();
  236.     }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement