Advertisement
pmtpenza22

Untitled

Feb 18th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\models;
  4. use kartik\file\FileInput;
  5. use Yii;
  6. use backend\models\ImagesManager;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\web\UploadedFile;
  11. use yii\helpers\FileHelper;
  12. use yii\helpers\Url;
  13. use yii\helpers\ArrayHelper;
  14. /**
  15. * This is the model class for table "product".
  16. *
  17. * @property int $id
  18. * @property string $name
  19. * @property int $status
  20. * @property string $description
  21. * @property string $price_roz
  22. * @property string $price_opt
  23. * @property int $hit
  24. * @property int $new
  25. * @property int $sale
  26. * @property string $image
  27. * @property int $category_id
  28. * @property int $meta_title
  29. * @property string $meta_keyword
  30. * @property string $meta_description
  31. */
  32. class Product extends \yii\db\ActiveRecord
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function tableName()
  38. {
  39. return 'product';
  40. }
  41.  
  42. public $file;
  43.  
  44. public function getCategory () {
  45.  
  46. return $this->hasOne (Cat::classname(),['id'=>'category_id'] );
  47. return $this->hashasMany (ImagesManager::classname(),['id'=>'product_id'] );
  48. }
  49.  
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function rules()
  54. {
  55. return [
  56. [['name', 'category_id'], 'required'],
  57. [['status', 'hit', 'new', 'sale', 'category_id'], 'integer'],
  58. [['price_roz', 'price_opt'], 'number'],
  59. [['name', 'description', 'meta_keyword', 'meta_description','meta_title','description',], 'string', 'max' => 255],
  60. [['file'],'image'],
  61. [['image'], 'string', 'max'=>100],
  62. ];
  63. }
  64.  
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function attributeLabels()
  69. {
  70. return [
  71. 'id' => 'ID',
  72. 'name' => 'Наименование',
  73. 'status' => 'Статус',
  74. 'description' => 'Описание',
  75. 'price_roz' => 'Цена розница',
  76. 'price_opt' => 'Цена опт',
  77. 'hit' => 'Хит',
  78. 'new' => 'Новинка',
  79. 'sale' => 'Акция',
  80. 'image' => 'Картинка',
  81. 'file'=>'Картинка',
  82. 'category_id' => 'Категории',
  83. 'meta_title' => 'Meta-заголовок',
  84. 'meta_keyword' => 'Ключевые слова',
  85. 'meta_description' => 'Meta-описание',
  86. ];
  87. }
  88.  
  89. /* public function beforeSave($insert){
  90. if ($file = UploadedFile::getInstance($this,'file')){
  91. $dir = Yii::getAlias('@images').'/product';
  92. if (file_exists($dir.$this->image)){
  93. unlink($dir.$this->image);
  94. }
  95. if(file_exists($dir.'50x50/'.$this->image)){
  96. unlink($dir.'50x50/'.$this->image);
  97. }
  98. if(file_exists($dir.'800x/'.$this->image)){
  99. unlink($dir.'800x/'.$this->image);
  100. }
  101. $this->image = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) .'.'.
  102. $file->extension;
  103. $file->saveAs($dir.$this->image);
  104. $imag=Yii::$app->image->load($dir.$this->image);
  105. $imag->background('#fff',0);
  106. $imag->resize('50','50', Yii\image\drivers\Image::INVERSE);
  107. $imag->crop('50','50');
  108. $imag->save($dir.'50x50/'.$this->image,90);
  109. $imag=Yii::$app->image->load($dir.$this->image);
  110. $imag->background('#fff',0);
  111. $imag->resize('800', null, Yii\image\drivers\Image::INVERSE);
  112. $imag->save($dir.'800x/'.$this->image,90);
  113. }
  114. return parent::beforeSave($insert);
  115. }*/
  116. public function getImages()
  117. {
  118. return $this->hasMany(ImagesManager::className(), ['product_id' => 'id'])->andWhere (['class'=>self::tableName()])->orderBy('sort');
  119. }
  120.  
  121. public function getImagesLinks()
  122. {
  123. return ArrayHelper::getColumn($this->images, 'imageUrl');
  124. }
  125.  
  126. public function getImagesLinksData ()
  127. {
  128. $arr = ArrayHelper::toArray($this->images,[
  129. ImagesManager::className()=>[
  130. 'caption'=>'name',
  131. 'key'=>'id',
  132. ]
  133. ]);
  134. return $arr;
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement