Last active
January 5, 2019 17:43
-
-
Save sepiariver/198b02d44351f129721a to your computer and use it in GitHub Desktop.
Font Awesome Input Options for MODX CMS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* fontAwesomeInputOptions | |
* MODX Snippet | |
* @author YJ Tso @sepiariver | |
* GPL, no warranties, etc. | |
* | |
* Usage: execute in TV input options, preferably with @CHUNK binding | |
* alternatively install as Content Blocks input (link to repo coming soon) | |
*/ | |
// source file | |
$cssUrl = $modx->getOption('cssUrl', $scriptProperties, 'https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'); | |
// scan options | |
$regexPrefix = $modx->getOption('regexPrefix', $scriptProperties, 'fa-'); | |
// label text output options | |
$mode = $modx->getOption('mode', $scriptProperties, 'tv'); // can be 'tv' or 'cb' | |
$titleCaseLabels = $modx->getOption('titleCaseLabels', $scriptProperties, 1); | |
$operator = $modx->getOption('operator', $scriptProperties, ''); | |
if (empty($operator)) { | |
$operator = ($mode === 'cb') ? '=' : '=='; | |
} | |
// value text output options | |
$outputPrefix = $modx->getOption('classPrefix', $scriptProperties, 'fa-'); | |
// list output options | |
$separator = $modx->getOption('separator', $scriptProperties, ''); | |
if (empty($separator)) { | |
$separator = ($mode === 'cb') ? "\n" : '||'; | |
} | |
$excludeClasses = array_filter(array_map('trim', explode(',', $modx->getOption('excludeClasses', $scriptProperties, 'ul,li')))); | |
// check cache | |
$cacheKey = $modx->getOption('cacheKey', $scriptProperties, 'fontawesomecsssource'); | |
$provider = $modx->cacheManager->getCacheProvider('default'); | |
$css = $provider->get($cacheKey); | |
if (!$css) { | |
// get source file | |
$css = file_get_contents($cssUrl); | |
if ($css) { | |
$provider->set($cacheKey, $css, 0); | |
} else { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[fontAwesomeInputOptions] could not get css source!'); | |
return ''; | |
} | |
} | |
// output | |
$output = array(); | |
$regex = "/\." . $regexPrefix . "([\w-]*)/"; | |
if (preg_match_all($regex, $css, $matches)) { | |
$icons = array_diff($matches[1], $excludeClasses); | |
foreach($icons as $icon) { | |
$label = ($titleCaseLabels) ? ucwords(str_replace('-', ' ', $icon)) : $icon; | |
$output[] = $label . $operator . $outputPrefix . $icon; | |
} | |
} | |
return implode($separator, $output); |
ghost
commented
Sep 16, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment