Skip to content

Instantly share code, notes, and snippets.

View sonicpunk's full-sized avatar

Benjamin Davis sonicpunk

View GitHub Profile
@sonicpunk
sonicpunk / migrateContent.php
Created January 11, 2018 07:15
migrateContent to JSON for importing to another site
<?php
$parents = explode(',',$modx->getOption('parents',$scriptProperties,'11,10,12,13,14'));
$parents = array_unique($parents);
$output = array();
foreach ($parents as $parent) {
$childrenOutput = array();
$children = $modx->getCollection('modResource', array('parent'=>$parent));
@sonicpunk
sonicpunk / rightTemplate.php
Created January 9, 2016 09:32
rightTemplate
/* modx event OnHandleRequest */
<?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)) {
@sonicpunk
sonicpunk / add_mediasourcepath
Created October 20, 2015 08:57
[[-+image:add_mediasourcepath=`2`:phpthumbof=`w=150`]]
<?php
$output = str_replace('./','',$input);
if ($mediasource = $modx->getObject('sources.modMediaSource',$options)){
$output = $mediasource->prepareOutputUrl($output);
}
return '/' . $output;
@sonicpunk
sonicpunk / getImagePath.php
Created October 20, 2015 08:56
getImagePath
<?php
return $options.$input;
@sonicpunk
sonicpunk / migxQuickParser.php
Last active October 20, 2015 08:55
migxQuickParser
<?php
$input = $modx->fromJSON($input);
$i=0;
$output = array();
if (!$input || empty($tpl)) return 'no stuff';
foreach ($input as $row) {
$i++;
$row['idx']=$i;
$output[] = $modx->getChunk($tpl, $row);
}
@sonicpunk
sonicpunk / htmlToBottom
Created January 29, 2014 15:18
htmlToBottom: for use with MODX to place any HTML to the bottom of the page from any chunk in your project
<?php
$content = $modx->getOption('content',$scriptProperties,'');
// For debugging:
$modx->log(modX::LOG_LEVEL_DEBUG
, '[htmlToBottom] called on page '. $modx->resource->id . ' with the following properties: '
.print_r($scriptProperties, true));
// Verify Inputs
if(isset($scriptProperties['content'])){
$modx->regClientHTMLBlock($content);
@sonicpunk
sonicpunk / sendJsBottom
Created December 13, 2013 11:03
Allows you to send a link to your javascript to the bottom of the page. Example call would be [[!sendJsBottom? &link=`/assets/js/scriptname.js`]]. With this snippet you can place it into a chunk that may contain elements for a component.
<?php
$link = $modx->getOption('link',$scriptProperties,'');
// For debugging:
$modx->log(modX::LOG_LEVEL_DEBUG
, '[sendJsBottom] called on page '. $modx->resource->id . ' with the following properties: '
.print_r($scriptProperties, true));
// Verify Inputs
if(isset($scriptProperties['link'])){
$modx->regClientScript($link);
}
@sonicpunk
sonicpunk / contentSplitter
Created November 14, 2013 13:20
MODX Snippet. This divides the [[*content]] into two separate text blocks that are divided by a chunk. You need to assign to it after how many characters you want to insert your chunk. I used it for inserting an image within the content. I found this script online.
<?php
function splitString($string, $amount)
{
$start = 0;
$end = $amount;
while ($end < strlen($string)+$amount) //while $end is less than the length of $string + $amount to make sure it gets it all
{
$chunk = substr($string, $start, $amount); //assign to variable instead of array
$chunk = strrev($chunk); // reverse string created
@sonicpunk
sonicpunk / randomChunk
Created November 14, 2013 13:16
MODX Snippet. It accepts a comma separated list of chunk names and randomly picks one for output. I used it to randomly assign class names to an html element. Example usage: <body class="color-1 h-style-1 text-1 [[!randomChunk? &chunks=`cloud1,cloud2,cloud3,cloud4`]]">
<?php
$output='';
$chunkArray=explode(',',$chunks);
$randomKeys=array_rand($chunkArray);//pick random key
$randomChunkName=$chunkArray[$randomKeys];
$output.=$modx->getChunk($randomChunkName);
return $output;
@sonicpunk
sonicpunk / haveSiblings
Created May 29, 2013 14:17
MODX snippet. It checks if the resource has siblings and outputs two different classes onto an HTML element such as a div
//[[haveSiblings? &classtwo=`no_sub_menu` &classone=`with_sub_menu`]]
<?php
if(!isset($modx)) return '';
/* setup vars */
$exclude = !empty($exclude) ? (is_array($exclude) ? $exclude : explode(',', str_replace(' ', '', $exclude))) : null;
$id = $modx->resource->get('id');
if(!is_null($exclude) && in_array($id, $exclude)) return '';