Skip to content

Instantly share code, notes, and snippets.

View sonicpunk's full-sized avatar

Benjamin Davis sonicpunk

View GitHub Profile
@sonicpunk
sonicpunk / ifElseTemplate
Created May 29, 2013 13:16
MODX Snippet. Decide which HTML chunk to output based on what template ID you are looking for.
//Snippet Call [[ifElseTemplate? &ifId=`1` &ifTpl=`kontakt_info` &elseId=`4` &elseTpl=`kontakt_person_info`]]
<?php
$output='';
$template=$modx->resource->get('template');
if($template==$ifId){
$output.=$modx->getChunk($ifTpl);
}else if($template==$elseId){
$output.=$modx->getChunk($elseTpl);
@sonicpunk
sonicpunk / checkTemplate
Created May 29, 2013 13:20
MODX Snippet. According to the given template ID you can include or exclude a specific HTML chunk.
//[[checkTemplate? &templateID=`1` &tpl=`bxsliderjs`]]
<?php
$output='';
$template=$modx->resource->get('template');
if($exclude!=1){
if($template==$templateID){
$output.=$modx->getChunk($tpl);
};
@sonicpunk
sonicpunk / findTemplateID
Created May 29, 2013 13:30
MODX snippet: finds all resources with a specified template and writes a list of ids
<?php
$c = $modx->newQuery('modResource');
$resIds = array();
/* we only want published and undeleted resources */
$c->where(array(
'published' => true,
'deleted' => false,
'template'=>$templateID
));
$resources = $modx->getCollection('modResource',$c);
@sonicpunk
sonicpunk / getLevel
Created May 29, 2013 13:34
MODX snippet:returns the level of a resource
<?php
$id = isset($id) ? $id : $modx->resource->get('id');
$pids = $modx->getParentIds($id, 100, array('context' => 'web'));
return count($pids);
@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 '';
@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 / 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 / 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 / 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 / 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);
}