Advertisement
Guest User

getObjects - Modified getResources MODX snippet

a guest
Nov 15th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.00 KB | None | 0 0
  1. <?php
  2. $output = array();
  3. $outputSeparator = isset($outputSeparator) ? $outputSeparator : "\n";
  4.  
  5. /* set default properties */
  6. if ($loadRehab) $modx->addPackage('Rehab', $modx->getOption('core_path').'components/rehab/model/','modx_rehab_');
  7. $objClass = !empty($objClass) ? $objClass : 'modResource';
  8. $tpl = !empty($tpl) ? $tpl : '';
  9. $parents = (!empty($parents) || $parents === '0') ? explode(',', $parents) : array(0);
  10. $parentField = !empty($parentField) ? $parentField : 'parent';
  11. array_walk($parents, 'trim');
  12. $parents = array_unique($parents);
  13.  
  14. $where = !empty($where) ? $modx->fromJSON($where) : array();
  15. $sortby = isset($sortby) && $sortby != 'publishedon' ? $sortby : 'id';
  16. $sortbyAlias = isset($sortbyAlias) ? $sortbyAlias : 'name';
  17. $sortdir = isset($sortdir) ? $sortdir : 'DESC';
  18. $limit = isset($limit) ? (integer) $limit : 10;
  19. $offset = isset($offset) ? (integer) $offset : 0;
  20. $totalVar = !empty($totalVar) ? $totalVar : 'total';
  21.  
  22. $dbCacheFlag = !isset($dbCacheFlag) ? false : $dbCacheFlag;
  23. if (is_string($dbCacheFlag) || is_numeric($dbCacheFlag)) {
  24.     if ($dbCacheFlag == '0') {
  25.         $dbCacheFlag = false;
  26.     } elseif ($dbCacheFlag == '1') {
  27.         $dbCacheFlag = true;
  28.     } else {
  29.         $dbCacheFlag = (integer) $dbCacheFlag;
  30.     }
  31. }
  32.  
  33.  
  34. /* build query */
  35. if (!$parentsIgnore) $criteria = array("$objClass.$parentField IN (" . implode(',', $parents) . ")");
  36. $criteria = $modx->newQuery($objClass, $criteria);
  37.  
  38. /* include/exclude resources, via &resources=`123,-456` prop */
  39. if (!empty($objects)) {
  40.     $resources = $objects;
  41.     $resourceConditions = array();
  42.     $resources = explode(',',$resources);
  43.     $include = array();
  44.     $exclude = array();
  45.     foreach ($resources as $resource) {
  46.         $resource = (int)$resource;
  47.         if ($resource == 0) continue;
  48.         if ($resource < 0) {
  49.             $exclude[] = abs($resource);
  50.         } else {
  51.             $include[] = $resource;
  52.         }
  53.     }
  54.     if (!empty($include)) {
  55.         $criteria->where(array('OR:'.$objClass.'.id:IN' => $include), xPDOQuery::SQL_OR);
  56.     }
  57.     if (!empty($exclude)) {
  58.         $criteria->where(array($objClass.'.id:NOT IN' => $exclude), xPDOQuery::SQL_AND, null, 1);
  59.     }
  60. }
  61. if (!empty($where)) {
  62.     $criteria->where($where);
  63. }
  64.  
  65. $total = $modx->getCount($objClass, $criteria);
  66. $modx->setPlaceholder($totalVar, $total);
  67.  
  68. $fields = array_keys($modx->getFields($objClass));
  69.  
  70. $criteria->select($fields);
  71.  
  72. if (!empty($sortby)) {
  73.     if (strpos($sortby, '{') === 0) {
  74.         $sorts = $modx->fromJSON($sortby);
  75.     } else {
  76.         $sorts = array($sortby => $sortdir);
  77.     }
  78.     if (is_array($sorts)) {
  79.         while (list($sort, $dir) = each($sorts)) {
  80.             if ($sortbyEscaped) $sort = $modx->escape($sort);
  81.             if (!empty($sortbyAlias)) $sort = $modx->escape($sortbyAlias) . ".{$sort}";
  82.             $criteria->sortby($sort, $dir);
  83.         }
  84.     }
  85. }
  86. if (!empty($limit)) $criteria->limit($limit, $offset);
  87.  
  88. if (!empty($debug)) {
  89.     $criteria->prepare();
  90.     $modx->log(modX::LOG_LEVEL_ERROR, $criteria->toSQL());
  91. }
  92.  
  93. $collection = $modx->getCollection($objClass, $criteria, $dbCacheFlag);
  94.  
  95. $idx = !empty($idx) && $idx !== '0' ? (integer) $idx : 1;
  96. $first = empty($first) && $first !== '0' ? 1 : (integer) $first;
  97. $last = empty($last) ? (count($collection) + $idx - 1) : (integer) $last;
  98.  
  99. /* include parseTpl */
  100. include_once $modx->getOption('getresources.core_path',null,$modx->getOption('core_path').'components/getresources/').'include.parsetpl.php';
  101.  
  102. /** @var modResource $resource */
  103. foreach ($collection as $resourceId => $resource) {
  104.  
  105.     $odd = ($idx & 1);
  106.     $properties = array_merge(
  107.         $scriptProperties
  108.         ,array(
  109.             'idx' => $idx
  110.             ,'first' => $first
  111.             ,'last' => $last
  112.             ,'odd' => $odd
  113.         )
  114.         ,$resource->get($fields)
  115.     );
  116.     $resourceTpl = '';
  117.     if ($idx == $first && !empty($tplFirst)) {
  118.         $resourceTpl = parseTpl($tplFirst, $properties);
  119.     }
  120.     if ($idx == $last && empty($resourceTpl) && !empty($tplLast)) {
  121.         $resourceTpl = parseTpl($tplLast, $properties);
  122.     }
  123.     $tplidx = 'tpl_' . $idx;
  124.     if (empty($resourceTpl) && !empty($$tplidx)) {
  125.         $resourceTpl = parseTpl($$tplidx, $properties);
  126.     }
  127.     if ($idx > 1 && empty($resourceTpl)) {
  128.         $divisors = getDivisors($idx);
  129.         if (!empty($divisors)) {
  130.             foreach ($divisors as $divisor) {
  131.                 $tplnth = 'tpl_n' . $divisor;
  132.                 if (!empty($$tplnth)) {
  133.                     $resourceTpl = parseTpl($$tplnth, $properties);
  134.                     if (!empty($resourceTpl)) {
  135.                         break;
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.     }
  141.     if ($odd && empty($resourceTpl) && !empty($tplOdd)) {
  142.         $resourceTpl = parseTpl($tplOdd, $properties);
  143.     }
  144.     if (!empty($tplCondition) && !empty($conditionalTpls) && empty($resourceTpl)) {
  145.         $conTpls = $modx->fromJSON($conditionalTpls);
  146.         $subject = $properties[$tplCondition];
  147.         $tplOperator = !empty($tplOperator) ? $tplOperator : '=';
  148.         $tplOperator = strtolower($tplOperator);
  149.         $tplCon = '';
  150.         foreach ($conTpls as $operand => $conditionalTpl) {
  151.             switch ($tplOperator) {
  152.                 case '!=':
  153.                 case 'neq':
  154.                 case 'not':
  155.                 case 'isnot':
  156.                 case 'isnt':
  157.                 case 'unequal':
  158.                 case 'notequal':
  159.                     $tplCon = (($subject != $operand) ? $conditionalTpl : $tplCon);
  160.                     break;
  161.                 case '<':
  162.                 case 'lt':
  163.                 case 'less':
  164.                 case 'lessthan':
  165.                     $tplCon = (($subject < $operand) ? $conditionalTpl : $tplCon);
  166.                     break;
  167.                 case '>':
  168.                 case 'gt':
  169.                 case 'greater':
  170.                 case 'greaterthan':
  171.                     $tplCon = (($subject > $operand) ? $conditionalTpl : $tplCon);
  172.                     break;
  173.                 case '<=':
  174.                 case 'lte':
  175.                 case 'lessthanequals':
  176.                 case 'lessthanorequalto':
  177.                     $tplCon = (($subject <= $operand) ? $conditionalTpl : $tplCon);
  178.                     break;
  179.                 case '>=':
  180.                 case 'gte':
  181.                 case 'greaterthanequals':
  182.                 case 'greaterthanequalto':
  183.                     $tplCon = (($subject >= $operand) ? $conditionalTpl : $tplCon);
  184.                     break;
  185.                 case 'isempty':
  186.                 case 'empty':
  187.                     $tplCon = empty($subject) ? $conditionalTpl : $tplCon;
  188.                     break;
  189.                 case '!empty':
  190.                 case 'notempty':
  191.                 case 'isnotempty':
  192.                     $tplCon = !empty($subject) && $subject != '' ? $conditionalTpl : $tplCon;
  193.                     break;
  194.                 case 'isnull':
  195.                 case 'null':
  196.                     $tplCon = $subject == null || strtolower($subject) == 'null' ? $conditionalTpl : $tplCon;
  197.                     break;
  198.                 case 'inarray':
  199.                 case 'in_array':
  200.                 case 'ia':
  201.                     $operand = explode(',', $operand);
  202.                     $tplCon = in_array($subject, $operand) ? $conditionalTpl : $tplCon;
  203.                     break;
  204.                 case 'between':
  205.                 case 'range':
  206.                 case '>=<':
  207.                 case '><':
  208.                     $operand = explode(',', $operand);
  209.                     $tplCon = ($subject >= min($operand) && $subject <= max($operand)) ? $conditionalTpl : $tplCon;
  210.                     break;
  211.                 case '==':
  212.                 case '=':
  213.                 case 'eq':
  214.                 case 'is':
  215.                 case 'equal':
  216.                 case 'equals':
  217.                 case 'equalto':
  218.                 default:
  219.                     $tplCon = (($subject == $operand) ? $conditionalTpl : $tplCon);
  220.                     break;
  221.             }
  222.         }
  223.         if (!empty($tplCon)) {
  224.             $resourceTpl = parseTpl($tplCon, $properties);
  225.         }
  226.     }    
  227.     if (!empty($tpl) && empty($resourceTpl)) {
  228.         $resourceTpl = parseTpl($tpl, $properties);
  229.     }
  230.     if (empty($resourceTpl)) {
  231.         $chunk = $modx->newObject('modChunk');
  232.         $chunk->setCacheable(false);
  233.         $output[]= $chunk->process(array(), '<pre>' . print_r($properties, true) .'</pre>');
  234.     } else {
  235.         $output[]= $resourceTpl;
  236.     }
  237.     $idx++;
  238. }
  239.  
  240. /* output */
  241. $toSeparatePlaceholders = $modx->getOption('toSeparatePlaceholders',$scriptProperties,false);
  242. if (!empty($toSeparatePlaceholders)) {
  243.     $modx->setPlaceholders($output,$toSeparatePlaceholders);
  244.     return '';
  245. }
  246.  
  247. $output = implode($outputSeparator, $output);
  248. $toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
  249. if (!empty($toPlaceholder)) {
  250.     $modx->setPlaceholder($toPlaceholder,$output);
  251.     return '';
  252. }
  253. return $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement