<?php
/**
* Copyright (c) 2021 Rhyme Digital LLC (https://rhyme.digital)
*
* @license LGPL-3.0-or-later
*/
namespace Rhyme\WMassArtsHub\Model\Submission;
use Contao\Controller;
use Contao\Date;
use Contao\FrontendTemplate;
use Contao\Model;
use Contao\Model\Collection;
use Contao\ModuleModel;
use Contao\PageModel;
use Contao\StringUtil;
use Contao\System;
use Rhyme\WMassArtsHub\Helper\ProfileHelper;
use Rhyme\WMassArtsHub\Helper\SubmissionHelper;
/**
* Class Venue
* @package Rhyme\WMassArtsHub\Model\Submission
*/
class Venue extends Model implements SubmissionInterface
{
/**
* @var string
*/
protected static $strTable = 'tl_artshub_submission_venue';
/**
* Get the alias key
* @return string
*/
public static function getAliasKey()
{
return 'venueitem';
}
/**
* @return int|mixed|null
*/
public function getDateSubmitted()
{
return $this->submissionDate;
}
/**
* @return string
*/
public function getClass()
{
return 'venue';
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return string
*/
public function getPageTitle()
{
return $this->title . ' | ArtsHub of Western Mass';
}
/**
* @param ModuleModel $objModule
* @return string|void
*/
public function generate($objModule)
{
$t = $this->getTable();
$strTemplate = $objModule->venue_template;
$objTemplate = new FrontendTemplate($strTemplate);
$venue = $this->row();
$objTemplate->setData($venue);
$objTemplate->model = $this;
$link = $this->venue_url;
//Also add related data if we have it
$relatedProfiles = $this->getRelated('profiles');
if($relatedProfiles !== null) {
$objTemplate->hasProfiles = true;
$objTemplate->profiles = $relatedProfiles;
if($relatedProfiles->count() === 1) {
//If there is one related profile we can use some of its data if it is missing here
$link = $this->venue_url ?: $relatedProfiles->website;
$objTemplate->venue_phone = $this->venue_phone ?: $relatedProfiles->phone;
$objTemplate->venue_email = $this->venue_email ?: $relatedProfiles->email;
}
}
$objTemplate->short_description = '<p>' . Controller::replaceInsertTags(StringUtil::substr(strip_tags(str_replace(['<br>', '<p>', '</p>'], ' ', $this->description)), 200)) . '</p>';
$objTemplate->description = str_replace('[nbsp]', ' ', Controller::replaceInsertTags($this->description));
$objTemplate->link = $link;
$objTemplate->link_absolute = $link;
$objTemplate->getContactInfo = function ($blnIncludeWebsite=false) { return $this->getFormattedContactInfo($blnIncludeWebsite); };
$objTemplate->getAddress = function () { return $this->getAddress(true); };
SubmissionHelper::addImageMethodsToTemplate($this, $objModule, $objTemplate);
System::loadLanguageFile($this->getTable());
$objTemplate->requestType = $GLOBALS['TL_LANG'][$t][$objTemplate->venue_type];
return $objTemplate->parse();
}
/**
* Get the formatted contact info
* @return string
*/
public function getFormattedContactInfo($blnIncludeWebsite=false) {
$emailLink = '<a href="mailto:' . StringUtil::encodeEmail($this->venue_email) . '" target="_blank" rel="noreferrer noopener">' . StringUtil::encodeEmail($this->venue_email) . '</a>';
$urlLink = '<a href="' . $this->venue_url . '" target="_blank" rel="noreferrer noopener">' . $this->venue_url . '</a>';
$strContact = $this->venue_phone ? '<li><span class="label phone">Phone: </span><span class="value">' . $this->venue_phone . '</span></li>' : '';
$strContact .= $this->venue_email ? '<li><span class="label email">Email: </span><span class="value">' . $emailLink . '</span></li>' : '';
if($blnIncludeWebsite) {
$strContact .= $this->venue_url ? '<li><span class="label website">Link: </span><span class="value">' . $urlLink . '</span></li>' : '';
}
return strlen($strContact) ? '<ul>' . $strContact . '</ul>' : '';
}
/**
* Get the address
* @param bool $blnFormatLines
* @return string
*/
public function getAddress($blnFormatLines = false)
{
$street = trim($this->street) === 'private' ? '' : $this->street;
return ProfileHelper::formatAddress($street, '', $this->city, $this->state, $this->postal, $blnFormatLines);
}
/**
* @param PageModel $objPage
* @return string|void
*/
public function generateUrl($objPage){
$objModule = ModuleModel::findOneByType('artshub_submission_combinedlister');
return SubmissionHelper::generateSubmissionUrlFromModule($this, $objModule);
}
/**
* Find all published
* @param array $arrOptions An optional options array
*
* @return Venue|Collection|null The model or null if there is none
*/
public static function findAllPublished(array $arrOptions=array())
{
$t = static::$strTable;
$time = Date::floorToMinute();
$arrColumns = array("($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'" . ($time + 60) . "') AND $t.published='1'");
return static::findBy($arrColumns, array(), $arrOptions);
}
/**
* Find an event post by its ID or alias
*
* @param mixed $varId The numeric ID or alias name
* @param array $arrOptions An optional options array
*
* @return Venue|null The model or null if there is no event
*/
public static function findPublishedByIdOrAlias($varId, array $arrOptions=array())
{
$t = static::$strTable;
$arrColumns = !is_numeric($varId) ? array("$t.alias=?") : array("$t.id=?");
if (!static::isPreviewMode($arrOptions))
{
$time = Date::floorToMinute();
$arrColumns[] = "($t.start='' OR $t.start<='$time') AND ($t.stop='' OR $t.stop>'" . ($time + 60) . "') AND $t.published='1'";
}
return static::findOneBy($arrColumns, $varId, $arrOptions);
}
}