getselections.snippet.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * getSelections
  4. *
  5. * DESCRIPTION
  6. *
  7. * This snippet is a helper for getResources call.
  8. * It will allows you to select all linked resources under specific Selection with a usage of getResources snippet.
  9. * Returns distinct list of linked Resources for given Selections
  10. *
  11. * getResources are required
  12. *
  13. * PROPERTIES:
  14. *
  15. * &sortdir string optional Direction of sorting by linked resource's menuindex. Default: ASC
  16. * &selections string optional Comma separated list of Selection IDs for which should be retrieved linked resources. Default: empty string
  17. * &getResourcesSnippet string optional Name of getResources snippet. Default: getResources
  18. *
  19. * USAGE:
  20. *
  21. * [[getSelections? &selections=`1` &tpl=`rowTpl`]]
  22. * [[getSelections? &selections=`1` &tpl=`rowTpl` &sortby=`RAND()`]]
  23. *
  24. */
  25. $collections = $modx->getService('collections','Collections',$modx->getOption('collections.core_path',null,$modx->getOption('core_path').'components/collections/').'model/collections/',$scriptProperties);
  26. if (!($collections instanceof Collections)) return '';
  27. $getResourcesSnippet = $modx->getOption('getResourcesSnippet', $scriptProperties, 'getResources');
  28. $getResourcesExists = $modx->getCount('modSnippet', array('name' => $getResourcesSnippet));
  29. if ($getResourcesExists == 0) return 'getResources not found';
  30. $sortDir = strtolower($modx->getOption('sortdir', $scriptProperties, 'asc'));
  31. $selections = $modx->getOption('selections', $scriptProperties, '');
  32. $sortBy = $modx->getOption('sortby', $scriptProperties, '');
  33. $excludeToPlaceholder = $modx->getOption('excludeToPlaceholder', $scriptProperties, '');
  34. $selections = $modx->collections->explodeAndClean($selections);
  35. if ($sortDir != 'asc') {
  36. $sortDir = 'desc';
  37. }
  38. $linkedResourcesQuery = $modx->newQuery('CollectionSelection');
  39. if (!empty($selections)) {
  40. $linkedResourcesQuery->where(array(
  41. 'collection:IN' => $selections
  42. ));
  43. }
  44. if ($sortBy == '') {
  45. $linkedResourcesQuery->sortby('menuindex', $sortDir);
  46. }
  47. $linkedResourcesQuery->select(array(
  48. 'resource' => 'DISTINCT(resource)',
  49. 'menuindex' => 'menuindex'
  50. ));
  51. $linkedResourcesQuery->prepare();
  52. $linkedResourcesQuery->stmt->execute();
  53. $linkedResources = $linkedResourcesQuery->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
  54. if (!empty($excludeToPlaceholder)) {
  55. $excludeResources = array();
  56. foreach($linkedResources as $res) {
  57. $excludeResources[] = '-' . $res;
  58. }
  59. $excludeResources = implode(',', $excludeResources);
  60. $modx->setPlaceholder($excludeToPlaceholder, $excludeResources);
  61. }
  62. $linkedResources = implode(',', $linkedResources);
  63. $properties = $scriptProperties;
  64. unset($properties['selections']);
  65. $properties['resources'] = $linkedResources;
  66. $properties['parents'] = ($properties['getResourcesSnippet'] == 'pdoResources') ? 0 : -1;
  67. if ($sortBy == '') {
  68. $properties['sortby'] = 'FIELD(modResource.id, ' . $linkedResources . ' )';
  69. $properties['sortdir'] = 'asc';
  70. }
  71. return $modx->runSnippet($getResourcesSnippet, $properties);