setData($data); // Generate instance wide unique ID $tmp= gettimeofday(); $this->id= 'l'.md5($tmp['sec']); } function setData($data) { if (!is_array($data)) { die ("sortableList needs an array as data!"); } // Transfer information $this->data= $data; // Create initial mapping $this->mapping= array_keys($data); // Find the number of coluns reset($this->data); $first= current($this->data); if (is_array($first)) { $this->columns= count($first); } else { $this->columns= 1; } } function setWidth($width) { $this->width= $width; } function setInstantDelete($flag) { $this->instantDelete= $flag; } function setEditable($flag) { $this->editable= $flag; } function setDeleteable($flag) { $this->deleteable= $flag; } function setHeight($height) { $this->height= $height; } function setCssClass($css) { $this->cssclass= $css; } function setHeader($header) { $this->header= $header; } function setColspecs($specs) { $this->colspecs= $specs; } function render() { $result= "
\n"; $result.= "cssclass)?" class='".$this->cssclass."'":"").">\n"; $action_width= 0; $edit_image= $this->editable?"":""; $delete_image= $this->deleteable?"":""; // Do we need colspecs? $action_width= ($this->editable?20:0) + ($this->deleteable?20:0); if ($this->colspecs) { $result.= " \n"; for ($i= 0; $i<$this->columns; $i++) { $result.= " \n"; } // Extend by another column if we've actions specified if ($action_width) { $result.= " \n"; } $result.= " \n"; } // Do we need a header? if ($this->header) { $result.= " \n \n"; for ($i= 0; $i<$this->columns; $i++) { $result.= " "; } if ($action_width) { $result.= ""; } $result.= "\n \n \n"; } // Render table body $result.= " \n"; foreach ($this->mapping as $nr => $row) { $result.= " \n"; foreach ($this->data[$row] as $column) { $result.= " \n"; } if ($action_width) { $result.= ""; } $result.= " \n"; } $result.= " \n
".(isset($this->header[$i])?$this->header[$i]:"")." 
".htmlentities($column)."".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image). str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."
\n
\n"; $result.= " \n"; // Append script stuff if needed $result.= ''; return $result; } function update() { // Do not do anything if we're not posted if (isset($_POST['reorder_'.$this->id])){ // Move requested? $move= $_POST['reorder_'.$this->id]; if ($move != "") { preg_match_all('/=([0-9]+)[&=]/', $move, $matches); $this->action= "reorder"; $tmp= array(); foreach ($matches[1] as $id => $row) { $tmp[$id]= $this->mapping[$row]; } $this->mapping= $tmp; $this->current_mapping= $matches[1]; return; } // Delete requested? foreach ($_POST as $key => $value) { if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) { $this->active_index= $this->mapping[$matches[1]]; $this->action= "delete"; if ($this->instantDelete) { $this->deleteEntry($this->active_index); } } } // Edit requested? foreach ($_POST as $key => $value) { if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) { $this->active_index= $this->mapping[$matches[1]]; $this->action= "edit"; } } } } function getAction() { // Do not do anything if we're not posted if(!isset($_POST['reorder_'.$this->id])) { return; } // For reordering, return current mapping if ($this->action == 'reorder') { return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action); } // Edit and delete $result= array("targets" => array($this->active_index), "action" => $this->action); return $result; } function deleteEntry($id) { $index= array_search($id, $this->mapping); if ($index !== false) { unset($this->mapping[$index]); $this->mappings= array_values($this->mappings); } } }