9ea6d40eebcdd508b8c146c382221db2527daa32
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='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
210 // Append script stuff if needed
211 $result.= '<script type="text/javascript" language="javascript">';
212 $result.= ' function updateOrder(){';
213 $result.= ' var ampcharcode= \'%26\';';
214 $result.= ' var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
215 $result.= ' $("reorder_'.$this->id.'").value= serializeOpts;';
216 $result.= ' document.mainform.submit();';
217 $result.= ' }';
218 $result.= 'Position.includeScrollOffsets = true;';
219 $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false,constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder})';
220 $result.= '</script>';
222 return $result;
223 }
226 function update()
227 {
228 // Do not do anything if we're not posted - or have no permission
229 if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
231 // Move requested?
232 $move= $_POST['reorder_'.$this->id];
233 if ($move != "") {
234 preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
235 $this->action= "reorder";
236 $tmp= array();
237 foreach ($matches[1] as $id => $row) {
238 $tmp[$id]= $this->mapping[$row];
239 }
240 $this->mapping= $tmp;
241 $this->current_mapping= $matches[1];
242 $this->modified= true;
243 return;
244 }
245 }
247 // Delete requested?
248 if (strpos($this->acl, 'd') !== false){
249 foreach ($_POST as $key => $value) {
250 if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
251 $this->active_index= $this->mapping[$matches[1]];
252 $this->action= "delete";
253 if ($this->instantDelete) {
254 $this->deleteEntry($this->active_index);
255 }
256 }
257 }
258 }
260 // Edit requested?
261 if (strpos($this->acl, 'w') !== false){
262 foreach ($_POST as $key => $value) {
263 if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
264 $this->active_index= $this->mapping[$matches[1]];
265 $this->action= "edit";
266 }
267 }
268 }
269 }
272 function getAction()
273 {
274 // Do not do anything if we're not posted
275 if(!isset($_POST['reorder_'.$this->id])) {
276 return;
277 }
279 // For reordering, return current mapping
280 if ($this->action == 'reorder') {
281 return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
282 }
284 // Edit and delete
285 $result= array("targets" => array($this->active_index), "action" => $this->action);
287 return $result;
288 }
291 function deleteEntry($id)
292 {
293 // Remove mapping
294 $index= array_search($id, $this->mapping);
295 if ($index !== false) {
296 unset($this->mapping[$index]);
297 $this->mapping= array_values($this->mapping);
298 $this->modified= true;
299 }
300 }
303 function getMaintainedData()
304 {
305 $tmp= array();
307 foreach ($this->mapping as $src => $dst) {
308 $tmp[$src]= $this->data[$dst];
309 }
311 return $tmp;
312 }
315 function isModified()
316 {
317 return $this->modified;
318 }
321 function setAcl($acl)
322 {
323 $this->acl= $acl;
324 }
326 }