noalyss Version-9
todo_list.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21
22/*!\file
23 * \brief the todo list is managed by this class
24 */
25
26require_once NOALYSS_INCLUDE.'/lib/function_javascript.php';
27
28/*!\brief
29 * This class manages the table todo_list
30 *
31 *
32 * Data Member :
33 * - $cn database connx
34 * - $variable
35 * - id (todo_list.tl_id)
36 * - date (todo_list.tl_Date)
37 * - title (todo_list.title)
38 * - desc (todo_list.tl_desc)
39 * - owner (todo_list.use_id)
40 *
41 */
43{
44
45 private static $variable=array(
46 "id"=>"tl_id",
47 "date"=>"tl_date",
48 "title"=>"tl_title",
49 "desc"=>"tl_desc",
50 "owner"=>"use_login",
51 "is_public"=>"is_public");
52 private $cn;
55 function __construct ($p_init)
56 {
57 $this->cn=$p_init;
58 $this->tl_id=0;
59 $this->tl_desc="";
60 $this->use_login=$_SESSION[SESSION_KEY.'g_user'];
61 $this->is_public="N";
62
63 }
64 public function get_parameter($p_string)
65 {
66 if ( array_key_exists($p_string,self::$variable) )
67 {
68 $idx=self::$variable[$p_string];
69 return $this->$idx;
70 }
71 else
72 throw new Exception("Attribut inexistant $p_string");
73 }
74 public function check($p_idx,&$p_value)
75 {
76 if ( strcmp ($p_idx, 'tl_id') == 0 )
77 {
78 if ( strlen($p_value) > 6 || isNumber ($p_value) == false) return false;
79 }
80 if ( strcmp ($p_idx, 'tl_date') == 0 )
81 {
82 if ( noalyss_strlentrim($p_value) ==0 ||strlen($p_value) > 12 || isDate ($p_value) == false) return false;
83 }
84 if ( strcmp ($p_idx, 'tl_title') == 0 )
85 {
86 $p_value=mb_substr($p_value,0,120) ;
87 return true;
88 }
89 if ( strcmp ($p_idx, 'tl_desc') == 0 )
90 {
91 $p_value=mb_substr($p_value,0,1000) ;
92 return true;
93 }
94 return true;
95 }
96 public function set_parameter($p_string,$p_value)
97 {
98 if ( array_key_exists($p_string,self::$variable) )
99 {
100 $idx=self::$variable[$p_string];
101 if ($this->check($idx,$p_value) == true ) $this->$idx=$p_value;
102 }
103 else
104 throw new Exception("Attribut inexistant $p_string");
105
106
107 }
108 public function get_info()
109 {
110 return var_export(self::$variable,true);
111 }
112 public function verify()
113 {
114 if ( isDate($this->tl_date) == false )
115 {
116 $this->tl_date=date('d.m.Y');
117 }
118 return 0;
119 }
120 public function save()
121 {
122 if ( $this->get_parameter("id") == 0 )
123 $this->insert();
124 else
125 $this->update();
126 }
127
128 public function insert()
129 {
130 if ( $this->verify() != 0 ) return;
131 if (trim($this->tl_title)=='')
132 $this->tl_title=mb_substr(trim($this->tl_desc),0,30);
133
134 if (trim($this->tl_title)=='')
135 {
136 alert('La note est vide');
137 return;
138 }
139
140 /* limit the title to 35 char */
141 $this->tl_title=mb_substr(trim($this->tl_title),0,30);
142
143 $sql="insert into todo_list (tl_date,tl_title,tl_desc,use_login,is_public) ".
144 " values (to_date($1,'DD.MM.YYYY'),$2,$3,$4,$5) returning tl_id";
145 $res=$this->cn->exec_sql(
146 $sql,
147 array($this->tl_date,
148 $this->tl_title,
149 $this->tl_desc,
150 $this->use_login,
151 $this->is_public)
152 );
153 $this->tl_id=Database::fetch_result($res,0,0);
154
155 }
156
157 public function update()
158 {
159 if ( $this->verify() != 0 ) return;
160
161 if (trim($this->tl_title)=='')
162 $this->tl_title=mb_substr(trim($this->tl_desc),0,40);
163
164 if (trim($this->tl_title)=='')
165 {
166
167 return;
168 }
169
170 /* limit the title to 35 char */
171 $this->tl_title=mb_substr(trim($this->tl_title),0,40);
172
173 $sql="update todo_list set tl_title=$1,tl_date=to_date($2,'DD.MM.YYYY'),tl_desc=$3,is_public=$5 ".
174 " where tl_id = $4";
175 $res=$this->cn->exec_sql(
176 $sql,
177 array($this->tl_title,
178 $this->tl_date,
179 $this->tl_desc,
180 $this->tl_id,
181 $this->is_public)
182 );
183
184 }
185 /*!\brief load all the task
186 *\return an array of the existing tasks of the current user
187 */
188 public function load_all()
189 {
190 $sql="select tl_id,
191 tl_title,
192 tl_desc,
193 to_char( tl_date,'DD.MM.YYYY') as tl_date,
194 is_public,
195 use_login
196 from todo_list
197 where
198 use_login=$1
199 or is_public = 'Y'
200 or tl_id in (select todo_list_id from todo_list_shared where use_login=$1)
201 order by tl_date::date desc";
202 $res=$this->cn->exec_sql(
203 $sql,
204 array($this->use_login));
206
207 return $array;
208 }
209 /**
210 * @brief load the todo_list row thanks it's ID
211 * @return boolean true if found else false
212 */
213 public function load():bool
214 {
215
216 $sql="select tl_id,tl_title,tl_desc,to_char( tl_date,'DD.MM.YYYY') as tl_date,is_public,use_login
217 from todo_list where tl_id=$1 ";
218
219 $res=$this->cn->exec_sql(
220 $sql,
221 array($this->tl_id)
222 );
223
224 if ( Database::num_row($res) == 0 ) return false;
226 $aIndex=array_values(self::$variable);
227 foreach ($aIndex as $idx)
228 {
229 $this->$idx=$row[$idx];
230 }
231 return true;
232 }
233 public function delete()
234 {
235 global $g_user;
236 if ( $this->use_login != $_SESSION[SESSION_KEY.'g_user'] && $g_user->check_action(SHARENOTEREMOVE)==0) return;
237
238 $sql="delete from todo_list_shared where todo_list_id=$1 ";
239 $res=$this->cn->exec_sql($sql,array($this->tl_id));
240
241 $sql="delete from todo_list where tl_id=$1 ";
242 $res=$this->cn->exec_sql($sql,array($this->tl_id));
243
244
245
246 }
247 /**
248 *@brief transform into xml for ajax answer
249 */
250 public function toXML()
251 {
252 $id='<tl_id>'.$this->tl_id.'</tl_id>';
253 $title='<tl_title>'.escape_xml($this->tl_title).'</tl_title>';
254 $desc='<tl_desc>'.escape_xml($this->tl_desc).'</tl_desc>';
255 $date='<tl_date>'.$this->tl_date.'</tl_date>';
256 $ret='<data>'.$id.$title.$desc.$date.'</data>';
257 return $ret;
258 }
259 /**
260 * @brief set a note public
261 * @param $p_value is Y or N
262 */
263 public function set_is_public($p_value)
264 {
265 global $g_user;
266 if ($g_user->check_action(SHARENOTEPUBLIC) == 1 )
267 {
268 $this->is_public=$p_value;
269 }
270 }
271 /**
272 * @brief Insert a share for current note
273 * in the table todo_list_shared
274 * @param string (use_login)
275 */
276 public function save_shared_with($p_array)
277 {
278 global $g_user;
279 if ($g_user->check_action(SHARENOTE) == 1 )
280 {
281 $this->cn->exec_sql('insert into todo_list_shared (todo_list_id,use_login) values ($1,$2)',
282 array($this->tl_id,$p_array));
283
284 }
285 }
286 /**
287 * @brief Insert a share for current note
288 * in the table todo_list_shared
289 * The public shared note cannot be removed
290 * @param string (use_login)
291 */
293 {
294 $this->cn->exec_sql('delete from todo_list_shared where todo_list_id = $1 and use_login=$2',
295 array($this->tl_id,$p_array));
296 }
297
298 /**
299 * Display the note
300 * @return html string
301 */
302 function display()
303 {
304 ob_start();
305 $this->load();
306 include NOALYSS_TEMPLATE.'/todo_list_display.php';
307 $ret=ob_get_clean();
308
309 return $ret;
310 }
311 /**
312 * Highlight today
313 * @return string
314 */
315 function get_class()
316 {
317 $p_odd="";
318 $a=date('d.m.Y');
319 if ($a == $this->tl_date) $p_odd='highlight';
320 return $p_odd;
321 }
322 function display_row($p_odd,$with_tag='Y')
323 {
324 $r="";
325 $highlight=$this->get_class();
326 $p_odd=($highlight == "")?$p_odd:$highlight;
327 if ( $with_tag == 'Y') $r = '<tr id="tr'.$this->tl_id.'" class="'.$p_odd.'">';
328 $r.=
329 '<td sorttable_customkey="'.format_date($this->tl_date,'DD.MM.YYYY','YYYYMMDD').'">'.
330 $this->tl_date.
331 '</td>'.
332 '<td>'.
333 '<a class="line" href="javascript:void(0)" onclick="todo_list_show(\''.$this->tl_id.'\')">'.
334 htmlspecialchars($this->tl_title).
335 '</a>'.
336 '</td>';
337 if ( $this->is_public == 'Y' && $this->use_login != $_SESSION[SESSION_KEY.'g_user'] )
338 { // This is a public note, cannot be removed
339 $r.= '<td></td>';
340 }
341 elseif ($this->use_login == $_SESSION[SESSION_KEY.'g_user'] )
342 {
343 // This a note the user owns
344 $r.= '<td>'.
345 Icon_Action::trash(uniqid(),sprintf("todo_list_remove('%s')",$this->tl_id)).
346 '</td>';
347 }
348 else
349 {
350 // this is a note shared by someone else
351 $r.= '<td>'.
352 Icon_Action::trash(uniqid(),sprintf("todo_list_remove_share('%s','%s','%s')",$this->tl_id,$this->use_login,Dossier::id())).
353 '</td>';
354 }
355
356 if ( $with_tag == 'Y') $r .= '</tr>';
357 return $r;
358 }
359 static function to_object ($p_cn,$p_array)
360 {
361
362 $ret=array();
363 if ( $p_array == FALSE ) return $ret;
364 $end=count($p_array);
365 for ($i=0;$i < $end;$i++)
366 {
367 $t=new Todo_List($p_cn);
368 $t->tl_id=$p_array[$i]['tl_id'];
369 $t->tl_date=$p_array[$i]['tl_date'];
370 $t->tl_title=$p_array[$i]['tl_title'];
371 $t->tl_desc=$p_array[$i]['tl_desc'];
372 $t->is_public=$p_array[$i]['is_public'];
373 $t->use_login=$p_array[$i]['use_login'];
374 $ret[$i]=clone $t;
375 }
376 return $ret;
377 }
378 /**
379 * @brief display all the user to select the user with who we want to share
380 * the connected user is not shown
381 * @global type $g_user
382 */
383 function display_user()
384 {
385 global $g_user;
386 // Get array of user
387 $p_array=Noalyss_user::get_list(Dossier::id());
388 $dossier=Dossier::id();
389 include NOALYSS_TEMPLATE.'/todo_list_list_user.php';
390
391 }
392 /**
393 * return the todo_list_shared.id of the note, if nothing is found then
394 * return 0
395 * @param $p_login
396 * @return int
397 */
398 function is_shared_with($p_login)
399 {
400 $ret=$this->cn->get_value("select id from todo_list_shared where use_login=$1 and todo_list_id=$2",array($p_login,$this->tl_id));
401 if ($ret == "")return 0;
402 return $ret;
403 }
404 /**
405 * @brief Add a share with someone
406 * @param type $p_login
407 */
408 function add_share($p_login)
409 {
410 $this->cn->exec_sql("insert into todo_list_shared(todo_list_id,use_login) values ($1,$2)",array($this->tl_id,$p_login));
411 }
412 /**
413 * @brief remove the share with someone
414 * @param type $p_login
415 */
416 function remove_share($p_login)
417 {
418 $this->cn->exec_sql("delete from todo_list_shared where todo_list_id = $1 and use_login = $2 ",array($this->tl_id,$p_login));
419 }
420 /*!\brief static testing function
421 */
422 static function test_me()
423 {
424 }
425
426}
427
428
isNumber($p_int)
Definition: ac_common.php:215
isDate($p_date)
Definition: ac_common.php:236
noalyss_strlentrim($p_string)
Definition: ac_common.php:1549
alert($p_msg, $buffer=false)
alert in javascript
Definition: ac_common.php:738
global $g_user
if no group available , then stop
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$idx
$anc_grandlivre from
$input_from cn
Definition: balance.inc.php:66
static fetch_all($ret)
wrapper for the function pg_fetch_all
static fetch_result($ret, $p_row=0, $p_col=0)
wrapper for the function pg_fetch_all
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
static num_row($ret)
wrapper for the function pg_num_rows
This class manages the table todo_list.
display()
Display the note.
save_shared_with($p_array)
Insert a share for current note in the table todo_list_shared.
load_all()
load all the task
load()
load the todo_list row thanks it's ID
display_row($p_odd, $with_tag='Y')
get_class()
Highlight today.
__construct($p_init)
remove_shared_with($p_array)
Insert a share for current note in the table todo_list_shared The public shared note cannot be remove...
set_parameter($p_string, $p_value)
check($p_idx, &$p_value)
static $variable
get_parameter($p_string)
set_is_public($p_value)
set a note public
toXML()
transform into xml for ajax answer
const SHARENOTEREMOVE
const SHARENOTEPUBLIC
const SHARENOTE