<?php

class PageController extends Controller {

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
    public function actionView($slug) {
        $model = $this->loadModel($slug);
		$this->setSeoProperty('keywords',$model->meta);
		if(isset($model->image)&& $model->image!="" && $model->image!=null)
			$this->setSeoProperty('image_src',Yii::app()->getBaseUrl(true)."/uploads/Page/".$model->image);
		$this->setSeoProperty('title',$model->title);
		$desc=substr(htmlspecialchars_decode(trim(strip_tags($model->content))),0,200);
		$this->setSeoProperty('description',$desc);
        $this->layout = "//layouts/{$model->layout}";
        if($slug=="faq")
        {
          $this->render('/site/faq');
        }
        elseif($slug=="board-of-directors")
        {
            $this->render('/site/directors');
        }
        elseif($slug=="members-of-the-legitimate-censorship-board")
        {
            $this->render('/site/legitimacyCensorship');
        }
        elseif($slug=="executive-administration")
        {
            //Executive administration
             $this->render('/site/executive_administration');
        }
        elseif($slug=="commission")
        {
            //Executive administration
             $this->render('/site/commission');
        }
        // elseif($slug=="vision-mission-and-message")
        // {
        //     $this->render('/site/visionMissionValues');
        // }
        
          else
        $this->render('view', array(
            'model' => $model,
        ));
    }
     

    /**
     * Manages all models.
     */
    public function actionSearch() {
        $model = new Page('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Page'])) {
            $model->attributes = $_GET['Page'];
        }
        if (isset($_GET['q'])) {
            $model->title = $_GET['q'];
        }
        $this->render('search', array(
            'model' => $model,
        ));
    }

    /**
     * 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 Page the loaded model
     * @throws CHttpException
     */
    public function loadModel($slug) {
//        validate slug for security reasons:
//	$slug_match = preg_match("/^[a-zA-Z0-9_-]+$/", $slug);
//	$model = null;
//	if ($slug_match)
//        {
        $model = Page::model()->findByAttributes(array('slug' => $slug));
//	}
        if ($model === null)
            throw new CHttpException(404, '');


        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param Page $model the model to be validated
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'page-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
