- Ajout d'un fichier core.php s'occupant des inclusions minimum pour le lancement...
[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 'core.php';
24
25 if(LSsession :: startLSsession()) {
26   if (isset($_REQUEST['LSobject'])) {
27     $LSobject = $_REQUEST['LSobject'];
28     $dn = $_REQUEST['dn'];
29     
30     if (LSsession :: in_menu($LSobject)) {
31     
32       if ( $LSobject == 'SELF' ) {
33         $LSobject = LSsession :: getLSuserObject() -> getType();
34         $dn = LSsession :: getLSuserObjectDn();
35       }
36       
37       if ( LSsession :: loadLSobject($LSobject) ) {
38         // Affichage d'un objet
39         if ( $dn!='' ) {
40           if (LSsession :: canAccess($LSobject,$dn)) {
41             if ( LSsession :: canEdit($LSobject,$dn) ) {
42               $LSview_actions[] = array(
43                 'label' => _('Modify'),
44                 'url' =>'modify.php?LSobject='.$LSobject.'&amp;dn='.$dn,
45                 'action' => 'modify'
46               );
47             }
48             
49             if (LSsession :: canCreate($LSobject)) {
50               $LSview_actions[] = array(
51                 'label' => _('Copy'),
52                 'url' =>'create.php?LSobject='.$LSobject.'&amp;load='.$dn,
53                 'action' => 'copy'
54               );
55             }
56             
57             if (LSsession :: canRemove($LSobject,$dn)) {
58               $LSview_actions[] = array(
59                 'label' => _('Delete'),
60                 'url' => 'remove.php?LSobject='.$LSobject.'&amp;dn='.$dn,
61                 'action' => 'delete'
62               );
63             }
64             
65             if (LSsession :: getLSuserObjectDn() != $dn) {
66               $object = new $LSobject();
67               $object -> loadData($dn);
68               $GLOBALS['Smarty'] -> assign('pagetitle',$object -> getDisplayName());
69             }
70             else {
71               $object = LSsession :: getLSuserObject();
72               $GLOBALS['Smarty'] -> assign('pagetitle',_('My account'));
73             }
74             
75             $view = $object -> getView();
76             $view -> displayView();
77             
78             // LSrelations
79             if (LSsession :: loadLSclass('LSrelation')) {
80               LSrelation :: displayInLSview($object);
81             }
82             
83             $GLOBALS['Smarty'] -> assign('LSview_actions',$LSview_actions);
84             LSsession :: setTemplate('view.tpl');
85           }
86           else {
87             LSerror :: addErrorCode('LSsession_11');
88           }
89         }
90         // Affichage d'une liste d'un type d'objet
91         else {
92           $objectList=array();
93           $object = new $LSobject();
94           
95           $GLOBALS['Smarty']->assign('pagetitle',$object -> getLabel());
96           $GLOBALS['Smarty']->assign('LSobject_list_objectname',$object -> getLabel());
97           
98           if (isset($_SESSION['LSsession']['LSsearch'][$LSobject])) {
99             $filter = $_SESSION['LSsession']['LSsearch'][$LSobject]['filter'];
100             $params = $_SESSION['LSsession']['LSsearch'][$LSobject]['params'];
101             $pattern = $_SESSION['LSsession']['LSsearch'][$LSobject]['pattern'];
102             $recur = $_SESSION['LSsession']['LSsearch'][$LSobject]['recur'];
103             if ($recur) {
104               $topDn = LSsession :: getTopDn();
105             }
106             else {
107               $topDn = $object -> config['container_dn'].','.LSsession :: getTopDn();
108             }
109             $approx = $_SESSION['LSsession']['LSsearch'][$LSobject]['approx'];
110             $orderby = $_SESSION['LSsession']['LSsearch'][$LSobject]['orderby'];
111             $ordersense = $_SESSION['LSsession']['LSsearch'][$LSobject]['ordersense'];
112             $doSubDn = $_SESSION['LSsession']['LSsearch'][$LSobject]['doSubDn'];
113           }
114           else {
115             $filter = NULL;
116             $topDn = $object -> config['container_dn'].','.LSsession :: getTopDn();
117             $params = array('scope' => 'one');
118             $pattern = false;
119             $recur = false;
120             $approx = false;
121             $orderby = false;
122             $_REQUEST['orderby']=$GLOBALS['LSobjects'][$LSobject]['orderby'];
123             $ordersense = 'ASC';
124             $subDnLdapServer = LSsession :: getSubDnLdapServer();
125             $doSubDn = (($subDnLdapServer)&&(!LSsession :: isSubDnLSobject($LSobject)));
126           }
127           
128           if (isset($_REQUEST['LSview_search_submit'])) {
129             if (isset($_REQUEST['LSview_pattern']) && ($_REQUEST['LSview_pattern']!=$pattern)) {
130               $pattern = $_REQUEST['LSview_pattern'];
131             }
132
133             $approx = (isset($_REQUEST['LSview_approx']));
134             
135             if ($pattern && $pattern!='') {
136               $filter=$object -> getPatternFilter($pattern,$approx);
137             }
138             else {
139               $filter = NULL;
140             }
141             
142             if (isset($_REQUEST['LSview_recur'])) {
143               $recur = true;
144               $params['scope'] = 'sub';
145               $topDn = LSsession :: getTopDn();
146             }
147             else {
148               $recur = false;
149               $params['scope'] = 'one';
150               $topDn = $object -> config['container_dn'].','.LSsession :: getTopDn();
151             }
152           }
153           
154           $sort=false;
155           if ((isset($_REQUEST['orderby']))) {
156             $possible_values= array('displayName','subDn');
157             if (in_array($_REQUEST['orderby'],$possible_values)) {
158               $sort=true;
159               if ($orderby==$_REQUEST['orderby']) {
160                 $ordersense = ($ordersense=='ASC')?'DESC':'ASC';
161               }
162               else {
163                 $ordersense = 'ASC';
164               }
165               $orderby=$_REQUEST['orderby'];
166             }
167           }
168           
169           $GLOBALS['Smarty']->assign('LSobject_list_subDn',$doSubDn);
170           
171           // Sauvegarde en Session
172           $_SESSION['LSsession']['LSsearch'][$LSobject] = array(
173             'filter' => $filter,
174             'topDn' => $topDn,
175             'params' => $params,
176             'pattern' => $pattern,
177             'recur' => $recur,
178             'approx' => $approx,
179             'orderby' => $orderby,
180             'ordersense' => $ordersense,
181             'doSubDn' => $doSubDn
182           );
183
184           $GLOBALS['Smarty']->assign('LSview_search_pattern',$pattern);
185
186           if ($recur) {
187             $GLOBALS['Smarty']->assign('LSview_search_recur',true);
188           }
189           if ($approx) {
190             $GLOBALS['Smarty']->assign('LSview_search_approx',true);
191           }
192           
193           // Hidden fields
194           $GLOBALS['Smarty']->assign('LSview_search_hidden_fields',array(
195             'LSobject' => $LSobject,
196             'LSview_search_submit' => 1
197           ));
198           
199           // Hash de la recherche déterminer à partir des paramètres de la recherche
200           $hash = mhash (MHASH_MD5, 
201             print_r(
202               array(
203                 'LSobject' => $LSobject,
204                 'filter' => $filter,
205                 'topDn' => $topDn,
206                 'params' => $params
207               ),
208               true
209             )
210           );
211           
212           if ((LSsession :: cacheSearch()) && isset($_SESSION['LSsession']['LSsearch'][$hash]) && (!isset($_REQUEST['refresh']))) {
213             // On affiche à partir du cache
214             $searchData=$_SESSION['LSsession']['LSsearch'][$hash];
215             LSdebug('Find : From cache');
216             if(!isset($searchData['LSview_actions']['create'])) {
217               LSdebug('Recherche : Check Create()');
218               if (LSsession :: canCreate($LSobject)) {
219                 $searchData['LSview_actions']['create'] = array (
220                   'label' => 'New',
221                   'url' => 'create.php?LSobject='.$LSobject,
222                   'action' => 'create'
223                 );
224               }
225               else {
226                 $searchData['LSview_actions']['create'] = false;
227               }
228               $_SESSION['LSsession']['LSsearch'][$hash]=$searchData;
229             }
230           }
231           else { // Load
232             LSdebug('Find : Load');
233             if (LSsession :: canCreate($LSobject)) {
234               $LSview_actions['create'] = array (
235                 'label' => 'New',
236                 'url' => 'create.php?LSobject='.$LSobject,
237                 'action' => 'create'
238               );
239               $canCopy=true;
240             }
241             else {
242               $LSview_actions['create'] = false;
243             }
244             $LSview_actions['refresh'] = array (
245               'label' => 'Refresh',
246               'url' => 'view.php?LSobject='.$LSobject.'&amp;refresh',
247               'action' => 'refresh'
248             );
249             
250             $list=$object -> listObjectsName($filter,$topDn,$params);
251
252             $nbObjects=0;
253             foreach($list as $objDn => $objName) {
254               if (LSsession :: canAccess($LSobject,$objDn)) {
255                 $subDn_name=false;
256                 if ($doSubDn) {
257                   $subDn_name = $object -> getSubDnName($objDn);
258                 }
259                 $nbObjects++;
260
261                 
262                 $objectList[]=array(
263                   'dn' => $objDn,
264                   'displayName' => $objName,
265                   'subDn' => $subDn_name
266                 );
267               }
268             }
269             
270             $searchData['LSobject_list_nbresult']=$nbObjects;
271             
272             $searchData['objectList']=$objectList;
273             $searchData['LSview_actions'] = $LSview_actions;
274             if ($orderby) {
275               $sort=true;
276             }
277           } // Fin Load
278           
279           if ((!isset($searchData['objectList'][0]['actions']))&&(!empty($searchData['objectList']))) {
280             LSdebug('Load actions');
281             for($i=0;$i<$searchData['LSobject_list_nbresult'];$i++) {
282               $actions=array();
283               
284               $actions[] = array(
285                 'label' => _('View'),
286                 'url' =>'view.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
287                 'action' => 'view'
288               );
289               
290               if (LSsession :: canEdit($LSobject,$searchData['objectList'][$i]['dn'])) {
291                 $actions[]=array(
292                   'label' => _('Modify'),
293                   'url' => 'modify.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
294                   'action' => 'modify'
295                 );
296               }
297               
298               if ($canCopy) {
299                 $actions[] = array(
300                   'label' => _('Copy'),
301                   'url' =>'create.php?LSobject='.$LSobject.'&amp;load='.$searchData['objectList'][$i]['dn'],
302                   'action' => 'copy'
303                 );
304               }
305               
306               if (LSsession :: canRemove($LSobject,$searchData['objectList'][$i]['dn'])) {
307                 $actions[] = array (
308                   'label' => _('Delete'),
309                   'url' => 'remove.php?LSobject='.$LSobject.'&amp;dn='.$searchData['objectList'][$i]['dn'],
310                   'action' => 'delete'
311                 );
312               }
313               $searchData['objectList'][$i]['actions']=$actions;
314             }
315           }
316           
317           $GLOBALS['Smarty']->assign('LSobject_list_nbresult',$searchData['LSobject_list_nbresult']);
318           
319           // Order by if $sort
320           if ($sort) {
321             function sortBy($a,$b) {
322               global $ordersense;
323               global $orderby;
324               
325               if ($ordersense=='ASC') {
326                 $sense = -1;
327               }
328               else {
329                 $sense = 1;
330               }
331               
332               if ($a == $b) return 0;
333               $sort = array($a[$orderby],$b[$orderby]);
334               sort($sort);
335               if ($sort[0]==$a[$orderby])
336                 return 1*$sense;
337               return -1*$sense;
338             }
339             if (!uasort($searchData['objectList'],'sortBy')) {
340               LSdebug('Error while sorting.');
341             }
342           } // Fin Order by
343           $GLOBALS['Smarty']->assign('LSobject_list_orderby',$orderby);
344           $GLOBALS['Smarty']->assign('LSobject_list_ordersense',$ordersense);
345           
346           // Mise en cache
347           if (LSsession :: cacheSearch()) {
348             $_SESSION['LSsession']['LSsearch'][$hash]=$searchData;
349           }
350           
351           // Pagination
352           if ($searchData['LSobject_list_nbresult'] > NB_LSOBJECT_LIST) {
353             if (isset($_REQUEST['page'])) {
354               $searchData['objectList'] = array_slice($searchData['objectList'], ($_REQUEST['page']) * NB_LSOBJECT_LIST, NB_LSOBJECT_LIST);
355               $GLOBALS['Smarty']->assign('LSobject_list_currentpage',$_REQUEST['page']);
356               
357             }
358             else {
359               $searchData['objectList'] = array_slice($searchData['objectList'], 0, NB_LSOBJECT_LIST);
360               $GLOBALS['Smarty']->assign('LSobject_list_currentpage',0);
361             }
362             $searchData['LSobject_list_nbpage']=ceil($searchData['LSobject_list_nbresult'] / NB_LSOBJECT_LIST);
363             $GLOBALS['Smarty']->assign('LSobject_list_nbpage',$searchData['LSobject_list_nbpage']);
364           } // Fin Pagination
365           
366           if (LSsession :: loadLSclass('LSform')) {
367             LSform :: loadDependenciesDisplayView();
368           }
369           
370           $GLOBALS['Smarty']->assign('LSview_search',array(
371             'action' => $_SERVER['PHP_SELF'],
372             'submit' => _('Search'),
373             'LSobject' => $LSobject
374           ));
375           
376           $GLOBALS['Smarty']->assign('LSview_search_recur_label',_('Recursive search'));
377           $GLOBALS['Smarty']->assign('LSview_search_approx_label',_('Approximative search'));
378
379           $GLOBALS['Smarty']->assign('LSobject_list_without_result_label',_("This search didn't get any result."));
380           $GLOBALS['Smarty']->assign('_Actions',_('Actions'));
381           $GLOBALS['Smarty']->assign('_Modifier',_('Modify'));
382           $GLOBALS['Smarty']->assign('LSobject_list',$searchData['objectList']);
383           $GLOBALS['Smarty']->assign('LSobject_list_objecttype',$LSobject);
384           $GLOBALS['Smarty'] -> assign('LSview_actions',$searchData['LSview_actions']);
385           LSsession :: setTemplate('viewList.tpl');
386         }
387       }
388     }
389     else {
390       LSerror :: addErrorCode('LSsession_11');
391     }
392   }
393   else {
394     LSerror :: addErrorCode('LSsession_12');
395   }
396 }
397 else {
398   LSsession :: setTemplate('login.tpl');
399 }
400
401 // Affichage des retours d'erreurs
402 LSsession :: displayTemplate();
403 ?>