Advertisement
pmtpenza22

Untitled

Feb 15th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace backend\models;
  4. use \yii\helpers\ArrayHelper;
  5. use yii\helpers\Url;
  6.  
  7. use Yii;
  8.  
  9. /**
  10. * This is the model class for table "images_manager".
  11. *
  12. * @property int $id
  13. * @property string $name
  14. * @property string $class
  15. * @property int $product_id
  16. * @property string $alt
  17. * @property string $sort
  18. */
  19. class ImagesManager extends \yii\db\ActiveRecord
  20. {
  21. public $attachment;
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return 'images_manager';
  28. }
  29.  
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['product_id'], 'required'],
  37. [['product_id', 'sort'], 'integer'],
  38. [['name', 'class'], 'string', 'max' => 250],
  39. [['alt'], 'string', 'max' => 50],
  40. [['attachment'], 'image'],
  41. [['sort'], 'default', 'value'=>function($model){
  42. $count = ImagesManager::find()->andWhere
  43. (['class'=>$model->class])->count();
  44. return ($count > 0)?$count++:0;
  45. }]
  46. ];
  47. }
  48.  
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'name' => 'Name',
  57. 'class' => 'Class',
  58. 'product_id' => 'Product ID',
  59. 'alt' => 'Alt',
  60. ];
  61. }
  62. public function getImageUrl()
  63. {
  64. if($this->name){
  65. $path = 'http://localhost/mobile-go/uploads/images/'.$this->name;
  66. }else{
  67. $path = str_replace('admin.','',Url::home(true)).'uploads/images/nophoto.svg';
  68. }
  69. return $path;
  70. }
  71. public function beforeDelete(){
  72. if (parent::beforeDelete()) {
  73. ImagesManager::updateAllCounters(['sort' => -1], ['and', ['class' => 'Product', 'product_id' =>$this->product_id],['>','sort',$this->sort]]);
  74. return true;
  75. } else {
  76. return false;
  77. }
  78.  
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement