Last active
November 6, 2018 16:51
-
-
Save eonarik/10b1e7c48d584996bbb1785882aed56b to your computer and use it in GitHub Desktop.
modx snippet fileList
This file contains 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 | |
// v.1.0.7 | |
/** | |
* string fileList - список файлов из директории | |
* @param validExts - список разрешенных типов файлов для вывода | |
* @param sortby - сортировка (name)(asc,desc) | |
* @param totalVar = 'total' | |
* @param limit | |
* @param page | |
* @param path - относительный путь к папке с файлом | |
* @param tpl | |
* | |
* ph [[+idx]] - номер по порядку начиная с 1 | |
* ph [[+ext]] - расширение файла (без точки) | |
* ph [[+name]] - наименование файла | |
* ph [[+path]] - относительный путь к файлу | |
* ph [[+size]] - размер файла | |
* | |
* интеграция с pdoPage, пример | |
* [[!pdoPage?element=`fileList`&path=`assets/images/`&limit=`4`&tpl=`tpl_file`]] [[!+page.nav]] | |
**/ | |
$validExts = array_map('trim', explode(',', $modx->getOption('validExts',$scriptProperties,'jpg,jpeg,png,gif'))); | |
$totalVar = $modx->getOption('totalVar', $scriptProperties, 'total'); | |
$limit = $modx->getOption('limit', $scriptProperties, 0); | |
$page = $modx->getOption('page', $scriptProperties, 1); | |
$tpl = str_replace(array('^','{{','}}'),array('`','[[',']]'), $tpl); | |
$inlineReg = '/^(@INLINE|@CODE)/'; | |
$out = array(); | |
$list = glob($modx->getOption('base_path').ltrim($path,'/').'*',GLOB_NOSORT); | |
if($sortby AND $sortby = $modx->fromJSON($sortby)){ | |
foreach($sortby as $sort=>$by){ | |
if($sort == 'name' && $by == 'asc'){ sort($list); } | |
if($sort == 'name' && $by == 'desc'){ rsort($list); } | |
} | |
} | |
foreach($list as $i => $el){ | |
$name = explode('.', $el); | |
$ext = $name[count($name) - 1]; | |
if (!in_array(strtolower($ext), $validExts)) { | |
unset($list[$i]); | |
} | |
} | |
$modx->setPlaceholder($totalVar, count($list)); | |
$i = 1; | |
foreach($list as $el){ | |
if ( | |
$limit > 0 | |
&& ( | |
$i > ($page - 1) * $limit | |
&& $i <= $limit * $page | |
) | |
|| !$limit | |
) { | |
$info = array(); | |
$info['idx'] = $i; | |
$info['path'] = str_replace(MODX_BASE_PATH, '', $el); | |
$el = explode('/', $el); | |
$info['name'] = $el[count($el) - 1]; | |
$info['name'] = explode('.', $info['name']); | |
$info['ext'] = $info['name'][count($info['name']) - 1]; | |
unset($info['name'][count($info['name']) - 1]); | |
$info['name'] = implode('.', $info['name']); | |
$el = implode('/', $el); | |
$info['size'] = filesize($el); | |
if(in_array(strtolower($info['ext']),$validExts)){ | |
if(preg_match($inlineReg,$tpl)){ | |
$chunk = $modx->newObject('modChunk'); | |
$chunk->set('snippet', preg_replace($inlineReg, '', $tpl)); | |
$chunk->setCacheable(false); | |
$out[] = $chunk->process($info); | |
} else { | |
$out[] = $modx->getChunk($tpl,$info); | |
} | |
} | |
} | |
$i++; | |
} | |
return implode("\n",$out); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment