75de2bee55a554c2a227037a69c831217179bd85
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 // Generate instance wide unique ID
64 $tmp= gettimeofday();
65 $this->id= 'l'.md5(microtime().$tmp['sec']);
67 // Set reorderable flag
68 $this->reorderable= $reorderable;
69 if (!$reorderable) {
70 $this->sortData();
71 }
72 }
75 public function setDefaultSortColumn($id)
76 {
77 $this->sortColumn = $id;
78 }
80 public function setListData($data, $displayData= null)
81 {
82 // Save data to display
83 $this->setData($data);
84 if (!$displayData) {
85 $displayData= array();
86 foreach ($data as $key => $value) {
87 $displayData[$key]= array("data" => array($value));
88 }
89 }
90 $this->setDisplayData($displayData);
91 }
94 private function setData($data)
95 {
96 $this->data= $data;
97 }
100 private function setDisplayData($data)
101 {
102 if (!is_array($data)) {
103 trigger_error ("sortableList needs an array as data!");
104 }
106 // Transfer information
107 $this->displayData= array();
108 $this->modes= array();
109 $this->mapping= array();
110 foreach ($data as $key => $value) {
111 $this->displayData[]= $value['data'];
112 if (isset($value['mode'])) {
113 $this->modes[]= $value['mode'];
114 }
115 }
116 $this->keys= array_keys($data);
118 // Create initial mapping
119 if(count($this->keys)){
120 $this->mapping= range(0, abs(count($this->keys)-1));
121 }
122 $this->current_mapping= $this->mapping;
124 // Find the number of coluns
125 reset($this->displayData);
126 $first= current($this->displayData);
127 if (is_array($first)) {
128 $this->columns= count($first);
129 } else {
130 $this->columns= 1;
131 }
133 // Preset sort orders to 'down'
134 for ($column= 0; $column<$this->columns; $column++) {
135 if(!isset($this->sortDirection[$column])){
136 $this->sortDirection[$column]= true;
137 }
138 }
139 }
142 public function setWidth($width)
143 {
144 $this->width= $width;
145 }
148 public function setInstantDelete($flag)
149 {
150 $this->instantDelete= $flag;
151 }
154 public function setColorAlternate($flag)
155 {
156 $this->colorAlternate= $flag;
157 }
160 public function setEditable($flag)
161 {
162 $this->editable= $flag;
163 }
166 public function setDeleteable($flag)
167 {
168 $this->deleteable= $flag;
169 }
172 public function setHeight($height)
173 {
174 $this->height= $height;
175 }
178 public function setCssClass($css)
179 {
180 $this->cssclass= $css;
181 }
184 public function setHeader($header)
185 {
186 $this->header= $header;
187 }
190 public function setColspecs($specs)
191 {
192 $this->colspecs= $specs;
193 }
196 public function render()
197 {
198 $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='min-width:".$this->width.";height: ".$this->height."'>\n";
199 $result.= "<table summary='"._("Sortable list")."' border='0' cellpadding='0' cellspacing='0' width='100%' style='width:100%' ".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
200 $action_width= 0;
201 if (strpos($this->acl, 'w') === false) {
202 $edit_image= $this->editable?image("images/lists/edit-grey.png"):"";
203 } else {
204 $edit_image= $this->editable?image('images/lists/edit.png', "%ID", _("Edit this entry")):"";
205 }
206 if (strpos($this->acl, 'd') === false) {
207 $delete_image= $this->deleteable?image('images/lists/trash-grey.png'):"";
208 } else {
209 $delete_image= $this->deleteable?image('images/lists/trash.png', "%ID", _("Delete this entry")):"";
210 }
212 // Do we need colspecs?
213 $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
214 if ($this->colspecs) {
215 $result.= " <colgroup>\n";
216 for ($i= 0; $i<$this->columns; $i++) {
217 if(isset($this->colspecs[$i]) && $this->colspecs[$i] != '*'){
218 $result.= " <col style='width:".($this->colspecs[$i])."'>\n";
219 }else{
220 $result.= " <col>\n";
221 }
222 }
224 // Extend by another column if we've actions specified
225 if ($action_width) {
226 $result.= " <col style='width:".$action_width."px' >\n";
227 }
228 $result.= " </colgroup>\n";
229 }
231 // Do we need a header?
232 if ($this->header) {
233 $result.= " <thead>\n <tr>\n";
234 $first= " style='border-left:0'";
235 for ($i= 0; $i<$this->columns; $i++) {
236 $link= "href='?plug=".$_GET['plug']."&PID=".$this->id."&act=SORT_$i'";
237 $sorter= "";
238 if ($i == $this->sortColumn){
239 $sorter= " ".image("images/lists/sort-".($this->sortDirection[$i]?"up":"down").".png", null, $this->sortDirection[$i]?_("Up"):_("Down"));
240 }
242 if ($this->reorderable) {
243 $result.= " <th$first>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
244 } else {
245 $result.= " <th$first><a $link>".(isset($this->header[$i])?$this->header[$i]:"")."</a>$sorter</th>";
246 }
247 $first= "";
248 }
249 if ($action_width) {
250 $result.= "<th> </th>";
251 }
252 $result.= "\n </tr>\n </thead>\n";
253 }
255 // Render table body if we've read permission
256 $result.= " <tbody id='".$this->id."'>\n";
257 $reorderable= $this->reorderable?"":" style='cursor:default'";
258 if (strpos($this->acl, 'r') !== false) {
259 foreach ($this->mapping as $nr => $row) {
260 $editable= $this->editable?" onClick='$(\"edit_".$this->id."_$nr\").click()'":"";
262 $id= "";
263 if (isset($this->modes[$row])) {
264 switch ($this->modes[$row]) {
265 case LIST_DISABLED:
266 $id= " sortableListItemDisabled";
267 $editable= "";
268 break;
269 case LIST_MARKED:
270 $id= " sortableListItemMarked";
271 break;
272 }
273 }
275 $result.= " <tr class='sortableListItem".((($nr&1)||!$this->colorAlternate)?'':'Odd')."$id' id='item_".$this->id."_$nr'$reorderable>\n";
276 $first= " style='border:0'";
278 foreach ($this->displayData[$row] as $column) {
279 $result.= " <td$editable$first>".$column."</td>\n";
280 $first= "";
281 }
283 if ($action_width) {
284 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
285 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
286 }
288 $result.= " </tr>\n";
289 }
290 }
292 // Add spacer
293 $result.= " <tr class='sortableListItemFill' style='height:100%'><td style='border:0'></td>";
294 $num= $action_width?$this->columns:$this->columns-1;
295 for ($i= 0; $i<$num; $i++) {
296 $result.= "<td class='sortableListItemFill'></td>";
297 }
298 $result.= "</tr>\n";
300 $result.= " </tbody>\n</table>\n</div>\n";
301 # $result.= " <input type='hidden' name='PID' value='".$this->id."' id='PID'>\n";
302 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
303 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
305 // Append script stuff if needed
306 $result.= '<script type="text/javascript" language="javascript">';
307 if ($this->reorderable) {
308 $result.= ' function updateOrder(){';
309 $result.= ' var ampcharcode= \'%26\';';
310 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
311 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
312 $result.= ' document.mainform.submit();';
313 $result.= ' }';
314 $result.= 'Position.includeScrollOffsets = true;';
315 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
316 }
317 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$this->scrollPosition.';';
318 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
319 $result.= '</script>';
321 return $result;
322 }
325 public function update()
326 {
328 // Filter GET with "act" attributes
329 if (!$this->reorderable){
330 if(isset($_GET['act']) && isset($_GET['PID']) && $this->id == $_GET['PID']) {
332 $key= validate($_GET['act']);
333 if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) {
335 // Switch to new column or invert search order?
336 $column= $match[1];
337 if ($this->sortColumn != $column) {
338 $this->sortColumn= $column;
339 } else {
340 $this->sortDirection[$column]= !$this->sortDirection[$column];
341 }
343 }
344 }
346 // Update mapping according to sort parameters
347 $this->sortData();
348 }
349 }
352 public function save_object()
353 {
354 // Do not do anything if this is not our PID, or there's even no PID available...
355 if(isset($_REQUEST['PID']) && $_REQUEST['PID'] != $this->id) {
356 return;
357 }
359 // Do not do anything if we're not posted - or have no permission
360 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
362 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
363 $this->scrollPosition= $_POST['position_'.$this->id];
364 }
366 // Move requested?
367 $move= $_POST['reorder_'.$this->id];
368 if ($move != "") {
369 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
370 $this->action= "reorder";
371 $tmp= array();
372 foreach ($matches[1] as $id => $row) {
373 $tmp[$id]= $this->mapping[$row];
374 }
375 $this->mapping= $tmp;
376 $this->current_mapping= $matches[1];
377 $this->modified= true;
378 return;
379 }
380 }
382 // Delete requested?
383 $this->action = "";
384 if (strpos($this->acl, 'd') !== false){
385 foreach ($_POST as $key => $value) {
386 if (preg_match('/^del_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
387 $this->active_index= $this->mapping[$matches[1]];
389 // Ignore request if mode requests it
390 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
391 $this->active_index= null;
392 continue;
393 }
395 // Set action
396 $this->action= "delete";
398 // Remove value if requested
399 if ($this->instantDelete) {
400 $this->deleteEntry($this->active_index);
401 }
402 }
403 }
404 }
406 // Edit requested?
407 if (strpos($this->acl, 'w') !== false){
408 foreach ($_POST as $key => $value) {
409 if (preg_match('/^edit_'.$this->id.'_([0-9]+)$/', $key, $matches)) {
410 $this->active_index= $this->mapping[$matches[1]];
412 // Ignore request if mode requests it
413 if (isset($this->modes[$this->active_index]) && $this->modes[$this->active_index] == LIST_DISABLED) {
414 $this->active_index= null;
415 continue;
416 }
418 $this->action= "edit";
419 }
420 }
421 }
422 }
425 public function getAction()
426 {
427 // Do not do anything if we're not posted
428 if(!isset($_POST['reorder_'.$this->id])) {
429 return;
430 }
432 // For reordering, return current mapping
433 if ($this->action == 'reorder') {
434 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
435 }
437 // Edit and delete
438 $result= array("targets" => array($this->active_index), "action" => $this->action);
440 return $result;
441 }
444 private function deleteEntry($id)
445 {
446 // Remove mapping
447 $index= array_search($id, $this->mapping);
448 if ($index !== false) {
449 unset($this->mapping[$index]);
450 $this->mapping= array_values($this->mapping);
451 $this->modified= true;
452 }
453 }
456 public function getMaintainedData()
457 {
458 $tmp= array();
459 foreach ($this->mapping as $src => $dst) {
460 $realKey = $this->keys[$dst];
461 $tmp[$realKey] = $this->data[$realKey];
462 }
463 return $tmp;
464 }
467 public function isModified()
468 {
469 return $this->modified;
470 }
473 public function setAcl($acl)
474 {
475 $this->acl= $acl;
476 }
479 public function sortData()
480 {
481 if(!count($this->data)) return;
483 // Extract data
484 $tmp= array();
485 foreach($this->displayData as $item) {
486 if (isset($item[$this->sortColumn])){
487 $tmp[]= $item[$this->sortColumn];
488 } else {
489 $tmp[]= "";
490 }
491 }
493 // Sort entries
494 if ($this->sortDirection[$this->sortColumn]) {
495 asort($tmp);
496 } else {
497 arsort($tmp);
498 }
500 // Adapt mapping accordingly
501 $this->mapping= array();
502 foreach ($tmp as $key => $value) {
503 $this->mapping[]= $key;
504 }
505 }
508 public function addEntry($entry, $displayEntry= null, $key= null)
509 {
510 // Only add if not already there
511 if (!$key) {
512 if (in_array($entry, $this->data)) {
513 return;
514 }
515 } else {
516 if (isset($this->data[$key])) {
517 return;
518 }
519 }
521 // Prefill with default value if not specified
522 if (!$displayEntry) {
523 $displayEntry= array('data' => array($entry));
524 }
526 // Append to data and mapping
527 if ($key) {
528 $this->data[$key]= $entry;
529 $this->keys[]= $key;
530 } else {
531 $this->data[]= $entry;
532 $this->keys[]= count($this->mapping);
533 }
534 $this->displayData[]= $displayEntry['data'];
535 $this->mapping[]= count($this->mapping);
536 $this->modified= true;
538 // Find the number of coluns
539 reset($this->displayData);
540 $first= current($this->displayData);
541 if (is_array($first)) {
542 $this->columns= count($first);
543 } else {
544 $this->columns= 1;
545 }
547 // Preset sort orders to 'down'
548 for ($column= 0; $column<$this->columns; $column++) {
549 if(!isset($this->sortDirection[$column])){
550 $this->sortDirection[$column]= true;
551 }
552 }
555 // Sort data after we've added stuff
556 $this->sortData();
557 }
560 public function getKey($index) {
561 return isset($this->keys[$index])?$this->keys[$index]:null;
562 }
564 public function getData($index) {
565 $realkey = $this->keys[$index];
566 return($this->data[$realkey]);
567 }
568 }