<?php
/**
* Copyright (c) 2021 Rhyme Digital LLC (https://rhyme.digital)
*
* @license LGPL-3.0-or-later
*/
namespace Rhyme\WMassArtsHub\Model\Submission;
use Contao\CalendarEventsModel;
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\SubmissionHelper;
/**
* Class Type
* @package Rhyme\WMassArtsHub\Model\Submission
*/
class Funding extends Model implements SubmissionInterface
{
/**
* @var string
*/
protected static $strTable = 'tl_artshub_submission_funding';
/**
* Get the alias key
* @return string
*/
public static function getAliasKey()
{
return 'fundingitem';
}
/**
* @return int|mixed|null
*/
public function getDateSubmitted()
{
return $this->submissionDate;
}
/**
* @return string
*/
public function getClass()
{
return 'funding';
}
/**
* @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->funding_template;
$objTemplate = new FrontendTemplate($strTemplate);
$funding = $this->row();
$objTemplate->setData($funding);
$objTemplate->model = $this;
$objTemplate->short_description = '<p>' . Controller::replaceInsertTags(StringUtil::substr(strip_tags($this->description), 200)) . '</p>';
$objTemplate->description = str_replace('[nbsp]', ' ', Controller::replaceInsertTags($objTemplate->description));
$objTemplate->link = SubmissionHelper::generateSubmissionUrlFromModule($this, $objModule);
$objTemplate->link_absolute = SubmissionHelper::generateSubmissionUrlFromModule($this, $objModule, true);
$objTemplate->getContactInfo = function () { return $this->getFormattedContactInfo(); };
SubmissionHelper::addImageMethodsToTemplate($this, $objModule, $objTemplate);
System::loadLanguageFile($this->getTable());
$objTemplate->requestType = $GLOBALS['TL_LANG'][$t][$objTemplate->funding_type];
return $objTemplate->parse();
}
/**
* Get the formatted contact info
* @return string
*/
public function getFormattedContactInfo() {
$emailLink = '<a href="mailto:' . StringUtil::encodeEmail($this->contact_email) . '" target="_blank">' . StringUtil::encodeEmail($this->contact_email) . '</a>';
$urlLink = '<a href="' . $this->contact_url . '" target="_blank">' . $this->contact_url . '</a>';
$strContact = $this->contact_name ? '<li><span class="label name">Contact: </span><span class="value">' . $this->contact_name . '</span></li>' : '';
$strContact .= $this->contact_phone ? '<li><span class="label phone">Phone: </span><span class="value">' . $this->contact_phone . '</span></li>' : '';
$strContact .= $this->contact_email ? '<li><span class="label email">Email: </span><span class="value">' . $emailLink . '</span></li>' : '';
$strContact .= $this->contact_url ? '<li><span class="label email">Link: </span><span class="value">' . $urlLink . '</span></li>' : '';
return strlen($strContact) ? '<ul>' . $strContact . '</ul>' : '';
}
/**
* @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 Funding|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 Funding|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);
}
}