Skip to content

Instantly share code, notes, and snippets.

@bwente
bwente / GetDeviceChunk.php
Last active October 19, 2016 11:44
MODx snippet. It displays the download link for the mobile device you are using.
<?php
//$iOS_chunk = $modx->getOption('ios',$scriptProperties);
//$Android_chunk = $modx->getOption('droid',$scriptProperties);
$iPod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
//check if user is using ipod, iphone or ipad...
@bwente
bwente / fltr.ordinal.php
Last active October 19, 2016 11:44
MODx output filter, converts cardinal to ordinal.
<?php
$test_c = abs($input) % 10;
$ext = ((abs($input) %100 < 21 && abs($input) %100 > 4) ? 'th'
: (($test_c < 4) ? ($test_c < 3) ? ($test_c < 2) ? ($test_c < 1)
? 'th' : 'st' : 'nd' : 'rd' : 'th'));
return $input.$ext;
?>
@bwente
bwente / showMe.php
Last active October 18, 2016 00:59
Show chunk inline at a scheduled time.
<?php
// [[!showMe? &start=`` &stop=`` &display=`featuresChunk` &default=`emptyChunk`]]
$start = strtotime($modx->getOption('start',$scriptProperties,time()));
$stop = strtotime($modx->getOption('stop',$scriptProperties,time()));
$display = $modx->getOption('display',$scriptProperties,'');
$default = $modx->getOption('default',$scriptProperties,'');
$rightNow = time();
if($rightNow > $start && $rightNow < $stop){
$output = $modx->getChunk($display);
@bwente
bwente / getChildCount.php
Last active October 19, 2016 11:43
Count the children from a parent.
<?php
// [[getChildCount? &parent=`[[+id]]`]]
$count = 0;
$parent = isset($parent) ? (integer) $parent : 0;
if ($parent > 0) {
$criteria = array(
'parent' => $parent,
'deleted' => false,
'published' => true,
);
@bwente
bwente / fltr.humanNumber.php
Last active October 19, 2016 11:42
Output an easy-to-read number
<?php
$optionsXpld = @explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = @explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
@bwente
bwente / fltr.number.php
Last active October 19, 2016 11:43
Rounding down feature to output friendlier vague numbers
<?php
$number = floatval($input);
$optionsXpld = @explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = @explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
@bwente
bwente / ContentBlocksDisabler.php
Last active August 29, 2015 14:15
Plugin to turn off ContentBlocks by default
<?php
$eventName = $modx->event->name;
switch($eventName) {
case 'OnDocFormRender':
if ($mode == modSystemEvent::MODE_NEW) {
$resource->setProperty('_isContentBlocks', false, 'contentblocks');
}
break;
}
@bwente
bwente / fltr.published.php
Last active August 29, 2015 14:19
Takes a string of resource ids and filters out the unpublished resources.
<?php
$linkArray = explode(',', $input);
foreach ($linkArray as $key => $resource ) {
$page = $modx->getObject('modResource', $resource);
$published = $page->get('published');
if ($published){
$publishedArray[] = $resource;
}
}
$publishedList = implode(",", $publishedArray);
@bwente
bwente / FormItYearOptions.php
Created June 2, 2016 17:21
Dynamic form selector for years
<?php
//[[!FormItYearOptions? &name=`rentexpireYear` &futureYears=`3` &pastYears=`1` &selectedOffset=`1`]]
$name = $modx->getOption('name',$scriptProperties,'year');
$futureYears = $modx->getOption('futureYears',$scriptProperties,'5');
$pastYears = $modx->getOption('pastYears',$scriptProperties,'1');
$selectedOffset = $modx->getOption('selectedOffset',$scriptProperties,'1');
$reverse = $modx->getOption('reverse',$scriptProperties,'');
$thisYear = date('Y');
@bwente
bwente / SaveSearchHook.php
Created June 2, 2016 17:26
Send search queries to Zapier webhook
<?php
$timestamp = date('m/d/Y h:i:s');
$ip = $_SERVER['HTTP_CLIENT_IP']?:($_SERVER['HTTP_X_FORWARDED_FOR']?:$_SERVER['REMOTE_ADDR']);
// post to webhook url
$url = $scriptProperties['webhook'];
if (isset($url)) {
$postvars = 'timestamp=' . $timestamp . '&ip=' . $ip . '&search=' . $search;
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_POST, 1);