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 $sortingEnabled= true;
37 private $data= array();
38 private $keys= array();
39 private $modes= array();
40 private $displayData= array();
41 private $columns= 0;
42 private $deleteable= false;
43 private $editable= false;
44 private $colorAlternate= false;
45 private $instantDelete= true;
46 private $action;
47 private $mapping;
48 private $current_mapping;
49 private $active_index;
50 private $scrollPosition= 0;
51 private $sortColumn= 0;
52 private $sortDirection= array();
54 private $acl= "";
55 private $modified= false;
58 public function sortableListing($data= array(), $displayData= null, $reorderable= false)
59 {
60 global $config;
62 // Save data to display
63 $this->setListData($data, $displayData);
65 // Get list of used IDs
66 if(!session::is_set('sortableListing_USED_IDS')){
67 session::set('sortableListing_USED_IDS',array());
68 }
69 $usedIds = session::get('sortableListing_USED_IDS');
71 // Generate instance wide unique ID
72 $id = "";
73 while($id == "" || in_array($id, $usedIds)){
75 // Wait 1 msec to ensure that we definately get a new id
76 if($id != "") usleep(1);
77 $tmp= gettimeofday();
78 $id = 'l'.md5(microtime().$tmp['sec']);
79 }
81 // Only keep the last 10 list IDsi
82 $usedIds = array_slice($usedIds, count($usedIds) -10, 10);
83 $usedIds[] = $id;
84 session::set('sortableListing_USED_IDS',$usedIds);
85 $this->id = $id;
87 // Set reorderable flag
88 $this->reorderable= $reorderable;
89 if (!$reorderable) {
90 $this->sortData();
91 }
92 }
94 public function setReorderable($bool)
95 {
96 $this->reorderable= $bool;
97 }
99 public function setDefaultSortColumn($id)
100 {
101 $this->sortColumn = $id;
102 }
104 /*
105 *
106 * Examples
107 * DatenARray ($data)
108 * @param: array( arbitrary object, arbitrary object)
109 * Datenarray will be manipulated by add, del and sort operations. According to this it will be returned from this widget.
110 * The index of a data entry must correspond to the entry of the "display array" following.
111 * DisplayArray ($displyData)
112 * @param: array("eins" array( "data"=> array("Uno", "2", "x" ) , "zwei" array( "data"=> array("Due", "3", "y" ))) ;
113 * label pointing on a list of columns that will be shown in the list.
114 */
115 public function setListData($data, $displayData= null)
116 {
117 // Save data to display
118 $this->setData($data);
119 if (!$displayData) {
120 $displayData= array();
121 foreach ($data as $key => $value) {
122 $displayData[$key]= array("data" => array($value));
123 }
124 }
125 $this->setDisplayData($displayData);
126 }
128 //setting flat data
129 private function setData($data)
130 {
131 $this->data= $data;
132 }
134 // collecting the display data -
135 private function setDisplayData($data)
136 {
137 if (!is_array($data)) {
138 trigger_error ("sortableList needs an array as data!");
139 }
141 // Transfer information
142 $this->displayData= array();
143 $this->modes= array();
144 $this->mapping= array();
145 foreach ($data as $key => $value) {
146 $this->displayData[]= $value['data'];
147 if (isset($value['mode'])) {
148 $this->modes[]= $value['mode'];
149 }
150 }
151 $this->keys= array_keys($data);
153 // Create initial mapping
154 if(count($this->keys)){
155 $this->mapping= range(0, abs(count($this->keys)-1));
156 }
157 $this->current_mapping= $this->mapping;
159 // Find the number of coluns
160 reset($this->displayData);
161 $first= current($this->displayData);
162 if (is_array($first)) {
163 $this->columns= count($first);
164 } else {
165 $this->columns= 1;
166 }
168 // Preset sort orders to 'down'
169 for ($column= 0; $column<$this->columns; $column++) {
170 if(!isset($this->sortDirection[$column])){
171 $this->sortDirection[$column]= true;
172 }
173 }
174 }
177 public function setWidth($width)
178 {
179 $this->width= $width;
180 }
183 public function setInstantDelete($flag)
184 {
185 $this->instantDelete= $flag;
186 }
189 public function setColorAlternate($flag)
190 {
191 $this->colorAlternate= $flag;
192 }
195 public function setEditable($flag)
196 {
197 $this->editable= $flag;
198 }
201 public function setDeleteable($flag)
202 {
203 $this->deleteable= $flag;
204 }
207 public function setHeight($height)
208 {
209 $this->height= $height;
210 }
213 public function setCssClass($css)
214 {
215 $this->cssclass= $css;
216 }
219 public function setHeader($header)
220 {
221 $this->header= $header;
222 }
225 public function setColspecs($specs)
226 {
227 $this->colspecs= $specs;
228 }
231 public function render()
232 {
233 $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='min-width:".$this->width.";height: ".$this->height."'>\n";
234 $result.= "<table summary='"._("Sortable list")."' border='0' cellpadding='0' cellspacing='0' width='100%' style='width:100%' ".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
235 $action_width= 0;
236 if (strpos($this->acl, 'w') === false) {
237 $edit_image= $this->editable?image("images/lists/edit-grey.png"):"";
238 } else {
239 $edit_image= $this->editable?image('images/lists/edit.png', "%ID", _("Edit this entry")):"";
240 }
241 if (strpos($this->acl, 'w') === false) {
242 $delete_image= $this->deleteable?image('images/lists/trash-grey.png'):"";
243 } else {
244 $delete_image= $this->deleteable?image('images/lists/trash.png', "%ID", _("Delete this entry")):"";
245 }
247 // Do we need colspecs?
248 $action_width= ($this->editable?30:0) + ($this->deleteable?30:0);
249 if ($this->colspecs) {
250 $result.= " <colgroup>\n";
251 for ($i= 0; $i<$this->columns; $i++) {
252 if(isset($this->colspecs[$i]) && $this->colspecs[$i] != '*'){
253 $result.= " <col style='width:".($this->colspecs[$i])."'>\n";
254 }else{
255 $result.= " <col>\n";
256 }
257 }
259 // Extend by another column if we've actions specified
260 if ($action_width) {
261 $result.= " <col style='width:".$action_width."px' >\n";
262 }
263 $result.= " </colgroup>\n";
264 }
266 // Do we need a header?
267 if ($this->header) {
268 $result.= " <thead>\n <tr>\n";
269 $first= " style='border-left:0'";
270 for ($i= 0; $i<$this->columns; $i++) {
271 $link= "href='?plug=".$_GET['plug']."&PID=".$this->id."&act=SORT_$i'";
272 $sorter= "";
273 if ($i == $this->sortColumn){
274 $sorter= " ".image("images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png", null, $this->sortDirection[$i]?_("Sort ascending"):_("Sort descending"));
275 }
277 if ($this->reorderable) {
278 $result.= " <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
279 } else {
280 $result.= " <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."</a>$sorter</th>";
281 }
282 $first= "";
283 }
284 if ($action_width) {
285 $result.= "<th> </th>";
286 }
287 $result.= "\n </tr>\n </thead>\n";
288 }
290 // Render table body if we've read permission
291 $result.= " <tbody id='".$this->id."'>\n";
292 $reorderable= $this->reorderable?"":" style='cursor:default'";
293 if (strpos($this->acl, 'r') !== false) {
294 foreach ($this->mapping as $nr => $row) {
295 $editable= $this->editable?" onClick='$(\"edit_".$this->id."_$nr\").click()'":"";
297 $id= "";
298 if (isset($this->modes[$row])) {
299 switch ($this->modes[$row]) {
300 case LIST_DISABLED:
301 $id= " sortableListItemDisabled";
302 $editable= "";
303 break;
304 case LIST_MARKED:
305 $id= " sortableListItemMarked";
306 break;
307 }
308 }
310 $result.= " <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."$id' id='item_".$this->id."_$nr'$reorderable>\n";
311 $first= " style='border:0'";
313 foreach ($this->displayData[$row] as $column) {
315 // Do NOT use the onClick statement for columns that contain links or buttons.
316 if(preg_match("<.*type=.submit..*>", $column) || preg_match("<a.*href=.*>", $column)){
317 $result.= " <td$first>".$column."</td>\n";
318 }else{
319 $result.= " <td$editable$first>".$column."</td>\n";
320 }
321 $first= "";
322 }
324 if ($action_width) {
325 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
326 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
327 }
329 $result.= " </tr>\n";
330 }
331 }
333 // Add spacer
334 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
335 $num= $action_width?$this->columns:$this->columns-1;
336 for ($i= 0; $i<$num; $i++) {
337 $result.= "<td class='sortableListItemFill'></td>";
338 }
339 $result.= "</tr>\n";
341 $result.= " </tbody>\n</table>\n</div>\n";
342 # $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
343 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
344 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
346 // Append script stuff if needed
347 $result.= '<script type="text/javascript" language="javascript">';
348 if ($this->reorderable) {
349 $result.= ' function updateOrder(){';
350 $result.= ' var ampcharcode= \'%26\';';
351 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
352 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
353 $result.= ' document.mainform.submit();';
354 $result.= ' }';
355 $result.= 'Position.includeScrollOffsets = true;';
356 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
357 }
358 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
359 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
360 $result.= '</script>';
362 return $result;
363 }
366 public function update()
367 {
369 // Filter GET with "act" attributes
370 if (!$this->reorderable){
371 if(isset($_GET['act']) && isset($_GET['PID']) && $this->id == $_GET['PID']) {
373 $key= validate($_GET['act']);
374 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
376 // Switch to new column or invert search order?
377 $column= $match[1];
378 if ($this->sortColumn != $column) {
379 $this->sortColumn= $column;
380 } else {
381 $this->sortDirection[$column]= !$this->sortDirection[$column];
382 }
384 }
385 }
387 // Update mapping according to sort parameters
388 $this->sortData();
389 }
390 }
393 public function save_object()
394 {
395 // Do not do anything if this is not our PID, or there's even no PID available...
396 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->id) {
397 return;
398 }
400 // Do not do anything if we're not posted - or have no permission
401 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
403 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
404 $this->scrollPosition= get_post('position_'.$this->id);
405 }
407 // Move requested?
408 $move= get_post('reorder_'.$this->id);
409 if ($move != "") {
410 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
411 $this->action= "reorder";
412 $tmp= array();
413 foreach ($matches[1] as $id => $row) {
414 $tmp[$id]= $this->mapping[$row];
415 }
416 $this->mapping= $tmp;
417 $this->current_mapping= $matches[1];
418 $this->modified= true;
419 return;
420 }
421 }
423 // Delete requested?
424 $this->action = "";
425 if (strpos($this->acl, 'd') !== false){
426 foreach ($_POST as $key => $value) {
427 $value = get_post($key);
428 if (preg_match('/^del_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
429 $this->active_index= $this->mapping[$matches[1]];
431 // Ignore request if mode requests it
432 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
433 $this->active_index= null;
434 continue;
435 }
437 // Set action
438 $this->action= "delete";
440 // Remove value if requested
441 if ($this->instantDelete) {
442 $this->deleteEntry($this->active_index);
443 }
444 }
445 }
446 }
448 // Edit requested?
449 if (strpos($this->acl, 'w') !== false){
450 foreach ($_POST as $key => $value) {
451 $value = get_post($key);
452 if (preg_match('/^edit_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
453 $this->active_index= $this->mapping[$matches[1]];
455 // Ignore request if mode requests it
456 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
457 $this->active_index= null;
458 continue;
459 }
461 $this->action= "edit";
462 }
463 }
464 }
465 }
468 public function getAction()
469 {
470 // Do not do anything if we're not posted
471 if(!isset($_POST['reorder_'.$this->id])) {
472 return;
473 }
475 // For reordering, return current mapping
476 if ($this->action == 'reorder') {
477 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
478 }
480 // Edit and delete
481 $result= array("targets" => array($this->active_index), "action" => $this->action);
483 return $result;
484 }
487 private function deleteEntry($id)
488 {
489 // Remove mapping
490 $index= array_search($id, $this->mapping);
491 if ($index !== false) {
492 unset($this->mapping[$index]);
493 $this->mapping= array_values($this->mapping);
494 $this->modified= true;
495 }
496 }
499 public function getMaintainedData()
500 {
501 $tmp= array();
502 foreach ($this->mapping as $src => $dst) {
503 $realKey = $this->keys[$dst];
504 $tmp[$realKey] = $this->data[$realKey];
505 }
506 return $tmp;
507 }
510 public function isModified()
511 {
512 return $this->modified;
513 }
516 public function setAcl($acl)
517 {
518 $this->acl= $acl;
519 }
522 public function sortingEnabled($bool = TRUE)
523 {
524 $this->sortingEnabled= $bool;
525 }
528 public function sortData()
529 {
530 if(!$this->sortingEnabled || !count($this->data)) return;
532 // Extract data
533 $tmp= array();
534 foreach($this->displayData as $item) {
535 if (isset($item[$this->sortColumn])){
536 $tmp[]= $item[$this->sortColumn];
537 } else {
538 $tmp[]= "";
539 }
540 }
542 // Sort entries
543 if ($this->sortDirection[$this->sortColumn]) {
544 asort($tmp);
545 } else {
546 arsort($tmp);
547 }
549 // Adapt mapping accordingly
550 $this->mapping= array();
551 foreach ($tmp as $key => $value) {
552 $this->mapping[]= $key;
553 }
554 }
557 public function addEntry($entry, $displayEntry= null, $key= null)
558 {
559 // Only add if not already there
560 if (!$key) {
561 if (in_array($entry, $this->data)) {
562 return;
563 }
564 } else {
565 if (isset($this->data[$key])) {
566 return;
567 }
568 }
570 // Prefill with default value if not specified
571 if (!$displayEntry) {
572 $displayEntry= array('data' => array($entry));
573 }
575 // Append to data and mapping
576 if ($key) {
577 $this->data[$key]= $entry;
578 $this->keys[]= $key;
579 } else {
580 $this->data[]= $entry;
581 $this->keys[]= count($this->mapping);
582 }
583 $this->displayData[]= $displayEntry['data'];
584 $this->mapping[]= count($this->mapping);
585 $this->modified= true;
587 // Find the number of coluns
588 reset($this->displayData);
589 $first= current($this->displayData);
590 if (is_array($first)) {
591 $this->columns= count($first);
592 } else {
593 $this->columns= 1;
594 }
596 // Preset sort orders to 'down'
597 for ($column= 0; $column<$this->columns; $column++) {
598 if(!isset($this->sortDirection[$column])){
599 $this->sortDirection[$column]= true;
600 }
601 }
604 // Sort data after we've added stuff
605 $this->sortData();
606 }
609 public function getKey($index) {
610 return isset($this->keys[$index])?$this->keys[$index]:null;
611 }
613 public function getData($index) {
614 $realkey = $this->keys[$index];
615 return($this->data[$realkey]);
616 }
617 }