Code

Fixed parameter order
[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-2008 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 class sortableListing {
24   private $header= null;
25   private $colspecs= null;
26   private $reorderable= true;
27   private $width= "400px";
28   private $height= "100px";
29   private $cssclass= "";
30   private $id;
32   private $data= array();
33   private $displayData= array();
34   private $columns= 0;
35   private $deleteable= false;
36   private $editable= false;
37   private $instantDelete= true;
38   private $action;
39   private $targets;
40   private $mapping;
41   private $current_mapping;
42   private $active_index;
43   private $scrollPosition= 0;
44   private $sortColumn= 0;
45   private $sortDirection= array();
47   private $acl= "";
48   private $modified= false;
50   public function sortableListing($data= array(), $displayData= null, $reorderable= false)
51   {
52     global $config;
54     // Save data to display
55     $this->setData($data);
56     if (!$displayData) {
57       $displayData= array();
58       foreach ($data as $value) {
59         $displayData[]= array($value);
60       }
61     } else {
62       $this->setDisplayData($displayData);
63     }
65     // Generate instance wide unique ID
66     $tmp= gettimeofday();
67     $this->id= 'l'.md5($tmp['sec']);
69     // Set reorderable flag
70     $this->reorderable= $reorderable;
71     if (!$reorderable) {
72       $this->sortData();
73     }
74   }
77   private function setData($data)
78   {
79     $this->data= $data;
80   }
83   private function setDisplayData($data)
84   {
85     if (!is_array($data)) {
86       die ("sortableList needs an array as data!");
87     }
89     // Transfer information
90     $this->displayData= $data;
92     // Create initial mapping
93     $this->mapping= array_keys($data);
94     $this->current_mapping= $this->mapping;
96     // Find the number of coluns
97     reset($this->displayData);
98     $first= current($this->displayData);
99     if (is_array($first)) {
100       $this->columns= count($first);
101     } else {
102       $this->columns= 1;
103     }
105     // Preset sort orders to 'down'
106     for ($column= 0; $column<$this->columns; $column++) {
107       $this->sortDirection[]= true;
108     }
109   }
112   public function setWidth($width)
113   {
114     $this->width= $width;
115   }
118   public function setInstantDelete($flag)
119   {
120     $this->instantDelete= $flag;
121   }
124   public function setEditable($flag)
125   {
126     $this->editable= $flag;
127   }
130   public function setDeleteable($flag)
131   {
132     $this->deleteable= $flag;
133   }
136   public function setHeight($height)
137   {
138     $this->height= $height;
139   }
142   public function setCssClass($css)
143   {
144     $this->cssclass= $css;
145   }
148   public function setHeader($header)
149   {
150     $this->header= $header;
151   }
154   public function setColspecs($specs)
155   {
156     $this->colspecs= $specs;
157   }
160   public function render()
161   {
162     $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='width: ".$this->width."; height: ".$this->height."'>\n";
163     $result.= "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%' style='position: relative;'".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
164     $action_width= 0;
165     if (strpos($this->acl, 'w') === false) {
166       $edit_image= $this->editable?"<img class='center' src='images/lists/edit-grey.png' alt='"._("Edit")."'>":"";
167     } else {
168       $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' id='%ID' title='"._("Edit this entry")."'>":"";
169     }
170     if (strpos($this->acl, 'd') === false) {
171       $delete_image= $this->deleteable?"<img class='center' src='images/lists/trash-grey.png' alt='"._("Delete")."'>":"";
172     } else {
173       $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
174     }
176     // Do we need colspecs?
177     $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
178     if ($this->colspecs) {
179       $result.= " <colgroup>\n";
180       for ($i= 0; $i<$this->columns; $i++) {
181         $result.= "  <col width='".(isset($this->colspecs[$i])?$this->colspecs[$i]:"*")."'/>\n";
182       }
184       // Extend by another column if we've actions specified
185       if ($action_width) {
186         $result.= "  <col width='$action_width'/>\n";
187       }
188       $result.= " </colgroup>\n";
189     }
191     // Do we need a header?
192     if ($this->header) {
193       $result.= " <thead>\n  <tr>\n";
194       for ($i= 0; $i<$this->columns; $i++) {
195         $link= "href='?plug=".$_GET['plug']."&amp;PID=".$this->id."&amp;act=SORT_$i'";
196         $sorter= "";
197         if ($i == $this->sortColumn){
198           $sorter= "&nbsp;<img border='0' title='".($this->sortDirection[$i]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png' align='top'>";
199         }
201         if ($this->reorderable) {
202           $result.= "   <th>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
203         } else {
204           $result.= "   <th><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."$sorter</a></th>";
205         }
206       }
207       if ($action_width) {
208         $result.= "<th>&nbsp;</th>";
209       }
210       $result.= "\n  </tr>\n </thead>\n";
211     }
213     // Render table body if we've read permission
214     $result.= " <tbody id='".$this->id."'>\n";
215     $reorderable= $this->reorderable?"":" style='cursor:default'";
216     if (strpos($this->acl, 'r') !== false) {
217       foreach ($this->mapping as $nr => $row) {
218         $editable= $this->editable?" onclick='$(\"edit_".$this->id."_$nr\").click()'":"";
219         $result.= "  <tr class='sortableListItem".($nr&1?'Odd':'')."' id='item_".$this->id."_$nr'$reorderable>\n";
220         foreach ($this->displayData[$row] as $column) {
221           $result.= "   <td$editable>".htmlentities($column)."</td>\n";
222         }
223         if ($action_width) {
224           $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
225                            str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
226         }
227         $result.= "  </tr>\n";
228       }
229     } else {
230       $result.= "  <tr class='sortableListItem' style='height:100%'></tr>\n";
231     }
233     $result.= " </tbody>\n</table>\n</div>\n";
234     $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
235     $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
236     $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
238     // Append script stuff if needed
239     $result.= '<script type="text/javascript" language="javascript">';
240     if ($this->reorderable) {
241       $result.= ' function updateOrder(){';
242       $result.= '    var ampcharcode= \'%26\';';
243       $result.= '    var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
244       $result.= '    $("reorder_'.$this->id.'").value= serializeOpts;';
245       $result.= '    document.mainform.submit();';
246       $result.= ' }';
247       $result.= 'Position.includeScrollOffsets = true;';
248       $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
249     }
250     $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
251     $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
252     $result.= '</script>';
254     return $result;
255   }
258   public function update()
259   {
260     // Do not do anything if this is not our PID, or there's even no PID available...
261     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) {
262       return;
263     }
265     // Filter GET with "act" attributes
266     if (!$this->reorderable && isset($_GET['act'])) {
267       $key= validate($_GET['act']);
268       if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
269         // Switch to new column or invert search order?
270         $column= $match[1];
271         if ($this->sortColumn != $column) {
272           $this->sortColumn= $column;
273         } else {
274           $this->sortDirection[$column]= !$this->sortDirection[$column];
275         }
277         // Update mapping according to sort parameters
278         $this->sortData();
279       }
280     }
282     // Do not do anything if we're not posted - or have no permission
283     if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
285       if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
286         $this->scrollPosition= $_POST['position_'.$this->id];
287       }
289       // Move requested?
290       $move= $_POST['reorder_'.$this->id];
291       if ($move != "") {
292         preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
293         $this->action= "reorder";
294         $tmp= array();
295         foreach ($matches[1] as $id => $row) {
296           $tmp[$id]= $this->mapping[$row];
297         }
298         $this->mapping= $tmp;
299         $this->current_mapping= $matches[1];
300         $this->modified= true;
301         return;
302       }
303     }
305     // Delete requested?
306     if (strpos($this->acl, 'd') !== false){
307       foreach ($_POST as $key => $value) {
308         if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
309           $this->active_index= $this->mapping[$matches[1]];
310           $this->action= "delete";
311           if ($this->instantDelete) {
312             $this->deleteEntry($this->active_index);
313           }
314         }
315       }
316     }
318     // Edit requested?
319     if (strpos($this->acl, 'w') !== false){
320       foreach ($_POST as $key => $value) {
321         if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
322           $this->active_index= $this->mapping[$matches[1]];
323           $this->action= "edit";
324         }
325       }
326     }
327   }
330   public function getAction()
331   {
332     // Do not do anything if we're not posted
333     if(!isset($_POST['reorder_'.$this->id])) {
334       return;
335     }
337     // For reordering, return current mapping
338     if ($this->action == 'reorder') {
339       return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
340     }
342     // Edit and delete
343     $result= array("targets" => array($this->active_index), "action" => $this->action);
345     return $result;
346   }
349   private function deleteEntry($id)
350   {
351     // Remove mapping
352     $index= array_search($id, $this->mapping);
353     if ($index !== false) {
354       unset($this->mapping[$index]);
355       $this->mapping= array_values($this->mapping);
356       $this->modified= true;
357     }
358   }
361   public function getMaintainedData()
362   {
363     $tmp= array();
365     foreach ($this->mapping as $src => $dst) {
366       $tmp[$src]= $this->data[$dst];
367     }
369     return $tmp;
370   }
373   public function isModified()
374   {
375     return $this->modified;
376   }
379   public function setAcl($acl)
380   {
381     $this->acl= $acl;
382   }
385   public function sortData()
386   {
387     // Extract data
388     $tmp= array();
389     foreach($this->displayData as $item) {
390       if (isset($item[$this->sortColumn])){
391         $tmp[]= $item[$this->sortColumn];
392       } else {
393         $tmp[]= "";
394       }
395     }
397     // Sort entries
398     if ($this->sortDirection[$this->sortColumn]) {
399       asort($tmp);
400     } else {
401       arsort($tmp);
402     }
404     // Adapt mapping accordingly
405     $this->mapping= array();
406     foreach ($tmp as $key => $value) {
407       $this->mapping[]= $key;
408     }
409   }
412   public function addEntry($entry, $displayEntry= null)
413   {
414     // Prefill with default value if not specified
415     if (!$displayEntry) {
416       $displayEntry= array($entry);
417     }
419     // Append to data and mapping
420     $this->data[]= $entry;
421     $this->displayData[]= $displayData;
422     $this->mapping[]= count($this->mapping);
423   }