100.include.cache.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * TaggerGetResourcesWhere
  4. *
  5. * DESCRIPTION
  6. *
  7. * This snippet generate SQL Query that can be used in WHERE condition in getResources snippet
  8. *
  9. * PROPERTIES:
  10. *
  11. * &tags string optional Comma separated list of Tags for which will be generated a Resource query. By default Tags from GET param will be loaded
  12. * &groups string optional Comma separated list of Tagger Groups. Only from those groups will Tags be allowed
  13. * &where string optional Original getResources where property. If you used where property in your current getResources call, move it here
  14. * &likeComparison int optional If set to 1, tags will compare using LIKE
  15. * &tagField string optional Field that will be used to compare with given tags. Default: alias
  16. * &matchAll int optional If set to 1, resource must have all specified tags. Default: 0
  17. * &field string optional modResource field that will be used to compare with assigned resource ID
  18. *
  19. * USAGE:
  20. *
  21. * [[!getResources? &where=`[[!TaggerGetResourcesWhere? &tags=`Books,Vehicles` &where=`{"isfolder": 0}`]]`]]
  22. *
  23. */
  24. $tagger = $modx->getService('tagger','Tagger',$modx->getOption('tagger.core_path',null,$modx->getOption('core_path').'components/tagger/').'model/tagger/',$scriptProperties);
  25. if (!($tagger instanceof Tagger)) return '';
  26. $tags = $modx->getOption('tags', $scriptProperties, '');
  27. $where = $modx->getOption('where', $scriptProperties, '');
  28. $tagField = $modx->getOption('tagField', $scriptProperties, 'alias');
  29. $likeComparison = (int) $modx->getOption('likeComparison', $scriptProperties, 0);
  30. $matchAll = (int) $modx->getOption('matchAll', $scriptProperties, 0);
  31. $field = $modx->getOption('field', $scriptProperties, 'id');
  32. $where = $modx->fromJSON($where);
  33. if ($where == false) {
  34. $where = array();
  35. }
  36. $tagsCount = 0;
  37. if ($tags == '') {
  38. $gc = $modx->newQuery('TaggerGroup');
  39. $gc->select($modx->getSelectColumns('TaggerGroup', '', '', array('alias')));
  40. $groups = $modx->getOption('groups', $scriptProperties, '');
  41. $groups = $tagger->explodeAndClean($groups);
  42. if (!empty($groups)) {
  43. $gc->where(array(
  44. 'name:IN' => $groups,
  45. 'OR:alias:IN' => $groups,
  46. 'OR:id:IN' => $groups,
  47. ));
  48. }
  49. $gc->prepare();
  50. $gc->stmt->execute();
  51. $groups = $gc->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
  52. $conditions = array();
  53. foreach ($groups as $group) {
  54. if (isset($_GET[$group])) {
  55. $groupTags = $tagger->explodeAndClean($_GET[$group]);
  56. if (!empty($groupTags)) {
  57. $like = array('AND:alias:IN' => $groupTags);
  58. if ($likeComparison == 1) {
  59. foreach ($groupTags as $tag) {
  60. $like[] = array('OR:alias:LIKE' => '%' . $tag . '%');
  61. }
  62. }
  63. $conditions[] = array(
  64. 'OR:Group.alias:=' => $group,
  65. $like
  66. );
  67. $tagsCount += count($groupTags);
  68. }
  69. }
  70. }
  71. if (count($conditions) == 0) {
  72. return $modx->toJSON($where);
  73. }
  74. $c = $modx->newQuery('TaggerTag');
  75. $c->leftJoin('TaggerGroup', 'Group');
  76. $c->where($conditions);
  77. } else {
  78. $tags = $tagger->explodeAndClean($tags);
  79. if (empty($tags)) {
  80. return $modx->toJSON($where);
  81. }
  82. $tagsCount = count($tags);
  83. $groups = $modx->getOption('groups', $scriptProperties, '');
  84. $groups = $tagger->explodeAndClean($groups);
  85. $c = $modx->newQuery('TaggerTag');
  86. $c->select($modx->getSelectColumns('TaggerTag', 'TaggerTag', '', array('id')));
  87. $compare = array(
  88. $tagField . ':IN' => $tags
  89. );
  90. if ($likeComparison == 1) {
  91. foreach ($tags as $tag) {
  92. $compare[] = array('OR:' . $tagField . ':LIKE' => '%' . $tag . '%');
  93. }
  94. }
  95. $c->where($compare);
  96. if (!empty($groups)) {
  97. $c->leftJoin('TaggerGroup', 'Group');
  98. $c->where(array(
  99. 'Group.id:IN' => $groups,
  100. 'OR:Group.name:IN' => $groups,
  101. 'OR:Group.alias:IN' => $groups,
  102. ));
  103. }
  104. }
  105. $c->prepare();
  106. $c->stmt->execute();
  107. $tagIDs = $c->stmt->fetchAll(PDO::FETCH_COLUMN, 0);
  108. if (count($tagIDs) == 0) {
  109. $tagIDs[] = 0;
  110. }
  111. if ($matchAll == 0) {
  112. $where[] = "EXISTS (SELECT 1 FROM {$modx->getTableName('TaggerTagResource')} r WHERE r.tag IN (" . implode(',', $tagIDs) . ") AND r.resource = modResource." . $field . ")";
  113. } else {
  114. $where[] = "EXISTS (SELECT 1 as found FROM {$modx->getTableName('TaggerTagResource')} r WHERE r.tag IN (" . implode(',', $tagIDs) . ") AND r.resource = modResource." . $field . " GROUP BY found HAVING count(found) = " . $tagsCount . ")";
  115. }
  116. return $modx->toJSON($where);
  117. return;