Quote from: michalc at Jun 21, 2008, 06:26 AM
I’m using this snippet in a Ditto call, so front end users can choose to edit a number of documents, all from one page. One thing I would like to do: be able for the user to manually change the order of the documents displayed.
I have found a solution: it may be a bit dodgy, but I’ll put it here in case anyone wants to do it. First add a function to FdmDocument class in fdm.db.class.inc.php
function setMenuIndex($newIndex) {
global $modx;
$this->Set('menuindex',$newIndex);
$this->Save();
}The add two actions in the class CFDM in fdm.class.inc.php "moveup" and "movedown". This means in the Run() function add near the top:
$this->link['moveup'] =str_replace('&','&',$modx->makeUrl($this->config['editorpageid'],'','&fdmaction=moveup'.'&fdmid='.$this->doc['id']));
$this->link['movedown'] =str_replace('&','&',$modx->makeUrl($this->config['editorpageid'],'','&fdmaction=movedown'.'&fdmid='.$this->doc['id']));
And in the switch ($this->config[’action’]) block add two cases:
case 'moveup':
if ($this->user['ismoderator'] || $this->user['isauthor']) {
$docObj = new FdmDocument($this->doc['id']);
// Find siblings ordered by menu index
$parentID = $modx->getParent($this->doc['id'],1,'id');
$parentID = $parentID['id'];
$siblings = $modx->getAllChildren($parentID,'menuindex','ASC','id,menuindex,deleted,hidemenu');$test = "";
// Ignore hidden or deleted siblings
for ($i = 0; $i < sizeof($siblings); $i += 1) {
if(($siblings[$i]['deleted'] == 1) || $siblings[$i]['hidemenu'] == 1) {
array_splice($siblings,$i,1);
$i = $i -1;
}
}
// Find element to swap index with (loops from second item in array)
for ($i = 1; $i < sizeof($siblings); $i += 1) {
if($siblings[$i]['id'] == $this->doc['id']) {
// Swap indices
$partnerDocObj = new FdmDocument($siblings[$i - 1]['id']);
$docObj->setMenuIndex($siblings[$i - 1]['menuindex']);
$partnerDocObj->setMenuIndex($siblings[$i]['menuindex']);
}
}
header('Location: '.$this->link['current']);
}
break;
case 'movedown':
if ($this->user['ismoderator'] || $this->user['isauthor']){
$docObj = new FdmDocument($this->doc['id']);
// Find siblings ordered by menu index
$parentID = $modx->getParent($this->doc['id'],1,'id');
$parentID = $parentID['id'];
$siblings = $modx->getAllChildren($parentID,'menuindex','ASC','id,menuindex,deleted,hidemenu');$test = "";
// Ignore hidden or deleted siblings
for ($i = 0; $i < sizeof($siblings); $i += 1) {
if(($siblings[$i]['deleted'] == 1) || $siblings[$i]['hidemenu'] == 1) {
array_splice($siblings,$i,1);
$i = $i -1;
}
}
// Find element to swap index with (loops until second last item)
for ($i = 0; $i < sizeof($siblings)-1; $i += 1) {
if($siblings[$i]['id'] == $this->doc['id']) {
// Swap indices
$partnerDocObj = new FdmDocument($siblings[$i + 1]['id']);
$docObj->setMenuIndex($siblings[$i + 1]['menuindex']);
$partnerDocObj->setMenuIndex($siblings[$i]['menuindex']);
}
}
header('Location: '.$this->link['current']);
}
break;
And then in your template you can use [+link.moveup:esc+] and [+link.movedown:esc+] for the links to perform the action.
There probably better ways of doing the to those more familiar with php and the modx api though.
Michal.