Advertisement
pmtpenza22

Untitled

Feb 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 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. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return 'product';
  41. }
  42.  
  43. public $file;
  44.  
  45. public function getCategory () {
  46.  
  47. return $this->hasOne (Cat::classname(),['id'=>'category_id'] );
  48. return $this->hashasMany (ImagesManager::classname(),['id'=>'product_id'] );
  49.  
  50. }
  51.  
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function rules()
  56. {
  57. return [
  58. [['name', 'category_id'], 'required'],
  59. [['status', 'hit', 'new', 'sale', 'category_id'], 'integer'],
  60. [['price_roz', 'price_opt'], 'number'],
  61. [['name', 'description', 'meta_keyword', 'meta_description','meta_title','description',], 'string', 'max' => 255],
  62. [['file'],'image'],
  63. [['image'], 'string', 'max'=>100],
  64. ];
  65. }
  66.  
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function attributeLabels()
  71. {
  72. return [
  73. 'id' => 'ID',
  74. 'name' => 'Наименование',
  75. 'status' => 'Статус',
  76. 'description' => 'Описание',
  77. 'price_roz' => 'Цена розница',
  78. 'price_opt' => 'Цена опт',
  79. 'hit' => 'Хит',
  80. 'new' => 'Новинка',
  81. 'sale' => 'Акция',
  82. 'image' => 'Картинка',
  83. 'file'=>'Картинка',
  84. 'category_id' => 'Категории',
  85. 'meta_title' => 'Meta-заголовок',
  86. 'meta_keyword' => 'Ключевые слова',
  87. 'meta_description' => 'Meta-описание',
  88. ];
  89. }
  90.  
  91. /* public function beforeSave($insert){
  92. if ($file = UploadedFile::getInstance($this,'file')){
  93. $dir = Yii::getAlias('@images').'/product';
  94. if (file_exists($dir.$this->image)){
  95. unlink($dir.$this->image);
  96. }
  97. if(file_exists($dir.'50x50/'.$this->image)){
  98. unlink($dir.'50x50/'.$this->image);
  99. }
  100. if(file_exists($dir.'800x/'.$this->image)){
  101. unlink($dir.'800x/'.$this->image);
  102.  
  103. }
  104. $this->image = strtotime('now').'_'.Yii::$app->getSecurity()->generateRandomString(6) .'.'.
  105. $file->extension;
  106. $file->saveAs($dir.$this->image);
  107. $imag=Yii::$app->image->load($dir.$this->image);
  108. $imag->background('#fff',0);
  109. $imag->resize('50','50', Yii\image\drivers\Image::INVERSE);
  110. $imag->crop('50','50');
  111. $imag->save($dir.'50x50/'.$this->image,90);
  112. $imag=Yii::$app->image->load($dir.$this->image);
  113. $imag->background('#fff',0);
  114. $imag->resize('800', null, Yii\image\drivers\Image::INVERSE);
  115. $imag->save($dir.'800x/'.$this->image,90);
  116. }
  117. return parent::beforeSave($insert);
  118. }*/
  119. public function getMainImage()
  120. {
  121. return $this->hasOne(ImagesManager::className(), ['product_id' => 'id'])->orderBy(['sort'=>SORT_DESC]);
  122. }
  123.  
  124. public function getImagesLinks()
  125. {
  126. return ArrayHelper::getColumn($this->images, 'imageUrl');
  127. }
  128.  
  129. public function getImagesLinksData ()
  130. {
  131. $arr = ArrayHelper::toArray($this->images,[
  132. ImagesManager::className()=>[
  133. 'caption'=>'name',
  134. 'key'=>'id',
  135. ]
  136. ]);
  137. return $arr;
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement