Code

Added permanent scroll position saver to sortable lists
[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: class_listing.inc 15087 2010-01-06 13:45:49Z hickert $$
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;
45   private $acl= "";
46   private $modified= false;
48   function sortableListing($data= array(), $displayData= null)
49   {
50     global $config;
52     // Save data to display
53     $this->setData($data);
54     if (!$displayData) {
55       $displayData= array();
56       foreach ($data as $value) {
57         $displayData[]= array($value);
58       }
59     } else {
60       $this->setDisplayData($displayData);
61     }
63     // Generate instance wide unique ID
64     $tmp= gettimeofday();
65     $this->id= 'l'.md5($tmp['sec']);
66   }
69   function setData($data)
70   {
71     $this->data= $data;
72   }
75   function setDisplayData($data)
76   {
77     if (!is_array($data)) {
78       die ("sortableList needs an array as data!");
79     }
81     // Transfer information
82     $this->displayData= $data;
84     // Create initial mapping
85     $this->mapping= array_keys($data);
86     $this->current_mapping= $this->mapping;
88     // Find the number of coluns
89     reset($this->displayData);
90     $first= current($this->displayData);
91     if (is_array($first)) {
92       $this->columns= count($first);
93     } else {
94       $this->columns= 1;
95     }
96   }
99   function setWidth($width)
100   {
101     $this->width= $width;
102   }
105   function setInstantDelete($flag)
106   {
107     $this->instantDelete= $flag;
108   }
111   function setEditable($flag)
112   {
113     $this->editable= $flag;
114   }
117   function setDeleteable($flag)
118   {
119     $this->deleteable= $flag;
120   }
123   function setHeight($height)
124   {
125     $this->height= $height;
126   }
129   function setCssClass($css)
130   {
131     $this->cssclass= $css;
132   }
135   function setHeader($header)
136   {
137     $this->header= $header;
138   }
141   function setColspecs($specs)
142   {
143     $this->colspecs= $specs;
144   }
147   function render()
148   {
149     $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='width: ".$this->width."; height: ".$this->height."'>\n";
150     $result.= "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%' style='position: relative;'".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
151     $action_width= 0;
152     if (strpos($this->acl, 'w') === false) {
153       $edit_image= $this->editable?"<img class='center' src='images/lists/edit-grey.png' alt='"._("Edit")."'>":"";
154     } else {
155       $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' title='"._("Edit this entry")."'>":"";
156     }
157     if (strpos($this->acl, 'd') === false) {
158       $delete_image= $this->deleteable?"<img class='center' src='images/lists/trash-grey.png' alt='"._("Delete")."'>":"";
159     } else {
160       $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
161     }
163     // Do we need colspecs?
164     $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
165     if ($this->colspecs) {
166       $result.= " <colgroup>\n";
167       for ($i= 0; $i<$this->columns; $i++) {
168         $result.= "  <col width='".(isset($this->colspecs[$i])?$this->colspecs[$i]:"*")."'/>\n";
169       }
171       // Extend by another column if we've actions specified
172       if ($action_width) {
173         $result.= "  <col width='$action_width'/>\n";
174       }
175       $result.= " </colgroup>\n";
176     }
178     // Do we need a header?
179     if ($this->header) {
180       $result.= " <thead>\n  <tr>\n";
181       for ($i= 0; $i<$this->columns; $i++) {
182         $result.= "   <th>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
183       }
184       if ($action_width) {
185         $result.= "<th>&nbsp;</th>";
186       }
187       $result.= "\n  </tr>\n </thead>\n";
188     }
190     // Render table body if we've read permission
191     $result.= " <tbody id='".$this->id."'>\n";
192     if (strpos($this->acl, 'r') !== false) {
193       foreach ($this->mapping as $nr => $row) {
194         $result.= "  <tr class='sortableListItem".($nr&1?'Odd':'')."' id='item_".$this->id."_$nr'>\n";
195         foreach ($this->displayData[$row] as $column) {
196           $result.= "   <td>".htmlentities($column)."</td>\n";
197         }
198         if ($action_width) {
199           $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
200                            str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
201         }
202         $result.= "  </tr>\n";
203       }
204     } else {
205       $result.= "  <tr class='sortableListItem' style='height:100%'></tr>\n";
206     }
208     $result.= " </tbody>\n</table>\n</div>\n";
209     $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
210     $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
212     // Append script stuff if needed
213     $result.= '<script type="text/javascript" language="javascript">';
214     $result.= ' function updateOrder(){';
215     $result.= '    var ampcharcode= \'%26\';';
216     $result.= '    var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
217     $result.= '    $("reorder_'.$this->id.'").value= serializeOpts;';
218     $result.= '    document.mainform.submit();';
219     $result.= ' }';
220     $result.= 'Position.includeScrollOffsets = true;';
221     $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
222     $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
223     $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
224     $result.= '</script>';
226     return $result;
227   }
230   function update()
231   {
232     // Do not do anything if we're not posted - or have no permission
233     if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
235       if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
236         $this->scrollPosition= $_POST['position_'.$this->id];
237       }
239       // Move requested?
240       $move= $_POST['reorder_'.$this->id];
241       if ($move != "") {
242         preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
243         $this->action= "reorder";
244         $tmp= array();
245         foreach ($matches[1] as $id => $row) {
246           $tmp[$id]= $this->mapping[$row];
247         }
248         $this->mapping= $tmp;
249         $this->current_mapping= $matches[1];
250         $this->modified= true;
251         return;
252       }
253     }
255     // Delete requested?
256     if (strpos($this->acl, 'd') !== false){
257       foreach ($_POST as $key => $value) {
258         if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
259           $this->active_index= $this->mapping[$matches[1]];
260           $this->action= "delete";
261           if ($this->instantDelete) {
262             $this->deleteEntry($this->active_index);
263           }
264         }
265       }
266     }
268     // Edit requested?
269     if (strpos($this->acl, 'w') !== false){
270       foreach ($_POST as $key => $value) {
271         if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
272           $this->active_index= $this->mapping[$matches[1]];
273           $this->action= "edit";
274         }
275       }
276     }
277   }
280   function getAction()
281   {
282     // Do not do anything if we're not posted
283     if(!isset($_POST['reorder_'.$this->id])) {
284       return;
285     }
287     // For reordering, return current mapping
288     if ($this->action == 'reorder') {
289       return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
290     }
292     // Edit and delete
293     $result= array("targets" => array($this->active_index), "action" => $this->action);
295     return $result;
296   }
299   function deleteEntry($id)
300   {
301     // Remove mapping
302     $index= array_search($id, $this->mapping);
303     if ($index !== false) {
304       unset($this->mapping[$index]);
305       $this->mapping= array_values($this->mapping);
306       $this->modified= true;
307     }
308   }
311   function getMaintainedData()
312   {
313     $tmp= array();
315     foreach ($this->mapping as $src => $dst) {
316       $tmp[$src]= $this->data[$dst];
317     }
319     return $tmp;
320   }
323   function isModified()
324   {
325     return $this->modified;
326   }
329   function setAcl($acl)
330   {
331     $this->acl= $acl;
332   }