Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $output = array();
- $outputSeparator = isset($outputSeparator) ? $outputSeparator : "\n";
- /* set default properties */
- if ($loadRehab) $modx->addPackage('Rehab', $modx->getOption('core_path').'components/rehab/model/','modx_rehab_');
- $objClass = !empty($objClass) ? $objClass : 'modResource';
- $tpl = !empty($tpl) ? $tpl : '';
- $parents = (!empty($parents) || $parents === '0') ? explode(',', $parents) : array(0);
- $parentField = !empty($parentField) ? $parentField : 'parent';
- array_walk($parents, 'trim');
- $parents = array_unique($parents);
- $where = !empty($where) ? $modx->fromJSON($where) : array();
- $sortby = isset($sortby) && $sortby != 'publishedon' ? $sortby : 'id';
- $sortbyAlias = isset($sortbyAlias) ? $sortbyAlias : 'name';
- $sortdir = isset($sortdir) ? $sortdir : 'DESC';
- $limit = isset($limit) ? (integer) $limit : 10;
- $offset = isset($offset) ? (integer) $offset : 0;
- $totalVar = !empty($totalVar) ? $totalVar : 'total';
- $dbCacheFlag = !isset($dbCacheFlag) ? false : $dbCacheFlag;
- if (is_string($dbCacheFlag) || is_numeric($dbCacheFlag)) {
- if ($dbCacheFlag == '0') {
- $dbCacheFlag = false;
- } elseif ($dbCacheFlag == '1') {
- $dbCacheFlag = true;
- } else {
- $dbCacheFlag = (integer) $dbCacheFlag;
- }
- }
- /* build query */
- if (!$parentsIgnore) $criteria = array("$objClass.$parentField IN (" . implode(',', $parents) . ")");
- $criteria = $modx->newQuery($objClass, $criteria);
- /* include/exclude resources, via &resources=`123,-456` prop */
- if (!empty($objects)) {
- $resources = $objects;
- $resourceConditions = array();
- $resources = explode(',',$resources);
- $include = array();
- $exclude = array();
- foreach ($resources as $resource) {
- $resource = (int)$resource;
- if ($resource == 0) continue;
- if ($resource < 0) {
- $exclude[] = abs($resource);
- } else {
- $include[] = $resource;
- }
- }
- if (!empty($include)) {
- $criteria->where(array('OR:'.$objClass.'.id:IN' => $include), xPDOQuery::SQL_OR);
- }
- if (!empty($exclude)) {
- $criteria->where(array($objClass.'.id:NOT IN' => $exclude), xPDOQuery::SQL_AND, null, 1);
- }
- }
- if (!empty($where)) {
- $criteria->where($where);
- }
- $total = $modx->getCount($objClass, $criteria);
- $modx->setPlaceholder($totalVar, $total);
- $fields = array_keys($modx->getFields($objClass));
- $criteria->select($fields);
- if (!empty($sortby)) {
- if (strpos($sortby, '{') === 0) {
- $sorts = $modx->fromJSON($sortby);
- } else {
- $sorts = array($sortby => $sortdir);
- }
- if (is_array($sorts)) {
- while (list($sort, $dir) = each($sorts)) {
- if ($sortbyEscaped) $sort = $modx->escape($sort);
- if (!empty($sortbyAlias)) $sort = $modx->escape($sortbyAlias) . ".{$sort}";
- $criteria->sortby($sort, $dir);
- }
- }
- }
- if (!empty($limit)) $criteria->limit($limit, $offset);
- if (!empty($debug)) {
- $criteria->prepare();
- $modx->log(modX::LOG_LEVEL_ERROR, $criteria->toSQL());
- }
- $collection = $modx->getCollection($objClass, $criteria, $dbCacheFlag);
- $idx = !empty($idx) && $idx !== '0' ? (integer) $idx : 1;
- $first = empty($first) && $first !== '0' ? 1 : (integer) $first;
- $last = empty($last) ? (count($collection) + $idx - 1) : (integer) $last;
- /* include parseTpl */
- include_once $modx->getOption('getresources.core_path',null,$modx->getOption('core_path').'components/getresources/').'include.parsetpl.php';
- /** @var modResource $resource */
- foreach ($collection as $resourceId => $resource) {
- $odd = ($idx & 1);
- $properties = array_merge(
- $scriptProperties
- ,array(
- 'idx' => $idx
- ,'first' => $first
- ,'last' => $last
- ,'odd' => $odd
- )
- ,$resource->get($fields)
- );
- $resourceTpl = '';
- if ($idx == $first && !empty($tplFirst)) {
- $resourceTpl = parseTpl($tplFirst, $properties);
- }
- if ($idx == $last && empty($resourceTpl) && !empty($tplLast)) {
- $resourceTpl = parseTpl($tplLast, $properties);
- }
- $tplidx = 'tpl_' . $idx;
- if (empty($resourceTpl) && !empty($$tplidx)) {
- $resourceTpl = parseTpl($$tplidx, $properties);
- }
- if ($idx > 1 && empty($resourceTpl)) {
- $divisors = getDivisors($idx);
- if (!empty($divisors)) {
- foreach ($divisors as $divisor) {
- $tplnth = 'tpl_n' . $divisor;
- if (!empty($$tplnth)) {
- $resourceTpl = parseTpl($$tplnth, $properties);
- if (!empty($resourceTpl)) {
- break;
- }
- }
- }
- }
- }
- if ($odd && empty($resourceTpl) && !empty($tplOdd)) {
- $resourceTpl = parseTpl($tplOdd, $properties);
- }
- if (!empty($tplCondition) && !empty($conditionalTpls) && empty($resourceTpl)) {
- $conTpls = $modx->fromJSON($conditionalTpls);
- $subject = $properties[$tplCondition];
- $tplOperator = !empty($tplOperator) ? $tplOperator : '=';
- $tplOperator = strtolower($tplOperator);
- $tplCon = '';
- foreach ($conTpls as $operand => $conditionalTpl) {
- switch ($tplOperator) {
- case '!=':
- case 'neq':
- case 'not':
- case 'isnot':
- case 'isnt':
- case 'unequal':
- case 'notequal':
- $tplCon = (($subject != $operand) ? $conditionalTpl : $tplCon);
- break;
- case '<':
- case 'lt':
- case 'less':
- case 'lessthan':
- $tplCon = (($subject < $operand) ? $conditionalTpl : $tplCon);
- break;
- case '>':
- case 'gt':
- case 'greater':
- case 'greaterthan':
- $tplCon = (($subject > $operand) ? $conditionalTpl : $tplCon);
- break;
- case '<=':
- case 'lte':
- case 'lessthanequals':
- case 'lessthanorequalto':
- $tplCon = (($subject <= $operand) ? $conditionalTpl : $tplCon);
- break;
- case '>=':
- case 'gte':
- case 'greaterthanequals':
- case 'greaterthanequalto':
- $tplCon = (($subject >= $operand) ? $conditionalTpl : $tplCon);
- break;
- case 'isempty':
- case 'empty':
- $tplCon = empty($subject) ? $conditionalTpl : $tplCon;
- break;
- case '!empty':
- case 'notempty':
- case 'isnotempty':
- $tplCon = !empty($subject) && $subject != '' ? $conditionalTpl : $tplCon;
- break;
- case 'isnull':
- case 'null':
- $tplCon = $subject == null || strtolower($subject) == 'null' ? $conditionalTpl : $tplCon;
- break;
- case 'inarray':
- case 'in_array':
- case 'ia':
- $operand = explode(',', $operand);
- $tplCon = in_array($subject, $operand) ? $conditionalTpl : $tplCon;
- break;
- case 'between':
- case 'range':
- case '>=<':
- case '><':
- $operand = explode(',', $operand);
- $tplCon = ($subject >= min($operand) && $subject <= max($operand)) ? $conditionalTpl : $tplCon;
- break;
- case '==':
- case '=':
- case 'eq':
- case 'is':
- case 'equal':
- case 'equals':
- case 'equalto':
- default:
- $tplCon = (($subject == $operand) ? $conditionalTpl : $tplCon);
- break;
- }
- }
- if (!empty($tplCon)) {
- $resourceTpl = parseTpl($tplCon, $properties);
- }
- }
- if (!empty($tpl) && empty($resourceTpl)) {
- $resourceTpl = parseTpl($tpl, $properties);
- }
- if (empty($resourceTpl)) {
- $chunk = $modx->newObject('modChunk');
- $chunk->setCacheable(false);
- $output[]= $chunk->process(array(), '<pre>' . print_r($properties, true) .'</pre>');
- } else {
- $output[]= $resourceTpl;
- }
- $idx++;
- }
- /* output */
- $toSeparatePlaceholders = $modx->getOption('toSeparatePlaceholders',$scriptProperties,false);
- if (!empty($toSeparatePlaceholders)) {
- $modx->setPlaceholders($output,$toSeparatePlaceholders);
- return '';
- }
- $output = implode($outputSeparator, $output);
- $toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
- if (!empty($toPlaceholder)) {
- $modx->setPlaceholder($toPlaceholder,$output);
- return '';
- }
- return $output;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement