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?image("images/lists/edit-grey.png"):"";
225 } else {
226 $edit_image= $this->editable?image('images/lists/edit.png', "%ID", _("Edit this entry")):"";
227 }
228 if (strpos($this->acl, 'd') === false) {
229 $delete_image= $this->deleteable?image('images/lists/trash-grey.png'):"";
230 } else {
231 $delete_image= $this->deleteable?image('images/lists/trash.png', "%ID", _("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= " ".image("images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png", null, $this->sortDirection[$i]?_("Sort ascending"):_("Sort descending"));
262 }
264 if ($this->reorderable) {
265 $result.= " <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
266 } else {
267 $result.= " <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."</a>$sorter</th>";
268 }
269 $first= "";
270 }
271 if ($action_width) {
272 $result.= "<th> </th>";
273 }
274 $result.= "\n </tr>\n </thead>\n";
275 }
277 // Render table body if we've read permission
278 $result.= " <tbody id='".$this->id."'>\n";
279 $reorderable= $this->reorderable?"":" style='cursor:default'";
280 if (strpos($this->acl, 'r') !== false) {
281 foreach ($this->mapping as $nr => $row) {
282 $editable= $this->editable?" onClick='$(\"edit_".$this->id."_$nr\").click()'":"";
284 $id= "";
285 if (isset($this->modes[$row])) {
286 switch ($this->modes[$row]) {
287 case LIST_DISABLED:
288 $id= " sortableListItemDisabled";
289 $editable= "";
290 break;
291 case LIST_MARKED:
292 $id= " sortableListItemMarked";
293 break;
294 }
295 }
297 $result.= " <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."$id' id='item_".$this->id."_$nr'$reorderable>\n";
298 $first= " style='border:0'";
300 foreach ($this->displayData[$row] as $column) {
302 // Do NOT use the onClick statement for columns that contain links or buttons.
303 if(preg_match("<.*type=.submit..*>", $column) || preg_match("<a.*href=.*>", $column)){
304 $result.= " <td$first>".$column."</td>\n";
305 }else{
306 $result.= " <td$editable$first>".$column."</td>\n";
307 }
308 $first= "";
309 }
311 if ($action_width) {
312 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
313 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
314 }
316 $result.= " </tr>\n";
317 }
318 }
320 // Add spacer
321 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
322 $num= $action_width?$this->columns:$this->columns-1;
323 for ($i= 0; $i<$num; $i++) {
324 $result.= "<td class='sortableListItemFill'></td>";
325 }
326 $result.= "</tr>\n";
328 $result.= " </tbody>\n</table>\n</div>\n";
329 # $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
330 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
331 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
333 // Append script stuff if needed
334 $result.= '<script type="text/javascript" language="javascript">';
335 if ($this->reorderable) {
336 $result.= ' function updateOrder(){';
337 $result.= ' var ampcharcode= \'%26\';';
338 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
339 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
340 $result.= ' document.mainform.submit();';
341 $result.= ' }';
342 $result.= 'Position.includeScrollOffsets = true;';
343 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
344 }
345 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
346 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
347 $result.= '</script>';
349 return $result;
350 }
353 public function update()
354 {
356 // Filter GET with "act" attributes
357 if (!$this->reorderable){
358 if(isset($_GET['act']) && isset($_GET['PID']) && $this->id == $_GET['PID']) {
360 $key= validate($_GET['act']);
361 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
363 // Switch to new column or invert search order?
364 $column= $match[1];
365 if ($this->sortColumn != $column) {
366 $this->sortColumn= $column;
367 } else {
368 $this->sortDirection[$column]= !$this->sortDirection[$column];
369 }
371 }
372 }
374 // Update mapping according to sort parameters
375 $this->sortData();
376 }
377 }
380 public function save_object()
381 {
382 // Do not do anything if this is not our PID, or there's even no PID available...
383 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->id) {
384 return;
385 }
387 // Do not do anything if we're not posted - or have no permission
388 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
390 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
391 $this->scrollPosition= get_post('position_'.$this->id);
392 }
394 // Move requested?
395 $move= get_post('reorder_'.$this->id);
396 if ($move != "") {
397 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
398 $this->action= "reorder";
399 $tmp= array();
400 foreach ($matches[1] as $id => $row) {
401 $tmp[$id]= $this->mapping[$row];
402 }
403 $this->mapping= $tmp;
404 $this->current_mapping= $matches[1];
405 $this->modified= true;
406 return;
407 }
408 }
410 // Delete requested?
411 $this->action = "";
412 if (strpos($this->acl, 'd') !== false){
413 foreach ($_POST as $key => $value) {
414 $value = get_post($key);
415 if (preg_match('/^del_'.$this->id.'_([0-9]+)$/', $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 $value = get_post($key);
439 if (preg_match('/^edit_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
440 $this->active_index= $this->mapping[$matches[1]];
442 // Ignore request if mode requests it
443 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
444 $this->active_index= null;
445 continue;
446 }
448 $this->action= "edit";
449 }
450 }
451 }
452 }
455 public function getAction()
456 {
457 // Do not do anything if we're not posted
458 if(!isset($_POST['reorder_'.$this->id])) {
459 return;
460 }
462 // For reordering, return current mapping
463 if ($this->action == 'reorder') {
464 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
465 }
467 // Edit and delete
468 $result= array("targets" => array($this->active_index), "action" => $this->action);
470 return $result;
471 }
474 private function deleteEntry($id)
475 {
476 // Remove mapping
477 $index= array_search($id, $this->mapping);
478 if ($index !== false) {
479 unset($this->mapping[$index]);
480 $this->mapping= array_values($this->mapping);
481 $this->modified= true;
482 }
483 }
486 public function getMaintainedData()
487 {
488 $tmp= array();
489 foreach ($this->mapping as $src => $dst) {
490 $realKey = $this->keys[$dst];
491 $tmp[$realKey] = $this->data[$realKey];
492 }
493 return $tmp;
494 }
497 public function isModified()
498 {
499 return $this->modified;
500 }
503 public function setAcl($acl)
504 {
505 $this->acl= $acl;
506 }
509 public function sortData()
510 {
511 if(!count($this->data)) return;
513 // Extract data
514 $tmp= array();
515 foreach($this->displayData as $item) {
516 if (isset($item[$this->sortColumn])){
517 $tmp[]= $item[$this->sortColumn];
518 } else {
519 $tmp[]= "";
520 }
521 }
523 // Sort entries
524 if ($this->sortDirection[$this->sortColumn]) {
525 asort($tmp);
526 } else {
527 arsort($tmp);
528 }
530 // Adapt mapping accordingly
531 $this->mapping= array();
532 foreach ($tmp as $key => $value) {
533 $this->mapping[]= $key;
534 }
535 }
538 public function addEntry($entry, $displayEntry= null, $key= null)
539 {
540 // Only add if not already there
541 if (!$key) {
542 if (in_array($entry, $this->data)) {
543 return;
544 }
545 } else {
546 if (isset($this->data[$key])) {
547 return;
548 }
549 }
551 // Prefill with default value if not specified
552 if (!$displayEntry) {
553 $displayEntry= array('data' => array($entry));
554 }
556 // Append to data and mapping
557 if ($key) {
558 $this->data[$key]= $entry;
559 $this->keys[]= $key;
560 } else {
561 $this->data[]= $entry;
562 $this->keys[]= count($this->mapping);
563 }
564 $this->displayData[]= $displayEntry['data'];
565 $this->mapping[]= count($this->mapping);
566 $this->modified= true;
568 // Find the number of coluns
569 reset($this->displayData);
570 $first= current($this->displayData);
571 if (is_array($first)) {
572 $this->columns= count($first);
573 } else {
574 $this->columns= 1;
575 }
577 // Preset sort orders to 'down'
578 for ($column= 0; $column<$this->columns; $column++) {
579 if(!isset($this->sortDirection[$column])){
580 $this->sortDirection[$column]= true;
581 }
582 }
585 // Sort data after we've added stuff
586 $this->sortData();
587 }
590 public function getKey($index) {
591 return isset($this->keys[$index])?$this->keys[$index]:null;
592 }
594 public function getData($index) {
595 $realkey = $this->keys[$index];
596 return($this->data[$realkey]);
597 }
598 }