Advertisement
Guest User

Untitled

a guest
Oct 7th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2.  
  3. $out = [];
  4. $leftJoin = [];
  5. $optionFilters = [];
  6. $where = [];
  7. $sortby = isset($sortby) ? $sortby : 'menuindex';
  8. $sortdir = isset($sortdir) ? $sortdir : 'ASC';
  9.  
  10. $props = [];
  11. foreach ($_REQUEST as $key => $val) {
  12.     if (preg_match('/sort|page|min_|max_|filter_/i', $key)) {
  13.         $props[$key] = $val;
  14.         if (preg_match('/filter_/i', $key) && gettype($val) == 'string') {
  15.             $props[$key] = explode(',', $val);
  16.         }
  17.     }
  18. }
  19. array_multisort($props); // для кэша
  20.  
  21. $cache_name = "msp_{$modx->resource->id}_" . md5(json_encode($props));
  22.  
  23. $cache = $modx->cacheManager->get($cache_name, [\xPDO::OPT_CACHE_KEY => 'resource']);
  24. if ($cache) {
  25.     $cache['debug']['cache'] = $cache_name;
  26.     return $cache;
  27. }
  28.  
  29. foreach ($props as $key => $val) {
  30.     if ($key == 'sort') {
  31.         $s = explode('_', $val);
  32.         if (isset($s[0])) {
  33.             $sortby = $s[0];
  34.             if (in_array($s[0], ['price'])) {
  35.                 $sortby = "Data.{$s[0]}";
  36.             }
  37.             if (isset($s[1]) && $s[1] == 'desc') {
  38.                 $sortdir = 'DESC';
  39.             }
  40.         }
  41.     }
  42.     if (preg_match('/min_|max_/i', $key)) {
  43.         $keys = explode('_', $key);
  44.         $c = $keys[0] == 'min' ? '>=' : '<=';
  45.         if ($keys[1] == 'price') {
  46.             $where[] = "Data.price {$c} " . (int)$val;
  47.         } else {
  48.             $leftJoin[$keys[1]] = [
  49.                 'class' => 'msProductOption',
  50.                 'alias' => $keys[1],
  51.                 'on' => "`{$keys[1]}`.product_id = Data.id AND `{$keys[1]}`.key = '{$keys[1]}'"
  52.             ];
  53.             $where[] = "(`{$keys[1]}`.value * 1) {$c} $val";
  54.             if ($keys[0] == 'max') {
  55.                 $where[] = "`{$keys[1]}`.value != ''";
  56.             }
  57.         }
  58.     }
  59.     if (preg_match('/filter_/i', $key)) {
  60.         $key = str_replace('filter_', '', $key);
  61.         $optionFilters["{$key}:IN"] = $val;
  62.     }
  63. }
  64.  
  65. $params = array_merge([
  66.     'element' => 'msProducts',
  67.     'limit' => 12,
  68.     'pageLimit' => 7,
  69.     'return' => 'JSON',
  70.     'setMeta' => 0, // без этого долго загружается
  71.     'sortby' => $sortby,
  72.     'sortdir' => $sortdir,
  73.     'tvPrefix'=>'',
  74.  
  75.     'leftJoin' => !empty($leftJoin) ? $leftJoin : '',
  76.     'optionFilters' => !empty($optionFilters) ? json_encode($optionFilters) : '',
  77.     'where' => count($where) ? '["' . implode(' AND ', $where) . '"]' : '',
  78.  
  79.     'tplPage'=>'@INLINE <li><a class="" href="{$href}" data-page="{$pageNo}">{$pageNo}</a></li>',
  80.     'tplPageActive'=>'@INLINE <li class="pagination-list-active">{$pageNo}</li>',
  81.     'tplPageSkip' => '@INLINE <li class="pagination-list-skip">...</li>',
  82.     'tplPageWrapper'=>'@INLINE <ul class="pagination-list">{$pages}</ul>',
  83. ], $scriptProperties);
  84.  
  85. $response = $modx->runSnippet('pdoPage', $params);
  86.  
  87. $out['debug'] = [
  88.     'params' => $params,
  89.     'props' => $props,
  90. ];
  91. $out['page'] = (int)$modx->placeholders['page'];
  92. $out['pages'] = (int)$modx->placeholders['pageCount'];
  93. $out['pagination'] = $modx->placeholders['page.nav'];
  94. $out['items'] = json_decode($response, true);
  95.  
  96. $modx->cacheManager->set($cache_name, $out, 0, [\xPDO::OPT_CACHE_KEY => 'resource']);
  97.  
  98. return $out;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement