We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 4172
    • 5,888 Posts
    I think, this is something, what could be usefull for you:

    https://github.com/Bruno17/migxResourceCategories
      -------------------------------

      you can buy me a beer, if you like MIGX

      http://webcmsolutions.de/migx.html

      Thanks!
      • 24629
      • 370 Posts
      Hi all,

      @Bruno, thanks but i'm not quite sure how this can help me?

      I , managed to create a plugin that onBeforeDocFormSave, loops through the right contentvalues (thanks Susan), re-seqeunce them all in case a user created a value thats out of range. So i always heva a clean sequence to start onDocFormSave.
      Then onDocFormSave everything is ordered, revalued and saved just fine except.. when a user fills in a value higher than the highest value.
      For example the existing values are
      rank1
      rank2
      rank3
      rank4
      rank5
      
      and the users changes rank3 to rank20 then, although my script works fine, in the end the value of 20 gets saved to the contentvalues table.
      How can i override the given value by the user, into a MAX value thats been created onFormDocSave?

      Tnx
      RDG
      • discuss.answer
        • 24629
        • 370 Posts
        Hi all,

        This for anyone in the future that might want to make a TV that behaves like menu-index. for ordering other menus for example or to create a top 10.
        This is my final plugin

        $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 '';
        


        cheers
        RDG