<?php
/**
 * Wrapper class to the editor library
 * @author shameer
 */

class Ckeditor {

    public static $_init = false;
    public static $_instance;
    public $editor ;

    private function  __construct() {
            
                                    }

    public static function getInstance(&$ObjView,$options)
    {
        //if(!self::$_instance instanceof  self) {
            self::$_instance = new self;
            $editor_script = self::$_instance->init($options,$ObjView);
			
            $ObjView->assign('common_editor_script'.$options['name'],$editor_script);
			
       // }
        $cookieSet = false;
        $params_uploads = $options['uploads'];
        if($params_uploads !='' && CUSTOM_UPLOAD_DIRS==true) {
                $upload_dir = rtrim(__CORE_PUBLIC_PATH.'/uploads/'.$params_uploads,'/');
				$upload_root_dir_temp	= "/".substr(__CORE_PUBLIC_PATH,(strrpos(__CORE_PUBLIC_PATH,"\\")+1),strlen(__CORE_PUBLIC_PATH));
				$upload_root_dir_temp	= str_replace($_SERVER["DOCUMENT_ROOT"],"",$upload_root_dir_temp);
                $upload_base_dir = rtrim($upload_root_dir_temp.'/uploads/'.$params_uploads,'/');
                if(is_dir($upload_dir)) {
                    if(is_writable(rtrim($upload_dir))){
                        $_SESSION['ck_upload'][$options['name']] = $upload_base_dir.'/';
                        if(!$cookieSet){
                            setcookie('instance',$options['name'],time()+3600,'/');
                            $cookieSet = true;
                        }
                    }
                    else{
                        throw new Exception("The directory is not writable");
                    }

                }
                else {
                    if(!mkdir($upload_dir,__DIR_PERMISSION,true)) {
                            throw new Exception('Unable to create upload directory '.$upload_dir);
                     }
                     else{
                         if(!in_array('exec',explode(', ',ini_get('disable_functions')))) {
                             exec('chmod -Rf '.__DIR_PERMISSION.' '.$upload_dir);
                             $_SESSION['ck_upload'][$options['name']] = $upload_base_dir.'/';
                         }
                         elseif(!chmod($upload_dir, __DIR_PERMISSION)) {
                             throw new Exception('Unable to change the directory permissions');
                         }
                     }
                }
          }
          else {
			 $upload_root_dir_temp	= "/".substr(__CORE_PUBLIC_PATH,(strrpos(__CORE_PUBLIC_PATH,"\\")+1),strlen(__CORE_PUBLIC_PATH));
			 $upload_root_dir_temp	= str_replace($_SERVER["DOCUMENT_ROOT"],"",$upload_root_dir_temp);
             $_SESSION['ck_upload'][$options['name']] = $upload_root_dir_temp.'/uploads/editor-images/';
             if(!$cookieSet){
                setcookie('instance',$options['name'],time()+3600,'/');
                $cookieSet = true;
             }
          }
	
          return editor($options['name'], $options['value'],$options['width'], $options['height'], $options['cols'], $options['rows'],$options['toggle']);
          //return self::$_instance;
      }

    /**
     * @access public
     * @param mixed  $name          Name of the editor instance
     * @param mixed  $content       Content of the editor
     * @param int    $width         Width of the editor
     * @param int    $height        Height of the editor
     * @param int    $col           Number of cols for the textarea
     * @param int    $row           Number of rows for the textarea
     * @param array  $options       Additional options to be passed while initialization
     * @return editor
     */
    public function  getEditor($name, $content='', $width='', $height='', $col='', $row='', $toggleMode=FALSE)
    { 
        return editor($name, $content, $width, $height, $col, $row, $toggleMode);
    }
    /**
     *
     * @param array $options
     * @return Editor script
     */    
    public function init($options='',$ObjView)
    {
		
       require __LIBRARY_PATH.'/ckeditor/config.php';
       $mode = ($options['mode']!='') ?$options['mode'] : '';
       $skin = ($options['skin']!='') ?$options['skin'] : '';
       $align= ($options['align']!='') ?$options['align'] : '';
       $statusBar = ($options['statusBar']!='') ?$options['statusBar'] : '';
       $location  =  ($options['location']!='') ?$options['location'] : '';
	   $id= ($options['id']!='') ?$options['id'] : $options['name'];
       $config["enable_css"]    = ($options['enable_css'] !='') ? $options['enable_css'] : $config["enable_css"];
       $config["relative_urls"] = ($options['relative_urls'] !='') ? $options['relative_urls'] : $config["relative_urls"];
	   $config["lang"]=	($options['lang']!='') ?$options['lang'] : ''; 
	   $config["deflang"]=	($options['deflang']!='') ?$options['deflang'] : '';
       $editorInit = initEditor($config,$mode,$skin,$align,$location,$statusBar,$id);	   
	   $ck_editor_js="<script type='text/javascript' src='".$config["global_js_directory"]."ckeditor.js'></script>";
	   $ObjView->assign('common_ckjs_script',$ck_editor_js);
       self::$_init = true;
       return $editorInit;
    }
    
}