<?php

class AlbumController extends Controller {

    /**
     * Displays a particular model.
     * @param integer $slug the ID of the model to be displayed
     */
    public function actionView($slug) {
        $this->render('view', array(
            'model' => $this->loadModel($slug),
        ));
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {

        $config = array(
            'pagination' => array(
                'pageSize' => 6
            ),
            'criteria' => new CDbCriteria(array('scopes' => 'published')),
        );

        $dataProvider = new CActiveDataProvider('Album', $config);
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionCategory($slug) {
        $category = $this->loadCategory($slug);

        $criteria = new CDbCriteria();
        $criteria->compare('category_id', $category->id);
        $dataProvider = new CActiveDataProvider('Album', array(
            'criteria' => $criteria,
        ));
        $this->render('category', array(
            'category' => $category,
            'dataProvider' => $dataProvider,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return Album the loaded model
     * @throws CHttpException
     */
    public function loadModel($slug) {
        $model = Album::model()->findByAttributes(array('slug' => $slug));
        if ($model === null)
            throw new CHttpException(404, '');
        return $model;
    }

    public function loadCategory($slug) {
        $model = Category::model()->findByAttributes(array('slug' => $slug));
        if ($model === null)
            throw new CHttpException(404, '');


        return $model;
    }

}
