We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    While I can hack around to get a list of resources in or not in a specified group, what would be the "proper" xPDO way to do this?

    This question has been answered by BobRay. See the first response.

      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 42602
      • 81 Posts
      By group you mean resource group?

      Hmmm... that was blunt X)
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Um... what other groups can resources be in?
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 42602
          • 81 Posts
          Not many, sorry for earlier. Sometimes just 'group' gets quite broad meanings, my bad.

          Simple snippet to fetch resource in or not in group with specified name:

          // Get by group name
          $group = $modx->getOption('group', $scriptProperties, 'defaultGroup');
          $inOrnot = $modx->getOption('inOrNot', $scriptProperties, true); // &inOrNot=`true` or `&inOrNot=`false`
          
          $c = $modx->newQuery('modResource');
          $c->innerJoin('modResourceGroupResource', 'modResourceGroupResource', 'modResourceGroupResource.document = modResource.id');
          $c->innerJoin('modResourceGroup', 'modResourceGroup', 'modResourceGroup.id = modResourceGroupResource.document_group');
          if (!$inOrNot) { // not in group
              $c->where(array('modResourceGroup.name:!=' => $group));
          } else {
              $c->where(array('modResourceGroup.name' => $group));
          }
          $collection = $modx->getCollection('modResource', $c);
          


          Example is missing the collection processing with loop.

          Usage:
          [[snippetName?group=`mygroup` &inOrNot=`true`]]
            • 42602
            • 81 Posts
            Actually bit more versatile version:

            $group = $modx->getOption('group', $scriptProperties, 'defaultGroup');
            $in = $modx->getOption('in', $scriptProperties, null);
            $notin = $modx->getOption('notin', $scriptProperties, null);
            
            $c = $modx->newQuery('modResource');
            $c->innerJoin('modResourceGroupResource', 'modResourceGroupResource', 'modResourceGroupResource.document = modResource.id');
            $c->innerJoin('modResourceGroup', 'modResourceGroup', 'modResourceGroup.id = modResourceGroupResource.document_group');
            
            if ($in !== null) {
                $c->where(array(
                    'modResourceGroup.name:IN' => explode(',', $in)
                ));
            }
            if ($notin !== null) {
                $c->where(array(
                    'modResourceGroup.name:NOT IN' => explode(',', $notin)
                ));
            }
            $collection = $modx->getCollection('modResource', $c);
            


            You can define the groups where the resources can lie with comma separated list and/or set comma separated list of groups where the resource is not in
              • 4172
              • 5,888 Posts
              with migxLoopCollection (not tested):

              [[migxLoopCollection?
              &classname = `modResource`
              &joins=`[{"alias":"ResourceGroupResources"}]`
              &where=`{"ResourceGroupResources.document_group":"25"}`
              &tpl=`myTpl`
              ]]
              [ed. note: Bruno17 last edited this post 13 years, 2 months ago.]
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Well, I was actually thinking more along the lines of using the getResources() function of the modResourceGroup object somewhere in there. It returns an array, so shouldn't I be able to get an array of resource IDs and compare it against the array returned by the getResources() function with a simple array_diff?
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Hm. Can't get dunnock's snippet to return anything.

                  Not sure how to use the migx snippet; what should the tpl look like?
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                    • 3749
                    • 24,544 Posts
                    Another way to go (&inGroup is 1 for resources in the group, 0 for resources outside the group):

                    [[!GroupResources? &groupId=`12` &inGroup=`0`]]



                    <?php
                    $groupId = $scriptProperties['groupId'];
                    $inGroup = $scriptProperties['inGroup'];
                    
                    $query = $modx->newQuery('modResourceGroupResource');
                    
                    if ($inGroup) {
                        $query->where(array('document_group' => $groupId));
                    } else {
                        $query->where(array('document_group:!=' => $groupId));
                    }
                    
                    $rgrs = $modx->getCollection('modResourceGroupResource', $query);
                    $count = count($rgrs);
                    
                    foreach ($rgrs as $rgr) {
                        $doc = $rgr->getOne('Resource');
                    
                        /* Do something with $doc object */
                    
                        echo '<br />' . $doc->get('pagetitle');
                    }
                    
                    return 'Did something with ' . $count . ' Resources';
                    
                    
                    [ed. note: BobRay last edited this post 13 years, 2 months 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
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      This is getting frustrating. Simply trying to get a list of resources in a group:
                      <?php
                      $object = $modx->getObject('modResourceGroup',array(
                             'name' => 'Testing' ));
                      $pages = $object->getResources();
                      echo $pages;
                      return;

                      This produces "Array", as expected. But any time I try to access the array in any way I just get a blank page, no errors, no nothing. huh
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org