Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace backend\models;
- use kartik\file\FileInput;
- use Yii;
- use backend\models\ImagesManager;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\web\UploadedFile;
- use yii\helpers\FileHelper;
- use yii\helpers\Url;
- use yii\helpers\ArrayHelper;
- /**
- * This is the model class for table "product".
- *
- * @property int $id
- * @property string $name
- * @property int $status
- * @property string $description
- * @property string $price_roz
- * @property string $price_opt
- * @property int $hit
- * @property int $new
- * @property int $sale
- * @property string $image
- * @property int $category_id
- * @property int $meta_title
- * @property string $meta_keyword
- * @property string $meta_description
- */
- class Product extends \yii\db\ActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return 'product';
- }
- public $file;
- public function getCategory () {
- return $this->hasOne (Cat::classname(),['id'=>'category_id'] );
- return $this->hashasMany (ImagesManager::classname(),['id'=>'product_id'] );
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['name', 'category_id'], 'required'],
- [['status', 'hit', 'new', 'sale', 'category_id'], 'integer'],
- [['price_roz', 'price_opt'], 'number'],
- [['name', 'description', 'meta_keyword', 'meta_description','meta_title','description',], 'string', 'max' => 255],
- [['file'],'image'],
- [['image'], 'string', 'max'=>100],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => 'Наименование',
- 'status' => 'Статус',
- 'description' => 'Описание',
- 'price_roz' => 'Цена розница',
- 'price_opt' => 'Цена опт',
- 'hit' => 'Хит',
- 'new' => 'Новинка',
- 'sale' => 'Акция',
- 'image' => 'Картинка',
- 'file'=>'Картинка',
- 'category_id' => 'Категории',
- 'meta_title' => 'Meta-заголовок',
- 'meta_keyword' => 'Ключевые слова',
- 'meta_description' => 'Meta-описание',
- ];
- }
- /* public function beforeSave($insert){
- if ($file = UploadedFile::getInstance($this,'file')){
- $dir = Yii::getAlias('@images').'/product';
- if (file_exists($dir.$this->image)){
- unlink($dir.$this->image);
- }
- if(file_exists($dir.'50x50/'.$this->image)){
- unlink($dir.'50x50/'.$this->image);
- }
- if(file_exists($dir.'800x/'.$this->image)){
- unlink($dir.'800x/'.$this->image);
- }
- $this->image = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) .'.'.
- $file->extension;
- $file->saveAs($dir.$this->image);
- $imag=Yii::$app->image->load($dir.$this->image);
- $imag->background('#fff',0);
- $imag->resize('50','50', Yii\image\drivers\Image::INVERSE);
- $imag->crop('50','50');
- $imag->save($dir.'50x50/'.$this->image,90);
- $imag=Yii::$app->image->load($dir.$this->image);
- $imag->background('#fff',0);
- $imag->resize('800', null, Yii\image\drivers\Image::INVERSE);
- $imag->save($dir.'800x/'.$this->image,90);
- }
- return parent::beforeSave($insert);
- }*/
- public function getMainImage()
- {
- return $this->hasOne(ImagesManager::className(), ['product_id' => 'id'])->orderBy(['sort'=>SORT_DESC]);
- }
- public function getImagesLinks()
- {
- return ArrayHelper::getColumn($this->images, 'imageUrl');
- }
- public function getImagesLinksData ()
- {
- $arr = ArrayHelper::toArray($this->images,[
- ImagesManager::className()=>[
- 'caption'=>'name',
- 'key'=>'id',
- ]
- ]);
- return $arr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement