<?php
/**
* Copyright (C) 2021 Rhyme Digital, LLC.
*
* @link https://rhyme.digital
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/
namespace Rhyme\WMassArtsHub\Helper;
use Contao\Config;
use Contao\Controller;
use Contao\Environment;
use Contao\File;
use Contao\FilesModel;
use Contao\ModuleModel;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\System;
use Contao\Template;
use Rhyme\WMassArtsHub\Model\Event;
use Rhyme\WMassArtsHub\Model\Lead;
use Rhyme\WMassArtsHub\Model\LeadData;
use Rhyme\WMassArtsHub\Model\Submission\Funding;
use Rhyme\WMassArtsHub\Model\Submission\Profdev;
use Rhyme\WMassArtsHub\Model\Submission\SubmissionInterface;
use Rhyme\WMassArtsHub\Model\Submission\Space;
use Rhyme\WMassArtsHub\Model\Submission\CallsForArt;
use numero2\OpenGraph3\OpenGraph3;
use Rhyme\WMassArtsHub\Model\Submission\Venue;
/**
* Class SubmissionHelper
* @package Rhyme\WMassArtsHub\Helper
*/
class SubmissionHelper extends Controller
{
/**
* Default jumpto pags for submissions
* @var int
*/
protected static $fundingJumpToDefault = 176;
protected static $spaceJumpToDefault = 186;
protected static $callsforartJumpToDefault = 187;
protected static $profdevJumpToDefault = 188;
protected static $venueJumpToDefault = 192;
/**
* URL cache array
* @var array
*/
private static $arrUrlCache = array();
/**
* Add image methods to a template
* @param SubmissionInterface $objItem
* @param mixed $objSource
* @param Template $objTemplate
* @param array $arrConfig
*/
public static function addImageMethodsToTemplate(SubmissionInterface $objItem, $objSource, Template $objTemplate, $arrConfig=[])
{
$fnAddImageToTemplate = function($uuid, $objItem, $objSource, $objTemplate, $arrConfig=[]) {
$arrImage = [];
$container = System::getContainer();
$rootDir = $container->getParameter('kernel.project_dir');
$staticUrl = $container->get('contao.assets.files_context')->getStaticUrl();
$arrSize = StringUtil::deserialize(\is_array($arrConfig) && $arrConfig['imgSize'] ? $arrConfig['imgSize'] : ($objSource->imgSize ?: $objSource->size), true);
$arrSize += array(0, 0, 'crop');
$objFileModel = FilesModel::findByUuid($uuid);
if ($objFileModel !== null && \file_exists(TL_ROOT . '/' . $objFileModel->path))
{
try {
$objFile = new File($objFileModel->path);
}
catch (\Exception $e) {
return [];
}
if ($objFile->isGdImage)
{
try
{
$picture = $container->get('contao.image.picture_factory')->create($rootDir . '/' . $objFileModel->path, $arrSize);
$picture = array
(
'img' => $picture->getImg($rootDir, $staticUrl),
'sources' => $picture->getSources($rootDir, $staticUrl)
);
$src = $picture['img']['src'];
if ($src !== $objFileModel->path)
{
$objFile = new File(\rawurldecode($src));
}
}
catch (\Exception $e)
{
System::log('Image "' . $objFileModel->path . '" could not be processed: ' . $e->getMessage(), __METHOD__, TL_ERROR);
//$src = '';
$picture = array('img'=>array('src'=>'', 'srcset'=>''), 'sources'=>array());
}
//Add special classes for tall, wide, and small images
$arrClasses = [];
$isTallImage = false;
if($objFile->height/$objFile->width > 1.5) {
$arrClasses[] = 'tall_image';
$isTallImage = true;
}
if($objFile->width/$objFile->height > 1.5) {
$arrClasses[] = 'wide_image';
}
if($objFile->width < 500 || $objFile->height < 500) {
$arrClasses[] = 'small_image';
}
// Add the image
$arrImage = array
(
'file' => $objFile,
'fileModel' => $objFileModel,
'originalSrc' => $objFileModel->path,
'height' => $objFile->height,
'width' => $objFile->width,
'isTallImage' => $isTallImage,
'additionalClasses' => (!empty($arrClasses) ? ' ' . implode(' ', $arrClasses) : ''),
'model' => $objItem,
'picture' => $picture,
'alt' => $objItem->title
);
}
}
return $arrImage;
};
$objTemplate->getListerMainImage = function() use ($objItem, $objSource, $objTemplate, $fnAddImageToTemplate, $arrConfig)
{
//Grab first
$arrImages = StringUtil::deserialize($objItem->images, true);
return $fnAddImageToTemplate($arrImages[0], $objItem, $objSource, $objTemplate, $arrConfig);
};
$objTemplate->getReaderMainImage = function() use ($objItem, $objSource, $objTemplate, $fnAddImageToTemplate, $arrConfig)
{
//Grab first
$arrImages = StringUtil::deserialize($objItem->images, true);
return $fnAddImageToTemplate($arrImages[0], $objItem, $objSource, $objTemplate, $arrConfig);
};
$objTemplate->getReaderOtherImages = function() use ($objItem, $objSource, $objTemplate, $fnAddImageToTemplate, $arrConfig)
{
$arrImages=[];
if($arrConfig['gallery_imgSize']) {
$arrConfig['imgSize'] = $arrConfig['gallery_imgSize'];
}
$arrImages = StringUtil::deserialize($objItem->images, true);
unset($arrImages[0]); //Skip first
foreach (\array_unique($arrImages) as $image) {
$arrImages[] = $fnAddImageToTemplate($image, $objItem, $objSource, $objTemplate, $arrConfig);
}
return $arrImages;
};
}
/**
* Returns a frontend URL for a promotion detail page
*
* @param SubmissionInterface $objSubmission
* @param ModuleModel|null $objModule
* @param boolean $blnAbsolute
* @return string
*/
public static function generateSubmissionUrlFromModule($objSubmission, $objModule=null, $blnAbsolute=false) {
$strCacheKey = 'id_' . $objSubmission::getTable() . '_'. $objSubmission->id . ($blnAbsolute ? '_absolute' : '');
// Load the URL from cache
if (isset(self::$arrUrlCache[$strCacheKey]))
{
return self::$arrUrlCache[$strCacheKey];
}
$jumpTo = 0;
$autoItem = '';
switch($objSubmission::getTable()) {
case Funding::getTable():
$jumpTo = $objModule && $objModule->funding_jumpTo ? $objModule->funding_jumpTo : static::$fundingJumpToDefault;
$autoItem = 'fundingitem';
break;
case Space::getTable():
$jumpTo = $objModule && $objModule->space_jumpTo ? $objModule->space_jumpTo : static::$spaceJumpToDefault;
$autoItem = 'spaceitem';
break;
case CallsForArt::getTable():
$jumpTo = $objModule && $objModule->callsforart_jumpTo ? $objModule->callsforart_jumpTo : static::$callsforartJumpToDefault;
$autoItem = 'callsforartitem';
break;
case Profdev::getTable():
$jumpTo = $objModule && $objModule->profdev_jumpTo ? $objModule->profdev_jumpTo : static::$profdevJumpToDefault;
$autoItem = 'profdevitem';
break;
case Venue::getTable():
$jumpTo = $objModule && $objModule->venue_jumpTo ? $objModule->venue_jumpTo : static::$venueJumpToDefault;
$autoItem = 'venueitem';
break;
}
$objPage = PageModel::findByPk($jumpTo);
if (!$objPage instanceof PageModel)
{
self::$arrUrlCache[$strCacheKey] = ampersand(Environment::get('request'));
}
else
{
$params = (Config::get('useAutoItem') ? '/' : '/'.$autoItem.'/') . ($objSubmission->alias ?: $objSubmission->id);
self::$arrUrlCache[$strCacheKey] = ampersand($blnAbsolute ? $objPage->getAbsoluteUrl($params) : $objPage->getFrontendUrl($params));
}
return self::$arrUrlCache[$strCacheKey];
}
/**
* Turn a lead into a space submission
* @param $leadID
*/
public static function convertLeadToSpace($leadID) {
$leadData = LeadData::findByPid($leadID);
if($leadData !== null) {
$isNewSubmission = false;
$space = Space::findByLead_id($leadID);
if($space === null) {
$space = new Space();
$space->lead_id = $leadID;
$space->tstamp = time();
$space->submissionDate = time();
$space->status = 'approved';
$space->save();
$isNewSubmission = true;
}
while($leadData->next()) {
switch($leadData->current()->name) {
case 'space_available_date':
$space->space_available_date = strtotime($leadData->current()->value);
break;
default :
$space->{$leadData->current()->name} = $leadData->current()->value;
break;
}
}
$space->alias = $isNewSubmission ? GeneralHelper::generateAlias($space->title, $space->id, Space::getTable()) : $space->alias;
$space->published = '1';
$space->save();
}
}
/**
* Turn a lead into a call for art submission
* @param $leadID
*/
public static function convertLeadToCallForArt($leadID) {
$leadData = LeadData::findByPid($leadID);
if($leadData !== null) {
$isNewSubmission = false;
$callforart = CallsForArt::findByLead_id($leadID);
if($callforart === null) {
$callforart = new CallsForArt();
$callforart->lead_id = $leadID;
$callforart->tstamp = time();
$callforart->submissionDate = time();
$callforart->status = 'approved';
$callforart->save();
$isNewSubmission = true;
}
while($leadData->next()) {
switch($leadData->current()->name) {
//Leaving this open in case we need to adjust values
default :
$callforart->{$leadData->current()->name} = $leadData->current()->value;
break;
}
}
$callforart->alias = $isNewSubmission ? GeneralHelper::generateAlias($callforart->title, $callforart->id, CallsForArt::getTable()) : $callforart->alias;
$callforart->published = '1';
$callforart->save();
}
}
/**
* Turn a lead into a funding submission
* @param $leadID
*/
public static function convertLeadToFunding($leadID) {
$leadData = LeadData::findByPid($leadID);
if($leadData !== null) {
$isNewSubmission = false;
$item = Funding::findByLead_id($leadID);
if($item === null) {
$item = new Funding();
$item->lead_id = $leadID;
$item->tstamp = time();
$item->submissionDate = time();
$item->status = 'approved';
$item->save();
$isNewSubmission = true;
}
while($leadData->next()) {
switch($leadData->current()->name) {
default :
$item->{$leadData->current()->name} = $leadData->current()->value;
break;
}
}
$item->alias = $isNewSubmission ? GeneralHelper::generateAlias($item->title, $item->id, Funding::getTable()) : $item->alias;
$item->published = '1';
$item->save();
}
}
/**
* Turn a lead into a profDev submission
* @param $leadID
*/
public static function convertLeadToProfdev($leadID) {
$leadData = LeadData::findByPid($leadID);
if($leadData !== null) {
$isNewSubmission = false;
$item = Profdev::findByLead_id($leadID);
if($item === null) {
$item = new Profdev();
$item->lead_id = $leadID;
$item->tstamp = time();
$item->submissionDate = time();
$item->status = 'approved';
$item->save();
$isNewSubmission = true;
}
while($leadData->next()) {
switch($leadData->current()->name) {
default :
$item->{$leadData->current()->name} = $leadData->current()->value;
break;
}
}
$item->alias = $isNewSubmission ? GeneralHelper::generateAlias($item->title, $item->id, Profdev::getTable()) : $item->alias;
$item->published = '1';
$item->save();
}
}
/**
* Turn a lead into a venue submission
* @param $leadID
*/
public static function convertLeadToVenue($leadID) {
$leadData = LeadData::findByPid($leadID);
if($leadData !== null) {
$isNewSubmission = false;
$item = Venue::findByLead_id($leadID);
if($item === null) {
$item = new Venue();
$item->lead_id = $leadID;
$item->tstamp = time();
$item->submissionDate = time();
$item->status = 'approved';
$item->save();
$isNewSubmission = true;
}
$strDescription = '';
$strAccessibility = '';
$strFeatures = '';
$strSeasons = '';
$strCapacity = '';
$strFee = '';
while($leadData->next()) {
switch($leadData->current()->name) {
case 'description':
$strDescription = $leadData->current()->value;
break;
case 'features':
$strFeatures = $leadData->current()->value;
break;
case 'accessibility':
$strAccessibility = $leadData->current()->value;
break;
case 'seasons':
$strSeasons = $leadData->current()->value;
break;
case 'capacity':
$strCapacity = $leadData->current()->value;
break;
case 'fee_range':
$strFee = $leadData->current()->value;
break;
default :
$item->{$leadData->current()->name} = $leadData->current()->value;
break;
}
}
$strDescription .= strlen($strFeatures) ? '<p><strong>Features:</strong><br> ' . $strFeatures . '</p>' : '';
$strDescription .= strlen($strSeasons) ? '<p><strong>Seasons Active:</strong><br> ' . $strSeasons . '</p>' : '';
$strDescription .= strlen($strAccessibility) ? '<p><strong>Accessibility:</strong><br> ' . $strAccessibility . '</p>' : '';
$strDescription .= strlen($strCapacity) ? '<p><strong>Capacity:</strong> ' . $strCapacity . '</p>' : '';
$strDescription .= strlen($strFee) ? '<p><strong>Fee range:</strong> ' . $strFee . '</p>' : '';
$item->description = $strDescription;
$item->alias = $isNewSubmission ? GeneralHelper::generateAlias($item->title, $item->id, Venue::getTable()) : $item->alias;
$item->published = '1';
$item->save();
}
}
}