<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * List_formsController
 * 
 * Handles the actions for list forms related tasks
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
class List_formsController extends Controller
{
    
    public function init()
    {
        $pageScripts = array(
            'list-forms' => AssetsUrl::js('list-forms.js'),
        );

        foreach ($pageScripts as $handler => $url) {
           Yii::app()->clientScript->registerScriptFile($url);
        }
        
        parent::init();    
    }
    
    public function actionIndex($list_uid)
    {
        $list = $this->loadListModel($list_uid);
        $request = Yii::app()->request;
        
        $criteria = new CDbCriteria();
        $criteria->compare('list_id', $list->list_id);
        $criteria->order = 'sort_order ASC';
        $fields = ListField::model()->findAll($criteria);
        
        $this->setData(array(
            'pageMetaTitle'     => $this->data->pageMetaTitle . ' | ' . Yii::t('list_forms', 'Your mail list forms'), 
            'pageBreadcrumbs'   => array(
                Yii::t('lists', 'Lists') => $this->createUrl('lists/index'),
                $list->name => $this->createUrl('lists/overview', array('list_uid' => $list->list_uid)),
                Yii::t('list_forms', 'Embed forms')
            )
        ));

        $this->render('index', compact('list', 'fields'));
    }

    /**
     * Helper method to load the list AR model
     */
    public function loadListModel($list_uid)
    {
        $model = Lists::model()->findByAttributes(array(
            'list_uid'      => $list_uid,
            'customer_id'   => (int)Yii::app()->customer->getId(),
        ));
        
        if ($model === null) {
            throw new CHttpException(404, Yii::t('app', 'The requested page does not exist.'));
        }
        
        return $model;
    }
}