id=uniqid("date"); * * @example inplace_edit.test.php */ class Inplace_Edit { /// HtmlInput object private $input; /// Json object to pass to JavaScript private $json; /// Php file which answered the ajax private $callback; /// Message to display if value is empty private $message; /** * Create a Inplace_Edit, initialise JSON and fullfill the default json value: * input which is the HtmlInput object serialized * @param HtmlInput $p_input */ function __construct(HtmlInput $p_input) { $this->input=$p_input; $x["input"]=base64_encode(serialize($p_input)); $this->json=json_encode($x, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP |JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); $this->message=_("Cliquez pour éditer"); } ///@brief build a Inplace_Edit object from /// a serialized string (ajax json parameter = input) static function build($p_serialize) { $input= unserialize(base64_decode($p_serialize)); $obj=new Inplace_Edit($input); return $obj; } function display() { echo $this->input->value; } /** * @brief response in ajax to be edited */ function ajax_input() { ob_start(); echo $this->input->input(); if ($this->input instanceof ITextarea) { echo '
'; } echo Icon_Action::validate("inplace_edit_ok".$this->input->id,""); echo Icon_Action::cancel("inplace_edit_cancel".$this->input->id, ""); echo << $('{$this->input->id}edit').addClassName('inplace_edit_input'); {$this->input->id}edit.onclick=null; inplace_edit_ok{$this->input->id}.onclick= function () { var json={$this->json}; json['ieaction']='ok'; json['value']=$('{$this->input->id}').value; new Ajax.Updater('{$this->input->id}edit' ,'{$this->callback}', {parameters: json ,evalScripts:true});} inplace_edit_cancel{$this->input->id}.onclick= function () { var json={$this->json}; json['ieaction']='cancel'; new Ajax.Updater('{$this->input->id}edit' ,'{$this->callback}', {parameters: json ,evalScripts:true});} EOF; $ret= ob_get_contents(); ob_end_clean(); return $ret; } /** * @brief display only the value , if the action after saving or cancelling * */ function value() { $v=$this->input->get_value(); $v=html_entity_decode($v??""); if ( $this->input instanceof ITextarea) { echo '
';
            $v=(trim($v)=="")?$this->message:$v;
            echo $v;
            echo '
'; echo'Cliquer pour changer '; } else { $v=(trim($v)=="")?$this->message:$v; echo $v, ' '; } echo " "; } /*** * @brief display the value with the click event */ function input() { ob_start(); $v=$this->input->get_value(); $v=html_entity_decode($v??""); if ( $this->input instanceof ITextarea) { echo ''; echo '
';
            $v=(trim($v)=="")?$this->message:$v;
            echo $v;
            echo '
'; echo'Cliquer pour changer '; } else { $v=(trim($v)=="")?$this->message:$v; echo << EOF; echo $v; echo' '; } echo "
"; echo " "; $ret= ob_get_contents(); ob_end_clean(); return $ret; } /** * @brief the php callback file to call for ajax */ function set_callback($callback) { $this->callback=$callback; } /*** * @brief the JSON parameter to give to the script, * this function shouldn't be used since it override the default JSON value * @see add_json_parameter */ function set_json($json) { $this->json=$json; } /** * Add json parameter to the current one, if there attribute already exists * it will be overwritten */ function add_json_param($p_attribute,$p_value) { $x=json_decode($this->json,TRUE); $x[$p_attribute]=$p_value; $this->json=json_encode($x, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP |JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); } /** * \brief return the HtmlObject , var input * */ function get_input() { return $this->input; } /** * @brief set the var input (HtmlObject) and update the * json attribute input * @param HtmlInput $p_input */ function set_input(HtmlInput $p_input) { $this->input = $p_input; $x=json_decode($this->json,TRUE); $x["input"]=base64_encode(serialize($p_input)); $this->json=json_encode($x, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP |JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_NUMERIC_CHECK); } /** * Set the value of the HtmlInput object $input * @param type $p_value */ function set_value($p_value) { $input=$this->get_input(); $this->input->set_value($p_value); $this->set_input($input); } /** * Message to display if the value is empty * @param string $p_str */ function set_message($p_str) { $this->message=$p_str; } }