forked from compleatguru/yii2-gallery-manager
-
Notifications
You must be signed in to change notification settings - Fork 2
/
GalleryManager.php
executable file
·103 lines (86 loc) · 2.73 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
103
<?php
namespace dlds\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 $sort = SORT_ASC;
public $prepend = false;
public $view;
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' => '@dlds/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($this->sort) as $image)
{
$images[] = array(
'id' => $image->id,
'rank' => $image->rank,
'preview' => $image->getUrl('preview'),
);
}
$baseUrl = [
$this->apiRoute,
'type' => $this->behavior->type,
'behaviorName' => $this->behaviorName,
'galleryId' => $this->model->getPrimaryKey()
];
$opts = array(
'uploadUrl' => Url::to($baseUrl + ['action' => 'ajaxUpload']),
'deleteUrl' => Url::to($baseUrl + ['action' => 'delete']),
'updateUrl' => Url::to($baseUrl + ['action' => 'changeData']),
'arrangeUrl' => Url::to($baseUrl + ['action' => 'order']),
'photos' => $images,
'prepend' => $this->prepend,
);
$opts = Json::encode($opts);
$view = $this->getView();
GalleryManagerAsset::register($view);
$view->registerJs("(function(){\$('#{$this->id}').galleryManager({$opts});})();", \yii\web\View::POS_READY);
$this->options['id'] = $this->id;
$this->options['class'] = 'gallery-manager';
if ($this->view)
{
return $this->render($this->view);
}
return $this->render('galleryManager');
}
}