0e346f8ade48a520d03a9116d2218963bb11839b
1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id: class_listing.inc 15087 2010-01-06 13:45:49Z hickert $$
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 class sortableListing {
24 private $header= null;
25 private $colspecs= null;
26 private $reorderable= true;
27 private $width= "400px";
28 private $height= "100px";
29 private $cssclass= "";
30 private $id;
32 private $data= array();
33 private $displayData= array();
34 private $columns= 0;
35 private $deleteable= false;
36 private $editable= false;
37 private $instantDelete= true;
38 private $action;
39 private $targets;
40 private $mapping;
41 private $current_mapping;
42 private $active_index;
44 private $acl= "";
45 private $modified= false;
47 function sortableListing($data= array(), $displayData= null)
48 {
49 global $config;
51 // Save data to display
52 $this->setData($data);
53 if (!$displayData) {
54 $displayData= array();
55 foreach ($data as $value) {
56 $displayData[]= array($value);
57 }
58 } else {
59 $this->setDisplayData($displayData);
60 }
62 // Generate instance wide unique ID
63 $tmp= gettimeofday();
64 $this->id= 'l'.md5($tmp['sec']);
65 }
68 function setData($data)
69 {
70 $this->data= $data;
71 }
74 function setDisplayData($data)
75 {
76 if (!is_array($data)) {
77 die ("sortableList needs an array as data!");
78 }
80 // Transfer information
81 $this->displayData= $data;
83 // Create initial mapping
84 $this->mapping= array_keys($data);
85 $this->current_mapping= $this->mapping;
87 // Find the number of coluns
88 reset($this->displayData);
89 $first= current($this->displayData);
90 if (is_array($first)) {
91 $this->columns= count($first);
92 } else {
93 $this->columns= 1;
94 }
95 }
98 function setWidth($width)
99 {
100 $this->width= $width;
101 }
104 function setInstantDelete($flag)
105 {
106 $this->instantDelete= $flag;
107 }
110 function setEditable($flag)
111 {
112 $this->editable= $flag;
113 }
116 function setDeleteable($flag)
117 {
118 $this->deleteable= $flag;
119 }
122 function setHeight($height)
123 {
124 $this->height= $height;
125 }
128 function setCssClass($css)
129 {
130 $this->cssclass= $css;
131 }
134 function setHeader($header)
135 {
136 $this->header= $header;
137 }
140 function setColspecs($specs)
141 {
142 $this->colspecs= $specs;
143 }
146 function render()
147 {
148 $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='width: ".$this->width."; height: ".$this->height."'>\n";
149 $result.= "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%' style='position: relative;'".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
150 $action_width= 0;
151 if (strpos($this->acl, 'w') === false) {
152 $edit_image= $this->editable?"<img class='center' src='images/lists/edit-grey.png' alt='"._("Edit")."'>":"";
153 } else {
154 $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' title='"._("Edit this entry")."'>":"";
155 }
156 if (strpos($this->acl, 'd') === false) {
157 $delete_image= $this->deleteable?"<img class='center' src='images/lists/trash-grey.png' alt='"._("Delete")."'>":"";
158 } else {
159 $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
160 }
162 // Do we need colspecs?
163 $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
164 if ($this->colspecs) {
165 $result.= " <colgroup>\n";
166 for ($i= 0; $i<$this->columns; $i++) {
167 $result.= " <col width='".(isset($this->colspecs[$i])?$this->colspecs[$i]:"*")."'/>\n";
168 }
170 // Extend by another column if we've actions specified
171 if ($action_width) {
172 $result.= " <col width='$action_width'/>\n";
173 }
174 $result.= " </colgroup>\n";
175 }
177 // Do we need a header?
178 if ($this->header) {
179 $result.= " <thead>\n <tr>\n";
180 for ($i= 0; $i<$this->columns; $i++) {
181 $result.= " <th>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
182 }
183 if ($action_width) {
184 $result.= "<th> </th>";
185 }
186 $result.= "\n </tr>\n </thead>\n";
187 }
189 // Render table body if we've read permission
190 $result.= " <tbody id='".$this->id."'>\n";
191 if (strpos($this->acl, 'r') !== false) {
192 foreach ($this->mapping as $nr => $row) {
193 $result.= " <tr class='sortableListItem".($nr&1?'Odd':'')."' id='item_".$this->id."_$nr'>\n";
194 foreach ($this->displayData[$row] as $column) {
195 $result.= " <td>".htmlentities($column)."</td>\n";
196 }
197 if ($action_width) {
198 $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
199 str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
200 }
201 $result.= " </tr>\n";
202 }
203 } else {
204 $result.= " <tr class='sortableListItem' style='height:100%'></tr>\n";
205 }
207 $result.= " </tbody>\n</table>\n</div>\n";
208 $result.= " <input type='hidden' name='position_".$this->id."' id='position_".$this->id."'>\n";
209 $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
211 // Append script stuff if needed
212 $result.= '<script type="text/javascript" language="javascript">';
213 $result.= ' function updateOrder(){';
214 $result.= ' var ampcharcode= \'%26\';';
215 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
216 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
217 $result.= ' document.mainform.submit();';
218 $result.= ' }';
219 $result.= 'Position.includeScrollOffsets = true;';
220 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false, constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder});';
221 if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
222 $result.= '$("scroll_'.$this->id.'").scrollTop= '.$_POST['position_'.$this->id].';';
223 }
224 $result.= 'var box = $("scroll_'.$this->id.'").onscroll= function() {$("position_'.$this->id.'").value= this.scrollTop;}';
225 $result.= '</script>';
227 return $result;
228 }
231 function update()
232 {
233 // Do not do anything if we're not posted - or have no permission
234 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
236 // Move requested?
237 $move= $_POST['reorder_'.$this->id];
238 if ($move != "") {
239 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
240 $this->action= "reorder";
241 $tmp= array();
242 foreach ($matches[1] as $id => $row) {
243 $tmp[$id]= $this->mapping[$row];
244 }
245 $this->mapping= $tmp;
246 $this->current_mapping= $matches[1];
247 $this->modified= true;
248 return;
249 }
250 }
252 // Delete requested?
253 if (strpos($this->acl, 'd') !== false){
254 foreach ($_POST as $key => $value) {
255 if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
256 $this->active_index= $this->mapping[$matches[1]];
257 $this->action= "delete";
258 if ($this->instantDelete) {
259 $this->deleteEntry($this->active_index);
260 }
261 }
262 }
263 }
265 // Edit requested?
266 if (strpos($this->acl, 'w') !== false){
267 foreach ($_POST as $key => $value) {
268 if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
269 $this->active_index= $this->mapping[$matches[1]];
270 $this->action= "edit";
271 }
272 }
273 }
274 }
277 function getAction()
278 {
279 // Do not do anything if we're not posted
280 if(!isset($_POST['reorder_'.$this->id])) {
281 return;
282 }
284 // For reordering, return current mapping
285 if ($this->action == 'reorder') {
286 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
287 }
289 // Edit and delete
290 $result= array("targets" => array($this->active_index), "action" => $this->action);
292 return $result;
293 }
296 function deleteEntry($id)
297 {
298 // Remove mapping
299 $index= array_search($id, $this->mapping);
300 if ($index !== false) {
301 unset($this->mapping[$index]);
302 $this->mapping= array_values($this->mapping);
303 $this->modified= true;
304 }
305 }
308 function getMaintainedData()
309 {
310 $tmp= array();
312 foreach ($this->mapping as $src => $dst) {
313 $tmp[$src]= $this->data[$dst];
314 }
316 return $tmp;
317 }
320 function isModified()
321 {
322 return $this->modified;
323 }
326 function setAcl($acl)
327 {
328 $this->acl= $acl;
329 }
331 }