<?php

/**
 * Description of FCKEditor
 *
 * @author Ekstazi
 * v 0.2
 */
class FCKEditor extends CInputWidget {

    const COLS = 40;
    const ROWS = 10;

    public $uploadBaseUrl = false;
    public $language = 'en';
    private $allowedLanguages = array(
        'af', 'ar', 'bg', 'bn', 'bs', 'ca', 'cs', 'da', 'de', 'el', 'en', 'en-au', 'en-ca',
        'en-uk', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'fr-ca', 'gl', 'gu', 'he',
        'hi', 'hr', 'hu', 'is', 'it', 'ja', 'km', 'ko', 'lt', 'lv', 'mn', 'ms', 'nb', 'nl',
        'no', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sk', 'sl', 'sr', 'sr-latn', 'sv', 'th', 'tr',
        'uk', 'vi', 'zh', 'zh-cn'
    );
    private $allowedEditorTemplates = array('full', 'basic', 'advanced', 'common');
    public $options = array();
    public $editorTemplate = 'full';
    public $plugins = array('youtube');
    public $contentCSS = '';
    public $width = false;
    public $height = false;
    public $toolbar = array();
    public $skin = false;
    public $theme = false;

    public function __construct($owner = null) {
        parent::__construct($owner);
    }

    public function getAllowedLangauges() {
        return $this->allowedLanguages;
    }

    public function getAllowedEditorTemplates() {
        return $this->allowedEditorTemplates;
    }

    protected function makeOptions($assets) {
        list($name, $id) = $this->resolveNameID();

        $options['language'] = $this->language;
        $options["filebrowserBrowseUrl"] = "{$assets}/ckfinder/ckfinder.html";
        $options["filebrowserImageBrowseUrl"] = "{$assets}/ckfinder/ckfinder.html?type=Images";
        $options["filebrowserFlashBrowseUrl"] = "{$assets}/ckfinder/ckfinder.html?type=Images?type=Flash";
        $options["filebrowserUploadUrl"] = "{$assets}/ckfinder/connector/php/connector.php?command=QuickUpload&type=Files";
        $options["filebrowserImageUploadUrl"] = "{$assets}/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images";
        $options["filebrowserFlashUploadUrl"] = "{$assets}/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash";




        // to make the content look like if it were in your target page
        if ($this->contentCSS !== '') {
            $options['contentsCss'] = $this->contentCSS;
        }

        switch (strtolower($this->editorTemplate)) {
            case 'full':
                $options['toolbar'] = 'Full';
                break;
            case 'basic':
                $options['toolbar'] = 'Basic';
                break;
            case 'common':
                $options['toolbar'] = 'Common';
                break;
            default:
                $options['toolbar'] = $this->toolbar;
        }


        $options['extraPlugins'] = implode(',', $this->plugins);

        if ($this->skin) {
            $options['skin'] = $this->skin;
        }
        if ($this->theme) {
            $options['theme'] = $this->theme;
        }
        // here any option is overriden by user's options
        if (is_array($this->options)) {
            $options = array_merge($options, $this->options);
        }

        return CJavaScript::encode($options);
    }

    public function run() {
        list($name, $id) = $this->resolveNameID();
        $assets = Yii::app()->baseUrl . '/js';

        $options = $this->makeOptions($assets);


        $cs = Yii::app()->getClientScript();

        $baseUrl = Yii::app()->getBaseUrl(false);



        // authorization
        $_SESSION['CK_Is_Authorized'] = true;
        // base url
        if ($this->uploadBaseUrl) {
            $_SESSION['CK_Upload_BaseUrl'] = $this->uploadBaseUrl;
        } else {
            $_SESSION['CK_Upload_BaseUrl'] = $baseUrl . '/uploads/within/';
        }


        $cs->registerScriptFile($assets . '/ckeditor/ckeditor.js', 2);


        $this->htmlOptions['id'] = $id;

        if (!array_key_exists('style', $this->htmlOptions)) {
            if ($this->width) {
                $this->htmlOptions['style'] = "width:{$this->width};";
            }
            if ($this->height) {
                $this->htmlOptions['style'] .= "height:{$this->height};";
            }
        }
        if (!array_key_exists('cols', $this->htmlOptions)) {
            $this->htmlOptions['cols'] = self::COLS;
        }
        if (!array_key_exists('rows', $this->htmlOptions)) {
            $this->htmlOptions['rows'] = self::ROWS;
        }

        $js = <<<EOP
        window.CKEDITOR_BASEPATH='{$assets}/ckeditor/';
        CKEDITOR.replace('{$id}',{$options});
EOP;
        $cs->registerScript('Yii.' . get_class($this) . '#' . $id, $js, CClientScript::POS_LOAD);

        if ($this->hasModel()) {
            $html = CHtml::activeTextArea($this->model, $this->attribute, $this->htmlOptions);
        } else {
            $html = CHtml::textArea($name, $this->value, $this->htmlOptions);
        }

        echo $html;
    }

}
