GallaryManager.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\forks\galleryManager;
  3. use Yii;
  4. use yii\base\Exception;
  5. use yii\helpers\Json;
  6. use yii\helpers\Url;
  7. use zxbodya\yii2\galleryManager\GalleryManager;
  8. use app\assets\GalleryManagerAsset;
  9. class GallaryManager extends GalleryManager
  10. {
  11. /** Render widget */
  12. public function run()
  13. {
  14. if ($this->apiRoute === null) {
  15. throw new Exception('$apiRoute must be set.', 500);
  16. }
  17. $images = array();
  18. foreach ($this->behavior->getImages() as $image) {
  19. $images[] = array(
  20. 'id' => $image->id,
  21. 'rank' => $image->rank,
  22. 'name' => (string)$image->name,
  23. 'description' => (string)$image->description,
  24. 'preview' => $image->getUrl('preview'),
  25. );
  26. }
  27. $baseUrl = [
  28. $this->apiRoute,
  29. 'type' => $this->behavior->type,
  30. 'behaviorName' => $this->behaviorName,
  31. 'galleryId' => $this->behavior->getGalleryId()
  32. ];
  33. $opts = array(
  34. 'hasName' => $this->behavior->hasName ? true : false,
  35. 'hasDesc' => $this->behavior->hasDescription ? true : false,
  36. 'uploadUrl' => Url::to($baseUrl + ['action' => 'ajaxUpload']),
  37. 'deleteUrl' => Url::to($baseUrl + ['action' => 'delete']),
  38. 'updateUrl' => Url::to($baseUrl + ['action' => 'changeData']),
  39. 'arrangeUrl' => Url::to($baseUrl + ['action' => 'order']),
  40. 'nameLabel' => Yii::t('galleryManager/main', 'Name'),
  41. 'descriptionLabel' => Yii::t('galleryManager/main', 'Description'),
  42. 'photos' => $images,
  43. );
  44. $opts = Json::encode($opts);
  45. $view = $this->getView();
  46. GalleryManagerAsset::register($view);
  47. $js = "$('#{$this->id}').galleryManager({$opts});";
  48. $view->registerJs($js);
  49. $this->options['id'] = $this->id;
  50. $this->options['class'] = 'gallery-manager';
  51. return $this->render('galleryManager',["js"=>$js]);
  52. }
  53. }