setData($data);
if (!$displayData) {
$displayData= array();
foreach ($data as $value) {
$displayData[]= array($value);
}
} else {
$this->setDisplayData($displayData);
}
// Generate instance wide unique ID
$tmp= gettimeofday();
$this->id= 'l'.md5($tmp['sec']);
}
function setData($data)
{
$this->data= $data;
}
function setDisplayData($data)
{
if (!is_array($data)) {
die ("sortableList needs an array as data!");
}
// Transfer information
$this->displayData= $data;
// Create initial mapping
$this->mapping= array_keys($data);
$this->current_mapping= $this->mapping;
// Find the number of coluns
reset($this->displayData);
$first= current($this->displayData);
if (is_array($first)) {
$this->columns= count($first);
} else {
$this->columns= 1;
}
}
function setWidth($width)
{
$this->width= $width;
}
function setReorderable($flag)
{
$this->reorderable= $flag;
}
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.= " \n";
$result.= " \n";
// Append script stuff if needed
$result.= '';
return $result;
}
function update()
{
// Do not do anything if we're not posted - or have no permission
if (strpos($this->acl, 'w') !== false && isset($_POST['reorder_'.$this->id])){
if (isset($_POST['position_'.$this->id]) && is_numeric($_POST['position_'.$this->id])) {
$this->scrollPosition= $_POST['position_'.$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];
$this->modified= true;
return;
}
}
// Delete requested?
if (strpos($this->acl, 'd') !== false){
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?
if (strpos($this->acl, 'w') !== false){
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)
{
// Remove mapping
$index= array_search($id, $this->mapping);
if ($index !== false) {
unset($this->mapping[$index]);
$this->mapping= array_values($this->mapping);
$this->modified= true;
}
}
function getMaintainedData()
{
$tmp= array();
foreach ($this->mapping as $src => $dst) {
$tmp[$src]= $this->data[$dst];
}
return $tmp;
}
function isModified()
{
return $this->modified;
}
function setAcl($acl)
{
$this->acl= $acl;
}
}