Source

i18d-galleryManager / GalleryManager.php

Full commit
<?php
/**
 * Widget to manage gallery.
 * Requires Twitter Bootstrap styles to work.
 *
 * @author Bogdan Savluk <savluk.bogdan@gmail.com>
 */
class GalleryManager extends CWidget
{
    /** @var Gallery Model of gallery to manage */
    public $gallery;
    /** @var string Route to gallery controller */
    public $controllerRoute = false;
    public $assets;

    public function init()
    {
        $this->assets = Yii::app()->getAssetManager()->publish(dirname(__FILE__) . '/assets');
    }

    public $htmlOptions = array();

    /** Render widget */
    public function run()
    {
        /** @var $cs CClientScript */
        $cs = Yii::app()->clientScript;
        $cs->registerCssFile($this->assets . '/galleryManager.css');

        $cs->registerCoreScript('jquery');
        $cs->registerCoreScript('jquery.ui');

        if (defined('YII_DEBUG')) {
            $cs->registerScriptFile($this->assets . '/jquery.iframe-transport.js');
            $cs->registerScriptFile($this->assets . '/jquery.galleryManager.js');
        } else {
            $cs->registerScriptFile($this->assets . '/jquery.iframe-transport.min.js');
            $cs->registerScriptFile($this->assets . '/jquery.galleryManager.min.js');
        }

        if ($this->controllerRoute === null)
            throw new CException('$controllerRoute must be set.', 500);

        $photos = array();
        foreach ($this->gallery->galleryPhotos as $photo) {
            $i18ns = array();
            foreach (Yii::app()->params['languages'] as $lang) {
                $i18ns[$lang] = array(
                    'name' => (string)$photo->i18ns[$lang]->name,
                    'description' => (string)$photo->i18ns[$lang]->description,
                    'link' => (string)$photo->i18ns[$lang]->link,
                );
            }
            $photos[] = array(
                'id' => $photo->id,
                'rank' => $photo->rank,
                'i18ns' => $i18ns,
                'preview' => $photo->getPreview(),
            );
        }
        $opts = CJavaScript::encode(array(
            'lang' => Yii::app()->language,
            'langs' => Yii::app()->params['languages'],
            'hasName' => $this->gallery->name ? true : false,
            'hasDesc' => $this->gallery->description ? true : false,
            'hasLink' => $this->gallery->link ? true : false,
            'uploadUrl' => Yii::app()->createUrl($this->controllerRoute . '/ajaxUpload', array('gallery_id' => $this->gallery->id)),
            'deleteUrl' => Yii::app()->createUrl($this->controllerRoute . '/delete'),
            'updateUrl' => Yii::app()->createUrl($this->controllerRoute . '/changeData'),
            'arrangeUrl' => Yii::app()->createUrl($this->controllerRoute . '/order'),
            'nameLabel' => Yii::t('galleryManager.main', 'Name'),
            'descriptionLabel' => Yii::t('galleryManager.main', 'Description'),
            'linkLabel' => Yii::t('galleryManager.main', 'Link'),
            'photos' => $photos,
        ));
        $src = "$('#{$this->id}').galleryManager({$opts});";
        $cs->registerScript('galleryManager#' . $this->id, $src);
        $model = new GalleryPhoto();

        $cls = "GalleryEditor ";
        if (!($this->gallery->name)) $cls .= 'no-name';

        if (!($this->gallery->description)) {
            $cls .= (($cls != ' ') ? '-' : '') . 'no-desc';
        }
        if (isset($this->htmlOptions['class']))
            $this->htmlOptions['class'] .= ' ' . $cls;
        else
            $this->htmlOptions['class'] = $cls;
        $this->htmlOptions['id'] = $this->id;

        $this->render('galleryManager', array(
            'model' => $model,
        ));
    }

}