Zahl Variable mit Custom Output Filter
Benutze ich z.B. um eine Zahl als Währung anzuzeigen, Link: numberformat (https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples#CustomOutputFilterExamples-numberformat)
(aus "1000,2" wird dann z.B. "1.000,20 €")
today_year
<?php
$today = date("Y");
return $today;
limitWholeWords
<?php
# z.B.: [[*content:limitWholeWords=`100`]]
# Damit wird das letzte Wort nicht abgeschnitten
# und evt. Parser mit preg_replace enfernen
$output = preg_replace('/\[\[(.*)\]\]/', ' ', $input);
$limit = (int) $options;
while (strlen($output) > $limit) {
$output = substr($output, 0, strrpos($output, ' '));
}
return $output;
getStrftime (aktuelle Zeit)
setlocale (LC_ALL, 'de_DE.UTF-8') ist seit Revo 2.2.x nicht mehr nötig, siehe Systemeinstellungen 'Lexikon und Sprache'
<?php
// setlocale(LC_TIME, 'German_Germany');
// nur noetig wenn Revo < 2.2
// ab Revo 2.2.x in der Area "Lexicon and Language" die Einstellung locale auf "de_DE.UTF-8"
//
// z.B.:
// Über getResources eine Liste von Terminen ausgeben,
// die einen TV eventDate enthalten.
// Immer genau die nächsten Termine ab dem heutigen Datum.
//
// Beispiele
// ab jetzt: &tvFilters=`tvEventDate>=[[!getStrftime? &format=`%Y-%m-%d %H:%M:%S`]]`
// oder ab heute: &tvFilters=`tvEventDate>=[[!getStrftime? &format=`%Y-%m-%d`]]`
//
# setlocale (LC_ALL, 'de_DE.UTF-8');
return strftime($format,time());
getIEBrowser (Warnung wenn ein alter IE benutzt wird)
<?php
# Beispiel: [[!getIEBrowser:gte=`9`:then=``:else=`<strong>Warnung:</strong> Ihr benutzt einen alten Internet Explorer [[!getIEBrowser]]`]]
<?php
if (strstr($_SERVER['HTTP_USER_AGENT'],'MSIE'))
# Name und Version
# $output=ereg_replace(".+\(.+MSIE ([0-9,\.]+).+","Internet Explorer \\1",$_SERVER['HTTP_USER_AGENT']);
# nur die Version
$output=ereg_replace(".+\(.+MSIE ([0-9,\.]+).+","\\1",$_SERVER['HTTP_USER_AGENT']);
$output = abs($output);
if ($output == 0) {
$output = 999;
}
return $output;
SMF 2.x SSI (recent topics)
<?php
# Info: http://wiki.smfportal.de/SSI_recentTopics
$filename = '/www/USER/html/smf/SSI.php';
if (file_exists($filename)) {
include_once($filename);
$what = ssi_recentTopics(5, NULL, 'array');
foreach ($what as $topic) {
$subject = utf8_encode($topic['subject']);
# nur ganze Wörter, max. 25 Zeichen ausgeben
$subject = explode('\n', wordwrap($subject, 25, '\n'));
if ($subject[1] != '') {
$subject = $subject[0].'...';
}
else {
$subject = $subject[0];
}
$output = $output.'<div style="padding-left: 6px; padding-bottom: 5px; font-size: 90%;">'.$topic['icon'].' '.'<a href="'.$topic['href'].'">'.$subject.'</a>';
$output = $output.'<div style="margin-left:20px"><span class="smallfont">'.$topic['time'].'</span></div></div>';
}
} else {
$output = 'Error: '.$filename.' !';
}
getImagesFolder (Danke an sam von modxcms.de) Update 2014-07-26
<?php
// e.g. [[!getImagesFolder? &path=`path/to/images` &extensions=`jpg` &tpl=`colorboxTpl` &class=`group1` &sort=`desc`]]
$path = trim($modx->getOption('path', $scriptProperties, ""), '/').'/';
$class = $modx->getOption('class', $scriptProperties, "");
$sort = $modx->getOption('sort', $scriptProperties, "asc");
$extensions = explode(',', str_replace(' ', '', $modx->getOption('extensions', $scriptProperties, "jpg,png")));
$tpl = $modx->getOption('tpl', $scriptProperties, '<img src="[[+path]][[+filename]]" width="[[+width]]" height="[[+height]]" alt="[[+filename]]" /><br />'.PHP_EOL);
if (($path === '') || !is_dir($path))
return;
$output = '';
$count = 0;
$fields = array();
$fields['path'] = $path;
$fields['class'] = $class;
if (!($handle = opendir($path)))
return "Error: Unable to open directrory '$path'!<br />".PHP_EOL;
while(($file = readdir($handle)) !== false) {
if (is_file($path.$file) && (($pos = strrpos($file, ".")) !== false) && in_array(substr($file, $pos + 1), $extensions))
$files[] = $file;
}
closedir($handle);
if (is_array($files))
{
if (($sort = strtolower(trim($sort))) === "asc")
sort($files);
elseif ($sort === "desc")
rsort($files);
foreach ($files as $file) {
list($fields['width'], $fields['height']) = getimagesize($path.$file);
$fields['filename'] = $file;
$fields['count'] = ++$count;
$chunk = $modx->getChunk($tpl, $fields);
if (!strlen($chunk)) {
$new_tpl = $modx->newObject('modChunk');
$new_tpl->setCacheable(false);
$new_tpl->setContent($tpl);
$chunk = $new_tpl->process($fields);
}
$output .= $chunk;
}
}
return $output;
html5FIX, als Output Filter werden die Attribute "cellspacing" und "cellpadding" (html5 obsolete) aus einer Tabelle entfernt.
<?php
# z.B. [[content:html5FIX]]
$content = $modx->resource->_output;
$newContent = preg_replace( '/(cellspacing|cellpadding)=\"\d*\"\s/', "", $content );
$modx->resource->_output = $newContent;
html5FIX, als Plugin werden die Attribute "cellspacing" und "cellpadding" (html5 obsolete) aus einer Tabelle beim Speichern gelöscht.
<?php
# Event OnBeforeDocFormSave
$content = $resource->getContent();
$newContent = preg_replace( '/(cellspacing|cellpadding)=\"\d*\"\s/', "", $content );
$resource->setContent($newContent);
Ein Plugin nur für bestimmte Kontexte (als Array) ausführen
<?php
$contextArr = array('web','web4','web5');
$contextName = $resource->get('context_key');
if (!in_array($contextName, $contextArr)){return;}
.... code
Plugin, [Alias] zu [ID]-[Alias] (Friendly URL)
<?php
# System Events: OnDocFormSave
# rtfm: http://rtfm.modx.com/display/revolution20/OnDocFormSave
# gilt fuer folgende Kontexte
$contextArr = array('web','web2','web6');
$contextName = $resource->get('context_key');
if (!in_array($contextName, $contextArr)){return;}
switch($mode) {
case 'new':
# resource created
$alias = $resource->get('alias');
$newalias = $id . '-' . $alias;
$resource->set('alias', $newalias);
$resource->save();
break;
case 'upd':
# resource updated
$alias = $resource->get('alias');
$pos = strpos($alias,'-');
$alias = substr($alias, $pos);
$newalias = $id . '-' . $alias;
$resource->set('alias', $newalias);
$resource->save();
break;
}
Abruf aller Children der aktuellen Resource in ein Array
<?php
$children = $modx->resource->getMany('Children', array('published' => true));
if (!$children) {return;}
foreach ($children as $child) {
$out = array('alias' => $child->get('alias'));
$outputAlias .= $out[alias];
}
return outputAlias;
cleanFilename plugin für den File Manager
Transliteration mit der Funktion iconv(). Zeichen welche im Ziel-Zeichensatz nicht zur Verfügung stehen, werden durch ähnliche Zeichen ersetzt.
<?php
/*
* MODX cleanFilename plugin
*
* Events: OnFileManagerUpload
*
*/
// Cleaning function
if (!function_exists('cleanFilename')) {
function cleanFilename($modx, $filename, $slug) {
// trim, lowercase, replace special chars, transliterate
if (function_exists('iconv')) {
setlocale(LC_ALL, strtolower($modx->getOption('cultureKey')) . '_' . strtoupper($modx->getOption('cultureKey')));
$filename = strtolower(trim(preg_replace('~[^0-9a-z-' . preg_quote(null, '~') . ']+~i', $slug, iconv('UTF-8', 'ASCII//TRANSLIT', $filename)), $slug));
} else {
// without transliterate
$filename = strtolower(trim(preg_replace('~[^0-9a-z-' . preg_quote(null, '~') . ']+~i', $slug, $filename), $slug));
}
if (empty($filename)) {return 'noname';}
return $filename;
}
}
// rename each of the uploaded files
foreach($files as $file) {
if ($file['error'] == 0) {
$slug = '_';
$pathInfo = pathinfo($file['name']);
$oldPath = $directory.$file['name'];
$newPath = cleanFilename($modx, $pathInfo['filename'], $slug).'.'.$pathInfo['extension'];
$newPath2 = $directory.$newPath;
// $modx->log(modX::LOG_LEVEL_ERROR, '[cleanFilename] oldPath: '.$oldPath);
// $modx->log(modX::LOG_LEVEL_ERROR, '[cleanFilename] newPath: '.$newPath);
if ($oldPath != $newPath2){$source->renameObject($oldPath, $newPath);}
}
}