Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\models;
- use app\models\queries\PrintGoodsRelationQuery;
- use yii;
- use app\models\behaviors\Avatar;
- /**
- * This is the model class for table "{{%clothing_prints}}".
- *
- * @property integer $id
- * @property string $name
- * @property string $descriptions
- * @property integer $status
- * @property integer $timer
- * @property float $price
- * @property string $image
- *
- * @property Goods[] $goods
- */
- class Prints extends ExtendsActiveRecord {
- public $imageFile;
- /**
- * Список тэгов, закреплённых за постом.
- *
- * @var array
- */
- protected $goods = [];
- /**
- * @param $goodsId
- */
- public function setGoods($goodsId) {
- $this->goods = (array)$goodsId;
- }
- /**
- * Возвращает массив идентификаторов одежды.
- *
- * @return array
- */
- public function getGoodsIds() {
- return yii\helpers\ArrayHelper::getColumn(
- $this->getPrintGoods()->all(), 'goods_id'
- );
- }
- /**
- * Возвращает массив идентификаторов одежды.
- *
- * @return yii\db\ActiveRecord[] | null
- */
- public function getGoods() {
- $ids = $this->getGoodsIds();
- if (is_array($ids) && count($ids)) {
- $result = Goods::findAll($ids);
- return $result;
- }
- return [];
- }
- /**
- * @param queries\GoodsQuery $query
- *
- * @return queries\Goods[]|array
- */
- public function getGoodsByCriteria(queries\GoodsQuery $query) {
- $query->rightJoin(PrintGoodsRelation::tableName() . ' `pgr`', 'pgr.goods_id=gds.id');
- $query->andFilterWhere(['pgr.print_id' => $this->id]);
- return $query->all();
- }
- public function getPrintGoods() {
- return $this->hasMany(PrintGoodsRelation::className(), ['print_id' => 'id']);
- }
- /**
- * @inheritdoc
- */
- public static function tableName() {
- return '{{%clothing_prints}}';
- }
- /**
- * @inheritdoc
- */
- public function rules() {
- return [
- [['name'], 'required'],
- [['descriptions'], 'string'],
- [['status', 'timer'], 'integer'],
- [['name'], 'string', 'max' => 45],
- [['price'], 'number', 'numberPattern' => '/^[0-9]{1,12}(\.[0-9]{0,4})?$/'],
- [['imageFile', 'goods'], 'safe'],
- [['imageFile'], 'file', 'extensions' => 'png'],
- [['image'], 'string', 'max' => 300]
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels() {
- return [
- 'id' => Yii::t('app', 'ID'),
- 'name' => Yii::t('app', 'Name'),
- 'descriptions' => Yii::t('app', 'Descriptions'),
- 'status' => Yii::t('app', 'Status'),
- 'price' => Yii::t('app', 'Price'),
- 'image' => Yii::t('app', 'Image'),
- 'timer' => Yii::t('app', 'Timer'),
- 'imageFile' => Yii::t('app', 'Image file'),
- 'goods' => Yii::t('app', 'Goods'),
- ];
- }
- public static function find() {
- return new queries\PrintsQuery(get_called_class());
- }
- public function getPrice() {
- return $this->price;
- }
- public function getTimer() {
- return date('d.m.Y H:i', $this->timer);
- }
- /**
- * Возвращает путь к картинке
- *
- * @return null|string
- */
- public function getImageUrl() {
- if ($this->image) {
- return '/uploads/prints/' . $this->image;
- }
- return null;
- }
- /**
- * Возвращает timer в unix формате
- *
- * @return int
- */
- public function getUnixTimer() {
- $a = preg_replace("/[0-9]/", '', $this->timer);
- if ($a != '') {
- return strtotime($this->timer);
- }
- return $this->timer;
- }
- /**
- * Возвращает путь сохранения принтов
- *
- * @return string
- */
- public function getSavePath() {
- return Yii::$app->basePath . '/public_html/uploads/prints/';
- }
- public function behaviors() {
- return [
- 'prints' => [
- 'class' => 'app\models\behaviors\Avatar',
- 'inAttribute' => 'image',
- 'inAttributeUploadImage' => 'imageFile'
- ]
- ];
- }
- /**
- * Возвращает актуален ли предзаказ принта
- *
- * @return bool
- */
- public function isActualPreOrder() {
- return ($this->getUnixTimer() > time()) ? true : false;
- }
- public function beforeValidate() {
- $this->timer = $this->getUnixTimer();
- return parent::beforeValidate();
- }
- public function afterSave($insert, $changedAttributes) {
- PrintGoodsRelation::deleteAll(['print_id' => $this->id]);
- $values = [];
- foreach ($this->goods as $id) {
- $values[] = [$this->id, $id];
- }
- self::getDb()->createCommand()
- ->batchInsert(PrintGoodsRelation::tableName(), ['print_id', 'goods_id'], $values)->execute();
- parent::afterSave($insert, $changedAttributes);
- }
- public function afterDelete() {
- PrintGoodsRelation::deleteAll(['print_id' => $this->id]);
- parent::afterDelete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement