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 $keys= array();
34 private $displayData= array();
35 private $columns= 0;
36 private $deleteable= false;
37 private $editable= false;
38 private $colorAlternate= false;
39 private $instantDelete= true;
40 private $action;
41 private $mapping;
42 private $current_mapping;
43 private $active_index;
44 private $scrollPosition= 0;
45 private $sortColumn= 0;
46 private $sortDirection= array();
48 private $acl= "";
49 private $modified= false;
51 public function sortableListing($data= array(), $displayData= null, $reorderable= false)
52 {
53 global $config;
55 // Save data to display
56 $this->setListData($data, $displayData);
58 // Generate instance wide unique ID
59 $tmp= gettimeofday();
60 $this->id= 'l'.md5($tmp['sec']);
62 // Set reorderable flag
63 $this->reorderable= $reorderable;
64 if (!$reorderable) {
65 $this->sortData();
66 }
67 }
70 public function setListData($data, $displayData= null)
71 {
72 // Save data to display
73 $this->setData($data);
74 if (!$displayData) {
75 $displayData= array();
76 foreach ($data as $key => $value) {
77 $displayData[$key]= array($value);
78 }
79 }
81 $this->setDisplayData($displayData);
82 }
85 private function setData($data)
86 {
87 $this->data= $data;
88 }
91 private function setDisplayData($data)
92 {
93 if (!is_array($data)) {
94 die ("sortableList needs an array as data!");
95 }
97 // Transfer information
98 $this->displayData= array_values($data);
99 $this->keys= array_keys($data);
101 // Create initial mapping
102 $this->mapping= range(0, abs(count($this->keys)-1));
103 $this->current_mapping= $this->mapping;
105 // Find the number of coluns
106 reset($this->displayData);
107 $first= current($this->displayData);
108 if (is_array($first)) {
109 $this->columns= count($first);
110 } else {
111 $this->columns= 1;
112 }
114 // Preset sort orders to 'down'
115 for ($column= 0; $column<$this->columns; $column++) {
116 $this->sortDirection[]= true;
117 }
118 }
121 public function setWidth($width)
122 {
123 $this->width= $width;
124 }
127 public function setInstantDelete($flag)
128 {
129 $this->instantDelete= $flag;
130 }
133 public function setColorAlternate($flag)
134 {
135 $this->colorAlternate= $flag;
136 }
139 public function setEditable($flag)
140 {
141 $this->editable= $flag;
142 }
145 public function setDeleteable($flag)
146 {
147 $this->deleteable= $flag;
148 }
151 public function setHeight($height)
152 {
153 $this->height= $height;
154 }
157 public function setCssClass($css)
158 {
159 $this->cssclass= $css;
160 }
163 public function setHeader($header)
164 {
165 $this->header= $header;
166 }
169 public function setColspecs($specs)
170 {
171 $this->colspecs= $specs;
172 }
175 public function render()
176 {
177 $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='min-width:".$this->width.";height: ".$this->height."'>\n";
178 $result.= "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%' ".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
179 $action_width= 0;
180 if (strpos($this->acl, 'w') === false) {
181 $edit_image= $this->editable?"<img class='center' src='images/lists/edit-grey.png' alt='"._("Edit")."'>":"";
182 } else {
183 $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' id='%ID' title='"._("Edit this entry")."'>":"";
184 }
185 if (strpos($this->acl, 'd') === false) {
186 $delete_image= $this->deleteable?"<img class='center' src='images/lists/trash-grey.png' alt='"._("Delete")."'>":"";
187 } else {
188 $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
189 }
191 // Do we need colspecs?
192 $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
193 if ($this->colspecs) {
194 $result.= " <colgroup>\n";
195 for ($i= 0; $i<$this->columns; $i++) {
196 $result.= " <col style='width:".(isset($this->colspecs[$i])?$this->colspecs[$i]:"*")."'/>\n";
197 }
199 // Extend by another column if we've actions specified
200 if ($action_width) {
201 $result.= " <col style='width:".$action_width."px'/>\n";
202 }
203 $result.= " </colgroup>\n";
204 }
206 // Do we need a header?
207 if ($this->header) {
208 $result.= " <thead>\n <tr>\n";
209 $first= " style='border:0'";
210 for ($i= 0; $i<$this->columns; $i++) {
211 $link= "href='?plug=".$_GET['plug']."&PID=".$this->id."&act=SORT_$i'";
212 $sorter= "";
213 if ($i == $this->sortColumn){
214 $sorter= " <img border='0' title='".($this->sortDirection[$i]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png' align='top'>";
215 }
217 if ($this->reorderable) {
218 $result.= " <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
219 } else {
220 $result.= " <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."$sorter</a></th>";
221 }
222 $first= "";
223 }
224 if ($action_width) {
225 $result.= "<th> </th>";
226 }
227 $result.= "\n </tr>\n </thead>\n";
228 }
230 // Render table body if we've read permission
231 $result.= " <tbody id='".$this->id."'>\n";
232 $reorderable= $this->reorderable?"":" style='cursor:default'";
233 if (strpos($this->acl, 'r') !== false) {
234 foreach ($this->mapping as $nr => $row) {
235 $editable= $this->editable?" onclick='$(\"edit_".$this->id."_$nr\").click()'":"";
236 $result.= " <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."' id='item_".$this->id."_$nr'$reorderable>\n";
237 $first= " style='border:0'";
238 foreach ($this->displayData[$row] as $column) {
239 $result.= " <td$editable$first>".htmlentities($column)."</td>\n";
240 $first= "";
241 }
242 if ($action_width) {
243 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
244 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
245 }
246 $result.= " </tr>\n";
247 }
248 }
250 // Add spacer
251 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
252 $num= $action_width?$this->columns:$this->columns-1;
253 for ($i= 0; $i<$num; $i++) {
254 $result.= "<td class='sortableListItemFill'></td>";
255 }
256 $result.= "</tr>\n";
258 $result.= " </tbody>\n</table>\n</div>\n";
259 $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
260 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
261 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
263 // Append script stuff if needed
264 $result.= '<script type="text/javascript" language="javascript">';
265 if ($this->reorderable) {
266 $result.= ' function updateOrder(){';
267 $result.= ' var ampcharcode= \'%26\';';
268 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
269 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
270 $result.= ' document.mainform.submit();';
271 $result.= ' }';
272 $result.= 'Position.includeScrollOffsets = true;';
273 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
274 }
275 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
276 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
277 $result.= '</script>';
279 return $result;
280 }
283 public function update()
284 {
285 // Do not do anything if this is not our PID, or there's even no PID available...
286 if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) {
287 return;
288 }
290 // Filter GET with "act" attributes
291 if (!$this->reorderable && isset($_GET['act'])) {
292 $key= validate($_GET['act']);
293 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
294 // Switch to new column or invert search order?
295 $column= $match[1];
296 if ($this->sortColumn != $column) {
297 $this->sortColumn= $column;
298 } else {
299 $this->sortDirection[$column]= !$this->sortDirection[$column];
300 }
302 // Update mapping according to sort parameters
303 $this->sortData();
304 }
305 }
307 // Do not do anything if we're not posted - or have no permission
308 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
310 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
311 $this->scrollPosition= $_POST['position_'.$this->id];
312 }
314 // Move requested?
315 $move= $_POST['reorder_'.$this->id];
316 if ($move != "") {
317 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
318 $this->action= "reorder";
319 $tmp= array();
320 foreach ($matches[1] as $id => $row) {
321 $tmp[$id]= $this->mapping[$row];
322 }
323 $this->mapping= $tmp;
324 $this->current_mapping= $matches[1];
325 $this->modified= true;
326 return;
327 }
328 }
330 // Delete requested?
331 if (strpos($this->acl, 'd') !== false){
332 foreach ($_POST as $key => $value) {
333 if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
334 $this->active_index= $this->mapping[$matches[1]];
335 $this->action= "delete";
336 if ($this->instantDelete) {
337 $this->deleteEntry($this->active_index);
338 }
339 }
340 }
341 }
343 // Edit requested?
344 if (strpos($this->acl, 'w') !== false){
345 foreach ($_POST as $key => $value) {
346 if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
347 $this->active_index= $this->mapping[$matches[1]];
348 $this->action= "edit";
349 }
350 }
351 }
352 }
355 public function getAction()
356 {
357 // Do not do anything if we're not posted
358 if(!isset($_POST['reorder_'.$this->id])) {
359 return;
360 }
362 // For reordering, return current mapping
363 if ($this->action == 'reorder') {
364 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
365 }
367 // Edit and delete
368 $result= array("targets" => array($this->active_index), "action" => $this->action);
370 return $result;
371 }
374 private function deleteEntry($id)
375 {
376 // Remove mapping
377 $index= array_search($id, $this->mapping);
378 if ($index !== false) {
379 unset($this->mapping[$index]);
380 $this->mapping= array_values($this->mapping);
381 $this->modified= true;
382 }
383 }
386 public function getMaintainedData()
387 {
388 $tmp= array();
390 foreach ($this->mapping as $src => $dst) {
391 $tmp[$this->keys[$dst]]= $this->data[$dst];
392 }
394 return $tmp;
395 }
398 public function isModified()
399 {
400 return $this->modified;
401 }
404 public function setAcl($acl)
405 {
406 $this->acl= $acl;
407 }
410 public function sortData()
411 {
412 // Extract data
413 $tmp= array();
414 foreach($this->displayData as $item) {
415 if (isset($item[$this->sortColumn])){
416 $tmp[]= $item[$this->sortColumn];
417 } else {
418 $tmp[]= "";
419 }
420 }
422 // Sort entries
423 if ($this->sortDirection[$this->sortColumn]) {
424 asort($tmp);
425 } else {
426 arsort($tmp);
427 }
429 // Adapt mapping accordingly
430 $this->mapping= array();
431 foreach ($tmp as $key => $value) {
432 $this->mapping[]= $key;
433 }
434 }
437 public function addEntry($entry, $displayEntry= null, $key= null)
438 {
439 // Only add if not already there
440 if (!$key) {
441 if (in_array($entry, $this->data)) {
442 return;
443 }
444 } else {
445 if (isset($this->data[$key])) {
446 return;
447 }
448 }
450 // Prefill with default value if not specified
451 if (!$displayEntry) {
452 $displayEntry= array($entry);
453 }
455 // Append to data and mapping
456 if ($key) {
457 $this->data[$key]= $entry;
458 $this->keys[]= $key;
459 } else {
460 $this->data[]= $entry;
461 $this->keys[]= count($this->mapping);
462 }
463 $this->displayData[]= $displayEntry;
464 $this->mapping[]= count($this->mapping);
465 $this->modified= true;
466 }
469 public function getKey($index) {
470 return isset($this->keys[$index])?$this->keys[$index]:null;
471 }
473 }