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, $reorderable= false)
57 {
58 global $config;
60 // Save data to display
61 $this->setListData($data, $displayData);
63 // Get list of used IDs
64 if(!session::is_set('sortableListing_USED_IDS')){
65 session::set('sortableListing_USED_IDS',array());
66 }
67 $usedIds = session::get('sortableListing_USED_IDS');
69 // Generate instance wide unique ID
70 $id = "";
71 while($id == "" || in_array($id, $usedIds)){
73 // Wait 1 msec to ensure that we definately get a new id
74 if($id != "") usleep(1);
75 $tmp= gettimeofday();
76 $id = 'l'.md5(microtime().$tmp['sec']);
77 }
79 // Only keep the last 10 list IDsi
80 $usedIds = array_slice($usedIds, count($usedIds) -10, 10);
81 $usedIds[] = $id;
82 session::set('sortableListing_USED_IDS',$usedIds);
83 $this->id = $id;
85 // Set reorderable flag
86 $this->reorderable= $reorderable;
87 if (!$reorderable) {
88 $this->sortData();
89 }
90 }
92 public function setReorderable($bool)
93 {
94 $this->reorderable= $bool;
95 }
97 public function setDefaultSortColumn($id)
98 {
99 $this->sortColumn = $id;
100 }
102 public function setListData($data, $displayData= null)
103 {
104 // Save data to display
105 $this->setData($data);
106 if (!$displayData) {
107 $displayData= array();
108 foreach ($data as $key => $value) {
109 $displayData[$key]= array("data" => array($value));
110 }
111 }
112 $this->setDisplayData($displayData);
113 }
116 private function setData($data)
117 {
118 $this->data= $data;
119 }
122 private function setDisplayData($data)
123 {
124 if (!is_array($data)) {
125 trigger_error ("sortableList needs an array as data!");
126 }
128 // Transfer information
129 $this->displayData= array();
130 $this->modes= array();
131 $this->mapping= array();
132 foreach ($data as $key => $value) {
133 $this->displayData[]= $value['data'];
134 if (isset($value['mode'])) {
135 $this->modes[]= $value['mode'];
136 }
137 }
138 $this->keys= array_keys($data);
140 // Create initial mapping
141 if(count($this->keys)){
142 $this->mapping= range(0, abs(count($this->keys)-1));
143 }
144 $this->current_mapping= $this->mapping;
146 // Find the number of coluns
147 reset($this->displayData);
148 $first= current($this->displayData);
149 if (is_array($first)) {
150 $this->columns= count($first);
151 } else {
152 $this->columns= 1;
153 }
155 // Preset sort orders to 'down'
156 for ($column= 0; $column<$this->columns; $column++) {
157 if(!isset($this->sortDirection[$column])){
158 $this->sortDirection[$column]= true;
159 }
160 }
161 }
164 public function setWidth($width)
165 {
166 $this->width= $width;
167 }
170 public function setInstantDelete($flag)
171 {
172 $this->instantDelete= $flag;
173 }
176 public function setColorAlternate($flag)
177 {
178 $this->colorAlternate= $flag;
179 }
182 public function setEditable($flag)
183 {
184 $this->editable= $flag;
185 }
188 public function setDeleteable($flag)
189 {
190 $this->deleteable= $flag;
191 }
194 public function setHeight($height)
195 {
196 $this->height= $height;
197 }
200 public function setCssClass($css)
201 {
202 $this->cssclass= $css;
203 }
206 public function setHeader($header)
207 {
208 $this->header= $header;
209 }
212 public function setColspecs($specs)
213 {
214 $this->colspecs= $specs;
215 }
218 public function render()
219 {
220 $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='min-width:".$this->width.";height: ".$this->height."'>\n";
221 $result.= "<table summary='"._("Sortable list")."' border='0' cellpadding='0' cellspacing='0' width='100%' style='width:100%' ".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
222 $action_width= 0;
223 if (strpos($this->acl, 'w') === false) {
224 $edit_image= $this->editable?"<img class='center' src='images/lists/edit-grey.png' alt='"._("Edit")."'>":"";
225 } else {
226 $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' id='%ID' title='"._("Edit this entry")."'>":"";
227 }
228 if (strpos($this->acl, 'w') === false) {
229 $delete_image= $this->deleteable?"<img class='center' src='images/lists/trash-grey.png' alt='"._("Delete")."'>":"";
230 } else {
231 $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
232 }
234 // Do we need colspecs?
235 $action_width= ($this->editable?30:0) + ($this->deleteable?30:0);
236 if ($this->colspecs) {
237 $result.= " <colgroup>\n";
238 for ($i= 0; $i<$this->columns; $i++) {
239 if(isset($this->colspecs[$i]) && $this->colspecs[$i] != '*'){
240 $result.= " <col style='width:".($this->colspecs[$i])."'>\n";
241 }else{
242 $result.= " <col>\n";
243 }
244 }
246 // Extend by another column if we've actions specified
247 if ($action_width) {
248 $result.= " <col style='width:".$action_width."px' >\n";
249 }
250 $result.= " </colgroup>\n";
251 }
253 // Do we need a header?
254 if ($this->header) {
255 $result.= " <thead>\n <tr>\n";
256 $first= " style='border-left:0'";
257 for ($i= 0; $i<$this->columns; $i++) {
258 $link= "href='?plug=".$_GET['plug']."&PID=".$this->id."&act=SORT_$i'";
259 $sorter= "";
260 if ($i == $this->sortColumn){
261 $sorter= " <img border='0' title='".($this->sortDirection[$i]?_("Up"):_("Down"))."'
262 src='images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png' align='top'>";
263 }
265 if ($this->reorderable) {
266 $result.= " <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
267 } else {
268 $result.= " <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."</a>$sorter</th>";
269 }
270 $first= "";
271 }
272 if ($action_width) {
273 $result.= "<th> </th>";
274 }
275 $result.= "\n </tr>\n </thead>\n";
276 }
278 // Render table body if we've read permission
279 $result.= " <tbody id='".$this->id."'>\n";
280 $reorderable= $this->reorderable?"":" style='cursor:default'";
281 if (strpos($this->acl, 'r') !== false) {
282 foreach ($this->mapping as $nr => $row) {
283 $editable= $this->editable?" onClick='$(\"edit_".$this->id."_$nr\").click()'":"";
285 $id= "";
286 if (isset($this->modes[$row])) {
287 switch ($this->modes[$row]) {
288 case LIST_DISABLED:
289 $id= " sortableListItemDisabled";
290 $editable= "";
291 break;
292 case LIST_MARKED:
293 $id= " sortableListItemMarked";
294 break;
295 }
296 }
298 $result.= " <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."$id' id='item_".$this->id."_$nr'$reorderable>\n";
299 $first= " style='border:0'";
301 foreach ($this->displayData[$row] as $column) {
303 // Do NOT use the onClick statement for columns that contain links or buttons.
304 if(preg_match("<.*type=.submit..*>", $column) || preg_match("<a.*href=.*>", $column)){
305 $result.= " <td$first>".$column."</td>\n";
306 }else{
307 $result.= " <td$editable$first>".$column."</td>\n";
308 }
309 $first= "";
310 }
312 if ($action_width) {
313 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
314 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
315 }
317 $result.= " </tr>\n";
318 }
319 }
321 // Add spacer
322 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
323 $num= $action_width?$this->columns:$this->columns-1;
324 for ($i= 0; $i<$num; $i++) {
325 $result.= "<td class='sortableListItemFill'></td>";
326 }
327 $result.= "</tr>\n";
329 $result.= " </tbody>\n</table>\n</div>\n";
330 # $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
331 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
332 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
334 // Append script stuff if needed
335 $result.= '<script type="text/javascript" language="javascript">';
336 if ($this->reorderable) {
337 $result.= ' function updateOrder(){';
338 $result.= ' var ampcharcode= \'%26\';';
339 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
340 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
341 $result.= ' document.mainform.submit();';
342 $result.= ' }';
343 $result.= 'Position.includeScrollOffsets = true;';
344 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
345 }
346 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
347 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
348 $result.= '</script>';
350 return $result;
351 }
354 public function update()
355 {
357 // Filter GET with "act" attributes
358 if (!$this->reorderable){
359 if(isset($_GET['act']) && isset($_GET['PID']) && $this->id == $_GET['PID']) {
361 $key= validate($_GET['act']);
362 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
364 // Switch to new column or invert search order?
365 $column= $match[1];
366 if ($this->sortColumn != $column) {
367 $this->sortColumn= $column;
368 } else {
369 $this->sortDirection[$column]= !$this->sortDirection[$column];
370 }
372 }
373 }
375 // Update mapping according to sort parameters
376 $this->sortData();
377 }
378 }
381 public function save_object()
382 {
383 // Do not do anything if this is not our PID, or there's even no PID available...
384 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->id) {
385 return;
386 }
388 // Do not do anything if we're not posted - or have no permission
389 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
391 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
392 $this->scrollPosition= $_POST['position_'.$this->id];
393 }
395 // Move requested?
396 $move= $_POST['reorder_'.$this->id];
397 if ($move != "") {
398 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
399 $this->action= "reorder";
400 $tmp= array();
401 foreach ($matches[1] as $id => $row) {
402 $tmp[$id]= $this->mapping[$row];
403 }
404 $this->mapping= $tmp;
405 $this->current_mapping= $matches[1];
406 $this->modified= true;
407 return;
408 }
409 }
411 // Delete requested?
412 $this->action = "";
413 if (strpos($this->acl, 'd') !== false){
414 foreach ($_POST as $key => $value) {
415 if (preg_match('/^del_'.$this->id.'_([0-9]+)_x$/', $key, $matches)) {
416 $this->active_index= $this->mapping[$matches[1]];
418 // Ignore request if mode requests it
419 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
420 $this->active_index= null;
421 continue;
422 }
424 // Set action
425 $this->action= "delete";
427 // Remove value if requested
428 if ($this->instantDelete) {
429 $this->deleteEntry($this->active_index);
430 }
431 }
432 }
433 }
435 // Edit requested?
436 if (strpos($this->acl, 'w') !== false){
437 foreach ($_POST as $key => $value) {
438 if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x$/', $key, $matches)) {
439 $this->active_index= $this->mapping[$matches[1]];
441 // Ignore request if mode requests it
442 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
443 $this->active_index= null;
444 continue;
445 }
447 $this->action= "edit";
448 }
449 }
450 }
451 }
454 public function getAction()
455 {
456 // Do not do anything if we're not posted
457 if(!isset($_POST['reorder_'.$this->id])) {
458 return;
459 }
461 // For reordering, return current mapping
462 if ($this->action == 'reorder') {
463 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
464 }
466 // Edit and delete
467 $result= array("targets" => array($this->active_index), "action" => $this->action);
469 return $result;
470 }
473 private function deleteEntry($id)
474 {
475 // Remove mapping
476 $index= array_search($id, $this->mapping);
477 if ($index !== false) {
478 unset($this->mapping[$index]);
479 $this->mapping= array_values($this->mapping);
480 $this->modified= true;
481 }
482 }
485 public function getMaintainedData()
486 {
487 $tmp= array();
488 foreach ($this->mapping as $src => $dst) {
489 $realKey = $this->keys[$dst];
490 $tmp[$realKey] = $this->data[$realKey];
491 }
492 return $tmp;
493 }
496 public function isModified()
497 {
498 return $this->modified;
499 }
502 public function setAcl($acl)
503 {
504 $this->acl= $acl;
505 }
508 public function sortData()
509 {
510 if(!count($this->data)) return;
512 // Extract data
513 $tmp= array();
514 foreach($this->displayData as $item) {
515 if (isset($item[$this->sortColumn])){
516 $tmp[]= $item[$this->sortColumn];
517 } else {
518 $tmp[]= "";
519 }
520 }
522 // Sort entries
523 if ($this->sortDirection[$this->sortColumn]) {
524 asort($tmp);
525 } else {
526 arsort($tmp);
527 }
529 // Adapt mapping accordingly
530 $this->mapping= array();
531 foreach ($tmp as $key => $value) {
532 $this->mapping[]= $key;
533 }
534 }
537 public function addEntry($entry, $displayEntry= null, $key= null)
538 {
539 // Only add if not already there
540 if (!$key) {
541 if (in_array($entry, $this->data)) {
542 return;
543 }
544 } else {
545 if (isset($this->data[$key])) {
546 return;
547 }
548 }
550 // Prefill with default value if not specified
551 if (!$displayEntry) {
552 $displayEntry= array('data' => array($entry));
553 }
555 // Append to data and mapping
556 if ($key) {
557 $this->data[$key]= $entry;
558 $this->keys[]= $key;
559 } else {
560 $this->data[]= $entry;
561 $this->keys[]= count($this->mapping);
562 }
563 $this->displayData[]= $displayEntry['data'];
564 $this->mapping[]= count($this->mapping);
565 $this->modified= true;
567 // Find the number of coluns
568 reset($this->displayData);
569 $first= current($this->displayData);
570 if (is_array($first)) {
571 $this->columns= count($first);
572 } else {
573 $this->columns= 1;
574 }
576 // Preset sort orders to 'down'
577 for ($column= 0; $column<$this->columns; $column++) {
578 if(!isset($this->sortDirection[$column])){
579 $this->sortDirection[$column]= true;
580 }
581 }
584 // Sort data after we've added stuff
585 $this->sortData();
586 }
589 public function getKey($index) {
590 return isset($this->keys[$index])?$this->keys[$index]:null;
591 }
593 public function getData($index) {
594 $realkey = $this->keys[$index];
595 return($this->data[$realkey]);
596 }
597 }