src/Rhyme/WMassArtsHub/Helper/GeneralHelper.php line 63

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\Database;
  10. use Contao\DataContainer;
  11. use Contao\File;
  12. use Contao\System;
  13. use Contao\Frontend;
  14. use Contao\Controller;
  15. use Contao\FilesModel;
  16. use Haste\Util\Format;
  17. use Rhyme\WMassArtsHub\Model\Submission\Space as ArtsHub_SubmissionSpace;
  18. /**
  19.  * Class GeneralHelper
  20.  * @package Rhyme\WMassArtsHub\Helper
  21.  */
  22. class GeneralHelper extends Controller
  23. {
  24.     /**
  25.      * Auto-generate an alias
  26.      *
  27.      * @param mixed         $aliasReference
  28.      * @param int           $id
  29.      * @param string            $t
  30.      * @return string
  31.      */
  32.     public static function generateAlias($aliasReferenceint $idstring $t): string
  33.     {
  34.         $aliasExists = function (string $alias) use ($id$t): bool
  35.         {
  36.             return Database::getInstance()
  37.                     ->prepare("SELECT id FROM $t WHERE alias=? AND id!=?")
  38.                     ->execute($alias$id)->numRows 0;
  39.         };
  40.         // Generate an alias
  41.         $varValue System::getContainer()->get('contao.slug')->generate($aliasReference, [], $aliasExists);
  42.         if ($aliasExists($varValue))
  43.         {
  44.             $varValue .= '-' $id//Add the ID to the end
  45.         }
  46.         return $varValue;
  47.     }
  48.     /**
  49.      * Return the needed data attributes for a photoswipe image
  50.      */
  51.     public static function getPhotoswipeDataForImage($arrData$lightbox='') {
  52.         $caption strlen($arrData['caption']) > $arrData['caption'] : $arrData['alt'];
  53.         return ' data-photoswipe="image"
  54.                         data-photoswipe-image-id="'.$arrData['id'].'"
  55.                         '. (strlen($lightbox) > ' data-photoswipe-image-gallery="'.$lightbox.'" ' '') . '
  56.                         data-photoswipe-image-size="'.$arrData['width'].'x'.$arrData['height'].'"
  57.                          '. (strlen($caption) > ' data-photoswipe-image-title="'.$caption.'" ' '');
  58.     }
  59.     /**
  60.      * Compile a color value and return a hex or rgba color
  61.      *
  62.      * @param mixed   $color
  63.      * @param boolean $blnWriteToFile
  64.      * @param array   $vars
  65.      *
  66.      * @return string
  67.      */
  68.     public static function compileColor($color$blnWriteToFile=false$vars=array())
  69.     {
  70.         if (!\is_array($color))
  71.         {
  72.             return '#' . static::shortenHexColor($color);
  73.         }
  74.         if (!isset($color[1]) || empty($color[1]))
  75.         {
  76.             return '#' . static::shortenHexColor($color[0]);
  77.         }
  78.         return 'rgba(' implode(',', static::convertHexColor($color[0], $blnWriteToFile$vars)) . ',' . ($color[1] / 100) . ')';
  79.     }
  80.     /**
  81.      * Try to shorten a hex color
  82.      *
  83.      * @param string $color
  84.      *
  85.      * @return string
  86.      */
  87.     public static function shortenHexColor($color)
  88.     {
  89.         if ($color[0] == $color[1] && $color[2] == $color[3] && $color[4] == $color[5])
  90.         {
  91.             return $color[0] . $color[2] . $color[4];
  92.         }
  93.         return $color;
  94.     }
  95.     /**
  96.      * Convert hex colors to rgb
  97.      *
  98.      * @param string  $color
  99.      * @param boolean $blnWriteToFile
  100.      * @param array   $vars
  101.      *
  102.      * @return array
  103.      *
  104.      * @see http://de3.php.net/manual/de/function.hexdec.php#99478
  105.      */
  106.     public static function convertHexColor($color$blnWriteToFile=false$vars=array())
  107.     {
  108.         // Support global variables
  109.         if (strncmp($color'$'1) === 0)
  110.         {
  111.             if (!$blnWriteToFile)
  112.             {
  113.                 return array($color);
  114.             }
  115.             $color str_replace(array_keys($vars), $vars$color);
  116.         }
  117.         $rgb = array();
  118.         // Try to convert using bitwise operation
  119.         if (\strlen($color) == 6)
  120.         {
  121.             $dec hexdec($color);
  122.             $rgb['red'] = 0xFF & ($dec >> 0x10);
  123.             $rgb['green'] = 0xFF & ($dec >> 0x8);
  124.             $rgb['blue'] = 0xFF $dec;
  125.         }
  126.         // Shorthand notation
  127.         elseif (\strlen($color) == 3)
  128.         {
  129.             $rgb['red'] = hexdec(str_repeat(substr($color01), 2));
  130.             $rgb['green'] = hexdec(str_repeat(substr($color11), 2));
  131.             $rgb['blue'] = hexdec(str_repeat(substr($color21), 2));
  132.         }
  133.         return $rgb;
  134.     }
  135.     /**
  136.      * Return a section of a string using a start and end (use "<input" and ">" to get any input elements)
  137.      * @param string
  138.      * @param string
  139.      * @param string
  140.      * @param boolean
  141.      * @param integer
  142.      * @return string
  143.      */
  144.     public static function getSectionOfString($strSubject$strStart$strEnd$blnCaseSensitive=true$intSearchStart=0)
  145.     {
  146.         // First index of start string
  147.         $varStart $blnCaseSensitive strpos($strSubject$strStart$intSearchStart) : stripos($strSubject$strStart$intSearchStart);
  148.         if ($varStart === false)
  149.         {
  150.             return false;
  151.         }
  152.         // First index of end string
  153.         $varEnd $blnCaseSensitive strpos($strSubject$strEnd, ($varStart strlen($strStart))) : stripos($strSubject$strEnd, ($varStart strlen($strStart)));
  154.         if ($varEnd === false)
  155.         {
  156.             return false;
  157.         }
  158.         // Return the string including the start string, end string, and everything in between
  159.         return substr($strSubject$varStart, ($varEnd strlen($strEnd) - $varStart));
  160.     }
  161.     /**
  162.      * Remove sections of a string using a start and end (use "[caption" and "]" to remove any caption blocks)
  163.      * @param string
  164.      * @param string
  165.      * @param string
  166.      * @return string
  167.      */
  168.     public static function replaceSectionsOfString($strSubject$strStart$strEnd$strReplace=''$blnCaseSensitive=true$blnRecursive=true)
  169.     {
  170.         // First index of start string
  171.         $varStart $blnCaseSensitive strpos($strSubject$strStart) : stripos($strSubject$strStart);
  172.         if ($varStart === false)
  173.             return $strSubject;
  174.         // First index of end string
  175.         $varEnd $blnCaseSensitive strpos($strSubject$strEnd$varStart+1) : stripos($strSubject$strEnd$varStart+1);
  176.         // The string including the start string, end string, and everything in between
  177.         $strFound $varEnd === false substr($strSubject$varStart) : substr($strSubject$varStart, ($varEnd strlen($strEnd) - $varStart));
  178.         // The string after the replacement has been made
  179.         $strResult $blnCaseSensitive str_replace($strFound$strReplace$strSubject) : str_ireplace($strFound$strReplace$strSubject);
  180.         // Check for another occurrence of the start string
  181.         $varStart $blnCaseSensitive strpos($strSubject$strStart) : stripos($strSubject$strStart);
  182.         // If this is recursive and there's another occurence of the start string, keep going
  183.         if ($blnRecursive && $varStart !== false)
  184.         {
  185.             $strResult = static::replaceSectionsofString($strResult$strStart$strEnd$strReplace$blnCaseSensitive$blnRecursive);
  186.         }
  187.         return $strResult;
  188.     }
  189.     /**
  190.      * Grab something from cURL
  191.      * @param $url
  192.      * @return bool|string
  193.      */
  194.     public static function curlGet($url) {
  195.         $curl = \curl_init($url);
  196.         \curl_setopt($curlCURLOPT_RETURNTRANSFER1);
  197.         \curl_setopt($curlCURLOPT_TIMEOUT30);
  198.         \curl_setopt($curlCURLOPT_FOLLOWLOCATION1);
  199.         $return = \curl_exec($curl);
  200.         \curl_close($curl);
  201.         return $return;
  202.     }
  203.     /**
  204.      * Use output buffer to var dump to a string
  205.      *
  206.      * @param    string
  207.      * @return    string
  208.      */
  209.     public static function varDumpToString($var)
  210.     {
  211.         ob_start();
  212.         var_dump($var);
  213.         $result ob_get_clean();
  214.         return $result;
  215.     }
  216. }