Code

ec72b92592dacb627a36de0db0a46219e1f462da
[gosa.git] / gosa-core / include / class_listing.inc
1 <?php
3 class listing {
5   var $xmlData;
6   var $entries;
7   var $departmentBrowser= false;
8   var $departmentRootVisible= false;
9   var $multiSelect= false;
10   var $template;
11   var $headline;
12   var $module;
13   var $header= array();
14   var $colprops= array();
15   var $filters= array();
16   var $pid;
17   var $objectTypes;
20   function listing($filename)
21   {
22     if (!$this->load($filename)) {
23       die("Cannot parse $filename!");
24     }
26     // Register build in filters
27     $this->registerElementFilter("objectType", "listing::filterObjectType");
28     $this->registerElementFilter("link", "listing::filterLink");
29     $this->registerElementFilter("actions", "listing::filterActions");
31     // Initialize pid
32     $this->pid= preg_replace("/[^0-9]/", "", microtime(TRUE));
33   }
36   function registerElementFilter($name, $call)
37   {
38     if (!isset($this->filters[$name])) {
39       $this->filters[$name]= $call;
40       return true;
41     }
43     return false;
44   }
47   function load($filename)
48   {
49     $contents = file_get_contents($filename);
50     $this->xmlData= xml::xml2array($contents, 1);
52     if (!isset($this->xmlData['list'])) {
53       return false;
54     }
56     $this->xmlData= $this->xmlData["list"];
58     // Load some definition values
59     foreach (array("departmentBrowser", "departmentRootVisible", "multiSelect") as $token) {
60       if (isset($this->xmlData['definition'][$token]) &&
61           $this->xmlData['definition'][$token] == "true"){
62         $this->$token= true;
63       }
64     }
66     // Fill objectTypes
67     if (isset($this->xmlData['definition']['objectType'])) {
68       foreach ($this->xmlData['definition']['objectType'] as $index => $otype) {
69         $this->objectTypes[$otype['objectClass']]= $this->xmlData['definition']['objectType'][$index];
70       }
71     }
73     // Parse layout per column
74     $this->colprops= $this->parseLayout($this->xmlData['table']['layout']);
76     // Prepare table headers
77     $this->header= array();
78     if (isset($this->xmlData['table']['column'])){
79       foreach ($this->xmlData['table']['column'] as $index => $config) {
80         if (isset($config['label'])) {
81           $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">"._($config['label'])."</td>";
82         } else {
83           $this->header[$index]= "<td class='listheader' ".$this->colprops[$index].">&nbsp;</td>";
84         }
85       }
86     }
88     // Assign headline/module
89     $this->headline= _($this->xmlData['definition']['label']);
90     $this->module= $this->xmlData['definition']['module'];
92     return true;  
93   }
96   function render()
97   {
98 echo "filter for images, action menu, sorting, department browsing, filter base handling, bottom list info, copy'n paste handler, snapshot handler<br>";
100     // Initialize list
101     $result= "<input type='hidden' value='$this->pid' name='PID'>";
102     $result.= "<div class='contentboxb' id='listing_container' style='border-top:1px solid #B0B0B0;'>";
103     $result.= "<table summary='$this->headline' style='width:600px;height:450px;' cellspacing='0' id='t_scrolltable'>
104 <tr><td class='scrollhead'><table summary='' style='width:100%;' cellspacing='0' id='t_scrollhead'>";
105     $num_cols= count($this->colprops) + ($this->multiSelect?1:0);
107     // Build list header
108     $result.= "<tr>";
109     if ($this->multiSelect) {
110       $result.= "<td class='listheader' style='width:20px;'><input type='checkbox' id='select_all' name='select_all' title='"._("Select all")."' onClick='toggle_all_(\"listing_selected_[0-9]*$\",\"select_all\");' ></td>";
111     }
112     foreach ($this->header as $header) {
113       $result.= $header;
114     }
116     // Add 13px for scroller
117     $result.= "<td class='listheader' style='width:13px;border-right:0px;'>&nbsp;</td></table></td></tr>";
119     // New table for the real list contents
120     $result.= "<tr><td colspan='$num_cols' class='scrollbody'><div style='width:600px;height:430px;' id='d_scrollbody' class='scrollbody'><table summary='' style='height:100%;width:581px;' cellspacing='0' id='t_scrollbody'>";
122     // No results? Just take an empty colspanned row
123     if (count($this->entries) == 0) {
124       $result.= "<tr class='rowxp0'><td class='list1nohighlight' colspan='$num_cols' style='height:100%;border-right:0px;width:100%;'>&nbsp;</td></tr>";
125     }
127     // Fill with contents
128     foreach ($this->entries as $row => $entry){
129       $result.="<tr class='rowxp".($row&1)."'>";
131       // Render multi select if needed
132       if ($this->multiSelect) {
133         $result.="<td style='text-align:center;width:20px;' class='list0'><input type='checkbox' id='listing_selected_$row' name='listing_selected_$row'></td>";
134       }
136       foreach ($this->xmlData['table']['column'] as $index => $config) {
137         $result.="<td ".$this->colprops[$index]." class='list0'>".$this->renderCell($config['value'], $entry, $row)."</td>";
138       }
139       $result.="</tr>";
140     }
142     // Need to fill the list if it's not full (nobody knows why this is 22 ;-))
143     if (count($this->entries) < 22) {
144       $result.= "<tr>";
145       for ($i= 0; $i<=$num_cols; $i++) {
146         if ($i == 0) {
147           $result.= "<td class='list1nohighlight' style='height:100%;'>&nbsp;</td>";
148           continue;
149         }
150         if ($i != $num_cols-1) {
151           $result.= "<td class='list1nohighlight''>&nbsp;</td>";
152         } else {
153           $result.= "<td class='list1nohighlight' style='border-right:0px'>&nbsp;</td>";
154         }
155       }
156       $result.= "</tr>";
157     }
159     $result.= "</table></div></td></tr></table></div>";
161     $smarty= get_smarty();
162     $smarty->assign("FILTER", $this->filter->render());
163     $smarty->assign("LIST", $result);
165     // Assign navigation elements
166     $nav= $this->renderNavigation();
167     foreach ($nav as $key => $html) {
168       $smarty->assign($key, $html);
169     }
171     // Assign action menu
172     $smarty->assign("ACTIONS", "TBD");
174     // Assign separator
175     $smarty->assign("SEPARATOR", "<img src='images/lists/seperator.png' alt='-' align='middle' height='16' width='1' class='center'>");
177     // Assign summary
178     $smarty->assign("HEADLINE", $this->headline);
180     return ($smarty->fetch(get_template_path($this->xmlData['definition']['template'], true)));
181   }
184   function setFilter($filter)
185   {
186     $this->filter= &$filter;
187     $this->entries= $this->filter->query();
188   }
191   function update()
192   {
193     // Do not do anything if this is not our PID
194     # DISABLED because the object is not in the session
195     #if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
196     #  return;
197     #}
199     $this->entries= $this->filter->query();
200   }
203   function parseLayout($layout)
204   {
205     $result= array();
206     $layout= preg_replace("/^\|/", "", $layout);
207     $layout= preg_replace("/\|$/", "", $layout);
208     $cols= split("\|", $layout);
209     foreach ($cols as $index => $config) {
210       if ($config != "") {
211         $components= split(';', $config);
212         $config= "";
213         foreach ($components as $part) {
214           if (preg_match("/^r$/", $part)) {
215             $config.= "text-align:right;";
216             continue;
217           }
218           if (preg_match("/^l$/", $part)) {
219             $config.= "text-align:left;";
220             continue;
221           }
222           if (preg_match("/^c$/", $part)) {
223             $config.= "text-align:center;";
224             continue;
225           }
226           if (preg_match("/^[0-9]+(|px|%)$/", $part)) {
227             $config.= "width:$part;";
228             continue;
229           }
230         }
232         $result[$index]= " style='$config' ";
233       } else {
234         $result[$index]= null;
235       }
236     }
238     return $result;
239   }
242   function renderCell($data, $config, $row)
243   {
244     // Replace flat attributes in data string
245     for ($i= 0; $i<$config['count']; $i++) {
246       $attr= $config[$i];
247       $value= "";
248       if (is_array($config[$attr])) {
249         $value= $config[$attr][0];
250       } else {
251         $value= $config[$attr];
252       }
253       $data= preg_replace("/%\{$attr\}/", $value, $data);
254     }
256     // Watch out for filters and prepare to execute them
257     $data= $this->processElementFilter($data, $config, $row);
259     return $data;
260   }
263   function processElementFilter($data, $config, $row)
264   {
265     preg_match_all("/%\{filter:([^(]+)\((.*)\)\}/", $data, $matches, PREG_SET_ORDER);
267     foreach ($matches as $match) {
268       if (!isset($this->filters[$match[1]])) {
269         continue;
270       }
271       $cl= preg_replace('/::.*$/', '', $this->filters[$match[1]]);
272       $method= preg_replace('/^.*::/', '', $this->filters[$match[1]]);
274       // Prepare params for function call
275       $params= array();
276       preg_match_all('/"[^"]+"|[^,]+/', $match[2], $parts);
277       foreach ($parts[0] as $param) {
279         // Row is replaced by the row number
280         if ($param == "row") {
281           $params[]= $row;
282         }
284         // pid is replaced by the current PID
285         if ($param == "pid") {
286           $params[]= $this->pid;
287         }
289         // Fixie with "" is passed directly
290         if (preg_match('/^".*"$/', $param)){
291           $params[]= preg_replace('/"/', '', $param);
292         }
294         // LDAP variables get replaced by their objects
295         for ($i= 0; $i<$config['count']; $i++) {
296           if ($param == $config[$i]) {
297             $values= $config[$config[$i]];
298             if (is_array($values)){
299               unset($values['count']);
300             }
301             $params[]= $values;
302           }
303         }
305         // Move dn if needed
306         if ($param == "dn") {
307           $params[]= LDAP::fix($config["dn"]);
308         }
309       }
311       // Replace information
312       if ($cl == "listing") {
313         // Non static call - seems to result in errors
314         $data= @preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($this, "$method"), $params), $data);
315       } else {
316         // Static call
317         $data= preg_replace('/'.preg_quote($match[0]).'/', call_user_func_array(array($cl, $method), $params), $data);
318       }
319     }
321     return $data;
322   }
325   function filterObjectType($dn, $classes)
326   {
327     // Walk thru classes and return on first match
328     $result= "&nbsp;";
329     $prio= 99;
330     foreach ($classes as $objectClass) {
331       if (isset($this->objectTypes[$objectClass])){
332         if (!isset($this->objectTypes[$objectClass]["priority"])){
333           $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$this->objectTypes[$objectClass]["image"]."'>";
334           return $result; 
335         }
337         if ($this->objectTypes[$objectClass]["priority"] < $prio){
338           $prio= $this->objectTypes[$objectClass]["priority"];
339           $result= "<img class='center' title='".LDAP::fix($dn)."' src='".$this->objectTypes[$objectClass]["image"]."'>";
340         }
341       }
342     }
344     return $result;
345   }
348   function filterLink()
349   {
350     $result= "&nbsp;";
352     $row= func_get_arg(0);
353     $pid= func_get_arg(1);
354     $dn= LDAP::fix(func_get_arg(2));
355     $params= array(func_get_arg(3));
357     // Collect sprintf params
358     for ($i = 4;$i < func_num_args();$i++) {
359       $val= func_get_arg($i);
360       if (is_array($val)){
361         $params[]= $val[0];
362         continue;
363       }
364       $params[]= $val;
365     }
367     $result= "&nbsp;";
368     $trans= call_user_func_array("sprintf", $params);
369     if ($trans != "") {
370       return("<a href='?plug=".$_GET['plug']."&amp;PID=$pid&amp;act=listing_edit_$row' title='$dn'>$trans</a>");
371     }
373     return $result;
374   }
377   function renderNavigation()
378   {
379     $result= array();
380     $enableBack = true;
381     $enableRoot = true;
382     $enableHome = true;
384     $ui = get_userinfo();
386     /* Check if base = first available base */
387     $deps = $ui->get_module_departments($this->module);
389     if(!count($deps) || $deps[0] == $this->filter->base){
390       $enableBack = false;
391       $enableRoot = false;
392     }
394     $listhead ="";
396     /* Check if we are in users home  department */
397     if(!count($deps) ||$this->filter->base == get_base_from_people($ui->dn)){
398       $enableHome = false;
399     }
401     /* Draw root button */
402     if($enableRoot){
403       $result["ROOT"]= "<input class='center' type='image' src='images/lists/root.png' align='middle' ".
404                        "title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>";
405     }else{
406       $result["ROOT"]= "<img src='images/lists/root_grey.png' class='center' alt='"._("Root")."'>";
407     }
409     /* Draw back button */
410     if($enableBack){
411       $result["BACK"]= "<input class='center' type='image' align='middle' src='images/lists/back.png' ".
412                        "title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>";
413     }else{
414       $result["BACK"]= "<img src='images/lists/back_grey.png' class='center' alt='"._("Up")."'>";
415     }
417     /* Draw home button */
418     if($enableHome){
419       $result["HOME"]= "<input class='center' type='image' align='middle' src='images/lists/home.png' ".
420                        "title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>";
421     }else{
422       $result["HOME"]= "<img src='images/lists/home_grey.png' class='center' alt='"._("Home")."'>";
423     }
425     /* Draw reload button, this button is enabled everytime */
426     $result["RELOAD"]= "<input class='center' type='image' src='images/lists/reload.png' align='middle' ".
427                        "title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>";
429     return ($result);
430   }
433   function getAction()
434   {
435     // Do not do anything if this is not our PID
436     # DISABLED because the object is not in the session
437     #if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->pid) {
438     #  return;
439     #}
441     $result= array("targets" => array(), "action" => "");
443     // Filter GET with "act" attributes
444     if (isset($_GET['act'])) {
445       $key= validate($_GET['act']);
446       $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)$/', '$1', $key);
447       if (isset($this->entries[$target]['dn'])) {
448         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+$/', '$1', $key);
449         $result['targets'][]= $this->entries[$target]['dn'];
450       }
451       return $result;
452     }
454     // Filter POST with "listing_" attributes
455     foreach ($_POST as $key => $prop) {
457       // Capture selections
458       if (preg_match('/^listing_selected_[0-9]+$/', $key)) {
459         $target= preg_replace('/^listing_selected_([0-9]+)$/', '$1', $key);
460         if (isset($this->entries[$target]['dn'])) {
461           $result['targets'][]= $this->entries[$target]['dn'];
462         }
463         continue;
464       }
466       // Capture action with target - this is a one shot
467       if (preg_match('/^listing_[a-zA-Z_]+_[0-9]+(|_x)$/', $key)) {
468         $target= preg_replace('/^listing_[a-zA-Z_]+_([0-9]+)(|_x)$/', '$1', $key);
469         if (isset($this->entries[$target]['dn'])) {
470           $result['action']= preg_replace('/^listing_([a-zA-Z_]+)_[0-9]+(|_x)$/', '$1', $key);
471           $result['targets']= array($this->entries[$target]['dn']);
472         }
473         break;
474       }
476       // Capture action without target
477       if (preg_match('/^listing_[a-zA-Z_]+(|_x)$/', $key)) {
478         $result['action']= preg_replace('/^listing_([a-zA-Z_]+)(|_x)$/', '$1', $key);
479         continue;
480       }
481     }
483     return $result;
484   }
488 ?>