From: cajus Date: Tue, 19 Jan 2010 13:25:00 +0000 (+0000) Subject: Enabled sorting X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=fa919250554e64617662c2e07e569263a53a1ee3;p=gosa.git Enabled sorting git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@15201 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-core/include/class_sortableListing.inc b/gosa-core/include/class_sortableListing.inc index 1b2bc4c75..c81faeb5b 100644 --- a/gosa-core/include/class_sortableListing.inc +++ b/gosa-core/include/class_sortableListing.inc @@ -42,12 +42,12 @@ class sortableListing { private $active_index; private $scrollPosition= 0; private $sortColumn= 0; - private $sortColumnOrder= array(); + private $sortDirection= array(); private $acl= ""; private $modified= false; - function sortableListing($data= array(), $displayData= null) + public function sortableListing($data= array(), $displayData= null, $reorderable= false) { global $config; @@ -65,16 +65,22 @@ class sortableListing { // Generate instance wide unique ID $tmp= gettimeofday(); $this->id= 'l'.md5($tmp['sec']); + + // Set reorderable flag + $this->reorderable= $reorderable; + if (!$reorderable) { + $this->sortData(); + } } - function setData($data) + private function setData($data) { $this->data= $data; } - function setDisplayData($data) + private function setDisplayData($data) { if (!is_array($data)) { die ("sortableList needs an array as data!"); @@ -95,64 +101,63 @@ class sortableListing { } else { $this->columns= 1; } - } - - function setWidth($width) - { - $this->width= $width; + // Preset sort orders to 'down' + for ($column= 0; $column<$this->columns; $column++) { + $this->sortDirection[]= true; + } } - function setReorderable($flag) + public function setWidth($width) { - $this->reorderable= $flag; + $this->width= $width; } - function setInstantDelete($flag) + public function setInstantDelete($flag) { $this->instantDelete= $flag; } - function setEditable($flag) + public function setEditable($flag) { $this->editable= $flag; } - function setDeleteable($flag) + public function setDeleteable($flag) { $this->deleteable= $flag; } - function setHeight($height) + public function setHeight($height) { $this->height= $height; } - function setCssClass($css) + public function setCssClass($css) { $this->cssclass= $css; } - function setHeader($header) + public function setHeader($header) { $this->header= $header; } - function setColspecs($specs) + public function setColspecs($specs) { $this->colspecs= $specs; } - function render() + public function render() { $result= "
\n"; $result.= "cssclass)?" class='".$this->cssclass."'":"").">\n"; @@ -187,7 +192,17 @@ class sortableListing { if ($this->header) { $result.= " \n \n"; for ($i= 0; $i<$this->columns; $i++) { - $result.= " "; + $link= "href='?plug=".$_GET['plug']."&PID=".$this->id."&act=SORT_$i'"; + $sorter= ""; + if ($i == $this->sortColumn){ + $sorter= " "; + } + + if ($this->reorderable) { + $result.= " "; + } else { + $result.= " "; + } } if ($action_width) { $result.= ""; @@ -201,9 +216,9 @@ class sortableListing { if (strpos($this->acl, 'r') !== false) { foreach ($this->mapping as $nr => $row) { $editable= $this->editable?" onclick='$(\"edit_".$this->id."_$nr\").click()'":""; - $result.= " \n"; + $result.= " \n"; foreach ($this->displayData[$row] as $column) { - $result.= " \n"; + $result.= " ".htmlentities($column)."\n"; } if ($action_width) { $result.= "
".(isset($this->header[$i])?$this->header[$i]:"")."".(isset($this->header[$i])?$this->header[$i]:"")."".(isset($this->header[$i])?$this->header[$i]:"")."$sorter 
".htmlentities($column)."".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image). @@ -216,6 +231,7 @@ class sortableListing { } $result.= " \n
\n
\n"; + $result.= " \n"; $result.= " \n"; $result.= " \n"; @@ -239,8 +255,30 @@ class sortableListing { } - function update() + public function update() { + // Do not do anything if this is not our PID, or there's even no PID available... + if(!isset($_REQUEST['PID']) || $_REQUEST['PID'] != $this->id) { + return; + } + + // Filter GET with "act" attributes + if (!$this->reorderable && isset($_GET['act'])) { + $key= validate($_GET['act']); + if (preg_match('/^SORT_([0-9]+)$/', $key, $match)) { + // Switch to new column or invert search order? + $column= $match[1]; + if ($this->sortColumn != $column) { + $this->sortColumn= $column; + } else { + $this->sortDirection[$column]= !$this->sortDirection[$column]; + } + + // Update mapping according to sort parameters + $this->sortData(); + } + } + // Do not do anything if we're not posted - or have no permission if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){ @@ -289,7 +327,7 @@ class sortableListing { } - function getAction() + public function getAction() { // Do not do anything if we're not posted if(!isset($_POST['reorder_'.$this->id])) { @@ -308,7 +346,7 @@ class sortableListing { } - function deleteEntry($id) + private function deleteEntry($id) { // Remove mapping $index= array_search($id, $this->mapping); @@ -320,7 +358,7 @@ class sortableListing { } - function getMaintainedData() + public function getMaintainedData() { $tmp= array(); @@ -332,15 +370,42 @@ class sortableListing { } - function isModified() + public function isModified() { return $this->modified; } - function setAcl($acl) + public function setAcl($acl) { $this->acl= $acl; } + + public function sortData() + { + // Extract data + $tmp= array(); + foreach($this->displayData as $item) { + if (isset($item[$this->sortColumn])){ + $tmp[]= $item[$this->sortColumn]; + } else { + $tmp[]= ""; + } + } + + // Sort entries + if ($this->sortDirection[$this->sortColumn]) { + asort($tmp); + } else { + arsort($tmp); + } + + // Adapt mapping accordingly + $this->mapping= array(); + foreach ($tmp as $key => $value) { + $this->mapping[]= $key; + } + } + }