Skip to content

Instantly share code, notes, and snippets.

@netProphET
Created May 16, 2011 17:17
Show Gist options
  • Save netProphET/974889 to your computer and use it in GitHub Desktop.
Save netProphET/974889 to your computer and use it in GitHub Desktop.
emulate @Directory binding, but from base_path of Resource's context #MODX
<?php
/**
* emulate @DIRECTORY binding, from base_path of Resource's context
*
* usage:
* a) create context settings named 'base_path' for each context (not neccessary for web context)
* (note: you may also want assets_path and/or other settings in your contexts as well)
* b) call with @EVAL in TV's Input Options field, e.g.
* @EVAL $dir='assets/images/'; return(include '/path/to/this/file/context_directory_binding.php');
*/
$dirBasePath = $this->xpdo->getOption('base_path');
$actionController = 'resource/create';
$actionId = $modx->request->action;
if(is_numeric($actionId) && isset($modx->actionMap[$actionId])) {
$actionController = $modx->actionMap[$actionId]['controller'];
}
// get context
$contextKey = '';
if($actionController == 'resource/update') {
$resourceId = (int) $_REQUEST['id'];
$contextQuery = $modx->newQuery('modResource', $resourceId, false);
$contextQuery->select($modx->getSelectColumns('modResource', 'modResource', '', array('context_key')));
$contextQuery->prepare();
$contextQuery->stmt->execute();
$contextKey = $contextQuery->stmt->fetchColumn();
} else {
if(isset($_REQUEST['context_key'])) {
$contextKey = $_REQUEST['context_key'];
}
}
if(empty($contextKey)) {
$contextKey = 'web';
}
// get base path
$basePathSetting = $modx->getObject('modContextSetting', array(
'context_key'=> $contextKey,
'key'=> 'base_path'
));
$basePath = $modx->getOption('base_path');
if($basePathSetting !== null) {
$basePath = $basePathSetting->get('value');
}
// make sure specified folder exists ($dir passed from @EVAL
if (substr($basePath,-1,1) != '/') { $basePath .= '/'; }
if (substr($dir,-1,1) != '/') { $dir .= '/'; }
$targetPath = $basePath . str_replace('..', '.', $dir);
// do @DIRECTORY logic
if (!is_dir($targetPath)) { return ''; }
$files = array();
$invalid = array('.','..','.svn','.git','.DS_Store');
foreach (new DirectoryIterator($targetPath) as $file) {
if (!$file->isReadable()) continue;
$basename = $file->getFilename();
if(!in_array($basename,$invalid)) {
$files[] = "{$basename}=={$param}{$basename}";
}
}
asort($files);
$output = implode('||',$files);
return $output;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment