Created
September 8, 2015 00:43
-
-
Save sepiariver/9b8e70e4d9e9080d47a4 to your computer and use it in GitHub Desktop.
MODX Plugin to convert TV date inputs to unix timestamp. By @theboxer
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 | |
/* | |
* DateToTimestamp Plugin for MODX Revolution | |
* @code @theboxer | |
* @comments @sepiariver | |
* | |
* DateToTimestamp is free software; you can redistribute it and/or modify it under the | |
* terms of the GNU General Public License as published by the Free Software | |
* Foundation; either version 2 of the License, or (at your option) any later | |
* version. | |
* | |
* DateToTimestamp is distributed in the hope that it will be useful, but WITHOUT ANY | |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | |
* A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License along with | |
* DateToTimestamp; if not, write to the Free Software Foundation, Inc., 59 Temple | |
* Place, Suite 330, Boston, MA 02111-1307 USA | |
* | |
*/ | |
// Only for use in the MODX mgr | |
if ($modx->context->get('key') != 'mgr') { | |
return; | |
} | |
// Enable events 'OnResourceTVFormRender' and 'OnDocFormSave' | |
$eventName = $modx->event->name; | |
switch($eventName) { | |
// When rendering, convert timestamps back to dates so standard TV Date Inputs can read it | |
case 'OnResourceTVFormRender': | |
// Must set IDs of TVs on which to operate in the Plugin's Properties | |
$tvs = $modx->getOption('tvs', $scriptProperties, ''); | |
$tvs = explode(',', $tvs); | |
$tvs = array_map('trim', $tvs); | |
$tvs = array_keys(array_flip($tvs)); | |
$tvs = array_filter($tvs); | |
$tvs = array_flip($tvs); | |
// John is awesome | |
foreach ($categories as $idc => $category) { | |
foreach ($category['tvs'] as $idt => $tv) { | |
if ($tv->type != 'date' || !isset($tvs[$tv->id])) { | |
continue; | |
} | |
$timestamp = $categories[$idc]['tvs'][$idt]->processedValue; | |
$badValue = $categories[$idc]['tvs'][$idt]->value; | |
$categories[$idc]['tvs'][$idt]->set('value', strftime('%Y-%m-%d %H:%M:%S', $timestamp)); | |
$categories[$idc]['tvs'][$idt]->set('formElement', str_replace($badValue, $categories[$idc]['tvs'][$idt]->value, $categories[$idc]['tvs'][$idt]->formElement)); | |
} | |
} | |
break; | |
// When saving, convert values to timestamp for sorting and comparing | |
case 'OnDocFormSave': | |
if (intval($resource->tvs) != 1) { | |
break; | |
} | |
// Must set IDs of TVs on which to operate in the Plugin's Properties | |
$tvs = $modx->getOption('tvs', $scriptProperties, ''); | |
$tvs = explode(',', $tvs); | |
$tvs = array_map('trim', $tvs); | |
$tvs = array_keys(array_flip($tvs)); | |
$tvs = array_filter($tvs); | |
foreach ($tvs as $tv) { | |
if ($resource->get('tv' . $tv) == '') { | |
continue; | |
} | |
$tvObject = $modx->getObject('modTemplateVar', $tv); | |
if (!$tvObject || $tvObject->type != 'date' ){ | |
continue; | |
} | |
$value = $modx->getObject('modTemplateVarResource', array('tmplvarid' => $tv, 'contentid' => $resource->id)); | |
if (!$value) { | |
continue; | |
} | |
$value->set('value', strtotime($value->value)); | |
$value->save(); | |
} | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment