Skip to content

Instantly share code, notes, and snippets.

@jrotering
jrotering / rightTemplate
Last active December 17, 2015 03:59
Fires on 'OnHandleRequest' event. Sets the template of new resources based on a property in the template of the parent resource.
<?php
if (isset($_GET['a'])) {
$action = $modx->getObject('modAction', $_GET['a']);
if (is_object($action) && $action->get('controller') == 'resource/create') {
$parentID = isset($_REQUEST['parent']) ? (int) $_REQUEST['parent'] : 0;
if ($parent = $modx->getObject('modResource', $parentID)) {
$parentTpl = $parent->get('template');
if ($parentTplObj = $modx->getObject('modTemplate', $parentTpl)) {
if ($props = $parentTplObj->getProperties()) {
$tpl = ($ff = $modx->fromJSON($props['childTemplate'])) ? $ff : null;
@jrotering
jrotering / pdflink.tpl
Last active December 16, 2015 00:29
Sample template chunk for a getResources call that returns PDF Static Resources
@jrotering
jrotering / filesize
Created April 9, 2013 14:53
Displays the file size of a file in the local filesystem (relative to MODX_BASE_PATH) in bytes, kilobytes, or megabytes depending on its size.
<?php
$path = MODX_BASE_PATH . $file;
$fs = filesize($path);
if ($fs < 1024) {
return "${fs}B";
}
elseif ($fs < (1024*1024)) {
return round($fs / 1024, 0) .'KB';
}
if ($user = $modx->getObject('modUser', $id)) {
if ($profile = $user->getOne('Profile')) {
return $profile->get('fullname');
}
}
return '';
if ($user = $modx->getObject('modUser', $id)) {
if ($profile = $user->getOne('Profile')) {
return $profile->get('fullname');
}
}
return '';
@jrotering
jrotering / getObjectName
Created March 26, 2013 20:21
Simple snippet that returns the name (or any other field you specify) of any MODX object. Example: [[getObjectName?&obj=`modTemplate`&id=`[[*template]]`]]
<?php
$fn = (isset($field)) ? $field : ($obj == 'modTemplate') ? 'templatename' : 'name';
if ($object = $modx->getObject($obj, $id)) {
if ($value = $object->get($fn)) {
return $value;
}
else {
$modx->log(modX::LOG_LEVEL_ERROR, 'getObjectName - could not find field "' . $fn . '" on object #' . $id . ' of type "' . $obj . '"');
}
@jrotering
jrotering / getFullname
Created March 22, 2013 04:00
Returns the fullname value for the given user ID. Example: <div>Last Edited By: [[getFullname?&id=`[[*editedby]]`]]</div>
if ($user = $modx->getObject('modUser', $id)) {
if ($profile = $user->getOne('Profile')) {
return $profile->get('fullname');
}
}
return '';