Last active
December 21, 2017 12:25
-
-
Save sepiariver/e5fe4f4adc0d75e149983c1704ee1e8d to your computer and use it in GitHub Desktop.
Fast snippet for listing MODX Resources. Outputs generic HTML list with link to each Resource. Not template-able except wrapper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* quickList | |
* | |
* Lists Resourcs super fast. Uses code by Garry Nutting of the MODX Core Team. | |
* | |
* @author YJ Tso <[email protected]>, Garry Nutting <[email protected]> | |
* | |
* | |
* 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; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment