d56db47f5be444872f5cb5db9ddb341a54a248a8
[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                   
124                   if($GLOBALS['LSsession'] -> loadLSobject($relationConf['LSobject'])) {
125                     if (method_exists($relationConf['LSobject'],$relationConf['list_function'])) {
126                       $objRel = new $relationConf['LSobject']();
127                       $list = $objRel -> $relationConf['list_function']($object);
128                       if (is_array($list)) {
129                         foreach($list as $o) {
130                           $o_infos = array(
131                             'text' => $o -> getDisplayValue(NULL,true),
132                             'dn' => $o -> getDn()
133                           );
134                           $return['objectList'][] = $o_infos;
135                         }
136                       }
137                       else {
138                         $return['objectList']=array();
139                       }
140                     }
141                     else {
142                       $GLOBALS['LSerror'] -> addErrorCode(1013,$relationName);
143                     }
144                     $LSrelations[]=$return;
145                   }
146                   else {
147                       $GLOBALS['LSerror'] -> addErrorCode(1016,array('relation' => $relationName,'LSobject' => $relationConf['LSobject']));
148                   }
149                 }
150               }
151               
152               $GLOBALS['LSsession'] -> addJSscript('LSconfirmBox.js');
153               $GLOBALS['LSsession'] -> addCssFile('LSconfirmBox.css');
154               $GLOBALS['Smarty'] -> assign('LSrelations',$LSrelations);
155               $GLOBALS['LSsession'] -> addJSconfigParam('LSrelations',$LSrelations_JSparams);
156             }
157             
158             $GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions);
159             $GLOBALS['LSsession'] -> addJSscript('LSsmoothbox.js');
160             $GLOBALS['LSsession'] -> addCssFile('LSsmoothbox.css');
161             $GLOBALS['LSsession'] -> setTemplate('view.tpl');
162           }
163           else {
164             $GLOBALS['LSerror'] -> addErrorCode(1011);
165           }
166         }
167         // Affichage d'une liste d'un type d'objet
168         else {
169           $objectList=array();
170           $object = new $LSobject();
171           
172           $GLOBALS['Smarty']->assign('pagetitle',$object -> getLabel());
173           $GLOBALS['Smarty']->assign('LSobject_list_objectname',$object -> getLabel());
174           
175           if (isset($_SESSION['LSsession']['LSsearch'][$LSobject])) {
176             $filter = $_SESSION['LSsession']['LSsearch'][$LSobject]['filter'];
177             $params = $_SESSION['LSsession']['LSsearch'][$LSobject]['params'];
178             $pattern = $_SESSION['LSsession']['LSsearch'][$LSobject]['pattern'];
179             $recur = $_SESSION['LSsession']['LSsearch'][$LSobject]['recur'];
180             if ($recur) {
181               $topDn = $GLOBALS['LSsession'] -> topDn;
182             }
183             else {
184               $topDn = $object -> config['container_dn'].','.$GLOBALS['LSsession'] -> topDn;
185             }
186             $approx = $_SESSION['LSsession']['LSsearch'][$LSobject]['approx'];
187             $orderby = $_SESSION['LSsession']['LSsearch'][$LSobject]['orderby'];
188             $ordersense = $_SESSION['LSsession']['LSsearch'][$LSobject]['ordersense'];
189             $doSubDn = $_SESSION['LSsession']['LSsearch'][$LSobject]['doSubDn'];
190           }
191           else {
192             $filter = NULL;
193             $topDn = $object -> config['container_dn'].','.$GLOBALS['LSsession'] -> topDn;
194             $params = array('scope' => 'one');
195             $pattern = false;
196             $recur = false;
197             $approx = false;
198             $orderby = false;
199             $_REQUEST['orderby']=$GLOBALS['LSobjects'][$LSobject]['orderby'];
200             $ordersense = 'ASC';
201             $subDnLdapServer = $GLOBALS['LSsession'] -> getSubDnLdapServer();
202             $doSubDn = (($subDnLdapServer)&&(!$GLOBALS['LSsession']->isSubDnLSobject($LSobject)));
203           }
204           
205           if (isset($_REQUEST['LSview_search_submit'])) {
206             if (isset($_REQUEST['LSview_pattern']) && ($_REQUEST['LSview_pattern']!=$pattern)) {
207               $pattern = $_REQUEST['LSview_pattern'];
208             }
209
210             $approx = (isset($_REQUEST['LSview_approx']));
211             
212             if ($pattern && $pattern!='') {
213               $filter='(|';
214               if ($approx) {
215                 foreach ($object -> attrs as $attr_name => $attr_val) {
216                   $filter.='('.$attr_name.'~='.$pattern.')';
217                 }
218               }
219               else {
220                 foreach ($object -> attrs as $attr_name => $attr_val) {
221                   $filter.='('.$attr_name.'=*'.$pattern.'*)';
222                 }
223               }
224               $filter.=')';
225             }
226             else {
227               $filter = NULL;
228             }
229             
230             if (isset($_REQUEST['LSview_recur'])) {
231               $recur = true;
232               $params['scope'] = 'sub';
233               $topDn = $GLOBALS['LSsession'] -> topDn;
234             }
235             else {
236               $recur = false;
237               $params['scope'] = 'one';
238               $topDn = $object -> config['container_dn'].','.$GLOBALS['LSsession'] -> topDn;
239             }
240           }
241           
242           $sort=false;
243           if ((isset($_REQUEST['orderby']))) {
244             $possible_values= array('displayValue','subDn');
245             if (in_array($_REQUEST['orderby'],$possible_values)) {
246               $sort=true;
247               if ($orderby==$_REQUEST['orderby']) {
248                 $ordersense = ($ordersense=='ASC')?'DESC':'ASC';
249               }
250               else {
251                 $ordersense = 'ASC';
252               }
253               $orderby=$_REQUEST['orderby'];
254             }
255           }
256           
257           $GLOBALS['Smarty']->assign('LSobject_list_subDn',$doSubDn);
258           
259           // Sauvegarde en Session
260           $_SESSION['LSsession']['LSsearch'][$LSobject] = array(
261             'filter' => $filter,
262             'topDn' => $topDn,
263             'params' => $params,
264             'pattern' => $pattern,
265             'recur' => $recur,
266             'approx' => $approx,
267             'orderby' => $orderby,
268             'ordersense' => $ordersense,
269             'doSubDn' => $doSubDn
270           );
271
272           $GLOBALS['Smarty']->assign('LSview_search_pattern',$pattern);
273
274           if ($recur) {
275             $GLOBALS['Smarty']->assign('LSview_search_recur',true);
276           }
277           if ($approx) {
278             $GLOBALS['Smarty']->assign('LSview_search_approx',true);
279           }
280           
281           // Hidden fields
282           $GLOBALS['Smarty']->assign('LSview_search_hidden_fields',array(
283             'LSobject' => $LSobject,
284             'LSview_search_submit' => 1
285           ));
286           
287           // Hash de la recherche déterminer à partir des paramètres de la recherche
288           $hash = mhash (MHASH_MD5, 
289             print_r(
290               array(
291                 'LSobject' => $LSobject,
292                 'filter' => $filter,
293                 'topDn' => $topDn,
294                 'params' => $params
295               ),
296               true
297             )
298           );
299           
300           if (($GLOBALS['LSsession'] -> cacheSearch()) && isset($_SESSION['LSsession']['LSsearch'][$hash]) && (!isset($_REQUEST['refresh']))) {
301             // On affiche à partir du cache
302             $searchData=$_SESSION['LSsession']['LSsearch'][$hash];
303             LSdebug('Recherche : From cache');
304           }
305           else {
306             LSdebug('Recherche : Load');
307             if ($GLOBALS['LSsession'] -> canCreate($LSobject)) {
308               $LSview_actions[] = array (
309                 'label' => _('Nouveau'),
310                 'url' => 'create.php?LSobject='.$LSobject,
311                 'action' => 'create'
312               );
313               $canCopy=true;
314             }
315             $LSview_actions[] = array (
316               'label' => _('Rafraîchir'),
317               'url' => 'view.php?LSobject='.$LSobject.'&amp;refresh',
318               'action' => 'refresh'
319             );
320             
321             $list=$object -> listObjectsName($filter,$topDn,$params);
322
323             $nbObjects=0;
324             foreach($list as $objDn => $objName) {
325               if ($GLOBALS['LSsession'] -> canAccess($LSobject,$objDn)) {
326                 $subDn_name=false;
327                 if ($doSubDn) {
328                   $subDn_name = $object -> getSubDnName($objDn);
329                 }
330                 $nbObjects++;
331
332                 
333                 $objectList[]=array(
334                   'dn' => $objDn,
335                   'displayValue' => $objName,
336                   'subDn' => $subDn_name
337                 );
338               }
339             }
340             
341             $searchData['LSobject_list_nbresult']=$nbObjects;
342             
343             $searchData['objectList']=$objectList;
344             $searchData['LSview_actions'] = $LSview_actions;
345             if ($orderby) {
346               $sort=true;
347             }
348           }
349           
350           if ((!isset($searchData['objectList'][0]['actions']))&&(!empty($searchData['objectList']))) {
351             LSdebug('Load actions');
352             for($i=0;$i<$searchData['LSobject_list_nbresult'];$i++) {
353               $actions=array();
354               
355               $actions[] = array(
356                 'label' => _('Voir'),
357                 'url' =>'view.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
358                 'action' => 'view'
359               );
360               
361               if ($GLOBALS['LSsession'] -> canEdit($LSobject,$searchData['objectList'][$i]['dn'])) {
362                 $actions[]=array(
363                   'label' => _('Modifier'),
364                   'url' => 'modify.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
365                   'action' => 'modify'
366                 );
367               }
368               
369               if ($canCopy) {
370                 $actions[] = array(
371                   'label' => _('Copier'),
372                   'url' =>'create.php?LSobject='.$LSobject.'&amp;load='.$searchData['objectList'][$i]['dn'],
373                   'action' => 'copy'
374                 );
375               }
376               
377               if ($GLOBALS['LSsession'] -> canRemove($LSobject,$searchData['objectList'][$i]['dn'])) {
378                 $actions[] = array (
379                   'label' => _('Supprimer'),
380                   'url' => 'remove.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
381                   'action' => 'delete'
382                 );
383               }
384               $searchData['objectList'][$i]['actions']=$actions;
385             }
386           }
387           
388           $GLOBALS['Smarty']->assign('LSobject_list_nbresult',$searchData['LSobject_list_nbresult']);
389           
390           // Order by if $sort
391           if ($sort) {
392             function sortBy($a,$b) {
393               global $ordersense;
394               global $orderby;
395               
396               if ($ordersense=='ASC') {
397                 $sense = -1;
398               }
399               else {
400                 $sense = 1;
401               }
402               
403               if ($a == $b) return 0;
404               $sort = array($a[$orderby],$b[$orderby]);
405               sort($sort);
406               if ($sort[0]==$a[$orderby])
407                 return 1*$sense;
408               return -1*$sense;
409             }
410             if (!uasort($searchData['objectList'],'sortBy')) {
411               LSdebug('Erreur durant le trie.');
412             }
413           }
414           $GLOBALS['Smarty']->assign('LSobject_list_orderby',$orderby);
415           $GLOBALS['Smarty']->assign('LSobject_list_ordersense',$ordersense);
416           
417           if ($GLOBALS['LSsession'] -> cacheSearch()) {
418             $_SESSION['LSsession']['LSsearch'][$hash]=$searchData;
419           }
420           
421           // Pagination
422           if ($searchData['LSobject_list_nbresult'] > NB_LSOBJECT_LIST) {
423             if (isset($_REQUEST['page'])) {
424               $searchData['objectList'] = array_slice($searchData['objectList'], ($_REQUEST['page']) * NB_LSOBJECT_LIST, NB_LSOBJECT_LIST);
425               $GLOBALS['Smarty']->assign('LSobject_list_currentpage',$_REQUEST['page']);
426               
427             }
428             else {
429               $searchData['objectList'] = array_slice($searchData['objectList'], 0, NB_LSOBJECT_LIST);
430               $GLOBALS['Smarty']->assign('LSobject_list_currentpage',0);
431             }
432             $searchData['LSobject_list_nbpage']=ceil($searchData['LSobject_list_nbresult'] / NB_LSOBJECT_LIST);
433             $GLOBALS['Smarty']->assign('LSobject_list_nbpage',$searchData['LSobject_list_nbpage']);
434           }
435           
436           $GLOBALS['LSsession'] -> addJSscript('LSview.js');
437           
438           $GLOBALS['Smarty']->assign('LSview_search',array(
439             'action' => $_SERVER['PHP_SELF'],
440             'submit' => _('Rechercher'),
441             'LSobject' => $LSobject
442           ));
443           
444           $GLOBALS['Smarty']->assign('LSview_search_recur_label',_('Recherche récursive'));
445           $GLOBALS['Smarty']->assign('LSview_search_approx_label',_('Recherche approximative'));
446
447           $GLOBALS['Smarty']->assign('LSobject_list_without_result_label',_("Cette recherche n'a retourné aucun résultat."));
448           $GLOBALS['Smarty']->assign('_Actions',_('Actions'));
449           $GLOBALS['Smarty']->assign('_Modifier',_('Modifier'));
450           $GLOBALS['Smarty']->assign('LSobject_list',$searchData['objectList']);
451           $GLOBALS['Smarty']->assign('LSobject_list_objecttype',$LSobject);
452           $GLOBALS['Smarty'] -> assign('LSview_actions',$searchData['LSview_actions']);
453           $GLOBALS['LSsession'] -> setTemplate('viewList.tpl');
454         }
455       }
456       else {
457         $GLOBALS['LSerror'] -> addErrorCode(1004,$LSobject);
458       }
459     }
460     else {
461       $GLOBALS['LSerror'] -> addErrorCode(1011);
462     }
463   }
464   else {
465     $GLOBALS['LSerror'] -> addErrorCode(1012);
466   }
467 }
468 else {
469   $GLOBALS['LSsession'] -> setTemplate('login.tpl');
470 }
471
472 // Affichage des retours d'erreurs
473 $GLOBALS['LSsession'] -> displayTemplate();
474 ?>