<?php
/**
 * Created by JetBrains PhpStorm.
 * User: z_bodya
 * Date: 8/2/12
 * Time: 10:53 PM
 * To change this template use File | Settings | File Templates.
 */
class GalleryInput extends CWidget
{
    /**
     * @var CModel the data model associated with this widget.
     */
    public $model;
    /**
     * @var string the attribute associated with this widget.
     * The name can contain square brackets (e.g. 'name[1]') which is used to collect tabular data input.
     */
    public $attribute;
    /**
     * @var array additional HTML options to be rendered in the input tag
     */
    public $htmlOptions = array();
    /**
     * @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 boolean does images in gallery need links */
    public $link=false;

    public function run()
    {
        $name = CHtml::resolveName($this->model, $this->attribute);
        $value = $this->model->{$this->attribute};

        if (empty($value)) {
            $gallery = new Gallery();
            $gallery->name = $this->name;
            $gallery->description = $this->description;
            $gallery->link = $this->link;
            $gallery->versions = $this->versions;
            $gallery->save();

            $value = $gallery->id;
        } else {
            $gallery = Gallery::model()->findByPk((int)$value);
            $changed = false;
            if ($gallery->name != (bool)$this->name) {
                $changed = true;
            }
            if ($gallery->description != (bool)$this->description) {
                $changed = true;
            }
            if ($gallery->link != (bool)$this->link) {
                $changed = true;
            }
            if ($gallery->versions_data != serialize($this->versions)) {
                $changed = true;
            }
            if ($changed) {
                foreach ($gallery->galleryPhotos as $photo) {
                    $photo->removeImages();
                }

                $gallery->name = $this->name;
                $gallery->description = $this->description;
                $gallery->link = $this->link;
                $gallery->versions = $this->versions;

                $gallery->save();

                foreach ($gallery->galleryPhotos as $photo) {
                    $photo->updateImages();
                }
            }
        }
//        CVarDumper::dump($gallery,10,true);exit();

        echo CHtml::hiddenField($name, $value);
        $this->widget('GalleryManager', array(
            'gallery' => $gallery,
            'htmlOptions' => $this->htmlOptions,
        ));
    }
}
