src/Rhyme/WMassArtsHub/Helper/ProfileHelper.php line 200

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\Environment;
  11. use Contao\File;
  12. use Contao\ModuleModel;
  13. use Contao\PageModel;
  14. use Contao\System;
  15. use Contao\StringUtil;
  16. use Contao\Controller;
  17. use Contao\FilesModel;
  18. use Contao\Template;
  19. use Haste\Util\Format;
  20. use MaeEventCategories\MaeEventCatModel;
  21. use Rhyme\WMassArtsHub\Model\Profile\Item as WMassArtsHub_ProfileItemModel;
  22. /**
  23.  * Class ProfileHelper
  24.  * @package Rhyme\WMassArtsHub\Helper
  25.  */
  26. class ProfileHelper extends Controller
  27. {
  28.     /**
  29.      * The detail page ID
  30.      * @var int
  31.      */
  32.     public static $profileDetailPage 159;
  33.     /**
  34.      * The placeholder image
  35.      * @var string
  36.      */
  37.     public static $profilePlaceholderImagePath 'files/assets/img/placeholders/profile-placeholder.jpg';
  38.     /**
  39.      * URL cache array
  40.      * @var array
  41.      */
  42.     private static $arrUrlCache = array();
  43.     /**
  44.      * Returns a frontend URL for a business detail page
  45.      *
  46.      * @param WMassArtsHub_ProfileItemModel  $objProfileItem
  47.      * @param ModuleModel                    $objModule
  48.      * @param boolean                        $blnAbsolute
  49.      *
  50.      * @return string
  51.      */
  52.     public static function generateProfileItemUrlFromModule($objProfileItem$objModule$blnAbsolute=false) {
  53.         $strCacheKey 'id_' $objProfileItem->id . ($blnAbsolute '_absolute' '');
  54.         // Load the URL from cache
  55.         if (isset(self::$arrUrlCache[$strCacheKey]))
  56.         {
  57.             return self::$arrUrlCache[$strCacheKey];
  58.         }
  59.         $jumpTo $objModule !== null && $objModule->jumpTo $objModule->jumpTo : static::$profileDetailPage;
  60.         $objPage PageModel::findByPk($jumpTo);
  61.         if (!$objPage instanceof PageModel)
  62.         {
  63.             self::$arrUrlCache[$strCacheKey] = ampersand(Environment::get('request'));
  64.         }
  65.         else
  66.         {
  67.             $params = (Config::get('useAutoItem') ? '/' '/detail/') . ($objProfileItem->alias ?: $objProfileItem->id);
  68.             self::$arrUrlCache[$strCacheKey] = ampersand($blnAbsolute $objPage->getAbsoluteUrl($params) : $objPage->getFrontendUrl($params));
  69.         }
  70.         return self::$arrUrlCache[$strCacheKey];
  71.     }
  72.     /**
  73.      * Add image methods to a template
  74.      * @param WMassArtsHub_ProfileItemModel $objItem
  75.      * @param mixed $objSource
  76.      * @param Template $objTemplate
  77.      * @param array $arrConfig
  78.      */
  79.     public static function addImageMethodsToTemplate(WMassArtsHub_ProfileItemModel $objItem$objSourceTemplate $objTemplate$arrConfig=[])
  80.     {
  81.         $fnAddImageToTemplate = function($uuid$objItem$objSource$objTemplate$arrConfig=[]) {
  82.             $arrImage = [];
  83.             $blnIsUsingPlaceholder false;
  84.             $container System::getContainer();
  85.             $rootDir $container->getParameter('kernel.project_dir');
  86.             $staticUrl $container->get('contao.assets.files_context')->getStaticUrl();
  87.             $arrSize StringUtil::deserialize(\is_array($arrConfig) && $arrConfig['imgSize'] ? $arrConfig['imgSize'] : ($objSource->imgSize ?: $objSource->size), true);
  88.             $arrSize += array(00'crop');
  89.             $objFileModel FilesModel::findByUuid($uuid);
  90.             //Grab placeholder as backup
  91.             if($objFileModel === null) {
  92.                 $blnIsUsingPlaceholder true;
  93.                 $objFileModel FilesModel::findByPath(static::$profilePlaceholderImagePath);
  94.             }
  95.             if ($objFileModel !== null && \file_exists(TL_ROOT '/' $objFileModel->path))
  96.             {
  97.                 try {
  98.                     $objFile = new File($objFileModel->path);
  99.                 }
  100.                 catch (\Exception $e) {
  101.                     return [];
  102.                 }
  103.                 if ($objFile->isGdImage)
  104.                 {
  105.                     try
  106.                     {
  107.                         $picture $container->get('contao.image.picture_factory')->create($rootDir '/' $objFileModel->path$arrSize);
  108.                         $picture = array
  109.                         (
  110.                             'img' => $picture->getImg($rootDir$staticUrl),
  111.                             'sources' => $picture->getSources($rootDir$staticUrl)
  112.                         );
  113.                         $src $picture['img']['src'];
  114.                         if ($src !== $objFileModel->path)
  115.                         {
  116.                             $objFile = new File(\rawurldecode($src));
  117.                         }
  118.                     }
  119.                     catch (\Exception $e)
  120.                     {
  121.                         System::log('Image "' $objFileModel->path '" could not be processed: ' $e->getMessage(), __METHOD__TL_ERROR);
  122.                         //$src = '';
  123.                         $picture = array('img'=>array('src'=>'''srcset'=>''), 'sources'=>array());
  124.                     }
  125.                     $meta StringUtil::deserialize($objFileModel->metatrue);
  126.                     //Add special classes for tall, wide, and small images
  127.                     $arrClasses = [];
  128.                     $isTallImage false;
  129.                     if($objFile->height/$objFile->width 1.5) {
  130.                         $arrClasses[] = 'tall_image';
  131.                         $isTallImage true;
  132.                     }
  133.                     if($objFile->width/$objFile->height 1.5) {
  134.                         $arrClasses[] = 'wide_image';
  135.                     }
  136.                     if($objFile->width 500 || $objFile->height 500) {
  137.                         $arrClasses[] = 'small_image';
  138.                     }
  139.                     // Add the image
  140.                     $arrImage = array
  141.                     (
  142.                         'isPlaceholder'     => $blnIsUsingPlaceholder,
  143.                         'file'              => $objFile,
  144.                         'fileModel'         => $objFileModel,
  145.                         'model'             => $objItem,
  146.                         'picture'           => $picture,
  147.                         'originalSrc'       => $objFileModel->path,
  148.                         'height'            => $objFile->height,
  149.                         'width'             => $objFile->width,
  150.                         'isTallImage'       => $isTallImage,
  151.                         'additionalClasses' => (!empty($arrClasses) ? ' ' implode(' '$arrClasses) : ''),
  152.                         'meta'              => $meta,
  153.                         'caption'           => $meta['en']['caption'],
  154.                         'alt'               => $meta['en']['alt'],
  155.                         'id'                => 'image-' $objFileModel->id,
  156.                     );
  157.                 }
  158.             }
  159.             return $arrImage;
  160.         };
  161.         $objTemplate->getListerMainImage = function() use ($objItem$objSource$objTemplate$fnAddImageToTemplate$arrConfig)
  162.         {
  163.             return $fnAddImageToTemplate($objItem->main_image$objItem$objSource$objTemplate$arrConfig);
  164.         };
  165.         $objTemplate->getReaderMainImage = function() use ($objItem$objSource$objTemplate$fnAddImageToTemplate$arrConfig)
  166.         {
  167.             return $fnAddImageToTemplate($objItem->main_image$objItem$objSource$objTemplate$arrConfig);
  168.         };
  169.         $objTemplate->getReaderOtherImages = function() use ($objItem$objSource$objTemplate$fnAddImageToTemplate$arrConfig)
  170.         {
  171.             $arrImages=[];
  172.             if($arrConfig['gallery_imgSize']) {
  173.                 $arrConfig['imgSize'] = $arrConfig['gallery_imgSize'];
  174.             }
  175.             foreach (\array_unique(StringUtil::deserialize($objItem->imagestrue)) as $image) {
  176.                 if($image !== $objItem->main_image) {
  177.                     $arrImages[] = $fnAddImageToTemplate($image$objItem$objSource$objTemplate$arrConfig);
  178.                 }
  179.             }
  180.             return $arrImages;
  181.         };
  182.     }
  183.     /**
  184.      * Format an address
  185.      * @param string $street1
  186.      * @param string $street2
  187.      * @param string $city
  188.      * @param string $state
  189.      * @param string $zip
  190.      * @return string
  191.      */
  192.     public static function formatAddress($street1$street2$city$state$zip$blnAddHTML=false)
  193.     {
  194.         $liO = function($prefix) use ($blnAddHTML) {
  195.             $class strlen($prefix) ? StringUtil::generateAlias($prefix) : 'none';
  196.             return ($blnAddHTML '<li class="'.StringUtil::generateAlias($prefix).'"><span class="name">'.$prefix.':</span> <span class="value">' '');
  197.         };
  198.         $liC = ($blnAddHTML '</span></li>' '');
  199.         $hasStreet strlen($street1) > || strlen($street2) > 0;
  200.         $hasCity strlen($city) > 0;
  201.         $hasState strlen($state) > 0;
  202.         $hasCityAndState $hasCity && $hasState;
  203.         $hasCityOrState $hasCity || $hasState;
  204.         $strAddress = ($blnAddHTML '<ul>' '');
  205.         $strAddress .= ($hasStreet $liO('Address') . trim($street1 . (strlen($street1) > 0  ' ' '') . $street2) . ' ' .$liC '');
  206.         $strAddress .= ($liO('City/State') . trim($city . ($hasCityAndState  ', ' '') . $state . ($hasCityOrState ' ' '') .  trim($zip)) .$liC);
  207.         return $strAddress . ($blnAddHTML '</ul>' '');
  208.     }
  209.     /**
  210.      * Format an address
  211.      * @param string $name
  212.      * @param string $title
  213.      * @param string $phone
  214.      * @param string $email
  215.      * @param string $fax
  216.      * @return string
  217.      */
  218.     public static function formatContactInfo($name$title$phone$email$fax$blnAddHTML=false)
  219.     {
  220.         $liO = function($prefix) use ($blnAddHTML) {
  221.             return ($blnAddHTML '<li class="'.StringUtil::generateAlias($prefix).'"><span class="name">'.$prefix.':</span> <span class="value">' '');
  222.         };
  223.         $liC = ($blnAddHTML '</span></li>' '');
  224.         $hasName strlen($name) > 0;
  225.         $hasNameOrTitle $hasName || strlen($title) > 0;
  226.         $hasNameAndTitle $hasName && strlen($title) > 0;
  227.         $strContact = ($blnAddHTML '<ul>' '');
  228.         $strContact .= ($hasNameOrTitle $liO('Name/Title') . trim($name . ($hasNameAndTitle  ', ' '') . $title) .$liC '');
  229.         $strContact .= (strlen($phone) > $liO('Phone') . '<a href="tel:'trim($phone) . '" target="_blank" title="Call this contact">'trim($phone) . '</a>' $liC '');
  230.         $strContact .= (strlen($email) > $liO('Email')  . '<a href="mailto:'trim($email) . '" target="_blank" title="Email this contact">'.  trim($email) . '</a>' .$liC '');
  231.         $strContact .= (strlen($fax) > $liO('Fax') . trim($fax) . $liC '');
  232.         return $strContact . ($blnAddHTML '</ul>' '');
  233.     }
  234.     /**
  235.      * Return the class that details how many info boxes are shown
  236.      * @param array $arrEducation
  237.      * @param array $arrAwards
  238.      * @param array $arrAssociations
  239.      * @param array $arrAdditionalContent
  240.      * @return string
  241.      */
  242.     public static function infoBoxClass($arrEducation$arrAwards$arrAssociations$arrAdditionalContent) {
  243.         $arrOptions = ['none''one''two''three''four''five'];
  244.         $intCount 0;
  245.         $arrAll = [$arrEducation$arrAwards$arrAssociations$arrAdditionalContent];
  246.         foreach($arrAll as $infoBox) {
  247.             if(!empty($infoBox)) $intCount++;
  248.         }
  249.         return 'totalboxes-' $arrOptions[$intCount];
  250.     }
  251.     /**
  252.      * Load profile field options
  253.      * @param $objWidget
  254.      */
  255.     public static function addOptionsToWidget(&$objWidget) {
  256.         $objProfiles WMassArtsHub_ProfileItemModel::findPublished(['order'=>'name ASC']);
  257.         if ($objProfiles !== null)
  258.         {
  259.             $arrOptions = array();
  260.             while ($objProfiles->next())
  261.             {
  262.                 $arrOptions[$objProfiles->current()->id] = array
  263.                 (
  264.                     'value' => $objProfiles->current()->id,
  265.                     'label' => $objProfiles->current()->name,
  266.                     'default' => '',
  267.                     'group' => '',
  268.                 );
  269.             }
  270.             $objWidget->options $arrOptions;
  271.             // Apply the "select2" library
  272.             $GLOBALS['TL_BODY'][] = "<script>
  273. jQuery(document).ready(function(){ jQuery('#ctrl_".$objWidget->id."').select2({placeholder: 'Select associated profiles/organizations'}); });
  274. jQuery(window).on('ajax_change', function(){ jQuery('#ctrl_".$objWidget->id."').select2({placeholder: 'Select associated profiles/organizations'}); });
  275. </script>";
  276.         }
  277.     }
  278.     /**
  279.      * Use output buffer to var dump to a string
  280.      *
  281.      * @param    string
  282.      * @return    string
  283.      */
  284.     public static function varDumpToString($var)
  285.     {
  286.         ob_start();
  287.         var_dump($var);
  288.         $result ob_get_clean();
  289.         return $result;
  290.     }
  291. }