Advertisement
pmtpenza22

Untitled

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