-
☆ A M B ☆
- 24,524 Posts
Got it! It returns an array of modResource objects! What a pain with no documentation!
<?php
$object = $modx->getObject('modResourceGroup',3);
$pages = $object->getResources();
$output = '';
foreach($pages as $value) {
$output .= $value->pagetitle . "<br>";
}
return $output;
It's an array of resource objects, so you have to do something like:
foreach($pages as $page) {
echo '<br />' . $page->get('pagetitle);
}
-
☆ A M B ☆
- 24,524 Posts
So here's my crude little utility for finding resources that aren't in a group, and making it easy to edit them so they can be added to the group.
// get an array of all resources - I suspect there already is one in memory somewhere
$c = $modx->newQuery('modResource');
$resources = $modx->getCollection('modResource');
$allresources = array();
foreach($resources as $resource) {
$allresources[] = $resource->id;
}
// get an array of all resources in a given group
$group = $modx->getOption('group', $scriptProperties, 'defaultGroup');
$object = $modx->getObject('modResourceGroup',array('name'=>$group));
$pages = $object->getResources();
$testresources = array();
foreach($pages as $value) {
$testresources[] = $value->id;
}
// get an array of the resources not in the group
$notingroup = array_diff($allresources,$testresources);
// make links for editing them
foreach($notingroup as $key=>$value) {
$output .= '#' . $value . ' - <a href="http://localhost/revo228/manager/?a=30&id=' . $value . '">Edit</a><br>';
}
return $output;
A little inefficient since you're getting the resources in the group twice, but if it works.
Did you try my snippet above that gets them via the modTemplateVarResource objects?
-
☆ A M B ☆
- 24,524 Posts
I have a question. Does $modx->getTree() go to the database, or work from an existing array in memory?
-
☆ A M B ☆
- 24,524 Posts
Quote from: BobRay at Jul 31, 2013, 06:25 PMA little inefficient since you're getting the resources in the group twice, but if it works. 
Did you try my snippet above that gets them via the modTemplateVarResource objects?
Hm. Has to be $modx->newQuery, not $xpdo->newQuery, and it's inGroup, not ingroup. That takes care of the errors, but it shows 0 all the time for !inGroup. How do you display the actual query that is being used?
Just tested this and it's working perfectly for me (and very fast):
Be sure to send the ID of the resource group, not the name, or the ID of a user group (and call the snippet uncached).
<?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) {
/** @var $rgr modResourceGroupResource */
$doc = $rgr->getOne('Resource');
echo "<br />" . $doc->get('pagetitle');
/* Do something with $doc object */
}
echo '<br /><br />Did something with ' . $count . ' Resources';
Quote from: sottwell at Jul 31, 2013, 04:33 PM
Not sure how to use the migx snippet; what should the tpl look like?
[[migxLoopCollection?
&classname = `modResource`
&joins=`[{"alias":"ResourceGroupResources"}]`
&where=`{"ResourceGroupResources.document_group":"25"}`
&tpl=`myTpl`
]]
I think
<li>[[+pagetitle]]([[+id]])</li>
should work
[ed. note: Bruno17 last edited this post 13 years, 2 months ago.]
-
☆ A M B ☆
- 24,524 Posts
Quote from: BobRay at Jul 31, 2013, 10:39 PMJust tested this and it's working perfectly for me (and very fast):
Be sure to send the ID of the resource group, not the name, or the ID of a user group (and call the snippet uncached).
Sorry, still comes up with 0 for &inGroup=`0` (or not using the property at all). I have four resources, one in the group, three not. This shows 1 just fine if I use &inGroup=`1`
-
☆ A M B ☆
- 24,524 Posts
<ul>
[[migxLoopCollection?
&classname = `modResource`
&joins=`[{"alias":"ResourceGroupResources"}]`
&where=`{"ResourceGroupResources.document_group":"3"`
&tpl=`@CODE:<li>[[+pagetitle]]([[+id]])</li>`
]]
</ul>
Just lists all resources, whether in the group or not.