|
<?php |
|
/** |
|
* Plugin: Previewer |
|
* Delivers a preview of the current input. |
|
* It uses the basic saving-process, but the old data is saved into an cachefile and resaved after the preview was regenerated. |
|
*/ |
|
|
|
$event = $modx->event->name; |
|
|
|
// Set cache options |
|
$cacheOptions = array( |
|
xPDO::OPT_CACHE_KEY => '', |
|
xPDO::OPT_CACHE_HANDLER => 'xPDOFileCache', |
|
xPDO::OPT_CACHE_EXPIRES => 0, |
|
); |
|
|
|
switch($event) { |
|
case 'OnDocFormRender': |
|
|
|
$parents = array_reverse($modx->getParentIds($id, 10, array('context' => 'web'))); |
|
if(in_array(33, $parents)) { |
|
$js =<<<JS |
|
<script type="text/javascript"> |
|
Ext.onReady(function() { |
|
Ext.getCmp('modx-abtn-preview').getEl().hide(); |
|
}) |
|
</script> |
|
JS; |
|
$modx->regClientStartupHTMLBlock($js); |
|
return; |
|
} |
|
|
|
// Only when resource exists already! |
|
$has_duplicate = false; |
|
if($mode == 'upd') { |
|
$prevUrl = $modx->makeUrl($id, 'web', array('is_preview' => 1), 'full'); |
|
$allParents = json_encode($parents); |
|
$_POST['id'] = $prev; |
|
$modx_version = $modx->getVersionData(); |
|
$update_processor = $modx_version['major_version'] > 2 ? 'resource/update' : 'update'; |
|
$js =<<<JS |
|
<script type="text/javascript"> |
|
var Previewr = function(config) { |
|
config = config || {}; |
|
Previewr.superclass.constructor.call(this,config); |
|
}; |
|
Ext.extend(Previewr, Ext.Component, { |
|
page:{},window:{},grid:{},tree:{},panel:{},combo:{},config: {} |
|
}); |
|
var Previewr = new Previewr(); |
|
Previewr.config = { |
|
prev_url: '$prevUrl', |
|
all_parents: '$allParents', |
|
update_processor: '$update_processor' |
|
} |
|
</script> |
|
JS; |
|
$modx->regClientStartupHTMLBlock($js); |
|
// Change this to the path to the JS-file in your installation |
|
$modx->regClientStartupScript($modx->getOption('assets_url'). 'js/plugins/previewr.plugin.js'); |
|
} |
|
|
|
$btn_js =<<<BTN_JS |
|
<script type="text/javascript"> |
|
Ext.onReady(function () { |
|
window.setInterval(function() { |
|
// Always enable the save button |
|
if(!Ext.getCmp('modx-abtn-save')) |
|
return; |
|
Ext.getCmp('modx-abtn-save').enable().setDisabled(false); |
|
}, 2000); |
|
}); |
|
</script> |
|
BTN_JS; |
|
$modx->regClientStartupHTMLBlock($btn_js); |
|
break; |
|
|
|
case 'OnBeforeDocFormSave': |
|
|
|
// Check if it's a preview-process |
|
if($_POST['preview_check'] != 1) |
|
return; |
|
|
|
// Get the resource |
|
$resource = $modx->getObject('modResource', $id); |
|
if(!$resource) |
|
return; |
|
|
|
// Add resource to the currently previewed resources |
|
$current_res = $modx->cacheManager->get('preview/current_resources', $cacheOptions); |
|
$current_res['resources'][] = $id; |
|
$modx->cacheManager->set('preview/current_resources', $current_res, 60, $cacheOptions); |
|
|
|
// Get all resource-data |
|
$temp['resource'] = $resource->toArray(); |
|
// Get all TV-data |
|
if ($tvs = $resource->getMany('TemplateVars', 'all')) { |
|
foreach ($tvs as $tv) { |
|
$temp_tvs[] = $tv->toArray(); |
|
} |
|
} |
|
$temp['tvs'] = $temp_tvs; |
|
// Get all resource-groups |
|
$groups = $resource->getMany('ResourceGroupResources'); |
|
foreach($groups as $name => $grpObject) { |
|
$temp_groups[] = $grpObject->toArray(); |
|
} |
|
$temp['groups'] = $temp_groups; |
|
|
|
// Write all data (resource,tv,groups) to a cachefile |
|
$modx->cacheManager->set('preview/'.$id.'.temp', $temp, 0, $cacheOptions); |
|
|
|
// Log preview-process |
|
$modx->logManagerAction('resource_preview', 'modResource', $id); |
|
|
|
return; |
|
break; |
|
|
|
case 'OnDocFormSave': |
|
// Exit if it's a preview process |
|
if($_POST['preview_check'] == 1) |
|
return; |
|
|
|
function generateRandomString($length = 10) { |
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
$randomString = ''; |
|
for ($i = 0; $i < $length; $i++) { |
|
$randomString .= $characters[rand(0, strlen($characters) - 1)]; |
|
} |
|
return $randomString; |
|
} |
|
|
|
if($id) { |
|
$url = $modx->makeUrl($id, 'web', null, 'full'); |
|
//use parameter 'nc' to prevent retrieving a cached resource |
|
$opts = array( |
|
'http'=>array( |
|
'method'=>"POST", |
|
'content'=>http_build_query(array('t'=> generateRandomString())), |
|
'timeout' => 0.1 |
|
) |
|
); |
|
$context = stream_context_create($opts); |
|
$fp = @fopen($url, 'r', false, $context); |
|
} |
|
return; |
|
break; |
|
|
|
case 'OnWebPageComplete': |
|
// Return if it is not a preview-process |
|
if(!$_GET['is_preview']) |
|
return; |
|
|
|
// Get old resource cache |
|
$data = $modx->cacheManager->get('preview/'.$modx->resource->get('id').'.temp', $cacheOptions); |
|
// Remove cache file |
|
$modx->cacheManager->clearCache(array('preview/'), array('objects' => null, 'extensions' => array('.temp.cache.php'))); |
|
|
|
// Set resource data |
|
$modx->resource->fromArray($data['resource']); |
|
// Set TVs |
|
foreach($data['tvs'] as $tv) { |
|
$modx->resource->setTVValue($tv['id'], $tv['value']); |
|
} |
|
// Set resource groups |
|
foreach($data['groups'] as $group) { |
|
$modx->resource->joinGroup($group['document_group']); |
|
} |
|
// Save the resource |
|
$modx->resource->save(); |
|
// Remove the page from current preview-resources |
|
$current_res = $modx->cacheManager->get('preview/current_resources', $cacheOptions); |
|
foreach (array_keys($current_res['resources'], $modx->resource->get('id'), true) as $key) { |
|
unset($current_res['resources'][$key]); |
|
} |
|
$modx->cacheManager->set('preview/current_resources', $current_res, 60, $cacheOptions); |
|
return; |
|
break; |
|
} |
|
return; |
Hi, the domain mentioned above shows a particular type of spicy content. I guess you might want to remove it from readme section.