forked from zxbodya/yii2-gallery-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGalleryBehavior.php
149 lines (128 loc) · 4.61 KB
/
GalleryBehavior.php
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace zxbodya\yii2\galleryManager;
/**
* Behavior for adding gallery to any model.
*
* @author Bogdan Savluk <[email protected]>
*/
class GalleryBehavior extends CActiveRecordBehavior
{
/** @var string Model attribute name to store created gallery id */
public $idAttribute;
/**
* @var array Settings for image auto-generation
* @example
* array(
* 'small' => array(
* 'resize' => array(200, null),
* ),
* 'medium' => array(
* 'resize' => array(800, null),
* )
* );
*/
public $versions;
/** @var boolean does images in gallery need names */
public $name;
/** @var boolean does images in gallery need descriptions */
public $description;
/** @var string Extensions for gallery images */
public $extension = 'jpg';
private $_gallery;
/** Will create new gallery after save if no associated gallery exists */
public function beforeSave($event)
{
parent::beforeSave($event);
if ($event->isValid) {
if (empty($this->getOwner()->{$this->idAttribute})) {
$gallery = new Gallery();
$gallery->name = $this->name;
$gallery->description = $this->description;
$gallery->versions = $this->versions;
$gallery->extension = $this->extension;
$gallery->save();
$this->getOwner()->{$this->idAttribute} = $gallery->id;
} else {
Yii::log(
'Gallery configuration change in web-worker process, this should be in migrations',
CLogger::LEVEL_WARNING
);
$this->changeConfig();
}
}
}
/** Will remove associated Gallery before object removal */
public function beforeDelete($event)
{
$gallery = $this->getGallery();
if ($gallery !== null) {
$gallery->delete();
}
parent::beforeDelete($event);
}
/** Method for changing gallery configuration and regeneration of images versions */
public function changeConfig($force = false)
{
$gallery = $this->getGallery();
if ($gallery == null) {
return;
}
$gallery->name = $this->name;
$gallery->description = $this->description;
if ($gallery->versions_data != serialize(
$this->versions
) || $force || $gallery->extension != $this->extension
) {
foreach ($gallery->galleryPhotos as $photo) {
$photo->removeImages();
}
if ($gallery->extension != $this->extension) {
foreach ($gallery->galleryPhotos as $photo) {
$photo->changeExtension($gallery->extension, $this->extension);
}
$gallery->extension = $this->extension;
}
$gallery->versions = $this->versions;
$gallery->save();
$gallery = Gallery::model()->findByPk($gallery->id);
foreach ($gallery->galleryPhotos as $photo) {
$photo->updateImages();
}
}
$gallery->save();
}
/** @return Gallery Returns gallery associated with model */
public function getGallery()
{
if (empty($this->_gallery)) {
$this->_gallery = Gallery::model()->findByPk($this->getOwner()->{$this->idAttribute});
}
return $this->_gallery;
}
/** @return GalleryPhoto[] Photos from associated gallery */
public function getPhotos()
{
$criteria = new CDbCriteria();
$criteria->condition = 'gallery_id = :gallery_id';
$criteria->params[':gallery_id'] = $this->getOwner()->{$this->idAttribute};
$criteria->order = '`rank` asc';
return GalleryPhoto::model()->findAll($criteria);
}
/** @return GalleryPhoto[] Photos from associated gallery */
public function getFirstPhoto()
{
$criteria = new CDbCriteria();
$criteria->condition = 'gallery_id = :gallery_id';
$criteria->params[':gallery_id'] = $this->getOwner()->{$this->idAttribute};
$criteria->order = '`rank` asc';
return GalleryPhoto::model()->find($criteria);
}
/** @return GalleryPhoto[] Photos from associated gallery */
public function getPhotoCount()
{
$criteria = new CDbCriteria();
$criteria->condition = 'gallery_id = :gallery_id';
$criteria->params[':gallery_id'] = $this->getOwner()->{$this->idAttribute};
return GalleryPhoto::model()->count($criteria);
}
}