-
Notifications
You must be signed in to change notification settings - Fork 61
/
GalleryManager.php
102 lines (83 loc) · 2.86 KB
/
GalleryManager.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
<?php
namespace zxbodya\yii2\galleryManager;
use Yii;
use yii\base\Exception;
use yii\base\Widget;
use yii\db\ActiveRecord;
use yii\helpers\Json;
use yii\helpers\Url;
/**
* Widget to manage gallery.
* Requires Twitter Bootstrap styles to work.
*
* @author Bogdan Savluk <[email protected]>
*/
class GalleryManager extends Widget
{
/** @var ActiveRecord */
public $model;
/** @var string */
public $behaviorName;
/** @var GalleryBehavior Model of gallery to manage */
protected $behavior;
/** @var string Route to gallery controller */
public $apiRoute = false;
public $options = array();
public function init()
{
parent::init();
$this->behavior = $this->model->getBehavior($this->behaviorName);
$this->registerTranslations();
}
public function registerTranslations()
{
$i18n = Yii::$app->i18n;
$i18n->translations['galleryManager/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@zxbodya/yii2/galleryManager/messages',
'fileMap' => [],
];
}
/** Render widget */
public function run()
{
if ($this->apiRoute === null) {
throw new Exception('$apiRoute must be set.', 500);
}
$images = array();
foreach ($this->behavior->getImages() as $image) {
$images[] = array(
'id' => $image->id,
'rank' => $image->rank,
'name' => (string)$image->name,
'description' => (string)$image->description,
'preview' => $image->getUrl('preview'),
);
}
$baseUrl = [
$this->apiRoute,
'type' => $this->behavior->type,
'behaviorName' => $this->behaviorName,
'galleryId' => $this->behavior->getGalleryId()
];
$opts = array(
'hasName' => $this->behavior->hasName ? true : false,
'hasDesc' => $this->behavior->hasDescription ? true : false,
'uploadUrl' => Url::to($baseUrl + ['action' => 'ajaxUpload']),
'deleteUrl' => Url::to($baseUrl + ['action' => 'delete']),
'updateUrl' => Url::to($baseUrl + ['action' => 'changeData']),
'arrangeUrl' => Url::to($baseUrl + ['action' => 'order']),
'nameLabel' => Yii::t('galleryManager/main', 'Name'),
'descriptionLabel' => Yii::t('galleryManager/main', 'Description'),
'photos' => $images,
);
$opts = Json::encode($opts);
$view = $this->getView();
GalleryManagerAsset::register($view);
$view->registerJs("$('#{$this->id}').galleryManager({$opts});");
$this->options['id'] = $this->id;
$this->options['class'] = 'gallery-manager';
return $this->render('galleryManager');
}
}