Album.php 6.51 KB
<?php

/**
 * This is the model class for table "album".
 *
 * The followings are the available columns in table 'album':
 * @property integer $id
 * @property string $link
 * @property string $date
 * @property integer $photo_gallery_id
 * @property integer $video_gallery_id
 * @property integer $albums_section_id
 * @property integer $hidden
 * @property string $code
 *
 * The followings are the available model relations:
 * @property AlbumI18n[] $i18ns
 * @property AlbumI18n $i18n
 *
 * @property ImageARBehavior $imageBehavior
 * @property GalleryBehavior  $videoGalleryBehavior
 * @property GalleryBehavior  $photoGalleryBehavior
 */
class Album extends CActiveRecord implements INodeCrudModel
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Album the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'album';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('photo_gallery_id, video_gallery_id', 'numerical', 'integerOnly' => true),
            array('dateRu, link', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, date, photo_gallery_id, video_gallery_id', 'safe', 'on' => 'search'),
            array('hidden, code', 'safe'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'i18ns' => array(self::HAS_MANY, 'AlbumI18n', 'id', 'index' => 'lang'),
            'i18n' => array(self::HAS_ONE, 'AlbumI18n', 'id', 'condition' => 'lang=\'' . Yii::app()->language . '\''),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'link' => 'Псевдоним в адресной строке',
            'date' => 'Дата',
            'dateRu' => 'Дата',
            'image' => 'Обложка альбома',
            'photo_gallery_id' => 'Фото галерея',
            'video_gallery_id' => 'Видео галерея',
            'hidden' => 'Страница скрыта',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

////        $criteria->compare('id', $this->id);
        $criteria->compare('albums_section_id', $this->albums_section_id);
        $criteria->compare('date', $this->date, true);
        $criteria->compare('photo_gallery_id', $this->photo_gallery_id);
        $criteria->compare('video_gallery_id', $this->video_gallery_id);

        $sort = new CSort();
        $sort->defaultOrder = array(
            'date' => true,
        );

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'sort' => $sort,
        ));
    }

    public function save($runValidation = true, $attributes = null)
    {
        $r = parent::save($runValidation, $attributes);
        if (empty($this->link)) {
            $this->link = $this->id;
            $this->saveAttributes($this->attributes);
        }
        return $r;
    }


    public function getDateRu()
    {
        return ($this->date !== null)
            ? date('d.m.Y', CDateTimeParser::parse($this->date, 'yyyy-MM-dd'))
            : date('d.m.Y');
    }

    public function setDateRu($value)
    {
        $this->date = date('Y-m-d', CDateTimeParser::parse($value, 'dd.MM.yyyy'));
    }

    public $image;

    public function behaviors()
    {
        return array(
            'imageBehavior' => array(
                'class' => 'ImageARBehavior',
                'attribute' => 'image', // this must exist
                'extension' => 'png, gif, jpg, jpeg', // possible extensions, comma separated
                'prefix' => 'img_',
                'relativeWebRootFolder' => 'images/albums', // this folder must exist

//                'forceExt' => 'png',
                'formats' => array(
                    'preview' => array(
                        'suffix' => '_medium',
                        'process' => array(
                            'centeredpreview' => array(168, 126),
                        ),
                    ),
                    'normal' => array(),
                ),

                'defaultName' => 'default',
            ),
            'photoGalleryBehavior' => array(
                'class' => 'GalleryBehavior',
                'idAttribute' => 'photo_gallery_id',
                'versions' => array(
                    'small' => array(
                        'centeredpreview' => array(168, 126),
                    ),
                    'medium' => array(
                        'cresize' => array(800, null),
                        'watermark' => array('images/watermark.png', 0, 0),
                    )
                ),
                'name' => true,
                'description' => true,
            ),
            'videoGalleryBehavior' => array(
                'class' => 'GalleryBehavior',
                'idAttribute' => 'video_gallery_id',
                'versions' => array(
                    'small' => array(
                        'centeredpreview' => array(168, 126),
                    ),
                    'medium' => array(
                        'cresize' => array(800, null),
                    )
                ),
                'name' => true,
                'description' => false,
                'link' => true,
            ),
        );
    }

    /**
     * @param Node $node
     * @return void
     */
    public function setNode($node)
    {
        $this->albums_section_id = $node->data_id;
    }
}