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) {
301 $result.= " <td$editable$first>".$column."</td>\n";
302 $first= "";
303 }
305 if ($action_width) {
306 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
307 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
308 }
310 $result.= " </tr>\n";
311 }
312 }
314 // Add spacer
315 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
316 $num= $action_width?$this->columns:$this->columns-1;
317 for ($i= 0; $i<$num; $i++) {
318 $result.= "<td class='sortableListItemFill'></td>";
319 }
320 $result.= "</tr>\n";
322 $result.= " </tbody>\n</table>\n</div>\n";
323 # $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
324 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
325 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
327 // Append script stuff if needed
328 $result.= '<script type="text/javascript" language="javascript">';
329 if ($this->reorderable) {
330 $result.= ' function updateOrder(){';
331 $result.= ' var ampcharcode= \'%26\';';
332 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
333 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
334 $result.= ' document.mainform.submit();';
335 $result.= ' }';
336 $result.= 'Position.includeScrollOffsets = true;';
337 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
338 }
339 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
340 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
341 $result.= '</script>';
343 return $result;
344 }
347 public function update()
348 {
350 // Filter GET with "act" attributes
351 if (!$this->reorderable){
352 if(isset($_GET['act']) && isset($_GET['PID']) && $this->id == $_GET['PID']) {
354 $key= validate($_GET['act']);
355 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
357 // Switch to new column or invert search order?
358 $column= $match[1];
359 if ($this->sortColumn != $column) {
360 $this->sortColumn= $column;
361 } else {
362 $this->sortDirection[$column]= !$this->sortDirection[$column];
363 }
365 }
366 }
368 // Update mapping according to sort parameters
369 $this->sortData();
370 }
371 }
374 public function save_object()
375 {
376 // Do not do anything if this is not our PID, or there's even no PID available...
377 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->id) {
378 return;
379 }
381 // Do not do anything if we're not posted - or have no permission
382 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
384 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
385 $this->scrollPosition= $_POST['position_'.$this->id];
386 }
388 // Move requested?
389 $move= $_POST['reorder_'.$this->id];
390 if ($move != "") {
391 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
392 $this->action= "reorder";
393 $tmp= array();
394 foreach ($matches[1] as $id => $row) {
395 $tmp[$id]= $this->mapping[$row];
396 }
397 $this->mapping= $tmp;
398 $this->current_mapping= $matches[1];
399 $this->modified= true;
400 return;
401 }
402 }
404 // Delete requested?
405 $this->action = "";
406 if (strpos($this->acl, 'd') !== false){
407 foreach ($_POST as $key => $value) {
408 if (preg_match('/^del_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
409 $this->active_index= $this->mapping[$matches[1]];
411 // Ignore request if mode requests it
412 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
413 $this->active_index= null;
414 continue;
415 }
417 // Set action
418 $this->action= "delete";
420 // Remove value if requested
421 if ($this->instantDelete) {
422 $this->deleteEntry($this->active_index);
423 }
424 }
425 }
426 }
428 // Edit requested?
429 if (strpos($this->acl, 'w') !== false){
430 foreach ($_POST as $key => $value) {
431 if (preg_match('/^edit_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
432 $this->active_index= $this->mapping[$matches[1]];
434 // Ignore request if mode requests it
435 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
436 $this->active_index= null;
437 continue;
438 }
440 $this->action= "edit";
441 }
442 }
443 }
444 }
447 public function getAction()
448 {
449 // Do not do anything if we're not posted
450 if(!isset($_POST['reorder_'.$this->id])) {
451 return;
452 }
454 // For reordering, return current mapping
455 if ($this->action == 'reorder') {
456 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
457 }
459 // Edit and delete
460 $result= array("targets" => array($this->active_index), "action" => $this->action);
462 return $result;
463 }
466 private function deleteEntry($id)
467 {
468 // Remove mapping
469 $index= array_search($id, $this->mapping);
470 if ($index !== false) {
471 unset($this->mapping[$index]);
472 $this->mapping= array_values($this->mapping);
473 $this->modified= true;
474 }
475 }
478 public function getMaintainedData()
479 {
480 $tmp= array();
481 foreach ($this->mapping as $src => $dst) {
482 $realKey = $this->keys[$dst];
483 $tmp[$realKey] = $this->data[$realKey];
484 }
485 return $tmp;
486 }
489 public function isModified()
490 {
491 return $this->modified;
492 }
495 public function setAcl($acl)
496 {
497 $this->acl= $acl;
498 }
501 public function sortData()
502 {
503 if(!count($this->data)) return;
505 // Extract data
506 $tmp= array();
507 foreach($this->displayData as $item) {
508 if (isset($item[$this->sortColumn])){
509 $tmp[]= $item[$this->sortColumn];
510 } else {
511 $tmp[]= "";
512 }
513 }
515 // Sort entries
516 if ($this->sortDirection[$this->sortColumn]) {
517 asort($tmp);
518 } else {
519 arsort($tmp);
520 }
522 // Adapt mapping accordingly
523 $this->mapping= array();
524 foreach ($tmp as $key => $value) {
525 $this->mapping[]= $key;
526 }
527 }
530 public function addEntry($entry, $displayEntry= null, $key= null)
531 {
532 // Only add if not already there
533 if (!$key) {
534 if (in_array($entry, $this->data)) {
535 return;
536 }
537 } else {
538 if (isset($this->data[$key])) {
539 return;
540 }
541 }
543 // Prefill with default value if not specified
544 if (!$displayEntry) {
545 $displayEntry= array('data' => array($entry));
546 }
548 // Append to data and mapping
549 if ($key) {
550 $this->data[$key]= $entry;
551 $this->keys[]= $key;
552 } else {
553 $this->data[]= $entry;
554 $this->keys[]= count($this->mapping);
555 }
556 $this->displayData[]= $displayEntry['data'];
557 $this->mapping[]= count($this->mapping);
558 $this->modified= true;
560 // Find the number of coluns
561 reset($this->displayData);
562 $first= current($this->displayData);
563 if (is_array($first)) {
564 $this->columns= count($first);
565 } else {
566 $this->columns= 1;
567 }
569 // Preset sort orders to 'down'
570 for ($column= 0; $column<$this->columns; $column++) {
571 if(!isset($this->sortDirection[$column])){
572 $this->sortDirection[$column]= true;
573 }
574 }
577 // Sort data after we've added stuff
578 $this->sortData();
579 }
582 public function getKey($index) {
583 return isset($this->keys[$index])?$this->keys[$index]:null;
584 }
586 public function getData($index) {
587 $realkey = $this->keys[$index];
588 return($this->data[$realkey]);
589 }
590 }