Code

Updated filters. Made list display non red framed icons.
[gosa.git] / gosa-core / include / class_sortableListing.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2010 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 define ('LIST_NORMAL', 0);
24 define ('LIST_MARKED', 1);
25 define ('LIST_DISABLED', 2);
27 class sortableListing {
28   private $header= null;
29   private $colspecs= null;
30   private $reorderable= true;
31   private $width= "400px";
32   private $height= "100px";
33   private $cssclass= "";
34   private $id;
36   private $data= array();
37   private $keys= array();
38   private $modes= array();
39   private $displayData= array();
40   private $columns= 0;
41   private $deleteable= false;
42   private $editable= false;
43   private $colorAlternate= false;
44   private $instantDelete= true;
45   private $action;
46   private $mapping;
47   private $current_mapping;
48   private $active_index;
49   private $scrollPosition= 0;
50   private $sortColumn= 0;
51   private $sortDirection= array();
53   private $acl= "";
54   private $modified= false;
56   public function sortableListing($data= array(), $displayData= null, $reorderable= false)
57   {
58     global $config;
60     // Save data to display
61     $this->setListData($data, $displayData);
63     // Generate instance wide unique ID
64     $tmp= gettimeofday();
65     $this->id= 'l'.md5($tmp['sec']);
67     // Set reorderable flag
68     $this->reorderable= $reorderable;
69     if (!$reorderable) {
70       $this->sortData();
71     }
72   }
75   public function setListData($data, $displayData= null)
76   {
77     // Save data to display
78     $this->setData($data);
79     if (!$displayData) {
80       $displayData= array();
81       foreach ($data as $key => $value) {
82         $displayData[$key]= array("data" => array($value));
83       }
84     }
85     $this->setDisplayData($displayData);
86   }
89   private function setData($data)
90   {
91     $this->data= $data;
92   }
95   private function setDisplayData($data)
96   {
97     if (!is_array($data)) {
98       die ("sortableList needs an array as data!");
99     }
101     // Transfer information
102     $this->displayData= array();
103     $this->modes= array();
104     foreach ($data as $key => $value) {
105       $this->displayData[]= $value['data'];
106       if (isset($value['mode'])) {
107         $this->modes[]= $value['mode'];
108       }
109     }
110     $this->keys= array_keys($data);
112     // Create initial mapping
113     $this->mapping= range(0, abs(count($this->keys)-1));
114     $this->current_mapping= $this->mapping;
116     // Find the number of coluns
117     reset($this->displayData);
118     $first= current($this->displayData);
119     if (is_array($first)) {
120       $this->columns= count($first);
121     } else {
122       $this->columns= 1;
123     }
125     // Preset sort orders to 'down'
126     for ($column= 0; $column<$this->columns; $column++) {
127       $this->sortDirection[]= true;
128     }
129   }
132   public function setWidth($width)
133   {
134     $this->width= $width;
135   }
138   public function setInstantDelete($flag)
139   {
140     $this->instantDelete= $flag;
141   }
144   public function setColorAlternate($flag)
145   {
146     $this->colorAlternate= $flag;
147   }
150   public function setEditable($flag)
151   {
152     $this->editable= $flag;
153   }
156   public function setDeleteable($flag)
157   {
158     $this->deleteable= $flag;
159   }
162   public function setHeight($height)
163   {
164     $this->height= $height;
165   }
168   public function setCssClass($css)
169   {
170     $this->cssclass= $css;
171   }
174   public function setHeader($header)
175   {
176     $this->header= $header;
177   }
180   public function setColspecs($specs)
181   {
182     $this->colspecs= $specs;
183   }
186   public function render()
187   {
188     $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='min-width:".$this->width.";height: ".$this->height."'>\n";
189     $result.= "<table summary='"._("Sortable list")."' border='0' cellpadding='0' cellspacing='0' width='100%' style='width:100%' ".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
190     $action_width= 0;
191     if (strpos($this->acl, 'w') === false) {
192       $edit_image= $this->editable?image("images/lists/edit-grey.png"):"";
193     } else {
194       $edit_image= $this->editable?image('images/lists/edit.png', "%ID", _("Edit this entry")):"";
195     }
196     if (strpos($this->acl, 'd') === false) {
197       $delete_image= $this->deleteable?image('images/lists/trash-grey.png'):"";
198     } else {
199       $delete_image= $this->deleteable?image('images/lists/trash.png', "%ID", _("Delete this entry")):"";
200     }
202     // Do we need colspecs?
203     $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
204     if ($this->colspecs) {
205       $result.= " <colgroup>\n";
206       for ($i= 0; $i<$this->columns; $i++) {
207         if(isset($this->colspecs[$i]) && $this->colspecs[$i] != '*'){
208           $result.= "  <col style='width:".($this->colspecs[$i])."'>\n";
209         }else{
210           $result.= "  <col>\n";
211         }
212       }
214       // Extend by another column if we've actions specified
215       if ($action_width) {
216         $result.= "  <col style='width:".$action_width."px' >\n";
217       }
218       $result.= " </colgroup>\n";
219     }
221     // Do we need a header?
222     if ($this->header) {
223       $result.= " <thead>\n  <tr>\n";
224       $first= " style='border:0'";
225       for ($i= 0; $i<$this->columns; $i++) {
226         $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->id."&amp;act=SORT_$i'";
227         $sorter= "";
228         if ($i == $this->sortColumn){
229           $sorter= "&nbsp;".image("images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png", null, $this->sortDirection[$i]?_("Up"):_("Down"));
230         }
232         if ($this->reorderable) {
233           $result.= "   <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
234         } else {
235           $result.= "   <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."$sorter</a></th>";
236         }
237         $first= "";
238       }
239       if ($action_width) {
240         $result.= "<th>&nbsp;</th>";
241       }
242       $result.= "\n  </tr>\n </thead>\n";
243     }
245     // Render table body if we've read permission
246     $result.= " <tbody id='".$this->id."'>\n";
247     $reorderable= $this->reorderable?"":" style='cursor:default'";
248     if (strpos($this->acl, 'r') !== false) {
249       foreach ($this->mapping as $nr => $row) {
250         $editable= $this->editable?" onclick='$(\"edit_".$this->id."_$nr\").click()'":"";
252         $id= "";
253         if (isset($this->modes[$row])) {
254           switch ($this->modes[$row]) {
255             case LIST_DISABLED:
256               $id= " sortableListItemDisabled";
257               $editable= "";
258               break;
259             case LIST_MARKED:
260               $id= " sortableListItemMarked";
261               break;
262           }
263         }
265         $result.= "  <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."$id' id='item_".$this->id."_$nr'$reorderable>\n";
266         $first= " style='border:0'";
268         foreach ($this->displayData[$row] as $column) {
269           $result.= "   <td$editable$first>".$column."</td>\n";
270           $first= "";
271         }
273         if ($action_width) {
274           $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
275                            str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
276         }
278         $result.= "  </tr>\n";
279       }
280     }
282     // Add spacer
283     $result.= "  <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
284     $num= $action_width?$this->columns:$this->columns-1;
285     for ($i= 0; $i<$num; $i++) {
286       $result.= "<td class='sortableListItemFill'></td>";
287     }
288     $result.= "</tr>\n";
290     $result.= " </tbody>\n</table>\n</div>\n";
291     $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
292     $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
293     $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
295     // Append script stuff if needed
296     $result.= '<script type="text/javascript" language="javascript">';
297     if ($this->reorderable) {
298       $result.= ' function updateOrder(){';
299       $result.= '    var ampcharcode= \'%26\';';
300       $result.= '    var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
301       $result.= '    $("reorder_'.$this->id.'").value= serializeOpts;';
302       $result.= '    document.mainform.submit();';
303       $result.= ' }';
304       $result.= 'Position.includeScrollOffsets = true;';
305       $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
306     }
307     $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
308     $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
309     $result.= '</script>';
311     return $result;
312   }
315   public function update()
316   {
317     // Do not do anything if this is not our PID, or there's even no PID available...
318     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) {
319       return;
320     }
322     // Filter GET with "act" attributes
323     if (!$this->reorderable && isset($_GET['act'])) {
324       $key= validate($_GET['act']);
325       if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
326         // Switch to new column or invert search order?
327         $column= $match[1];
328         if ($this->sortColumn != $column) {
329           $this->sortColumn= $column;
330         } else {
331           $this->sortDirection[$column]= !$this->sortDirection[$column];
332         }
334         // Update mapping according to sort parameters
335         $this->sortData();
336       }
337     }
338   }
341   public function save_object()
342   {
343     // Do not do anything if this is not our PID, or there's even no PID available...
344     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) {
345       return;
346     }
348     // Do not do anything if we're not posted - or have no permission
349     if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
351       if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
352         $this->scrollPosition= $_POST['position_'.$this->id];
353       }
355       // Move requested?
356       $move= $_POST['reorder_'.$this->id];
357       if ($move != "") {
358         preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
359         $this->action= "reorder";
360         $tmp= array();
361         foreach ($matches[1] as $id => $row) {
362           $tmp[$id]= $this->mapping[$row];
363         }
364         $this->mapping= $tmp;
365         $this->current_mapping= $matches[1];
366         $this->modified= true;
367         return;
368       }
369     }
371     // Delete requested?
372     $this->action = "";
373     if (strpos($this->acl, 'd') !== false){
374       foreach ($_POST as $key => $value) {
375         if (preg_match('/^del_'.$this->id.'([0-9]+)$/', $key, $matches)) {
376           $this->active_index= $this->mapping[$matches[1]];
378           // Ignore request if mode requests it
379           if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
380             $this->active_index= null;
381             continue;
382           }
384           // Set action
385           $this->action= "delete";
387           // Remove value if requested
388           if ($this->instantDelete) {
389             $this->deleteEntry($this->active_index);
390           }
391         }
392       }
393     }
395     // Edit requested?
396     if (strpos($this->acl, 'w') !== false){
397       foreach ($_POST as $key => $value) {
398         if (preg_match('/^edit_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
399           $this->active_index= $this->mapping[$matches[1]];
401           // Ignore request if mode requests it
402           if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
403             $this->active_index= null;
404             continue;
405           }
407           $this->action= "edit";
408         }
409       }
410     }
411   }
414   public function getAction()
415   {
416     // Do not do anything if we're not posted
417     if(!isset($_POST['reorder_'.$this->id])) {
418       return;
419     }
421     // For reordering, return current mapping
422     if ($this->action == 'reorder') {
423       return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
424     }
426     // Edit and delete
427     $result= array("targets" => array($this->active_index), "action" => $this->action);
429     return $result;
430   }
433   private function deleteEntry($id)
434   {
435     // Remove mapping
436     $index= array_search($id, $this->mapping);
437     if ($index !== false) {
438       unset($this->mapping[$index]);
439       $this->mapping= array_values($this->mapping);
440       $this->modified= true;
441     }
442   }
445   public function getMaintainedData()
446   {
447     $tmp= array();
449     foreach ($this->mapping as $src => $dst) {
450       $tmp[$this->keys[$dst]]= $this->data[$dst];
451     }
453     return $tmp;
454   }
457   public function isModified()
458   {
459     return $this->modified;
460   }
463   public function setAcl($acl)
464   {
465     $this->acl= $acl;
466   }
469   public function sortData()
470   {
471     // Extract data
472     $tmp= array();
473     foreach($this->displayData as $item) {
474       if (isset($item[$this->sortColumn])){
475         $tmp[]= $item[$this->sortColumn];
476       } else {
477         $tmp[]= "";
478       }
479     }
481     // Sort entries
482     if ($this->sortDirection[$this->sortColumn]) {
483       asort($tmp);
484     } else {
485       arsort($tmp);
486     }
488     // Adapt mapping accordingly
489     $this->mapping= array();
490     foreach ($tmp as $key => $value) {
491       $this->mapping[]= $key;
492     }
493   }
496   public function addEntry($entry, $displayEntry= null, $key= null)
497   {
498     // Only add if not already there
499     if (!$key) {
500       if (in_array($entry, $this->data)) {
501         return;
502       }
503     } else {
504       if (isset($this->data[$key])) {
505         return;
506       }
507     }
509     // Prefill with default value if not specified
510     if (!$displayEntry) {
511       $displayEntry= array('data' => array($entry));
512     }
514     // Append to data and mapping
515     if ($key) {
516       $this->data[$key]= $entry;
517       $this->keys[]= $key;
518     } else {
519       $this->data[]= $entry;
520       $this->keys[]= count($this->mapping);
521     }
522     $this->displayData[]= $displayEntry['data'];
523     $this->mapping[]= count($this->mapping);
524     $this->modified= true;
526     // Sort data after we've added stuff
527     $this->sortData();
528   }
531   public function getKey($index) {
532     return isset($this->keys[$index])?$this->keys[$index]:null;
533   }