Catalog.php
3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace app\modules\admin\models;
use Yii;
use app\components\Translite;
use app\components\resize;
use yii\web\UploadedFile;
class Catalog extends \yii\db\ActiveRecord
{
public $old_image;
public static function tableName()
{
return 'catalog';
}
public function rules()
{
return [
[['name'], 'required'],
[['parent_id','sort','old_image','body','translit','meta_title','meta_keywords','meta_description',
'fasovka_name','type_name','brends_name'], 'safe'],
[['image'], 'file', 'extensions'=>'jpg, gif, png', 'skipOnEmpty'=>true],
];
}
public function attributeLabels()
{
return [
'name'=>'Название',
'body'=>'Описание',
'sort'=>'Сорт.',
'parent_id'=>'Родитель',
'image'=>'Изображения',
'fasovka_name'=>'Название в место фасовки',
'type_name'=>'Название в место типа',
'brends_name'=>'Название в место производителя',
];
}
public function beforeSave($insert) {
if(empty($this->parent_id))$this->parent_id = 0;
if (!$this->translit)
$this->translit = Translite::rusencode($this->name);
if($image = UploadedFile::getInstance($this,'image')){
$this->deleteImage($this->old_image);
//$this->image = $image;
$this->image = time() . '_' . rand(1, 1000) . '.' . $image->extension;
$image->saveAs('upload/catalog/'.$this->image);
$resizeObj = new resize('upload/catalog/'.$this->image);
$resizeObj -> resizeImage(195, 186, 'crop');
$resizeObj -> saveImage('upload/catalog/ico/'.$this->image, 100);
}else $this->image = $this->old_image;
return parent::beforeSave($insert);
}
public function beforeDelete() {
$this->deleteImage($this->image);
return parent::beforeDelete();
}
public function deleteImage($file){
if(!empty($file)){
@unlink('upload/catalog/'.$file);
@unlink('upload/catalog/ico/'.$file);
}
}
public function getDataTree($type = 'list',$parent_id = 0,$level = -1,&$list = array())
{
//global $key;
//print_r($arr);
$res = Catalog::find()->where(
'parent_id=:parent_id',
[':parent_id'=>$parent_id]
)->orderBy('id')->all();
//print_r($res);exit;
$level++;
foreach($res as $row){
//$row->level = $level;
$row->name = str_repeat("---", $level) . $row->name;
$list[] = $row;
$this->getDataTree($type,$row->id,$level, $list);
}
return $list ;
}
public function getCatFasovka(){
return (!empty($this->fasovka_name)) ? $this->fasovka_name : 'Фасовка';
}
public function getCatType(){
return (!empty($this->type_name)) ? $this->type_name : 'Тип';
}
public function getCatBrends(){
return (!empty($this->brends_name)) ? $this->brends_name : 'Производители';
}
}