We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38970
    • 59 Posts
    Hey Bob,

    I think this will work nicely however when I tried to use the code it says : Fatal error: Call to undefined method modX::getChildren()
      • 3749
      • 24,544 Posts
      Sorry, that should be $modx->getChildIds($tr).

      I've corrected the code above.


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38970
        • 59 Posts
        That worked perfectly Bob, I have attached the final code I used here:

        <?php
        /* allows access only to the tree_root_id resource and its children */
        $tr = $modx->getOption('tree_root_id');
        if(is_numeric(strrpos($tr,','))){
           $tr = explode(',',$tr);
        }else{
            $tr = array($tr);
        }
        $allowed = array();
        foreach($tr as $t){
            $children = $modx->getChildIds($tr);
            $allowed = array_merge($allowed,$children);
        }
        /* add the tree_root_id resource the array -- remove if they shouldn't access the parent */
        $allowed = array_merge($allowed,$tr);
        
        if ( ! in_array($modx->resource->get('id'), $allowed)) {
        
           $modx->sendRedirect('http://www.domain.com/manager/index.php?a=30&id='.$allowed[0]
                                );
        }
        

        I made a modification so that tree_root_id could be comma separated 6,4 it will then explode on the , and then allow all children to be allowed based on that.

        Thanks again for all the help Bob.
          • 38970
          • 59 Posts
          A quick note to people on line 19 the forums are rewriting the url that we are passing just remove the a href's out of the send redirect.
            • 38970
            • 59 Posts
            I am working on something similar now, how would you get about setting a user setting would it just be setOption ?
              • 3749
              • 24,544 Posts
              Set option will work if you only want to change it for the duration of the current request. It will revert when a new page is loaded.

              You can change it permanently, but it's a little more complicated.


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 38970
                • 59 Posts
                Hey Bob,

                Yes it would need to be permantly added to the user setting this is what I have come up with from other forum threads. Does this look right ?

                   $uid = $modx->user->get('id');
                     $properties = array(
                        'fk' => $uid, // user id
                        'key' => 'tree_root_id',
                        'name' => 'treerootid',
                        'description' => 'Local A User To This ID',
                        'xtype' => 'textfield',
                        'namespace' => 'accountinfo',
                        'value' => $doc->get('id'),
                    );
                    $response = $modx->runProcessor('system/setting/create', $properties);
                    if ($response->isError()) {
                        return $response->getMessage();
                    }
                    $setting = $response->getObject();
                     
                    // refresh cache
                    $modx->cacheManager->refresh(array(
                        'system_settings' => array(),
                    ));
                
                  • 3749
                  • 24,544 Posts
                  It looks about right to me. You shouldn't need line 15 unless you're going to get information from the setting (or for debugging). Line 9 may not work if it's a newly created resource.


                  ------------------------------------------------------------------------------------------
                  PLEASE, PLEASE specify the version of MODX you are using.
                  MODX info for everyone: http://bobsguides.com/modx.html
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 38970
                    • 59 Posts
                    Line 11 Response is not returning anything it seems here is the properties array
                    Array
                    (
                        [fk] => 1
                        [key] => tree_root_id
                        [name] => treerootid
                        [description] => Local A User To This ID
                        [xtype] => textfield
                        [namespace] => accountinfo
                        [value] => 13699
                    )
                    
                      • 3749
                      • 24,544 Posts
                      Are you sure the setting doesn't exist? The create processor will balk if the setting exists.

                      I should have looked more closely at your code.

                      AFAIK, a System Setting has no 'fk' field or 'description' field (though I haven't looked at the processor, which might use those) and all of the keys and values should be in single quotes.

                      Also, you're creating a System Setting, not a User Setting.


                      Here is how I usually do it:

                      <?php
                      /* @var $setting modUserSetting */
                      /* @var $modx modX */
                      
                      $userId = $modx->user->get('id');  // or $user->get('id') if it's in a plugin
                      
                      $setting = $modx->getObject('modUserSetting', array('key' => 'tree_root_id', 'user => $userId));
                      
                      if (empty ($setting)) {
                          $setting = $modx->newObject('modUserSetting');
                          $setting->fromArray(array(
                             'user' => $userId;
                             'key' => 'tree_root_id',
                             'value' => '13699',
                             'xtype' => 'textfield',
                             'namespace' => 'accountinfo',
                             'area' => 'accountinfo',
                            ), '', true, true);
                      
                          $setting->save();
                      } else {
                          $setting->set('value', '13699' );
                          $setting->save();
                      }




                      ------------------------------------------------------------------------------------------
                      PLEASE, PLEASE specify the version of MODX you are using.
                      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 1 month ago.]
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting