rank1 rank2 rank3 rank4 rank5
$tvid = 8; //the ID of the TV thats the rank
$eventName = $modx->event->name;
$thisid = $resource->get('id');
//--------------------
if(!function_exists('getranks')){
function getranks($tvid){
global $modx;
$c = $modx->newQuery('modTemplateVarResource');
$c->sortby('value', 'ASC');
$c->where(array('tmplvarid' => $tvid));
$tvs = $modx->getCollection('modTemplateVarResource', $c);
usort($tvs, 'sortByValue');
return $tvs;
}
}
if(!function_exists('sortByValue')){
function sortByValue($a, $b){
return (int)$a->get('value') - (int)$b->get('value');
}
}
//--------------------------------------
if(!function_exists('getrank')){
function getrank($contentid){
global $modx;
$c = $modx->newQuery('modTemplateVarResource');
$c->where(array('contentid' => $contentid));
$tv = $modx->getObject('modTemplateVarResource', $c);
return $tv;
}
}
//-----------------------------------
if(!function_exists('reorder')){
//move one indexed item to new index
function reorder(&$a, $old, $new = null, $thisid = null){
if($old < $new){
foreach ($a as $tv) {
$v = $tv->get('value');
if($v > $old && $v <= $new){
$v--;
}
$tv->set('value', $v);
$tv->save();
}
} elseif($old > $new) {
foreach ($a as $tv) {
$v = $tv->get('value');
if($v < $old && $v >= $new){
$v++;
}
$tv->set('value', $v);
$tv->save();
}
} else {
foreach ($a as $tv) {
$v = $tv->get('value');
if($v > $old){
$v--;
}
$tv->set('value', $v);
$tv->save();
}
}
}
}
//--------------------
if(!function_exists('removeElementWithValue')){
function removeElementWithValue($array, $key, $value){
foreach ($array as $subKey => $subArray) {
if($subArray[$key] == $value){
unset($array[$subKey]);
}
}
return $array;
}
}
//----------------------------------------
if (!function_exists('reindex')) {
/* orderby value, renumber indices to 0 till max, re-sequence indices */
function reindex($tvs)
{
$order = array();
foreach ($tvs as $tv) {
$order[(int)$tv->get('value')] = $tv;
}
ksort($order);
$order = array_values($order);
foreach ($order as $key => &$tv) {
$tv->set('value', $key + 1);
$tv->save();
}
return $order;
}
}
switch ($eventName) {
case 'OnBeforeDocFormSave':
$field = $_POST['tv' . $tvid];
$value = $resource->getTVValue($tvid);
if(!empty($field) && empty($value)){
$modx->rankaction = 'new';
}
if(empty($field) && !empty($value)){
$modx->rankaction = 'remove';
}
if(!empty($field) && !empty($value)){
$modx->rankaction = 'update';
}
if($field != $value){
$modx->rankold = $value;
// get all tvvalues, order by value and revalue them sequentionaly 1 to n
$tvs = getranks($tvid);
$i = 0;
foreach ($tvs as $tv) {
$i++;
$tv->set('value', $i);
$tv->save();
}
}
break;
case 'OnDocFormSave':
if($modx->rankaction){
$new = $resource->getTVValue($tvid);
$tvs = getranks($tvid);
switch ($modx->rankaction) {
case 'new':
$amt = count($tvs);
$new = ($amt < $new) ? $amt : $new;
$old = ($modx->rankold) ? $old = $modx->rankold : count($tvs);
reorder($tvs, $old, $new, $thisid);
$resource->setTVValue($tvid, $new);
break;
case 'update':
$amt = count($tvs);
$new = ($amt < $new) ? $amt : $new;
$old = ($modx->rankold) ? $old = $modx->rankold : count($tvs);
reorder($tvs, $old, $new, $thisid);
$resource->setTVValue($tvid, $new);
break;
case 'remove':
reindex($tvs);
break;
}
unset($modx->rankold);
unset($modx->rankaction);
}
break;
}
return '';