Skip to content

Instantly share code, notes, and snippets.

@bwente
bwente / FormItToJSON.php
Created March 12, 2019 03:50
Write MODX FormIt submission to JSON
<?php
// config settings
$server = $hook->formit->config['server'];
$server = (!empty($server)) ? $server : 'https://www.modx.com';
$formname = $hook->formit->config['formname'];
$formname = (!empty($formname)) ? $formname : '';
$resultTpl = $hook->formit->config['resultTpl'];
$toPlaceholder = $hook->formit->config['toPlaceholder'];
@bwente
bwente / svgLoader.php
Last active August 19, 2022 17:11
svgLoader -- MODX snippet to load an SVG file and modify the style.
<?php
$svg_file = $modx->getOption('file',$scriptProperties, '');
$svg_id = $modx->getOption('id',$scriptProperties,'mySVGId');
$svg_class = $modx->getOption('class',$scriptProperties,'mySVGClass');
$svg_name = $modx->getOption('name',$scriptProperties,'SVG');
$svg_height = $modx->getOption('height',$scriptProperties,'440px');
$svg_width = $modx->getOption('width',$scriptProperties,'440px');
$svg_background_color = $modx->getOption('background_color',$scriptProperties,'transparent');
$svg_svgstyle = $modx->getOption('svgstyle',$scriptProperties,'top:2px;position:inherit;');
@bwente
bwente / FormItPostToJSON.php
Created September 28, 2018 14:32
FormIt Post JSON
<?php
// config settings
$server = $hook->formit->config['server'];
$server = (!empty($server)) ? $server : 'http://www.apiwebhook.com/rest/';
$formname = $hook->formit->config['formname'];
$resultTpl = $hook->formit->config['resultTpl'];
$toPlaceholder = $hook->formit->config['toPlaceholder'];
// from system settings
@bwente
bwente / scrapeElement.php
Created August 30, 2018 17:12
Scrape a HTML element from a site and return it.
<?php
$url = $modx->getOption('url',$scriptProperties,'https://www.google.com');
$elementId = $modx->getOption('elementId',$scriptProperties,'hplogo');
$formatted = $modx->getOption('formatted',$scriptProperties,true);
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
@bwente
bwente / months2years.php
Created August 30, 2018 17:09
MODX output filter to convert months to years
<?php
$years = floor($input/12);
$months = $input%12;
$yearsDisplay = $years.($years > 1 ? ' years' : ' year');
if($years == 0) {
$yearsDisplay = '';
}
$monthsDisplay = $months.($months > 1 ? ' months' : ' month');
if($months == 0) {
@bwente
bwente / FormItPDF.php
Created November 27, 2017 15:29
FormItPDF
<?php
$fileIn = $hook->formit->config['fileIn'];
$fileOut = $hook->formit->config['fileOut'];
$fileInPath = $modx->config['base_path'].$fileIn;
$fileOutPath = $modx->config['base_path'].$fileOut;
$formData = $hook->getValues();
// FDF header section
@bwente
bwente / updateSetting.php
Created November 8, 2017 15:58
Snippet to update a MODX system setting
<?php
$setting = $modx->getOption('setting', $scriptProperties);
$value = $modx->getOption('value', $scriptProperties);
$systemSetting = $modx->getObject('modSystemSetting', $setting);
$systemSetting->set('value',$value);
$systemSetting->save();
$modx->reloadConfig();
return true;
@bwente
bwente / NotePad.php
Created November 8, 2017 15:57
MODX snippet to write to a text file.
<?php
$content = $modx->getOption('content', $scriptProperties, 'eof');
$filename = $modx->getOption('filename', $scriptProperties, 'text.txt');
$filedir = $modx->getOption('filedir', $scriptProperties, 'assets');
$append = $modx->getOption('append', $scriptProperties, false);
$fopen = "w";
if ($append > 0){
$fopen = "a";
};
@bwente
bwente / WebHook.php
Created November 8, 2017 15:55
Very basic webhook for MODX, posts JSON to a URL. Works well with Zapier.
<?php
// [[!WebHook? &secret=`dh730>soteW` &runsnippet=`updateSettings` &params=`setting=savings`]]
$mySecret = $modx->getOption('secret', $scriptProperties);
$mySnippet = $modx->getOption('runsnippet', $scriptProperties);
$myParams = $modx->getOption('params', $scriptProperties);
$achunks = array_chunk(preg_split('/(=|,)/', $myParams), 2);
$myParams = array_combine(array_column($achunks, 0), array_column($achunks, 1));
$webhookContent = "";
@bwente
bwente / fmt.stopString.php
Created September 20, 2017 16:14
Truncates string prior to the stop string.
<?php
return substr($input, 0, strpos($input, $options));