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