- LSview : Utilisation de LSconfirmBox pour la confirmation de suppression.
[ldapsaisie.git] / trunk / view.php
1 <?php
2 /*******************************************************************************
3  * Copyright (C) 2007 Easter-eggs
4  * http://ldapsaisie.labs.libre-entreprise.org
5  *
6  * Author: See AUTHORS file in top-level directory.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
21 ******************************************************************************/
22
23 require_once 'includes/functions.php';
24 require_once 'includes/class/class.LSsession.php';
25
26 $GLOBALS['LSsession'] = new LSsession();
27
28 if($LSsession -> startLSsession()) {
29   if (isset($_REQUEST['LSobject'])) {
30     $LSobject = $_REQUEST['LSobject'];
31     $dn = $_REQUEST['dn'];
32     
33     if ($GLOBALS['LSsession'] -> in_menu($LSobject)) {
34     
35       if ( $LSobject == 'SELF' ) {
36         $LSobject = $GLOBALS['LSsession']-> LSuserObject -> getType();
37         $dn = $GLOBALS['LSsession']-> LSuserObject -> getValue('dn');
38       }
39       
40       if ( $GLOBALS['LSsession'] -> loadLSobject($LSobject) ) {
41         // Affichage d'un objet
42         if ( $dn!='' ) {
43           if ($GLOBALS['LSsession'] -> canAccess($LSobject,$dn)) {
44             if ( $GLOBALS['LSsession'] -> canEdit($LSobject,$dn) ) {
45               $LSview_actions[] = array(
46                 'label' => _('Modifier'),
47                 'url' =>'modify.php?LSobject='.$LSobject.'&amp;dn='.$dn,
48                 'action' => 'modify'
49               );
50             }
51             
52             if ($GLOBALS['LSsession'] -> canCreate($LSobject)) {
53               $LSview_actions[] = array(
54                 'label' => _('Copier'),
55                 'url' =>'create.php?LSobject='.$LSobject.'&amp;load='.$dn,
56                 'action' => 'copy'
57               );
58             }
59             
60             if ($GLOBALS['LSsession'] -> canRemove($LSobject,$dn)) {
61               $LSview_actions[] = array(
62                 'label' => _('Supprimer'),
63                 'url' => 'remove.php?LSobject='.$LSobject.'&amp;dn='.$dn,
64                 'action' => 'delete'
65               );
66             }
67             
68             if ($GLOBALS['LSsession']-> LSuserObject -> getValue('dn') != $dn) {
69               $object = new $LSobject();
70               $object -> loadData($dn);
71               $GLOBALS['Smarty'] -> assign('pagetitle',$object -> getDisplayValue());
72             }
73             else {
74               $object = &$GLOBALS['LSsession']-> LSuserObject;
75               $GLOBALS['Smarty'] -> assign('pagetitle',_('Mon compte'));
76             }
77             
78             $view = $object -> getView();
79             $view -> displayView();
80             
81             // Relations
82             if (is_array($object -> config['relations'])) {
83               $LSrelations=array();
84               $LSrelations_JSparams=array();
85               foreach($object -> config['relations'] as $relationName => $relationConf) {
86                 if ($GLOBALS['LSsession'] -> relationCanAccess($object -> getValue('dn'),$LSobject,$relationName)) {
87                   $return=array(
88                     'label' => $relationConf['label'],
89                     'LSobject' => $relationConf['LSobject']
90                   );
91                   
92                   if (isset($relationConf['emptyText'])) {
93                     $return['emptyText'] = $relationConf['emptyText'];
94                   }
95                   else {
96                     $return['emptyText'] = _('Aucun objet en relation.');
97                   }
98                   
99                   $id=rand();
100                   $return['id']=$id;
101                   $LSrelations_JSparams[$id]=array(
102                     'emptyText' => $return['emptyText']
103                   );
104                   $_SESSION['LSrelation'][$id] = array(
105                     'relationName' => $relationName,
106                     'objectType' => $object -> getType(),
107                     'objectDn' => $object -> getDn(),
108                   );
109                   if ($GLOBALS['LSsession'] -> relationCanEdit($object -> getValue('dn'),$LSobject,$relationName)) {
110                     $return['actions'][] = array(
111                       'label' => _('Modifier'),
112                       'url' => 'select.php?LSobject='.$relationConf['LSobject'].'&amp;multiple=1',
113                       'action' => 'modify'
114                     );
115                   }
116                   
117                   $GLOBALS['LSsession'] -> addJSscript('LSselect.js');
118                   $GLOBALS['LSsession'] -> addCssFile('LSselect.css');
119                   $GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js');
120                   $GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css');
121                   $GLOBALS['LSsession'] -> addJSscript('LSrelation.js');
122                   $GLOBALS['LSsession'] -> addCssFile('LSrelation.css');
123                   $GLOBALS['LSsession'] -> addJSscript('LSconfirmBox.js');
124                   $GLOBALS['LSsession'] -> addCssFile('LSconfirmBox.css');
125                   $GLOBALS['LSsession'] -> addJSscript('LSview.js');
126                   
127                   if($GLOBALS['LSsession'] -> loadLSobject($relationConf['LSobject'])) {
128                     if (method_exists($relationConf['LSobject'],$relationConf['list_function'])) {
129                       $objRel = new $relationConf['LSobject']();
130                       $list = $objRel -> $relationConf['list_function']($object);
131                       if (is_array($list)) {
132                         foreach($list as $o) {
133                           $o_infos = array(
134                             'text' => $o -> getDisplayValue(NULL,true),
135                             'dn' => $o -> getDn()
136                           );
137                           $return['objectList'][] = $o_infos;
138                         }
139                       }
140                       else {
141                         $return['objectList']=array();
142                       }
143                     }
144                     else {
145                       $GLOBALS['LSerror'] -> addErrorCode(1013,$relationName);
146                     }
147                     $LSrelations[]=$return;
148                   }
149                   else {
150                       $GLOBALS['LSerror'] -> addErrorCode(1016,array('relation' => $relationName,'LSobject' => $relationConf['LSobject']));
151                   }
152                 }
153               }
154               
155               $GLOBALS['LSsession'] -> addJSscript('LSconfirmBox.js');
156               $GLOBALS['LSsession'] -> addCssFile('LSconfirmBox.css');
157               $GLOBALS['Smarty'] -> assign('LSrelations',$LSrelations);
158               $GLOBALS['LSsession'] -> addJSconfigParam('LSrelations',$LSrelations_JSparams);
159             }
160             
161             $GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions);
162             $GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js');
163             $GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css');
164             $GLOBALS['LSsession'] -> setTemplate('view.tpl');
165           }
166           else {
167             $GLOBALS['LSerror'] -> addErrorCode(1011);
168           }
169         }
170         // Affichage d'une liste d'un type d'objet
171         else {
172           $objectList=array();
173           $object = new $LSobject();
174           
175           $GLOBALS['Smarty']->assign('pagetitle',$object -> getLabel());
176           $GLOBALS['Smarty']->assign('LSobject_list_objectname',$object -> getLabel());
177           
178           if (isset($_SESSION['LSsession']['LSsearch'][$LSobject])) {
179             $filter = $_SESSION['LSsession']['LSsearch'][$LSobject]['filter'];
180             $params = $_SESSION['LSsession']['LSsearch'][$LSobject]['params'];
181             $pattern = $_SESSION['LSsession']['LSsearch'][$LSobject]['pattern'];
182             $recur = $_SESSION['LSsession']['LSsearch'][$LSobject]['recur'];
183             if ($recur) {
184               $topDn = $GLOBALS['LSsession'] -> topDn;
185             }
186             else {
187               $topDn = $object -> config['container_dn'].','.$GLOBALS['LSsession'] -> topDn;
188             }
189             $approx = $_SESSION['LSsession']['LSsearch'][$LSobject]['approx'];
190             $orderby = $_SESSION['LSsession']['LSsearch'][$LSobject]['orderby'];
191             $ordersense = $_SESSION['LSsession']['LSsearch'][$LSobject]['ordersense'];
192             $doSubDn = $_SESSION['LSsession']['LSsearch'][$LSobject]['doSubDn'];
193           }
194           else {
195             $filter = NULL;
196             $topDn = $object -> config['container_dn'].','.$GLOBALS['LSsession'] -> topDn;
197             $params = array('scope' => 'one');
198             $pattern = false;
199             $recur = false;
200             $approx = false;
201             $orderby = false;
202             $_REQUEST['orderby']=$GLOBALS['LSobjects'][$LSobject]['orderby'];
203             $ordersense = 'ASC';
204             $subDnLdapServer = $GLOBALS['LSsession'] -> getSubDnLdapServer();
205             $doSubDn = (($subDnLdapServer)&&(!$GLOBALS['LSsession']->isSubDnLSobject($LSobject)));
206           }
207           
208           if (isset($_REQUEST['LSview_search_submit'])) {
209             if (isset($_REQUEST['LSview_pattern']) && ($_REQUEST['LSview_pattern']!=$pattern)) {
210               $pattern = $_REQUEST['LSview_pattern'];
211             }
212
213             $approx = (isset($_REQUEST['LSview_approx']));
214             
215             if ($pattern && $pattern!='') {
216               $filter='(|';
217               if ($approx) {
218                 foreach ($object -> attrs as $attr_name => $attr_val) {
219                   $filter.='('.$attr_name.'~='.$pattern.')';
220                 }
221               }
222               else {
223                 foreach ($object -> attrs as $attr_name => $attr_val) {
224                   $filter.='('.$attr_name.'=*'.$pattern.'*)';
225                 }
226               }
227               $filter.=')';
228             }
229             else {
230               $filter = NULL;
231             }
232             
233             if (isset($_REQUEST['LSview_recur'])) {
234               $recur = true;
235               $params['scope'] = 'sub';
236               $topDn = $GLOBALS['LSsession'] -> topDn;
237             }
238             else {
239               $recur = false;
240               $params['scope'] = 'one';
241               $topDn = $object -> config['container_dn'].','.$GLOBALS['LSsession'] -> topDn;
242             }
243           }
244           
245           $sort=false;
246           if ((isset($_REQUEST['orderby']))) {
247             $possible_values= array('displayValue','subDn');
248             if (in_array($_REQUEST['orderby'],$possible_values)) {
249               $sort=true;
250               if ($orderby==$_REQUEST['orderby']) {
251                 $ordersense = ($ordersense=='ASC')?'DESC':'ASC';
252               }
253               else {
254                 $ordersense = 'ASC';
255               }
256               $orderby=$_REQUEST['orderby'];
257             }
258           }
259           
260           $GLOBALS['Smarty']->assign('LSobject_list_subDn',$doSubDn);
261           
262           // Sauvegarde en Session
263           $_SESSION['LSsession']['LSsearch'][$LSobject] = array(
264             'filter' => $filter,
265             'topDn' => $topDn,
266             'params' => $params,
267             'pattern' => $pattern,
268             'recur' => $recur,
269             'approx' => $approx,
270             'orderby' => $orderby,
271             'ordersense' => $ordersense,
272             'doSubDn' => $doSubDn
273           );
274
275           $GLOBALS['Smarty']->assign('LSview_search_pattern',$pattern);
276
277           if ($recur) {
278             $GLOBALS['Smarty']->assign('LSview_search_recur',true);
279           }
280           if ($approx) {
281             $GLOBALS['Smarty']->assign('LSview_search_approx',true);
282           }
283           
284           // Hidden fields
285           $GLOBALS['Smarty']->assign('LSview_search_hidden_fields',array(
286             'LSobject' => $LSobject,
287             'LSview_search_submit' => 1
288           ));
289           
290           // Hash de la recherche déterminer à partir des paramètres de la recherche
291           $hash = mhash (MHASH_MD5, 
292             print_r(
293               array(
294                 'LSobject' => $LSobject,
295                 'filter' => $filter,
296                 'topDn' => $topDn,
297                 'params' => $params
298               ),
299               true
300             )
301           );
302           
303           if (($GLOBALS['LSsession'] -> cacheSearch()) && isset($_SESSION['LSsession']['LSsearch'][$hash]) && (!isset($_REQUEST['refresh']))) {
304             // On affiche à partir du cache
305             $searchData=$_SESSION['LSsession']['LSsearch'][$hash];
306             LSdebug('Recherche : From cache');
307           }
308           else {
309             LSdebug('Recherche : Load');
310             if ($GLOBALS['LSsession'] -> canCreate($LSobject)) {
311               $LSview_actions[] = array (
312                 'label' => _('Nouveau'),
313                 'url' => 'create.php?LSobject='.$LSobject,
314                 'action' => 'create'
315               );
316               $canCopy=true;
317             }
318             $LSview_actions[] = array (
319               'label' => _('Rafraîchir'),
320               'url' => 'view.php?LSobject='.$LSobject.'&amp;refresh',
321               'action' => 'refresh'
322             );
323             
324             $list=$object -> listObjectsName($filter,$topDn,$params);
325
326             $nbObjects=0;
327             foreach($list as $objDn => $objName) {
328               if ($GLOBALS['LSsession'] -> canAccess($LSobject,$objDn)) {
329                 $subDn_name=false;
330                 if ($doSubDn) {
331                   $subDn_name = $object -> getSubDnName($objDn);
332                 }
333                 $nbObjects++;
334
335                 
336                 $objectList[]=array(
337                   'dn' => $objDn,
338                   'displayValue' => $objName,
339                   'subDn' => $subDn_name
340                 );
341               }
342             }
343             
344             $searchData['LSobject_list_nbresult']=$nbObjects;
345             
346             $searchData['objectList']=$objectList;
347             $searchData['LSview_actions'] = $LSview_actions;
348             if ($orderby) {
349               $sort=true;
350             }
351           }
352           
353           if ((!isset($searchData['objectList'][0]['actions']))&&(!empty($searchData['objectList']))) {
354             LSdebug('Load actions');
355             for($i=0;$i<$searchData['LSobject_list_nbresult'];$i++) {
356               $actions=array();
357               
358               $actions[] = array(
359                 'label' => _('Voir'),
360                 'url' =>'view.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
361                 'action' => 'view'
362               );
363               
364               if ($GLOBALS['LSsession'] -> canEdit($LSobject,$searchData['objectList'][$i]['dn'])) {
365                 $actions[]=array(
366                   'label' => _('Modifier'),
367                   'url' => 'modify.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
368                   'action' => 'modify'
369                 );
370               }
371               
372               if ($canCopy) {
373                 $actions[] = array(
374                   'label' => _('Copier'),
375                   'url' =>'create.php?LSobject='.$LSobject.'&amp;load='.$searchData['objectList'][$i]['dn'],
376                   'action' => 'copy'
377                 );
378               }
379               
380               if ($GLOBALS['LSsession'] -> canRemove($LSobject,$searchData['objectList'][$i]['dn'])) {
381                 $actions[] = array (
382                   'label' => _('Supprimer'),
383                   'url' => 'remove.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
384                   'action' => 'delete'
385                 );
386               }
387               $searchData['objectList'][$i]['actions']=$actions;
388             }
389           }
390           
391           $GLOBALS['Smarty']->assign('LSobject_list_nbresult',$searchData['LSobject_list_nbresult']);
392           
393           // Order by if $sort
394           if ($sort) {
395             function sortBy($a,$b) {
396               global $ordersense;
397               global $orderby;
398               
399               if ($ordersense=='ASC') {
400                 $sense = -1;
401               }
402               else {
403                 $sense = 1;
404               }
405               
406               if ($a == $b) return 0;
407               $sort = array($a[$orderby],$b[$orderby]);
408               sort($sort);
409               if ($sort[0]==$a[$orderby])
410                 return 1*$sense;
411               return -1*$sense;
412             }
413             if (!uasort($searchData['objectList'],'sortBy')) {
414               LSdebug('Erreur durant le trie.');
415             }
416           }
417           $GLOBALS['Smarty']->assign('LSobject_list_orderby',$orderby);
418           $GLOBALS['Smarty']->assign('LSobject_list_ordersense',$ordersense);
419           
420           if ($GLOBALS['LSsession'] -> cacheSearch()) {
421             $_SESSION['LSsession']['LSsearch'][$hash]=$searchData;
422           }
423           
424           // Pagination
425           if ($searchData['LSobject_list_nbresult'] > NB_LSOBJECT_LIST) {
426             if (isset($_REQUEST['page'])) {
427               $searchData['objectList'] = array_slice($searchData['objectList'], ($_REQUEST['page']) * NB_LSOBJECT_LIST, NB_LSOBJECT_LIST);
428               $GLOBALS['Smarty']->assign('LSobject_list_currentpage',$_REQUEST['page']);
429               
430             }
431             else {
432               $searchData['objectList'] = array_slice($searchData['objectList'], 0, NB_LSOBJECT_LIST);
433               $GLOBALS['Smarty']->assign('LSobject_list_currentpage',0);
434             }
435             $searchData['LSobject_list_nbpage']=ceil($searchData['LSobject_list_nbresult'] / NB_LSOBJECT_LIST);
436             $GLOBALS['Smarty']->assign('LSobject_list_nbpage',$searchData['LSobject_list_nbpage']);
437           }
438           
439           $GLOBALS['LSsession'] -> addJSscript('LSconfirmBox.js');
440           $GLOBALS['LSsession'] -> addCssFile('LSconfirmBox.css');
441           $GLOBALS['LSsession'] -> addJSscript('LSview.js');
442           
443           $GLOBALS['Smarty']->assign('LSview_search',array(
444             'action' => $_SERVER['PHP_SELF'],
445             'submit' => _('Rechercher'),
446             'LSobject' => $LSobject
447           ));
448           
449           $GLOBALS['Smarty']->assign('LSview_search_recur_label',_('Recherche récursive'));
450           $GLOBALS['Smarty']->assign('LSview_search_approx_label',_('Recherche approximative'));
451
452           $GLOBALS['Smarty']->assign('LSobject_list_without_result_label',_("Cette recherche n'a retourné aucun résultat."));
453           $GLOBALS['Smarty']->assign('_Actions',_('Actions'));
454           $GLOBALS['Smarty']->assign('_Modifier',_('Modifier'));
455           $GLOBALS['Smarty']->assign('LSobject_list',$searchData['objectList']);
456           $GLOBALS['Smarty']->assign('LSobject_list_objecttype',$LSobject);
457           $GLOBALS['Smarty'] -> assign('LSview_actions',$searchData['LSview_actions']);
458           $GLOBALS['LSsession'] -> setTemplate('viewList.tpl');
459         }
460       }
461       else {
462         $GLOBALS['LSerror'] -> addErrorCode(1004,$LSobject);
463       }
464     }
465     else {
466       $GLOBALS['LSerror'] -> addErrorCode(1011);
467     }
468   }
469   else {
470     $GLOBALS['LSerror'] -> addErrorCode(1012);
471   }
472 }
473 else {
474   $GLOBALS['LSsession'] -> setTemplate('login.tpl');
475 }
476
477 // Affichage des retours d'erreurs
478 $GLOBALS['LSsession'] -> displayTemplate();
479 ?>