noalyss Version-9
inplace_edit.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21// Copyright Author Dany De Bontridder dany@alchimerys.be
22
23/**
24 * @file
25 * @brief Inplace_edit class for ajax update of HtmlInput object
26 */
27/**
28 * @class Inplace_Edit
29 * @brief Inplace_edit class for ajax update of HtmlInput object.
30 * You need an ajax to response and modify the data. Some parameters will be sent
31 * by default when you click on the element
32 * - input : htmlInput object serialized
33 * - ieaction : ok or cancel , nothing if you just want to display the input
34 *
35 * Very important it is the DOM ID of the HtmlInput element, it must unique. For the date
36 * set a uniq dom id, otherwise it fails
37 * $id_limit_date->id=uniqid("date");
38 *
39 * @example inplace_edit.test.php
40 */
42{
43
44
45 /// HtmlInput object
46 private $input;
47 /// Json object to pass to JavaScript
48 private $json;
49 /// Php file which answered the ajax
50 private $callback;
51 /// Message to display if value is empty
52 private $message;
53 /**
54 * Create a Inplace_Edit, initialise JSON and fullfill the default json value:
55 * input which is the HtmlInput object serialized
56 * @param HtmlInput $p_input
57 */
58 function __construct(HtmlInput $p_input) {
59 $this->input=$p_input;
60 $x["input"]=base64_encode(serialize($p_input));
61 $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);
62 $this->message=_("Cliquez pour éditer");
63 }
64 ///@brief build a Inplace_Edit object from
65 /// a serialized string (ajax json parameter = input)
66 static function build($p_serialize)
67 {
68 $input= unserialize(base64_decode($p_serialize));
70 return $obj;
71 }
72 function display()
73 {
74 echo $this->input->value;
75 }
76 /**
77 * @brief response in ajax to be edited
78 */
79 function ajax_input() {
80 ob_start();
81 echo $this->input->input();
82 if ($this->input instanceof ITextarea) {
83 echo '<br>';
84 }
85 echo Icon_Action::validate("inplace_edit_ok".$this->input->id,"");
86 echo Icon_Action::cancel("inplace_edit_cancel".$this->input->id, "");
87 echo <<<EOF
88 <script>
89 $('{$this->input->id}edit').addClassName('inplace_edit_input');
90 {$this->input->id}edit.onclick=null;
91 inplace_edit_ok{$this->input->id}.onclick= function () {
92 var json={$this->json};
93 json['ieaction']='ok';
94 json['value']=$('{$this->input->id}').value;
95 new Ajax.Updater('{$this->input->id}edit'
96 ,'{$this->callback}',
97 {parameters: json ,evalScripts:true});}
98 inplace_edit_cancel{$this->input->id}.onclick= function () {
99 var json={$this->json};
100 json['ieaction']='cancel';
101 new Ajax.Updater('{$this->input->id}edit'
102 ,'{$this->callback}',
103 {parameters: json ,evalScripts:true});}
104
105 </script>
106EOF;
107 $ret= ob_get_contents();
108 ob_end_clean();
109 return $ret;
110 }
111 /**
112 * @brief display only the value , if the action after saving or cancelling
113 *
114 */
115 function value()
116 {
117 $v=$this->input->get_value();
118 $v=html_entity_decode($v??"");
119 if ( $this->input instanceof ITextarea) {
120
121 echo '<pre class="field_follow_up">';
122 $v=(trim($v)=="")?$this->message:$v;
123 echo $v;
124 echo '</pre>';
125 echo'<span class="smallbutton" style="margin-left:5px">Cliquer pour changer</span> ';
126 } else {
127 $v=(trim($v)=="")?$this->message:$v;
128 echo $v,
129 '<span class="smallicon icon" style="margin-left:5px">&#xe80d;</span> ';
130 }
131
132 echo "
133 <script>
134 $('{$this->input->id}edit').removeClassName('inplace_edit_input');
135 {$this->input->id}edit.onclick=function() {
136 new Ajax.Updater('{$this->input->id}edit'
137 ,'{$this->callback}',
138 {parameters: {$this->json} ,evalScripts:true});}
139 </script>
140 ";
141 }
142 /***
143 * @brief display the value with the click event
144 */
145 function input() {
146 ob_start();
147 $v=$this->input->get_value();
148 $v=html_entity_decode($v??"");
149 if ( $this->input instanceof ITextarea) {
150 echo '<span class="" id="'.$this->input->id.'edit" >';
151 echo '<pre class="field_follow_up">';
152 $v=(trim($v)=="")?$this->message:$v;
153 echo $v;
154 echo '</pre>';
155 echo'<span class="smallbutton" style="margin-left:5px">Cliquer pour changer</span> ';
156 } else {
157 $v=(trim($v)=="")?$this->message:$v;
158 echo <<<EOF
159 <span class="inplace_edit" id="{$this->input->id}edit" >
160EOF;
161 echo $v;
162
163 echo'<span class="smallicon icon" style="margin-left:5px">&#xe80d;</span> ';
164 }
165 echo "</span>";
166 echo "
167
168 <script>
169 {$this->input->id}edit.onclick=function() {
170 new Ajax.Updater('{$this->input->id}edit'
171 ,'{$this->callback}',
172 {parameters: {$this->json} ,evalScripts:true});
173 }
174 </script>
175 ";
176 $ret= ob_get_contents();
177 ob_end_clean();
178 return $ret;
179 }
180 /**
181 * @brief the php callback file to call for ajax
182 */
184 $this->callback=$callback;
185 }
186 /***
187 * @brief the JSON parameter to give to the script,
188 * this function shouldn't be used since it override the default JSON value
189 * @see add_json_parameter
190 */
191 function set_json($json) {
192 $this->json=$json;
193 }
194 /**
195 * Add json parameter to the current one, if there attribute already exists
196 * it will be overwritten
197 */
198 function add_json_param($p_attribute,$p_value) {
199 $x=json_decode($this->json,TRUE);
200 $x[$p_attribute]=$p_value;
201 $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);
202 }
203 /**
204 * \brief return the HtmlObject , var input
205 *
206 */
207 function get_input() {
208 return $this->input;
209 }
210 /**
211 * @brief set the var input (HtmlObject) and update the
212 * json attribute input
213 * @param HtmlInput $p_input
214 */
215 function set_input(HtmlInput $p_input) {
216 $this->input = $p_input;
217 $x=json_decode($this->json,TRUE);
218 $x["input"]=base64_encode(serialize($p_input));
219 $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);
220 }
221 /**
222 * Set the value of the HtmlInput object $input
223 * @param type $p_value
224 */
225 function set_value($p_value) {
226 $input=$this->get_input();
227 $this->input->set_value($p_value);
228 $this->set_input($input);
229 }
230 /**
231 * Message to display if the value is empty
232 * @param string $p_str
233 */
234 function set_message($p_str) {
235 $this->message=$p_str;
236 }
237}
span($p_string, $p_extra='')
Definition: ac_common.php:43
class widget This class is used to create all the HTML INPUT TYPE and some specials which works with ...
Manage the TEXTAREA html element.
static validate($p_id, $p_javascript)
Display the icon to modify a idem.
static cancel($p_id, $p_javascript)
Display the icon to modify a idem.
Inplace_edit class for ajax update of HtmlInput object.
$json
Json object to pass to JavaScript.
get_input()
return the HtmlObject , var input
__construct(HtmlInput $p_input)
Create a Inplace_Edit, initialise JSON and fullfill the default json value: input which is the HtmlIn...
$callback
Php file which answered the ajax.
set_message($p_str)
Message to display if the value is empty.
add_json_param($p_attribute, $p_value)
Add json parameter to the current one, if there attribute already exists it will be overwritten.
set_input(HtmlInput $p_input)
set the var input (HtmlObject) and update the json attribute input
$message
Message to display if value is empty.
ajax_input()
response in ajax to be edited
set_callback($callback)
the php callback file to call for ajax
set_value($p_value)
Set the value of the HtmlInput object $input.
value()
display only the value , if the action after saving or cancelling
static build($p_serialize)
build a Inplace_Edit object from a serialized string (ajax json parameter = input)
$input
HtmlInput object.