<?php
/**
* cat-prototype.inc.php (classe per gestire effetti javascript con prototype)
*
* +----------------------------------------------------------------------+
* |                                                                      |
* | web-sytem 2.0rc : Web Site Management System                         |
* |                                                                      |
* +----------------------------------------------------------------------+
* | Copyright (c) davcaffa@gmail.com                                     |
* +----------------------------------------------------------------------+
* | Authors: Davide Caffaratti <davcaffa@gmail.com>                      |
* |                                                                      |
* +----------------------------------------------------------------------+
*
*/
if (defined('PROTOTYPE_TOOLS')) { return true; }
define('PROTOTYPE_TOOLS'true);

if (!
defined('CAT_HEADER')) {
    
// funzioni header
    
include(dirname(__FILE__).'/cat-header.inc.php');
}

/**
* @desc Classe da estendere wrapper della classe Cat_Header
* @package Cat_Prototype
* @uses Cat_Header
* @category javascript
* @author Davide Caffaratti <davcaffa@gmail.com>
* @version 1.0
*/
class Cat_Prototype extends Cat_Header
{
    
/**
    * @desc Costruttore della classe
    * @param string $urlJavascript Url dei file javascript con '/' finale
    * @return void
    */
     
function __construct($urlJavascript)
     {
         
// Costruisco classe padre
         
Cat_Header::__construct($urlJavascript);
         
// Setto libreria js
         
$this->setJsFile('prototype.js');
    }
    
/**
    * @desc distruttore della classe
    */
    
function __destruct()
    {
         
// Distruggo classe padre
         
Cat_Header::__destruct();
    }
    
/**
    * @desc Ritorna option javascript
    * @access public
    * @param string $option Nome opzione
    * @param string $value Valore opzione
    * @return void
    */
    
function getJsOption($option$value)
    {
        if (empty(
$option)) {
            
trigger_error('$option is empty, can\'t write option !'E_USER_ERROR);
            return 
false;
        }
        if (empty(
$value)) {
            
trigger_error('$value is empty, can\'t write option !'E_USER_ERROR);
            return 
false;
        }
        if (
is_bool($value)) {
            
$val = ($value == true) ? 'true' 'false';
        }
        else {
            
$val = (is_numeric($value)) ? $value "'".html_entity_decode($value)."'";
        }

        return 
$option.": ".$val;
    }
    
/**
    * @desc Ritorna array javascript
    * @access public
    * @param array $arrayName Nome dell'array
    * @param array $options Opzioni array
    * @return string
    */
    
function getJsArray($arrayName$options)
    {
        if (empty(
$arrayName)) {
            
trigger_error('\$arrayName is empty, can\'t write array !'E_USER_ERROR);
            return 
false;
        }
        if (!
is_array($options)) {
            
trigger_error('\$opctions is not an array, can\'t write array !'E_USER_ERROR);
            return 
false;
        }
        
// apro array
        
$ret "    ".$arrayName.": {";

        
$i 0;
        foreach(
$options as $key=>$val) {
            
$ret .= (($i == 0) ? "\n" ",\n").
                    
"      ".$this->getJsOption($key$val)."";
            
$i 1;
        }
        
$ret .= "\n    }\n";
        return 
$ret;
    }
}
/**
* @desc Editor inline con ajax
* http://josephscott.org/code/js/eip/example.html
*
* @package Cat_Edit_Inline
* @uses Cat_Prototype
* @category javascript
* @author Davide Caffaratti <davcaffa@gmail.com>
* @version 1.0
*/
class Cat_Edit_Inline extends Cat_Prototype
{
    
/**
    * @desc Array Opzioni settate nella classe
    * @access private
    * @see function setElement($options);
    * @var array $options
    * Possibili opzioni:
    * $options = array(
    *    'type'=>'text',
    *    'save_url'=>'false',
    *    'size'=>'false',  // will be calculated at run time
    *    'max_size'=>100,
    *    'rows'=>'false',  // will be calculated at run time
    *    'cols'=>50,
    *    'escape_function'=>'escape',    // or encodeURIComponent
    *    'on_blur'=>'', ,
    *    'click'=>'click',  // or dblclick
    *    'mouseover_class'=>'eip_mouseover',
    *    'editfield_class'=>'eip_mouseover',
    *    'savebutton_class'=>'eip_savebutton',
    *    'cancelbutton_class'=>'eip_cancelbutton'
    *    'cancelbutton_text'=>'CANCEL',
    *    'savebutton_text'=>'SAVE',
    *    'empty_text'=>'Click To Edit',
    *    'saving_text:'=>'Saving...',
    *    'edit_title'=>'Click To Edit',
    *    'savefailed_text'=>'Failed to save changes.',
    *    'evalJS'=>'false'
    * );
    */
    
var $options = array('type'=>'text','empty_text'=>'Click To Edit');
    
/**
    * @desc Array elementi nell'header della pagina settati classe che si possono modificare inline
    * @access private
    * @var string $headerElement
    */
    
var $headerElement '';
    
/**
    * @desc Array elementi corpo pagina settati nella classe che si possono modificare inline
    * @access private
    * @var string $bodyElement
    */
    
var $bodyElement '';

     
/**
     * @desc Costruttore della classe
    * @access public
    * @param string $urlJavascript Url dei file javascript con '/' finale
    * @param string $urlCss Url dei file css con '/' finale
    * @param string $urlAjax Url file ajax default
    * @see Cat_Prototype::__construct();
    * @return void
    */
     
function Cat_Edit_Inline($urlJavascript$urlCss$urlAjax)
     {
         
// Costruisco classe padre
         
Cat_Prototype::__construct($urlJavascript$urlCss);
         
// Setto libreria js eip
        
$this->setJsFile('prototype-eip.js');
        
// Setto url ajax
        
$this->setInitEditOptions('save_url'$urlAjax);
     }
    
/**
    * @desc distruttore della classe
    */
    
function __destruct()
    {
         
// Distruggo classe padre
         
Cat_Prototype::__destruct();
         
$this->options null;
         
$this->defaultOptions null;
         
$this->classLang null;
         
$this->headerElement null;
         
$this->bodyElement null;
    }
    
/**
    * @desc Setta nella classe opzioni default
    * @access public
    * @param string $key
    * @param string $val
    * @example: (void)setInitEditOptions('size', 400);
    * @return void
    */
    
function setInitEditOptions($key$val)
    {
        
$this->options[$key] = html_entity_decode($val);
    }
    
/**
    * @desc Setta elemento nella classe
    * @access public
    * @param array $options
    * Possibili opzioni:
    * $options = array(
    *    'id'=>'false',
    *    'type'=>'text',
    *    'save_url'=>'false',
    *    'ajax_data'=>'false',
    *    'select_text'=>'false',    // doesn't work in Safari
    *    'options'=>'false', // Opcion select
    *    'size'=>'false',  // will be calculated at run time
    *    'max_size'=>100,
    *    'rows'=>'false',  // will be calculated at run time
    *    'cols'=>50,
    *    'escape_function'=>'escape',    // or encodeURIComponent
    *    'on_blur'=>'', ,
    *    'click'=>'click',  // or dblclick
    *    'mouseover_class'=>'eip_mouseover',
    *    'editfield_class'=>'eip_mouseover',
    *    'savebutton_class'=>'eip_savebutton',
    *    'cancelbutton_class'=>'eip_cancelbutton',
    *    'saving_class'=>'eip_saving',
    *    'cancelbutton_text'=>'CANCEL',
    *    'savebutton_text'=>'SAVE',
    *    'saving_text:'=>'Saving...',
    *    'edit_title'=>'Click To Edit',
    *    'savefailed_text'=>'Failed to save changes.'
    * );
    * @return void
    */
    
function setElement($options)
    {
        if (!
is_array($options)) {
            
trigger_error('No data set in this element !'E_USER_ERROR);
            return 
false;
        }
        if (empty(
$options['id'])) {
            
trigger_error('Empty id set for * '.$options['id'].' * !!'E_USER_ERROR);
            return 
false;
        }

        
// Apro funzione
        
$ret "  EditInPlace.makeEditable({\n";

        
$i 0;
        
// ciclo tra i parametri settati e li ritorno
        
foreach($options as $key=>$val) {
             
$ret .= ($i == 0) ? '' ",\n";
             if (
is_array($val)) {
                
$ret .= $this->getJsArray($key$val);
             }
             else {
                
$ret .= "    ".$this->getJsOption($key$val);
             }
             
$i 1;
        }
        
// Chiudo funzione
        
$ret .= "  });\n";
        
// Setto header element
        
$this->headerElement .= $ret;
    }
    
/**
    * @desc Setta header javascript nella classe
    * @access public
    * @return void
    */
    
function initEditInline()
    {
         
// Setto css
        
$this->setEditCss();
        
$ret =
        
"<script type=\"text/javascript\">\n".
         
"//<![CDATA[\n".
        
"Event.observe(window, 'load', init, false);\n".
        
"function init() {\n".
        
"  // Over ride some of the default options.\n";
        
// Se abbiamo elementi default da stampare
        
if ($this->options) {
            foreach (
$this->options as $key=>$val) {

                if (
is_bool($val)) {
                    
$value = ($val == true) ? 'true' 'false';
                }
                else {
                    
$value = (is_numeric($val)) ? $val "'".$val."'";
                }
                 
$ret .= "  EditInPlace.defaults['".$key."'] = ".$value.";\n";
            }
        }
        
$ret .=
        
$this->headerElement.
        
"}\n".
        
"//]]>\n".
        
"</script>\n";
        
$this->setJsHeader($ret);
    }
    
/**
    * @desc Setta elementi del corpo pagina
    * @access public
    * @param string $el_id Id elemento interessato
    * @param string $el_value Valore elemento da stampare
    * @param string $title Titolo elemento pagina
    * @param array $options Opzioni aggiuntive
    * Possibili sono:
    * $options = array(
    *            'html_tag'=>null, // Html tag differente dal p (default), se lasciato vuoto, non torna il tag
    *            'html_start'=>null, // Stringa html aggiuntiva prima dello span
    *            'html_hend'=>null // Stringa html aggiuntiva alla fine del span
    *            );
    * @return void
    */
    
function setElementBody($el_id$el_value=''$options=false)
    {
         
$html_start '';
        if (
$options && isset($options['html_start'])) {
             
$html_start "  ".$options['html_start']."\n";
        }
         
$html_hend '';
        if (
$options && isset($options['html_hend'])) {
             
$html_hend "  ".$options['html_hend']."\n";
        }
        
$class = (empty($el_value)) ? "eip_empty" '';

        if (
$options && isset($options['html_tag'])) {
            
$ret = ($options['html_tag'] == '') ? '' "<".$options['html_tag'].">\n";
            
$footer = (empty($ret)) ? '' "</".$options['html_tag'].">\n";
        }
        else {
            
$ret "<p>\n";
            
$footer "</p>\n";
        }

        
$ret .= $html_start.
                
"  <span class=\"".$class."\" title=\"".
                
$this->options['empty_text']."\" id=\"".$el_id."\">".$el_value."</span>\n".
                
$html_hend.
                
$footer;
        
$this->bodyElement .= $ret;
    }
    
/**
    * @desc Setta elemento nel body senza style edit inline
    * @access public
    * @param string $body Elemento da aggiungere
    * @return void
    */
    
function setOnlyBody($body)
    {
        
$this->bodyElement .= $body;
    }
    
/**
    * @desc Ritorna elementi boody
    * @access public
    * @return string
    */
    
function getBody()
    {
        return 
$this->bodyElement;
    }
    
/**
    * @desc Ritorna css inline
    * @access public
    * @return void
    */
    
function setEditCss()
    {
        
$ret =
        
".eip_mouseover { background-color: #ff9; padding: 3px; cursor:pointer; }\n".
        
".eip_savebutton { background-color: #36f; color: #fff; cursor:pointer; }\n".
        
".eip_cancelbutton { background-color: #000; color: #fff; cursor:pointer; }\n".
        
".eip_saving { background: #903 url(".URL_THEMES."loading_little.gif) center left no-repeat; color: #fff; padding:2px 4px 2px 28px; }\n".
        
".eip_empty { color: #afafaf; }\n";
        
$this->setCssInline($ret);
    }
    
/**
    * @desc Ritorna dati inviati da ajax o false se non ci sono dati inviati
    * @access public
    * @return mixed array|false
    */
    
function getAjaxDatas()
    {
         
$isPost count($_POST);
         
// Se abbiamo dati nel post ed esiste il valore è testo vuoto nel db inserisco campo vuoto
         
if ($isPost && isset($_POST['content']) && $_POST['content'] == $this->options['empty_text']) {
            
$_POST['content'] = '';
         }
         return (
$isPost) ? $_POST false;
    }
}
?>