100.cache.php 4.8 KB

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