<?php

class DefaultController extends Controller
{

    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index'),
                'expression' => AccessManager::checkSessionPermission($this->module->name),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
	$data = array();
	$data["lang"] = Yii::app()->language;
        $this->render('index', $data);
    }

    public function getArrayDataProvider()
    {
        $translationTags = $this->getTranslationTags();
        ksort($translationTags);
        $currentTranslations = $this->getCurrentTranslation();
        $data = array();
        $id = 0;
        foreach ($translationTags as $key => $value) {
            $data[$id]['id'] = $id;
            $data[$id]['translationTags'] = $key;
            $data[$id]['english'] = $value;
            $currentLangTranslation = $key;
            if (isset($currentTranslations[$key])) {
                $currentLangTranslation = $currentTranslations[$key];
            }
            $data[$id]['currentLang'] = $currentLangTranslation;
            $id++;
        }

        $dataProvider = new CArrayDataProvider($data, array(
            'pagination' => false
        ));
        return $dataProvider;
    }

    protected function getTranslationTags()
    {
        $filePath = Yii::app()->basePath . '/messages/en_us/messages.php';
        return $this->open($filePath);
    }

    protected function getCurrentTranslation()
    {
        $filePath = Yii::app()->basePath . '/messages/' . Yii::app()->language . '/messages.php';
        return $this->open($filePath);
    }

    public function open($filePath)
    {
        if (file_exists($filePath)) {
            $fileContents = include $filePath;
            if (is_array($fileContents)) {
                return $fileContents;
            }
        }
    }

    public function renderCurrentLanguageValue($data)
    {
        $key = $data['translationTags'];
        $english = $data['english'];
        $translated =  $data['currentLang'];

        $color = "#000";
        if ($translated == $key) {
            $color = "#8B0000";
        } else if ($translated == $english) {
            $color = "#DAA520";
	}
	
        return '<b style="color: '.$color.'" data-translation-id="' . strtoupper($data['translationTags']) . '">' . __(strtoupper($data['translationTags'])) . '</b>';
    }
}
