<?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\Database;
use Contao\DataContainer;
use Contao\File;
use Contao\System;
use Contao\Frontend;
use Contao\Controller;
use Contao\FilesModel;
use Haste\Util\Format;
use Rhyme\WMassArtsHub\Model\Submission\Space as ArtsHub_SubmissionSpace;
/**
* Class GeneralHelper
* @package Rhyme\WMassArtsHub\Helper
*/
class GeneralHelper extends Controller
{
/**
* Auto-generate an alias
*
* @param mixed $aliasReference
* @param int $id
* @param string $t
* @return string
*/
public static function generateAlias($aliasReference, int $id, string $t): string
{
$aliasExists = function (string $alias) use ($id, $t): bool
{
return Database::getInstance()
->prepare("SELECT id FROM $t WHERE alias=? AND id!=?")
->execute($alias, $id)->numRows > 0;
};
// Generate an alias
$varValue = System::getContainer()->get('contao.slug')->generate($aliasReference, [], $aliasExists);
if ($aliasExists($varValue))
{
$varValue .= '-' . $id; //Add the ID to the end
}
return $varValue;
}
/**
* Return the needed data attributes for a photoswipe image
*/
public static function getPhotoswipeDataForImage($arrData, $lightbox='') {
$caption = strlen($arrData['caption']) > 0 ? $arrData['caption'] : $arrData['alt'];
return ' data-photoswipe="image"
data-photoswipe-image-id="'.$arrData['id'].'"
'. (strlen($lightbox) > 0 ? ' data-photoswipe-image-gallery="'.$lightbox.'" ' : '') . '
data-photoswipe-image-size="'.$arrData['width'].'x'.$arrData['height'].'"
'. (strlen($caption) > 0 ? ' data-photoswipe-image-title="'.$caption.'" ' : '');
}
/**
* Compile a color value and return a hex or rgba color
*
* @param mixed $color
* @param boolean $blnWriteToFile
* @param array $vars
*
* @return string
*/
public static function compileColor($color, $blnWriteToFile=false, $vars=array())
{
if (!\is_array($color))
{
return '#' . static::shortenHexColor($color);
}
if (!isset($color[1]) || empty($color[1]))
{
return '#' . static::shortenHexColor($color[0]);
}
return 'rgba(' . implode(',', static::convertHexColor($color[0], $blnWriteToFile, $vars)) . ',' . ($color[1] / 100) . ')';
}
/**
* Try to shorten a hex color
*
* @param string $color
*
* @return string
*/
public static function shortenHexColor($color)
{
if ($color[0] == $color[1] && $color[2] == $color[3] && $color[4] == $color[5])
{
return $color[0] . $color[2] . $color[4];
}
return $color;
}
/**
* Convert hex colors to rgb
*
* @param string $color
* @param boolean $blnWriteToFile
* @param array $vars
*
* @return array
*
* @see http://de3.php.net/manual/de/function.hexdec.php#99478
*/
public static function convertHexColor($color, $blnWriteToFile=false, $vars=array())
{
// Support global variables
if (strncmp($color, '$', 1) === 0)
{
if (!$blnWriteToFile)
{
return array($color);
}
$color = str_replace(array_keys($vars), $vars, $color);
}
$rgb = array();
// Try to convert using bitwise operation
if (\strlen($color) == 6)
{
$dec = hexdec($color);
$rgb['red'] = 0xFF & ($dec >> 0x10);
$rgb['green'] = 0xFF & ($dec >> 0x8);
$rgb['blue'] = 0xFF & $dec;
}
// Shorthand notation
elseif (\strlen($color) == 3)
{
$rgb['red'] = hexdec(str_repeat(substr($color, 0, 1), 2));
$rgb['green'] = hexdec(str_repeat(substr($color, 1, 1), 2));
$rgb['blue'] = hexdec(str_repeat(substr($color, 2, 1), 2));
}
return $rgb;
}
/**
* Return a section of a string using a start and end (use "<input" and ">" to get any input elements)
* @param string
* @param string
* @param string
* @param boolean
* @param integer
* @return string
*/
public static function getSectionOfString($strSubject, $strStart, $strEnd, $blnCaseSensitive=true, $intSearchStart=0)
{
// First index of start string
$varStart = $blnCaseSensitive ? strpos($strSubject, $strStart, $intSearchStart) : stripos($strSubject, $strStart, $intSearchStart);
if ($varStart === false)
{
return false;
}
// First index of end string
$varEnd = $blnCaseSensitive ? strpos($strSubject, $strEnd, ($varStart + strlen($strStart))) : stripos($strSubject, $strEnd, ($varStart + strlen($strStart)));
if ($varEnd === false)
{
return false;
}
// Return the string including the start string, end string, and everything in between
return substr($strSubject, $varStart, ($varEnd + strlen($strEnd) - $varStart));
}
/**
* Remove sections of a string using a start and end (use "[caption" and "]" to remove any caption blocks)
* @param string
* @param string
* @param string
* @return string
*/
public static function replaceSectionsOfString($strSubject, $strStart, $strEnd, $strReplace='', $blnCaseSensitive=true, $blnRecursive=true)
{
// First index of start string
$varStart = $blnCaseSensitive ? strpos($strSubject, $strStart) : stripos($strSubject, $strStart);
if ($varStart === false)
return $strSubject;
// First index of end string
$varEnd = $blnCaseSensitive ? strpos($strSubject, $strEnd, $varStart+1) : stripos($strSubject, $strEnd, $varStart+1);
// The string including the start string, end string, and everything in between
$strFound = $varEnd === false ? substr($strSubject, $varStart) : substr($strSubject, $varStart, ($varEnd + strlen($strEnd) - $varStart));
// The string after the replacement has been made
$strResult = $blnCaseSensitive ? str_replace($strFound, $strReplace, $strSubject) : str_ireplace($strFound, $strReplace, $strSubject);
// Check for another occurrence of the start string
$varStart = $blnCaseSensitive ? strpos($strSubject, $strStart) : stripos($strSubject, $strStart);
// If this is recursive and there's another occurence of the start string, keep going
if ($blnRecursive && $varStart !== false)
{
$strResult = static::replaceSectionsofString($strResult, $strStart, $strEnd, $strReplace, $blnCaseSensitive, $blnRecursive);
}
return $strResult;
}
/**
* Grab something from cURL
* @param $url
* @return bool|string
*/
public static function curlGet($url) {
$curl = \curl_init($url);
\curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
\curl_setopt($curl, CURLOPT_TIMEOUT, 30);
\curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$return = \curl_exec($curl);
\curl_close($curl);
return $return;
}
/**
* Use output buffer to var dump to a string
*
* @param string
* @return string
*/
public static function varDumpToString($var)
{
ob_start();
var_dump($var);
$result = ob_get_clean();
return $result;
}
}