src/Rhyme/WMassArtsHub/Helper/SubmissionHelper.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (C) 2021 Rhyme Digital, LLC.
  4.  *
  5.  * @link       https://rhyme.digital
  6.  * @license    http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  7.  */
  8. namespace Rhyme\WMassArtsHub\Helper;
  9. use Contao\Config;
  10. use Contao\Controller;
  11. use Contao\Environment;
  12. use Contao\File;
  13. use Contao\FilesModel;
  14. use Contao\ModuleModel;
  15. use Contao\PageModel;
  16. use Contao\StringUtil;
  17. use Contao\System;
  18. use Contao\Template;
  19. use Rhyme\WMassArtsHub\Model\Event;
  20. use Rhyme\WMassArtsHub\Model\Lead;
  21. use Rhyme\WMassArtsHub\Model\LeadData;
  22. use Rhyme\WMassArtsHub\Model\Submission\Funding;
  23. use Rhyme\WMassArtsHub\Model\Submission\Profdev;
  24. use Rhyme\WMassArtsHub\Model\Submission\SubmissionInterface;
  25. use Rhyme\WMassArtsHub\Model\Submission\Space;
  26. use Rhyme\WMassArtsHub\Model\Submission\CallsForArt;
  27. use numero2\OpenGraph3\OpenGraph3;
  28. use Rhyme\WMassArtsHub\Model\Submission\Venue;
  29. /**
  30.  * Class SubmissionHelper
  31.  * @package Rhyme\WMassArtsHub\Helper
  32.  */
  33. class SubmissionHelper extends Controller
  34. {
  35.     /**
  36.      * Default jumpto pags for submissions
  37.      * @var int
  38.      */
  39.     protected static $fundingJumpToDefault 176;
  40.     protected static $spaceJumpToDefault 186;
  41.     protected static $callsforartJumpToDefault 187;
  42.     protected static $profdevJumpToDefault 188;
  43.     protected static $venueJumpToDefault 192;
  44.     /**
  45.      * URL cache array
  46.      * @var array
  47.      */
  48.     private static $arrUrlCache = array();
  49.     /**
  50.      * Add image methods to a template
  51.      * @param SubmissionInterface $objItem
  52.      * @param mixed $objSource
  53.      * @param Template $objTemplate
  54.      * @param array $arrConfig
  55.      */
  56.     public static function addImageMethodsToTemplate(SubmissionInterface $objItem$objSourceTemplate $objTemplate$arrConfig=[])
  57.     {
  58.         $fnAddImageToTemplate = function($uuid$objItem$objSource$objTemplate$arrConfig=[]) {
  59.             $arrImage = [];
  60.             $container System::getContainer();
  61.             $rootDir $container->getParameter('kernel.project_dir');
  62.             $staticUrl $container->get('contao.assets.files_context')->getStaticUrl();
  63.             $arrSize StringUtil::deserialize(\is_array($arrConfig) && $arrConfig['imgSize'] ? $arrConfig['imgSize'] : ($objSource->imgSize ?: $objSource->size), true);
  64.             $arrSize += array(00'crop');
  65.             $objFileModel FilesModel::findByUuid($uuid);
  66.             if ($objFileModel !== null && \file_exists(TL_ROOT '/' $objFileModel->path))
  67.             {
  68.                 try {
  69.                     $objFile = new File($objFileModel->path);
  70.                 }
  71.                 catch (\Exception $e) {
  72.                     return [];
  73.                 }
  74.                 if ($objFile->isGdImage)
  75.                 {
  76.                     try
  77.                     {
  78.                         $picture $container->get('contao.image.picture_factory')->create($rootDir '/' $objFileModel->path$arrSize);
  79.                         $picture = array
  80.                         (
  81.                             'img' => $picture->getImg($rootDir$staticUrl),
  82.                             'sources' => $picture->getSources($rootDir$staticUrl)
  83.                         );
  84.                         $src $picture['img']['src'];
  85.                         if ($src !== $objFileModel->path)
  86.                         {
  87.                             $objFile = new File(\rawurldecode($src));
  88.                         }
  89.                     }
  90.                     catch (\Exception $e)
  91.                     {
  92.                         System::log('Image "' $objFileModel->path '" could not be processed: ' $e->getMessage(), __METHOD__TL_ERROR);
  93.                         //$src = '';
  94.                         $picture = array('img'=>array('src'=>'''srcset'=>''), 'sources'=>array());
  95.                     }
  96.                     //Add special classes for tall, wide, and small images
  97.                     $arrClasses = [];
  98.                     $isTallImage false;
  99.                     if($objFile->height/$objFile->width 1.5) {
  100.                         $arrClasses[] = 'tall_image';
  101.                         $isTallImage true;
  102.                     }
  103.                     if($objFile->width/$objFile->height 1.5) {
  104.                         $arrClasses[] = 'wide_image';
  105.                     }
  106.                     if($objFile->width 500 || $objFile->height 500) {
  107.                         $arrClasses[] = 'small_image';
  108.                     }
  109.                     // Add the image
  110.                     $arrImage = array
  111.                     (
  112.                         'file'              => $objFile,
  113.                         'fileModel'         => $objFileModel,
  114.                         'originalSrc'       => $objFileModel->path,
  115.                         'height'            => $objFile->height,
  116.                         'width'             => $objFile->width,
  117.                         'isTallImage'       => $isTallImage,
  118.                         'additionalClasses' => (!empty($arrClasses) ? ' ' implode(' '$arrClasses) : ''),
  119.                         'model'             => $objItem,
  120.                         'picture'           => $picture,
  121.                         'alt'               => $objItem->title
  122.                     );
  123.                 }
  124.             }
  125.             return $arrImage;
  126.         };
  127.         $objTemplate->getListerMainImage = function() use ($objItem$objSource$objTemplate$fnAddImageToTemplate$arrConfig)
  128.         {
  129.             //Grab first
  130.             $arrImages StringUtil::deserialize($objItem->imagestrue);
  131.             return $fnAddImageToTemplate($arrImages[0], $objItem$objSource$objTemplate$arrConfig);
  132.         };
  133.         $objTemplate->getReaderMainImage = function() use ($objItem$objSource$objTemplate$fnAddImageToTemplate$arrConfig)
  134.         {
  135.             //Grab first
  136.             $arrImages StringUtil::deserialize($objItem->imagestrue);
  137.             return $fnAddImageToTemplate($arrImages[0], $objItem$objSource$objTemplate$arrConfig);
  138.         };
  139.         $objTemplate->getReaderOtherImages = function() use ($objItem$objSource$objTemplate$fnAddImageToTemplate$arrConfig)
  140.         {
  141.             $arrImages=[];
  142.             if($arrConfig['gallery_imgSize']) {
  143.                 $arrConfig['imgSize'] = $arrConfig['gallery_imgSize'];
  144.             }
  145.             $arrImages StringUtil::deserialize($objItem->imagestrue);
  146.             unset($arrImages[0]); //Skip first
  147.             foreach (\array_unique($arrImages) as $image) {
  148.                 $arrImages[] = $fnAddImageToTemplate($image$objItem$objSource$objTemplate$arrConfig);
  149.             }
  150.             return $arrImages;
  151.         };
  152.     }
  153.     /**
  154.      * Returns a frontend URL for a promotion detail page
  155.      *
  156.      * @param SubmissionInterface   $objSubmission
  157.      * @param ModuleModel|null      $objModule
  158.      * @param boolean               $blnAbsolute
  159.      * @return string
  160.      */
  161.     public static function generateSubmissionUrlFromModule($objSubmission$objModule=null$blnAbsolute=false) {
  162.         $strCacheKey 'id_' $objSubmission::getTable() . '_'$objSubmission->id . ($blnAbsolute '_absolute' '');
  163.         // Load the URL from cache
  164.         if (isset(self::$arrUrlCache[$strCacheKey]))
  165.         {
  166.             return self::$arrUrlCache[$strCacheKey];
  167.         }
  168.         $jumpTo 0;
  169.         $autoItem '';
  170.         switch($objSubmission::getTable()) {
  171.             case Funding::getTable():
  172.                 $jumpTo $objModule && $objModule->funding_jumpTo $objModule->funding_jumpTo : static::$fundingJumpToDefault;
  173.                 $autoItem 'fundingitem';
  174.                 break;
  175.             case Space::getTable():
  176.                 $jumpTo $objModule && $objModule->space_jumpTo $objModule->space_jumpTo : static::$spaceJumpToDefault;
  177.                 $autoItem 'spaceitem';
  178.                 break;
  179.             case CallsForArt::getTable():
  180.                 $jumpTo $objModule && $objModule->callsforart_jumpTo $objModule->callsforart_jumpTo : static::$callsforartJumpToDefault;
  181.                 $autoItem 'callsforartitem';
  182.                 break;
  183.             case Profdev::getTable():
  184.                 $jumpTo $objModule && $objModule->profdev_jumpTo $objModule->profdev_jumpTo : static::$profdevJumpToDefault;
  185.                 $autoItem 'profdevitem';
  186.                 break;
  187.             case Venue::getTable():
  188.                 $jumpTo $objModule && $objModule->venue_jumpTo $objModule->venue_jumpTo : static::$venueJumpToDefault;
  189.                 $autoItem 'venueitem';
  190.                 break;
  191.         }
  192.         $objPage PageModel::findByPk($jumpTo);
  193.         if (!$objPage instanceof PageModel)
  194.         {
  195.             self::$arrUrlCache[$strCacheKey] = ampersand(Environment::get('request'));
  196.         }
  197.         else
  198.         {
  199.             $params = (Config::get('useAutoItem') ? '/' '/'.$autoItem.'/') . ($objSubmission->alias ?: $objSubmission->id);
  200.             self::$arrUrlCache[$strCacheKey] = ampersand($blnAbsolute $objPage->getAbsoluteUrl($params) : $objPage->getFrontendUrl($params));
  201.         }
  202.         return self::$arrUrlCache[$strCacheKey];
  203.     }
  204.     /**
  205.      * Turn a lead into a space submission
  206.      * @param $leadID
  207.      */
  208.     public static function convertLeadToSpace($leadID) {
  209.         $leadData LeadData::findByPid($leadID);
  210.         if($leadData !== null) {
  211.             $isNewSubmission false;
  212.             $space Space::findByLead_id($leadID);
  213.             if($space === null) {
  214.                 $space = new Space();
  215.                 $space->lead_id $leadID;
  216.                 $space->tstamp time();
  217.                 $space->submissionDate time();
  218.                 $space->status 'approved';
  219.                 $space->save();
  220.                 $isNewSubmission true;
  221.             }
  222.             while($leadData->next()) {
  223.                 switch($leadData->current()->name) {
  224.                     case 'space_available_date':
  225.                         $space->space_available_date strtotime($leadData->current()->value);
  226.                         break;
  227.                     default :
  228.                         $space->{$leadData->current()->name} = $leadData->current()->value;
  229.                         break;
  230.                 }
  231.             }
  232.             $space->alias $isNewSubmission GeneralHelper::generateAlias($space->title$space->idSpace::getTable()) : $space->alias;
  233.             $space->published '1';
  234.             $space->save();
  235.         }
  236.     }
  237.     /**
  238.      * Turn a lead into a call for art submission
  239.      * @param $leadID
  240.      */
  241.     public static function convertLeadToCallForArt($leadID) {
  242.         $leadData LeadData::findByPid($leadID);
  243.         if($leadData !== null) {
  244.             $isNewSubmission false;
  245.             $callforart CallsForArt::findByLead_id($leadID);
  246.             if($callforart === null) {
  247.                 $callforart = new CallsForArt();
  248.                 $callforart->lead_id $leadID;
  249.                 $callforart->tstamp time();
  250.                 $callforart->submissionDate time();
  251.                 $callforart->status 'approved';
  252.                 $callforart->save();
  253.                 $isNewSubmission true;
  254.             }
  255.             while($leadData->next()) {
  256.                 switch($leadData->current()->name) {
  257.                     //Leaving this open in case we need to adjust values
  258.                     default :
  259.                         $callforart->{$leadData->current()->name} = $leadData->current()->value;
  260.                         break;
  261.                 }
  262.             }
  263.             $callforart->alias $isNewSubmission GeneralHelper::generateAlias($callforart->title$callforart->idCallsForArt::getTable()) : $callforart->alias;
  264.             $callforart->published '1';
  265.             $callforart->save();
  266.         }
  267.     }
  268.     /**
  269.      * Turn a lead into a funding submission
  270.      * @param $leadID
  271.      */
  272.     public static function convertLeadToFunding($leadID) {
  273.         $leadData LeadData::findByPid($leadID);
  274.         if($leadData !== null) {
  275.             $isNewSubmission false;
  276.             $item Funding::findByLead_id($leadID);
  277.             if($item === null) {
  278.                 $item = new Funding();
  279.                 $item->lead_id $leadID;
  280.                 $item->tstamp time();
  281.                 $item->submissionDate time();
  282.                 $item->status 'approved';
  283.                 $item->save();
  284.                 $isNewSubmission true;
  285.             }
  286.             while($leadData->next()) {
  287.                 switch($leadData->current()->name) {
  288.                     default :
  289.                         $item->{$leadData->current()->name} = $leadData->current()->value;
  290.                         break;
  291.                 }
  292.             }
  293.             $item->alias $isNewSubmission GeneralHelper::generateAlias($item->title$item->idFunding::getTable()) : $item->alias;
  294.             $item->published '1';
  295.             $item->save();
  296.         }
  297.     }
  298.     /**
  299.      * Turn a lead into a profDev submission
  300.      * @param $leadID
  301.      */
  302.     public static function convertLeadToProfdev($leadID) {
  303.         $leadData LeadData::findByPid($leadID);
  304.         if($leadData !== null) {
  305.             $isNewSubmission false;
  306.             $item Profdev::findByLead_id($leadID);
  307.             if($item === null) {
  308.                 $item = new Profdev();
  309.                 $item->lead_id $leadID;
  310.                 $item->tstamp time();
  311.                 $item->submissionDate time();
  312.                 $item->status 'approved';
  313.                 $item->save();
  314.                 $isNewSubmission true;
  315.             }
  316.             while($leadData->next()) {
  317.                 switch($leadData->current()->name) {
  318.                     default :
  319.                         $item->{$leadData->current()->name} = $leadData->current()->value;
  320.                         break;
  321.                 }
  322.             }
  323.             $item->alias $isNewSubmission GeneralHelper::generateAlias($item->title$item->idProfdev::getTable()) : $item->alias;
  324.             $item->published '1';
  325.             $item->save();
  326.         }
  327.     }
  328.     /**
  329.      * Turn a lead into a venue submission
  330.      * @param $leadID
  331.      */
  332.     public static function convertLeadToVenue($leadID) {
  333.         $leadData LeadData::findByPid($leadID);
  334.         if($leadData !== null) {
  335.             $isNewSubmission false;
  336.             $item Venue::findByLead_id($leadID);
  337.             if($item === null) {
  338.                 $item = new Venue();
  339.                 $item->lead_id $leadID;
  340.                 $item->tstamp time();
  341.                 $item->submissionDate time();
  342.                 $item->status 'approved';
  343.                 $item->save();
  344.                 $isNewSubmission true;
  345.             }
  346.             $strDescription '';
  347.             $strAccessibility '';
  348.             $strFeatures '';
  349.             $strSeasons '';
  350.             $strCapacity '';
  351.             $strFee '';
  352.             while($leadData->next()) {
  353.                 switch($leadData->current()->name) {
  354.                     case 'description':
  355.                         $strDescription $leadData->current()->value;
  356.                         break;
  357.                     case 'features':
  358.                         $strFeatures $leadData->current()->value;
  359.                         break;
  360.                     case 'accessibility':
  361.                         $strAccessibility $leadData->current()->value;
  362.                         break;
  363.                     case 'seasons':
  364.                         $strSeasons $leadData->current()->value;
  365.                         break;
  366.                     case 'capacity':
  367.                         $strCapacity $leadData->current()->value;
  368.                         break;
  369.                     case 'fee_range':
  370.                         $strFee $leadData->current()->value;
  371.                         break;
  372.                     default :
  373.                         $item->{$leadData->current()->name} = $leadData->current()->value;
  374.                         break;
  375.                 }
  376.             }
  377.             $strDescription .= strlen($strFeatures) ? '<p><strong>Features:</strong><br> ' $strFeatures '</p>' '';
  378.             $strDescription .= strlen($strSeasons) ? '<p><strong>Seasons Active:</strong><br> ' $strSeasons '</p>' '';
  379.             $strDescription .= strlen($strAccessibility) ? '<p><strong>Accessibility:</strong><br> ' $strAccessibility '</p>' '';
  380.             $strDescription .= strlen($strCapacity) ? '<p><strong>Capacity:</strong>  ' $strCapacity '</p>' '';
  381.             $strDescription .= strlen($strFee) ? '<p><strong>Fee range:</strong>  ' $strFee '</p>' '';
  382.             $item->description $strDescription;
  383.             $item->alias $isNewSubmission GeneralHelper::generateAlias($item->title$item->idVenue::getTable()) : $item->alias;
  384.             $item->published '1';
  385.             $item->save();
  386.         }
  387.     }
  388. }