noalyss Version-9
dossier.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 danydb@aevalys.eu
22
23/*!\file
24 * \brief the class for the dossier, everywhere we need to know to
25 * which folder we are connected, because we can't use $_SESSION, we
26 * need to pass the dossier_id via a _GET or a POST variable
27 */
28
29/*! \brief manage the current dossier, everywhere we need to know to
30 * which folder we are connected, because we can't use $_SESSION, we
31 * need to pass the dossier_id via a _GET or a POST variable
32 * private static $variable=array("id"=>"dos_id",
33 "name"=>"dos_name",
34 "desc"=>"dos_description");
35 *
36 */
37require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
38
40{
41
42 private static $variable=array("id"=>"dos_id",
43 "name"=>"dos_name",
44 "desc"=>"dos_description",
45 "max_email"=>'dos_email');
46
47 var $cn; //!< Database connexion
48 var $dos_id; //!< number of db
52
54 {
55 $this->cn=new Database(); // Connect to the repository
56 $this->dos_id=$p_id;
57 }
58
59 /*!\brief return the 'gDossier' value after a check */
60
61 static function id()
62 {
63 $http=new HttpInput();
64
65 return $http->request('gDossier','number');
66 }
67
68 /**
69 * @brief Show the folder where user have access.
70 * @param p_type string
71 - A for all dossiers
72 - R for accessible folders
73 - X forbidden folders
74 * @param p_login is the user name
75 * @param p_text is a part of the name where are looking for
76 * @return nothing
77 *
78 */
79 static function show_dossier($p_type, $p_login="", $p_text="", $limit=0)
80 {
81 $cn=new Database();
82 $str_limit=($limit==0)?'':' limit '.$limit;
83 if ($p_type=="A")
84 {
85 $l_sql="select *, 'W' as priv_priv "
86 ."from ac_dossier "
87 ."where "
88 ."dos_name ilike '%'|| $2 ||'%'"
89 ."or dos_description ilike '%'|| $2 || '%' "
90 ."ORDER BY dos_name $str_limit ";
91 $a_row=$cn->get_array($l_sql, $p_text);
92 return $a_row;
93 }
94 else if ($p_type=="R")
95 {
96 $l_sql="select * from jnt_use_dos
97 natural join ac_dossier
98 natural join ac_users
99 where
100 use_login=$1
101 and ( dos_name ilike '%'|| $2 || '%' or dos_description ilike '%'|| $2|| '%' )
102
103 order by dos_name
104 $str_limit
105 ";
106
107 $a_row=$cn->get_array($l_sql, array($p_login, $p_text));
108 return $a_row;
109 }
110 else if ($p_type=='X')
111 {
112 $l_sql=" select * from ac_dossier where dos_id not in
113 (select dos_id from jnt_use_dos where use_id=$1)
114 and ( dos_name ilike '%'|| $2|| '%' or dos_description ilike '%'|| $2 || '%')
115 order by dos_name ".$str_limit;
116 $a_row=$cn->get_array($l_sql, array($p_login, $p_text));
117 return $a_row;
118 }
119 else
120 {
121 throw new Exception(_("Erreur paramètre"));
122 }
123 }
124
125 /**
126 * Count the number of folder in the repository
127 * @return integer
128 */
129 function count()
130 {
131 $nb_folder=$this->cn->get_value('select count(*) from ac_dossier');
132 return $nb_folder;
133 }
134
135 /**
136 * \brief Return all the users as an array but NOALYSS_ADMINISTRATOR, that user cannot be changed by the
137 * interface for administrating user
138 * \param SQL $sql sql string to add to the query :
139 * \note that string MUST be the result of Database::escape_string
140 */
142 {
143
144 $sql="
145 select
146 use_id,
147 use_first_name,
148 use_name,
149 use_email,
150 use_login,
151 use_active,
152 use_admin,
153 ag_dossier
154 from
155 ac_users as ac
156 left join
157 (select array_to_string(array_agg(dos_name),',') as ag_dossier,
158 jt.use_id as jt_use_id
159 from ac_dossier as ds
160 join jnt_use_dos as jt on (jt.dos_id=ds.dos_id)
161 group by jt.use_id) as dossier_name on (jt_use_id=ac.use_id)
162 where
163 use_login!=$1
164 $sql
165 ";
166
167 $res=$this->cn->get_array($sql, array(NOALYSS_ADMINISTRATOR));
168 return $res;
169 }
170
171 /*!\brief check if gDossier is set
172 * ?? dead code ???
173 */
174
175 static function check()
176 {
177 try {
178 $http=new HttpInput();
179 $id=$http->request("gDossier","number");
180 if ($id > 999999 || $id < 0) throw new \Exception(_("Dossier max dépassé "));
181 } catch (\Exception $e) {
182
183 die('Dossier invalide ');
184 }
185
186 }
187
188 /*!
189 * \brief return a string to put to gDossier into a GET
190 */
191
192 static function get()
193 {
194 $http=new \HttpInput();
195 return "gDossier=".$http->request("gDossier","number");
196 }
197
198 /*!
199 * \brief return a string to set gDossier into a FORM
200 */
201
202 static function hidden()
203 {
204 $http=new \HttpInput();
205
206 return '<input type="hidden" id="gDossier" name="gDossier" value="'.$http->request("gDossier","number").'">';
207 }
208
209 /*!\brief retrieve the name of the current dossier */
210
211 static function name($id=0)
212 {
213
214 $http=new \HttpInput();
215 $cn=new Database();
216 $id=($id==0)?$http->request("gDossier","number"):$id;
217 $name=$cn->get_value("select dos_name from ac_dossier where dos_id=$1", array($id));
218 return $name;
219 }
220
221 public function get_parameter($p_string)
222 {
223 if (array_key_exists($p_string, self::$variable))
224 {
225 $idx=self::$variable[$p_string];
226 return $this->$idx;
227 }
228 else
229 throw new Exception("Attribut inexistant $p_string");
230 }
231
232 public function set_parameter($p_string, $p_value)
233 {
234 if (array_key_exists($p_string, self::$variable))
235 {
236 $idx=self::$variable[$p_string];
237 $this->$idx=$p_value;
238 }
239 else
240 throw new Exception("Attribut inexistant $p_string");
241 }
242
243 public function get_info()
244 {
245 return var_export(self::$variable, true);
246 }
247
248 public function save()
249 {
250 $this->update();
251 }
252
253 public function update()
254 {
255 if (noalyss_strlentrim($this->dos_name)==0)
256 return;
257
258 if ($this->cn->get_value("select count(*) from ac_dossier "
259 ." where dos_name=$1 and dos_id<>$2", array($this->dos_name, $this->dos_id))!=0)
260 return;
261
262 $sql="update ac_dossier set dos_name=$1,dos_description=$2 ,dos_email=$3".
263 " where dos_id = $4";
264 $res=$this->cn->exec_sql(
265 $sql, array(trim($this->dos_name),
266 trim($this->dos_description),
267 $this->dos_email,
268 $this->dos_id)
269 );
270 }
271
272 public function load()
273 {
274
275 $sql="select * from ac_dossier where dos_id=$1";
276
277 $res=$this->cn->exec_sql(
278 $sql, array($this->dos_id)
279 );
280
281 if (Database::num_row($res)==0)
282 return;
284 foreach ( self::$variable as $idx)
285 {
286 $this->$idx=$row[$idx];
287 }
288 }
289
290 static function get_version($p_cn)
291 {
292 return $p_cn->get_value('select max(val) from version');
293 }
294
295 static function connect()
296 {
297 static $cn=null;
298 if ($cn!=null)
299 return $cn;
301 $cn=new Database($id);
302 return $cn;
303 }
304
305 /**
306 * @brief connect to folder and give to admin. the profile Admin(builtin)
307 * @param int $p_id dossier::id()
308 */
309 static function synchro_admin($p_id)
310 {
311// connect to target
312 $cn=new Database($p_id);
313
314 if (!$cn->exist_table("profile_menu"))
315 {
316 echo_warning("Dossier invalide");
317 return;
318 }
319// connect to repo
320 $repo=new Database();
321
322 $a_admin=$repo->get_array("select use_login from ac_users where
323 use_admin=1 and use_active=1");
324 try
325 {
326 /**
327 * synchro global
328 */
329 $cn->start();
330 for ($i=0; $i<count($a_admin); $i++)
331 {
332 Noalyss_user::grant_admin_access($a_admin[$i]['use_login'], $p_id);
333 }
334 $cn->commit();
335 }
336 catch (Exception $e)
337 {
338
339 echo_warning($e->getMessage());
340 record_log($e);
341 $cn->rollback();
342 }
343 }
344 /**
345 * @brief Upgrade check if the folder ACCOUNT_REPOSITORY needs to be upgrade thanks the variable DBVERSIONREPO
346 * and run all the SQL script named ac-upgradeX.sql from the folder noalyss/include/sql/patch
347 * until X equal DBVERSIONREPO-1
348 * After it will call the function apply_patch, remove_inexistant_user and clean_orphan_log for each folder
349 *
350 */
351 static function upgrade()
352 {
353 $rep=new Database();
354 /* If multi folders */
355 $Resdossier=$rep->exec_sql("select dos_id, dos_name from ac_dossier");
356 $MaxDossier=$rep->size($Resdossier);
357
358 //----------------------------------------------------------------------
359 // Upgrade the account_repository
360 //----------------------------------------------------------------------
361 echo "<h2>"._("Mise à jour de la base de données principale")."</h2>";
362 $cn=new Database();
363 if (DEBUGNOALYSS == 0)
364 ob_start();
365 $MaxVersion=DBVERSIONREPO-1;
366 for ($i=4; $i<=$MaxVersion; $i++)
367 {
368 if ($cn->get_version()<=$i)
369 {
370 $cn->execute_script(NOALYSS_INCLUDE.'/sql/patch/ac-upgrade'.$i.'.sql');
371 }
372 }
373 if (DEBUGNOALYSS == 0) ob_end_clean();
374 //----------------------------------------------------------------------
375 // Upgrade the folders
376 //----------------------------------------------------------------------
377 echo "<h2>"._("Mise à jour dossiers")."</h2>";
378
379 for ($e=0; $e<$MaxDossier; $e++)
380 {
381 $db_row=Database::fetch_array($Resdossier, $e);
382 $name=$rep->format_name($db_row['dos_id'], 'dos');
383 echo "<h3>Patching ".$db_row['dos_name'].'</h3>';
384 echo _('Base de données')." ".$name;
385
386 if ($rep->exist_database($name)>0)
387 {
388 $db=new Database($db_row['dos_id'], 'dos');
389 $db->apply_patch($db_row['dos_name']);
390 Dossier::synchro_admin($db_row['dos_id']);
391 Noalyss_user::remove_inexistant_user($db_row['dos_id']);
392 $db->clean_orphan_lob();
393 }
394 else
395 {
396 echo_warning(_("Dossier inexistant")." $name");
397 }
398 }
399
400 //----------------------------------------------------------------------
401 // Upgrade the template
402 //----------------------------------------------------------------------
403 $Resdossier=$rep->exec_sql("select mod_id, mod_name from modeledef");
404 $MaxDossier=$rep->size();
405 echo "<h2>"._("Mise à jour modèles")."</h2>";
406
407 for ($e=0; $e<$MaxDossier; $e++)
408 {
409 $db_row=Database::fetch_array($Resdossier, $e);
410 $name=$rep->format_name($db_row['mod_id'], 'mod');
411 echo "<h3>Patching ".$db_row['mod_name']."</h3>";
412 echo _('Base de données')." ".$name;
413 if ($rep->exist_database($name)>0)
414 {
415 $db=new Database($db_row['mod_id'], 'mod');
416 $db->apply_patch($db_row['mod_name']);
417 $db->clean_orphan_lob();
418 }
419 else
420 {
421 echo_warning(_("Modèle inexistant")." $name");
422 }
423 }
424 }
425
426 /**
427 * Set the current dossier to $p_dossier
428 * @param numeric $p_dossier
429 */
430 static function set_current($p_dossier) {
431
432 self::check($p_dossier);
433 put_global([ [ "key"=>"gDossier","value"=>$p_dossier]]);
434
435 }
436
437}
put_global($array)
Put in superglobal (get,post,request) the value contained in the parameter field (me_parameter)
Definition: ac_common.php:1233
noalyss_strlentrim($p_string)
Definition: ac_common.php:1549
echo_warning($p_string)
warns
Definition: ac_common.php:589
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
$idx
if(isset($_REQUEST['gDossier']) && $http->request("gDossier","number", 0) !=0) $repo
$input_from cn
Definition: balance.inc.php:66
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
contains the class for connecting to Noalyss
manage the current dossier, everywhere we need to know to which folder we are connected,...
get_parameter($p_string)
static upgrade()
Upgrade check if the folder ACCOUNT_REPOSITORY needs to be upgrade thanks the variable DBVERSIONREPO ...
get_user_folder($sql="")
Return all the users as an array but NOALYSS_ADMINISTRATOR, that user cannot be changed by the interf...
$dos_id
number of db
count()
Count the number of folder in the repository.
static synchro_admin($p_id)
connect to folder and give to admin.
static get_version($p_cn)
static $variable
static id()
return the 'gDossier' value after a check
static check()
check if gDossier is set ?? dead code ???
__construct($p_id)
static show_dossier($p_type, $p_login="", $p_text="", $limit=0)
Show the folder where user have access.
$cn
Database connexion.
set_parameter($p_string, $p_value)
static hidden()
return a string to set gDossier into a FORM
static set_current($p_dossier)
Set the current dossier to $p_dossier.
static connect()
static name($id=0)
retrieve the name of the current dossier
manage the http input (get , post, request) and extract from an array
const DBVERSIONREPO
Definition: constant.php:37
NOALYSS_ADMINISTRATOR
Definition: install.php:735