yj-t
10/14/2016 - 7:30 PM

Fast snippet for listing MODX Resources. Outputs generic HTML list with link to each Resource. Not template-able except wrapper.

Fast snippet for listing MODX Resources. Outputs generic HTML list with link to each Resource. Not template-able except wrapper.

<?php
/**
 * quickList
 *
 * Lists Resourcs super fast. Uses code by Garry Nutting of the MODX Core Team.
 *
 * @author YJ Tso <yj@modx.com>, Garry Nutting <garry@modx.com>
 *
 *
 * quickList is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 *
 * quickList is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * quickList; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * @package googlesitemap
 */

// "300 lives of men I've walked this earth and now I have no time"
ini_set('max_execution_time', 0);

// Set cache options
// set $cacheKey prefix to be appended later
$cacheKey = $modx->getoption('cachePrefix', $scriptProperties, 'quicklist');
// subfolder of core/cache/
$cachePartition = $modx->getoption('cachePartition', $scriptProperties, 'googlesitemap');
$expires = $modx->getOption('cacheExpires', $scriptProperties, 86400);
$options = array(
  xPDO::OPT_CACHE_KEY => $cachePartition,
);

$cacheKey .= '.' . md5($modx->toJSON($scriptProperties));

// Fetch from cache
$output = null;
$output = $modx->cacheManager->get($cacheKey, $options);
if ($output !== null) return $output;

/* Map specified filter properties to new variables for convenience */
$filters = array();
$parent = $modx->getOption('parent', $scriptProperties, '');
if (!empty($parent)) $filters[] = 's.parent = ' . $parent;
$context = $modx->getOption('context', $scriptProperties, '');
if (!empty($context)) $filters[] = 's.context_key = ' . $context;
if (!$modx->getOption('showDeleted', $scriptProperties, false)) $filters[] =  's.deleted = 0';
if (!$modx->getOption('showHidden', $scriptProperties, false)) $filters[] = 's.hidemenu = 0';
if (!$modx->getOption('showUnpublished', $scriptProperties, false)) $filters[] = 's.published = 1';
if (!$modx->getOption('showUnsearchable', $scriptProperties, false)) $filters[] = 's.searchable = 1';

/* Sorting */
$sortBy = $modx->getOption('sortBy', $scriptProperties, 'publishedon');
$sortDir = $modx->getOption('sortDir', $scriptProperties, 'DESC');
$orderby = 's.' . strtolower($sortBy) . ' ' . strtoupper($sortDir);

$containerTpl = $modx->getOption('containerTpl', $scriptProperties, '');

/* Query */
$items = '';
$tablePrefix = $modx->getOption('table_prefix');
    
$criteria = implode(' AND ', $filters);
// Add all resources that meet criteria
$stmt = $modx->query("
        SELECT
    	    GROUP_CONCAT(
                CONCAT('<li><a href=\"', uri, '\">', pagetitle, '</a></li>')
                SEPARATOR ''
            ) AS node
        FROM " . $tablePrefix . "site_content AS s
        WHERE " . $criteria . "
        GROUP BY s.id
        ORDER BY " . $orderby . "
");

// Add to output
if ($stmt) {
    $rows = $stmt->fetchAll(PDO::FETCH_COLUMN);
    $items .= implode(PHP_EOL, $rows);
} else {
    $modx->log(modX::LOG_LEVEL_WARN, 'quickList could not generate a list of resources.');
    return;
}

/* get container tpl and content */
$output = (!empty($containerTpl)) ? $modx->getChunk($containerTpl, array(
        'items' => $items,
    )) : $items;

$modx->cacheManager->set($cacheKey, $output, $expires, $options);
return $output;