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, $modes= null, $reorderable= false)
57 {
58 global $config;
60 // Save data to display
61 $this->setListData($data, $displayData, $modes);
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, $modes= 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($value);
83 }
84 }
85 if (!$modes) {
86 $modes= array();
87 foreach ($data as $key => $value) {
88 $modes[$key]= LIST_NORMAL;
89 }
90 }
92 $this->setDisplayData($displayData);
93 $this->setModes($modes);
94 }
97 private function setData($data)
98 {
99 $this->data= $data;
100 }
103 private function setModes($modes)
104 {
105 $this->modes= array_values($modes);
106 }
109 private function setDisplayData($data)
110 {
111 if (!is_array($data)) {
112 die ("sortableList needs an array as data!");
113 }
115 // Transfer information
116 $this->displayData= array_values($data);
117 $this->keys= array_keys($data);
119 // Create initial mapping
120 $this->mapping= range(0, abs(count($this->keys)-1));
121 $this->current_mapping= $this->mapping;
123 // Find the number of coluns
124 reset($this->displayData);
125 $first= current($this->displayData);
126 if (is_array($first)) {
127 $this->columns= count($first);
128 } else {
129 $this->columns= 1;
130 }
132 // Preset sort orders to 'down'
133 for ($column= 0; $column<$this->columns; $column++) {
134 $this->sortDirection[]= true;
135 }
136 }
139 public function setWidth($width)
140 {
141 $this->width= $width;
142 }
145 public function setInstantDelete($flag)
146 {
147 $this->instantDelete= $flag;
148 }
151 public function setColorAlternate($flag)
152 {
153 $this->colorAlternate= $flag;
154 }
157 public function setEditable($flag)
158 {
159 $this->editable= $flag;
160 }
163 public function setDeleteable($flag)
164 {
165 $this->deleteable= $flag;
166 }
169 public function setHeight($height)
170 {
171 $this->height= $height;
172 }
175 public function setCssClass($css)
176 {
177 $this->cssclass= $css;
178 }
181 public function setHeader($header)
182 {
183 $this->header= $header;
184 }
187 public function setColspecs($specs)
188 {
189 $this->colspecs= $specs;
190 }
193 public function render()
194 {
195 $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='min-width:".$this->width.";height: ".$this->height."'>\n";
196 $result.= "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%' ".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
197 $action_width= 0;
198 if (strpos($this->acl, 'w') === false) {
199 $edit_image= $this->editable?"<img class='center' src='images/lists/edit-grey.png' alt='"._("Edit")."'>":"";
200 } else {
201 $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' id='%ID' title='"._("Edit this entry")."'>":"";
202 }
203 if (strpos($this->acl, 'd') === false) {
204 $delete_image= $this->deleteable?"<img class='center' src='images/lists/trash-grey.png' alt='"._("Delete")."'>":"";
205 } else {
206 $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
207 }
209 // Do we need colspecs?
210 $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
211 if ($this->colspecs) {
212 $result.= " <colgroup>\n";
213 for ($i= 0; $i<$this->columns; $i++) {
214 $result.= " <col style='width:".(isset($this->colspecs[$i])?$this->colspecs[$i]:"*")."'/>\n";
215 }
217 // Extend by another column if we've actions specified
218 if ($action_width) {
219 $result.= " <col style='width:".$action_width."px'/>\n";
220 }
221 $result.= " </colgroup>\n";
222 }
224 // Do we need a header?
225 if ($this->header) {
226 $result.= " <thead>\n <tr>\n";
227 $first= " style='border:0'";
228 for ($i= 0; $i<$this->columns; $i++) {
229 $link= "href='?plug=".$_GET['plug']."&PID=".$this->id."&act=SORT_$i'";
230 $sorter= "";
231 if ($i == $this->sortColumn){
232 $sorter= " <img border='0' title='".($this->sortDirection[$i]?_("Up"):_("Down"))."' src='images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png' align='top'>";
233 }
235 if ($this->reorderable) {
236 $result.= " <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
237 } else {
238 $result.= " <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."$sorter</a></th>";
239 }
240 $first= "";
241 }
242 if ($action_width) {
243 $result.= "<th> </th>";
244 }
245 $result.= "\n </tr>\n </thead>\n";
246 }
248 // Render table body if we've read permission
249 $result.= " <tbody id='".$this->id."'>\n";
250 $reorderable= $this->reorderable?"":" style='cursor:default'";
251 if (strpos($this->acl, 'r') !== false) {
252 foreach ($this->mapping as $nr => $row) {
253 $editable= $this->editable?" onclick='$(\"edit_".$this->id."_$nr\").click()'":"";
255 $id= "";
256 switch ($this->modes[$row]) {
257 case LIST_DISABLED:
258 $id= " sortableListItemDisabled";
259 $editable= false;
260 break;
261 case LIST_MARKED:
262 $id= " sortableListItemMarked";
263 break;
264 }
266 $result.= " <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."$id' id='item_".$this->id."_$nr'$reorderable>\n";
267 $first= " style='border:0'";
269 foreach ($this->displayData[$row] as $column) {
270 $result.= " <td$editable$first>".$column."</td>\n";
271 $first= "";
272 }
274 if ($action_width) {
275 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
276 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
277 }
279 $result.= " </tr>\n";
280 }
281 }
283 // Add spacer
284 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
285 $num= $action_width?$this->columns:$this->columns-1;
286 for ($i= 0; $i<$num; $i++) {
287 $result.= "<td class='sortableListItemFill'></td>";
288 }
289 $result.= "</tr>\n";
291 $result.= " </tbody>\n</table>\n</div>\n";
292 $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
293 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
294 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
296 // Append script stuff if needed
297 $result.= '<script type="text/javascript" language="javascript">';
298 if ($this->reorderable) {
299 $result.= ' function updateOrder(){';
300 $result.= ' var ampcharcode= \'%26\';';
301 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
302 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
303 $result.= ' document.mainform.submit();';
304 $result.= ' }';
305 $result.= 'Position.includeScrollOffsets = true;';
306 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
307 }
308 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
309 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
310 $result.= '</script>';
312 return $result;
313 }
316 public function update()
317 {
318 // Do not do anything if this is not our PID, or there's even no PID available...
319 if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) {
320 return;
321 }
323 // Filter GET with "act" attributes
324 if (!$this->reorderable && isset($_GET['act'])) {
325 $key= validate($_GET['act']);
326 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
327 // Switch to new column or invert search order?
328 $column= $match[1];
329 if ($this->sortColumn != $column) {
330 $this->sortColumn= $column;
331 } else {
332 $this->sortDirection[$column]= !$this->sortDirection[$column];
333 }
335 // Update mapping according to sort parameters
336 $this->sortData();
337 }
338 }
340 // Do not do anything if we're not posted - or have no permission
341 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
343 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
344 $this->scrollPosition= $_POST['position_'.$this->id];
345 }
347 // Move requested?
348 $move= $_POST['reorder_'.$this->id];
349 if ($move != "") {
350 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
351 $this->action= "reorder";
352 $tmp= array();
353 foreach ($matches[1] as $id => $row) {
354 $tmp[$id]= $this->mapping[$row];
355 }
356 $this->mapping= $tmp;
357 $this->current_mapping= $matches[1];
358 $this->modified= true;
359 return;
360 }
361 }
363 // Delete requested?
364 if (strpos($this->acl, 'd') !== false){
365 foreach ($_POST as $key => $value) {
366 if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
367 $this->active_index= $this->mapping[$matches[1]];
368 $this->action= "delete";
369 if ($this->instantDelete) {
370 $this->deleteEntry($this->active_index);
371 }
372 }
373 }
374 }
376 // Edit requested?
377 if (strpos($this->acl, 'w') !== false){
378 foreach ($_POST as $key => $value) {
379 if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
380 $this->active_index= $this->mapping[$matches[1]];
381 $this->action= "edit";
382 }
383 }
384 }
385 }
388 public function getAction()
389 {
390 // Do not do anything if we're not posted
391 if(!isset($_POST['reorder_'.$this->id])) {
392 return;
393 }
395 // For reordering, return current mapping
396 if ($this->action == 'reorder') {
397 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
398 }
400 // Edit and delete
401 $result= array("targets" => array($this->active_index), "action" => $this->action);
403 return $result;
404 }
407 private function deleteEntry($id)
408 {
409 // Remove mapping
410 $index= array_search($id, $this->mapping);
411 if ($index !== false) {
412 unset($this->mapping[$index]);
413 $this->mapping= array_values($this->mapping);
414 $this->modified= true;
415 }
416 }
419 public function getMaintainedData()
420 {
421 $tmp= array();
423 foreach ($this->mapping as $src => $dst) {
424 $tmp[$this->keys[$dst]]= $this->data[$dst];
425 }
427 return $tmp;
428 }
431 public function isModified()
432 {
433 return $this->modified;
434 }
437 public function setAcl($acl)
438 {
439 $this->acl= $acl;
440 }
443 public function sortData()
444 {
445 // Extract data
446 $tmp= array();
447 foreach($this->displayData as $item) {
448 if (isset($item[$this->sortColumn])){
449 $tmp[]= $item[$this->sortColumn];
450 } else {
451 $tmp[]= "";
452 }
453 }
455 // Sort entries
456 if ($this->sortDirection[$this->sortColumn]) {
457 asort($tmp);
458 } else {
459 arsort($tmp);
460 }
462 // Adapt mapping accordingly
463 $this->mapping= array();
464 foreach ($tmp as $key => $value) {
465 $this->mapping[]= $key;
466 }
467 }
470 public function addEntry($entry, $displayEntry= null, $key= null, $mode= null)
471 {
472 // Only add if not already there
473 if (!$key) {
474 if (in_array($entry, $this->data)) {
475 return;
476 }
477 } else {
478 if (isset($this->data[$key])) {
479 return;
480 }
481 }
483 // Prefill with default value if not specified
484 if (!$displayEntry) {
485 $displayEntry= array($entry);
486 }
488 // Append to data and mapping
489 if ($key) {
490 $this->data[$key]= $entry;
491 $this->keys[]= $key;
492 } else {
493 $this->data[]= $entry;
494 $this->keys[]= count($this->mapping);
495 }
496 $this->modes[]= $mode;
497 $this->displayData[]= $displayEntry;
498 $this->mapping[]= count($this->mapping);
499 $this->modified= true;
500 }
503 public function getKey($index) {
504 return isset($this->keys[$index])?$this->keys[$index]:null;
505 }
507 }