Code

Fixed add entry
[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     }
63     $this->setDisplayData($displayData);
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' height='100%' width='100%' style='table-layout:fixed; 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         $first= " style='border:0'";
221         foreach ($this->displayData[$row] as $column) {
222           $result.= "   <td$editable$first>".htmlentities($column)."</td>\n";
223           $first= "";
224         }
225         if ($action_width) {
226           $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
227                            str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
228         }
229         $result.= "  </tr>\n";
230       }
231     }
233     // Add spacer
234     $result.= "  <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
235     $num= $action_width?$this->columns:$this->columns-1;
236     for ($i= 0; $i<$num; $i++) {
237       $result.= "<td class='sortableListItemFill'></td>";
238     }
239     $result.= "</tr>\n";
241     $result.= " </tbody>\n</table>\n</div>\n";
242     $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
243     $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
244     $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
246     // Append script stuff if needed
247     $result.= '<script type="text/javascript" language="javascript">';
248     if ($this->reorderable) {
249       $result.= ' function updateOrder(){';
250       $result.= '    var ampcharcode= \'%26\';';
251       $result.= '    var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
252       $result.= '    $("reorder_'.$this->id.'").value= serializeOpts;';
253       $result.= '    document.mainform.submit();';
254       $result.= ' }';
255       $result.= 'Position.includeScrollOffsets = true;';
256       $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
257     }
258     $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
259     $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
260     $result.= '</script>';
262     return $result;
263   }
266   public function update()
267   {
268     // Do not do anything if this is not our PID, or there's even no PID available...
269     if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) {
270       return;
271     }
273     // Filter GET with "act" attributes
274     if (!$this->reorderable && isset($_GET['act'])) {
275       $key= validate($_GET['act']);
276       if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
277         // Switch to new column or invert search order?
278         $column= $match[1];
279         if ($this->sortColumn != $column) {
280           $this->sortColumn= $column;
281         } else {
282           $this->sortDirection[$column]= !$this->sortDirection[$column];
283         }
285         // Update mapping according to sort parameters
286         $this->sortData();
287       }
288     }
290     // Do not do anything if we're not posted - or have no permission
291     if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
293       if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
294         $this->scrollPosition= $_POST['position_'.$this->id];
295       }
297       // Move requested?
298       $move= $_POST['reorder_'.$this->id];
299       if ($move != "") {
300         preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
301         $this->action= "reorder";
302         $tmp= array();
303         foreach ($matches[1] as $id => $row) {
304           $tmp[$id]= $this->mapping[$row];
305         }
306         $this->mapping= $tmp;
307         $this->current_mapping= $matches[1];
308         $this->modified= true;
309         return;
310       }
311     }
313     // Delete requested?
314     if (strpos($this->acl, 'd') !== false){
315       foreach ($_POST as $key => $value) {
316         if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
317           $this->active_index= $this->mapping[$matches[1]];
318           $this->action= "delete";
319           if ($this->instantDelete) {
320             $this->deleteEntry($this->active_index);
321           }
322         }
323       }
324     }
326     // Edit requested?
327     if (strpos($this->acl, 'w') !== false){
328       foreach ($_POST as $key => $value) {
329         if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
330           $this->active_index= $this->mapping[$matches[1]];
331           $this->action= "edit";
332         }
333       }
334     }
335   }
338   public function getAction()
339   {
340     // Do not do anything if we're not posted
341     if(!isset($_POST['reorder_'.$this->id])) {
342       return;
343     }
345     // For reordering, return current mapping
346     if ($this->action == 'reorder') {
347       return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
348     }
350     // Edit and delete
351     $result= array("targets" => array($this->active_index), "action" => $this->action);
353     return $result;
354   }
357   private function deleteEntry($id)
358   {
359     // Remove mapping
360     $index= array_search($id, $this->mapping);
361     if ($index !== false) {
362       unset($this->mapping[$index]);
363       $this->mapping= array_values($this->mapping);
364       $this->modified= true;
365     }
366   }
369   public function getMaintainedData()
370   {
371     $tmp= array();
373     foreach ($this->mapping as $src => $dst) {
374       $tmp[$src]= $this->data[$dst];
375     }
377     return $tmp;
378   }
381   public function isModified()
382   {
383     return $this->modified;
384   }
387   public function setAcl($acl)
388   {
389     $this->acl= $acl;
390   }
393   public function sortData()
394   {
395     // Extract data
396     $tmp= array();
397     foreach($this->displayData as $item) {
398       if (isset($item[$this->sortColumn])){
399         $tmp[]= $item[$this->sortColumn];
400       } else {
401         $tmp[]= "";
402       }
403     }
405     // Sort entries
406     if ($this->sortDirection[$this->sortColumn]) {
407       asort($tmp);
408     } else {
409       arsort($tmp);
410     }
412     // Adapt mapping accordingly
413     $this->mapping= array();
414     foreach ($tmp as $key => $value) {
415       $this->mapping[]= $key;
416     }
417   }
420   public function addEntry($entry, $displayEntry= null)
421   {
422     // Prefill with default value if not specified
423     if (!$displayEntry) {
424       $displayEntry= array($entry);
425     }
427     // Append to data and mapping
428     $this->data[]= $entry;
429     $this->displayData[]= $displayEntry;
430     $this->mapping[]= count($this->mapping);
431   }