Skip to content

Instantly share code, notes, and snippets.

View sepiariver's full-sized avatar

YJ Tso sepiariver

View GitHub Profile
<?php
/**
* getResourcesColumns
* @author @sepiariver
*
* &parent optional ID of Resource from which to fetch children. Defaults to current resource.
* &fields optional Resource field(s) from which to fetch values. Defaults to 'pagetitle,longtitle,introtext,parent,hidemenu'.
* &columns optional Number of columns to sort Resources into and wrap with colWrapper_n;
* &columnDirection optionalVertical or horizontal column sorting. Defaults to horizontal
* &depth optional Depth to seek children via getChildIds(). Defaults to 1
@sepiariver
sepiariver / sample_json_export_config.json
Last active December 21, 2017 12:27
Example of JSON config file consumed by sample_json_export.php script.
[
{
"class": "modSnippet",
"where": [
{
"name:=": "SomeCustomSnippet"
}
],
"toWhere": "create"
},
@sepiariver
sepiariver / sample_json_import.php
Last active April 1, 2019 14:21
Imports a JSON file as output from the sample_json_export.php script.
<?php
// Only run via SSH
if (PHP_SAPI !== 'cli') exit();
// Instantiate MODX
@include(dirname(__FILE__) . '/config.core.php');
if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', dirname(__FILE__) . '/core/');
include_once (MODX_CORE_PATH . "model/modx/modx.class.php");
$modx= new modX();
$modx->initialize('web');
@sepiariver
sepiariver / sample_json_export.php
Last active December 22, 2023 12:26
Example of export script from MODX Resources to JSON, for importing to another site.
<?php
// Only executable via SSH
if (PHP_SAPI !== 'cli') exit();
// Instantiate MODX
@include(dirname(__FILE__) . '/config.core.php');
if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', dirname(__FILE__) . '/core/');
include_once (MODX_CORE_PATH . "model/modx/modx.class.php");
$modx= new modX();
$modx->initialize('web');
@sepiariver
sepiariver / sample_csv_import.php
Last active May 31, 2022 21:03
Consumes CSV as output by the sample_csv_export.php script
<?php
// Only ssh users can execute
if (PHP_SAPI !== 'cli') return;
// Comes in handy sometimes
ini_set('memory_limit', '2048M');
// Instantiate MODX
@include(dirname(__FILE__) . '/config.core.php');
if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', dirname(__FILE__) . '/core/');
include_once (MODX_CORE_PATH . "model/modx/modx.class.php");
$modx= new modX();
@sepiariver
sepiariver / sample_csv_export.php
Last active January 16, 2023 11:53
Example export script to turn MODX Resources into CSV entries for importing in another site.
<?php
// Only run this via SSH
if (PHP_SAPI !== 'cli') return;
// Sometimes helpful if processing lots of Resources
ini_set('memory_limit', '2048M');
// Init @modx
@include(dirname(__FILE__) . '/config.core.php');
if (!defined('MODX_CORE_PATH')) define('MODX_CORE_PATH', dirname(__FILE__) . '/core/');
include_once (MODX_CORE_PATH . "model/modx/modx.class.php");
$modx= new modX();
@sepiariver
sepiariver / web_rules.conf
Created May 19, 2016 23:35
Web Rules (nginx conf) in MODX Cloud for CDN, proxy caching assets, and statcache
# statcache bits
set $cache_prefix 'statcache';
if ($http_user_agent = 'MODX RegenCache') {
set $cache_prefix 'no cache';
}
# CORS for CDN pull zone and expires directives
location ~* \.(?:ico|css|js|jpe?g|png|gif|svg|pdf|mov|mp4|mp3|woff|woff2|ttf|ttc|otf|eot|font.css)$ {
expires 7d;
add_header Access-Control-Allow-Origin "*";
add_header Pragma public;
// Array.diff()
if (typeof Array.prototype.diff === 'undefined') {
Array.prototype.diff = function() {
var result = arguments[0];
delete arguments[0];
if (!(result instanceof Array)) {
throw new TypeError('1st argument passed to Array.diff was not an array');
@sepiariver
sepiariver / imageplusvalue.snippet.php
Last active December 21, 2017 12:31
ImagePlusValue output modifier.
<?php
/**
* imageplusvalue is a Snippet/Output Modifier to parse
* the ImagePlus stored JSON and return values or thumbs.
*
* @author YJ Tso <yj@modx.com>
*
* Example usage:
* <img src="[[*image:imageplusvalue=`croppedImg:w=840&zc=1`]]" alt="[[*image:imageplusvalue=`altTag`]]">
*
@sepiariver
sepiariver / numberformat.snippet.php
Created April 7, 2016 04:25
MODX output modifier to format number using php number_format
<?php
if (empty($input) || !is_numeric($input)) return $input;
$opts = array_filter(array_map('trim', explode(',', $options)));
$args = array();
$args['decimals'] = (isset($opts[0])) ? intval($opts[0]) : 2;
$args['dec_point'] = (isset($opts[1])) ? (string) $opts[1] : '.';
$args['thousands_sep'] = (isset($opts[2])) ? (string) $opts[2] : ',';
return number_format(floatval($input), $args['decimals'], $args['dec_point'], $args['thousands_sep']);