Last active
December 12, 2018 17:20
-
-
Save jonleverrier/fa50f7adb66be7df9ef715a2b4072b2b to your computer and use it in GitHub Desktop.
getResourceThumbnails snippet for MODX. Searches inside the tv_posttype field and pulls out the images for use in open graph tags. If the resource is a "default" resource, it will display the default Open Graph image from ClientConfig.
This file contains 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 | |
/** | |
* getResourceThumbnails snippet | |
* Searches inside the tv_posttype field, and pulls out images | |
* for use in Open Graph tags. | |
* If the resource is a default resource, it displays the default | |
* Open Graph image from ClientConfig | |
* | |
* | |
* @author Jon Leverrier ([email protected]) | |
* @version 1.0 | |
* @since 10th December 2017 | |
*/ | |
// get current page ID | |
$id = (!empty($id) ? $id : $modx->resource->get('id')); | |
$resId = (!empty($id) ? $id : $modx->resource->get('id')); | |
// fetch some system settings to use to make the url | |
$baseUrl = $modx->getOption('http_host'); | |
$server = $modx->getOption('server_protocol'); | |
// get baseUrl path for media source with ID 2 | |
$mediaSource = $modx->getObject('modMediaSource', 2); | |
$properties = $mediaSource->getProperties(); | |
$mediaSourcePath = $properties['baseUrl']['value']; | |
// name of tv to inspect | |
$tvName = tv_posttype; | |
// post type template vars | |
$tvThumbBlog = tv_pt_blog_thumbnail; | |
$tvThumbNews = tv_pt_news_thumbnail; | |
$tvThumbEvent = tv_pt_event_thumbnail; | |
$tvThumbProject = tv_pt_project_thumbnail; | |
$tvThumbProperty = tv_pt_property_thumbnail; | |
$tvThumbService = tv_pt_service_thumbnail; | |
$tvThumbTeam = tv_pt_team_profilepicture; | |
$tvThumbResource = tv_pt_resource_thumbnail; | |
// imageplus chunk template to use for output | |
$imagePlusTpl = tpl_head_thumbnail; | |
$imagePlusTplPerson = tpl_head_thumbnail_person; | |
// get default open graph image from client config | |
$outputDeafultImg = array(); | |
$getClientConfigImg = $modx->query("SELECT * FROM `modx_clientconfig_setting` WHERE `id` ='4'"); | |
if ($getClientConfigImg) { | |
while ($row = $getClientConfigImg->fetch(PDO::FETCH_ASSOC)) { | |
$placeholders = array_merge($scriptProperties, $row); | |
$outputDeafultImg[] = print_r($placeholders[value], true); | |
} | |
} | |
$getClientConfigImageClean = @implode("\n", $outputDeafultImg); | |
$getClientConfigImageUrl = $server . '://' . $baseUrl . $mediaSourcePath . $getClientConfigImageClean; | |
// get tv label per $resId for the tv defined in $tvName | |
$resources = array(); | |
if ($resId) { | |
$resId = @explode(',', $resId); | |
$resources = $modx->getCollection('modResource', array( | |
'id:IN' => $resId | |
)); | |
} | |
if (empty($resources)) { | |
$modx->log(modX::LOG_LEVEL_ERROR, '[getResourceThumbnails] : $resources is empty'); | |
return; | |
} | |
$tv = $modx->getObject('modTemplateVar', array( | |
'name' => $tvName | |
)); | |
if (empty($tv)) { | |
return; | |
} | |
$outputArray = array(); | |
foreach ($resources as $resource) { | |
$tvValue = $resource->getTVValue($tvName); | |
$inputOptions = $tv->parseInputOptions($tv->processBindings($tv->get('elements'), $resource->get('id'))); | |
$tvOptions = array(); | |
foreach ($inputOptions as $v) { | |
$exp = @explode('==', $v); | |
$text = isset($exp[1]) ? $exp[0] : $exp[0]; | |
$val = isset($exp[1]) ? $exp[1] : $exp[0]; | |
$tvOptions[$val] = $text; | |
} | |
$values = array_map('trim', @explode('||', $tvValue)); | |
foreach ($values as $value) { | |
if (empty($value)) { | |
continue; | |
} | |
if (isset($outputArray[$value])) { | |
continue; | |
} | |
else { | |
$outputArray[$value] = $tvOptions[$value]; | |
} | |
} | |
} | |
$myTvLabel = @implode('\n', $outputArray); | |
// if tv is set to default "please select", do something... | |
if (strpos($myTvLabel, 'Please select...') !== false) { | |
// check to see if the tv value is empty | |
if (empty($getClientConfigImageClean)) { | |
return; | |
} | |
else { | |
return $getClientConfigImageUrl; | |
} | |
} | |
// if tv is set to default "News", do something... | |
if (strpos($myTvLabel, 'News') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbNews, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Blog", do something... | |
if (strpos($myTvLabel, 'Blog') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbBlog, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Event", do something... | |
if (strpos($myTvLabel, 'Event') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbEvent, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Project", do something... | |
if (strpos($myTvLabel, 'Project') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbProject, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Property", do something... | |
if (strpos($myTvLabel, 'Property') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbProperty, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Service", do something... | |
if (strpos($myTvLabel, 'Service') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbService, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Person", do something... | |
if (strpos($myTvLabel, 'Person') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbTeam, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTplPerson | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} | |
// if tv is set to default "Resource", do something... | |
if (strpos($myTvLabel, 'Resource') !== false) { | |
$outputCachedImage = $modx->runSnippet('ImagePlus', array( | |
'tvname' => $tvThumbResource, | |
'docid' => $id, | |
'type' => 'tpl', | |
'tpl' => $imagePlusTpl | |
)); | |
$finalOutput = $server . '://' . $baseUrl . $outputCachedImage; | |
// check to see if the tv value is empty | |
if (empty($outputCachedImage)) { | |
return; | |
} | |
else { | |
return $finalOutput; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment