Skip to content

Commit

Permalink
Fix Contao 5 errors (see #57 )
Browse files Browse the repository at this point in the history
  • Loading branch information
cliffparnitzky committed Jun 20, 2023
1 parent 4924fd5 commit 66ffa4b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
86 changes: 50 additions & 36 deletions src/Resources/contao/forms/FormCalendarField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
/**
* Run in a custom namespace, so the class can be replaced
*/

namespace Hofff\Contao\Calendarfield;

use Contao\CoreBundle\Exception\InternalServerErrorException;
use Contao\Date;
use Contao\FilesModel;
use Contao\FormTextField;
use Contao\FormText;
use Contao\StringUtil;
use Contao\System;
use Contao\Validator;

class FormCalendarField extends FormTextField
class FormCalendarField extends FormText
{
const DATE_FORMAT_PHP = "d-m-Y";

/**
* Template
*
Expand All @@ -30,7 +31,7 @@ class FormCalendarField extends FormTextField
* @var string
*/
protected $strPrefix = 'widget widget-text widget-calendar';

/**
* Always set rgxp to `date`
*
Expand All @@ -50,10 +51,12 @@ public function __construct($arrAttributes = null)
*
* @return string The template markup
*/
public function parse($arrAttributes=null)
public function parse($arrAttributes = null)
{
// do not add in back end
if (TL_MODE == 'BE')
$request = System::getContainer()->get('request_stack')->getCurrentRequest();

if ($request && System::getContainer()->get('contao.routing.scope_matcher')->isBackendRequest($request))
{
return parent::parse($arrAttributes);
}
Expand All @@ -74,29 +77,36 @@ public function parse($arrAttributes=null)
// add the min/max date to the template
switch ($this->dateDirection)
{
case 'ltToday' : $this->maxDate = "new Date().fp_incr(-1)";
break;
case 'leToday' : $this->maxDate = "new Date().fp_incr(0)";
break;
case 'geToday' : $this->minDate = "new Date().fp_incr(0)";
break;
case 'gtToday' : $this->minDate = "new Date().fp_incr(1)";
break;
case 'ownMinMax': $arrMinMax = StringUtil::deserialize($this->dateDirectionMinMax, true);
if (!empty($arrMinMax[0]))
{
$this->minDate = sprintf("new Date().fp_incr(%s)", $arrMinMax[0]);
}
if (!empty($arrMinMax[1]))
{
$this->maxDate = sprintf("new Date().fp_incr(%s)", $arrMinMax[1]);
}
break;
case 'ltToday':
$this->maxDate = "new Date().fp_incr(-1)";
break;
case 'leToday':
$this->maxDate = "new Date().fp_incr(0)";
break;
case 'geToday':
$this->minDate = "new Date().fp_incr(0)";
break;
case 'gtToday':
$this->minDate = "new Date().fp_incr(1)";
break;
case 'ownMinMax':
$arrMinMax = StringUtil::deserialize($this->dateDirectionMinMax, true);
if (!empty($arrMinMax[0]))
{
$this->minDate = sprintf("new Date().fp_incr(%s)", $arrMinMax[0]);
}
if (!empty($arrMinMax[1]))
{
$this->maxDate = sprintf("new Date().fp_incr(%s)", $arrMinMax[1]);
}
break;
}

if ($this->dateImage) {
if ($this->dateImage)
{
$strIcon = '';
if (Validator::isUuid($this->dateImageSRC)) {
if (Validator::isUuid($this->dateImageSRC))
{
$objModel = FilesModel::findByUuid($this->dateImageSRC);

if ($objModel !== null && is_file(System::getContainer()->getParameter('kernel.project_dir') . '/' . $objModel->path))
Expand Down Expand Up @@ -153,7 +163,7 @@ public function validator($varInput)
// Disable regular date validation
$this->rgxp = '';

if (strlen($varInput) && !preg_match('/'. $this->getRegexp($this->dateFormat) .'/i', $varInput))
if (strlen($varInput) && !preg_match('/' . $this->getRegexp($this->dateFormat) . '/i', $varInput))
{
$this->addError(sprintf($GLOBALS['TL_LANG']['ERR']['date'], $objToday->getInputFormat($this->dateFormat)));
}
Expand Down Expand Up @@ -197,14 +207,14 @@ public function validator($varInput)
}
break;
}

//validate disallowed weekdays
$disabledWeekdays = StringUtil::deserialize($this->dateDisabledWeekdays, true);
if (in_array(date("w", $intTstamp), $disabledWeekdays))
{
$this->addError($GLOBALS['TL_LANG']['ERR']['calendarfield_disabled_weekday']);
}

//validate disallowed days
if (in_array(date(static::DATE_FORMAT_PHP, $intTstamp), $this->getActiveDisabledDays(static::DATE_FORMAT_PHP)))
{
Expand Down Expand Up @@ -331,28 +341,32 @@ private function dateformat_PHP_to_jQueryUI($php_format)
);
$jqueryui_format = "";
$escaping = false;
for($i = 0; $i < strlen($php_format); $i++)
for ($i = 0; $i < strlen($php_format); $i++)
{
$char = $php_format[$i];
if($char === '\\') // PHP date format escaping character
if ($char === '\\') // PHP date format escaping character
{
$i++;
if($escaping) $jqueryui_format .= $php_format[$i];
if ($escaping) $jqueryui_format .= $php_format[$i];
else $jqueryui_format .= '\'' . $php_format[$i];
$escaping = true;
}
else
{
if($escaping) { $jqueryui_format .= "'"; $escaping = false; }
if(isset($SYMBOLS_MATCHING[$char]))
if ($escaping)
{
$jqueryui_format .= "'";
$escaping = false;
}
if (isset($SYMBOLS_MATCHING[$char]))
$jqueryui_format .= $SYMBOLS_MATCHING[$char];
else
$jqueryui_format .= $char;
}
}
return $jqueryui_format;
}

private function getActiveDisabledDays($dateFormat)
{
$arrDateDisabledDays = StringUtil::deserialize($this->dateDisabledDays, true);
Expand All @@ -366,4 +380,4 @@ private function getActiveDisabledDays($dateFormat)
}
return $arrDateDisabledDaysActive;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (!empty($this->dateCssTheme)) {
<?php endif; ?>

<div id="ctrl_<?= $this->id ?>_wrapper">
<input type="<?= $this->type ?>" name="<?= $this->name ?>" id="ctrl_<?= $this->id ?>" class="text<?php if ($this->class): ?> <?= $this->class ?><?php endif; ?>" value="<?= specialchars($this->value) ?>"<?= $this->getAttributes() ?> data-input autocomplete="off">
<input type="<?= $this->type ?>" name="<?= $this->name ?>" id="ctrl_<?= $this->id ?>" class="text<?php if ($this->class): ?> <?= $this->class ?><?php endif; ?>" value="<?= $this->value ?>"<?= $this->getAttributes() ?> data-input autocomplete="off">

<?php if($this->dateImage): ?>
<a class="input-button" style="cursor: pointer;" title="<?= $this->buttonText ?>" data-toggle="">
Expand Down

0 comments on commit 66ffa4b

Please sign in to comment.