Skip to content

Commit

Permalink
Trigger the onload_callback when featuring news/events (see contao#2532)
Browse files Browse the repository at this point in the history
Description
-----------

| Q                | A
| -----------------| ---
| Fixed issues     | Fixes contao#2485
| Docs PR or issue | -

Same PR as contao#2531 for Contao 4.10 where we also have featured events.

Commits
-------

c3a849c Trigger the onload_callback when featuring news/events
  • Loading branch information
leofeyer committed Dec 1, 2020
1 parent 13c00f1 commit bc8447d
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 4 deletions.
69 changes: 67 additions & 2 deletions calendar-bundle/src/Resources/contao/dca/tl_calendar_events.php
Expand Up @@ -1142,14 +1142,49 @@ public function toggleFeatured($intId, $blnVisible, DataContainer $dc=null)
Input::setGet('id', $intId);
Input::setGet('act', 'feature');

$this->checkPermission();
if ($dc)
{
$dc->id = $intId; // see #8043
}

// Trigger the onload_callback
if (is_array($GLOBALS['TL_DCA']['tl_calendar_events']['config']['onload_callback']))
{
foreach ($GLOBALS['TL_DCA']['tl_calendar_events']['config']['onload_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($dc);
}
elseif (is_callable($callback))
{
$callback($dc);
}
}
}

// Check permissions to feature
if (!$this->User->hasAccess('tl_calendar_events::featured', 'alexf'))
{
throw new AccessDeniedException('Not enough permissions to feature/unfeature event ID ' . $intId . '.');
}

$objRow = $this->Database->prepare("SELECT * FROM tl_calendar_events WHERE id=?")
->limit(1)
->execute($intId);

if ($objRow->numRows < 1)
{
throw new AccessDeniedException('Invalid event ID ' . $intId . '.');
}

// Set the current record
if ($dc)
{
$dc->activeRecord = $objRow;
}

$objVersions = new Versions('tl_calendar_events', $intId);
$objVersions->initialize();

Expand All @@ -1170,11 +1205,41 @@ public function toggleFeatured($intId, $blnVisible, DataContainer $dc=null)
}
}

$time = time();

// Update the database
$this->Database->prepare("UPDATE tl_calendar_events SET tstamp=" . time() . ", featured='" . ($blnVisible ? 1 : '') . "' WHERE id=?")
$this->Database->prepare("UPDATE tl_calendar_events SET tstamp=$time, featured='" . ($blnVisible ? 1 : '') . "' WHERE id=?")
->execute($intId);

if ($dc)
{
$dc->activeRecord->tstamp = $time;
$dc->activeRecord->published = ($blnVisible ? '1' : '');
}

// Trigger the onsubmit_callback
if (is_array($GLOBALS['TL_DCA']['tl_calendar_events']['config']['onsubmit_callback']))
{
foreach ($GLOBALS['TL_DCA']['tl_calendar_events']['config']['onsubmit_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($dc);
}
elseif (is_callable($callback))
{
$callback($dc);
}
}
}

$objVersions->create();

if ($dc)
{
$dc->invalidateCacheTags();
}
}

/**
Expand Down
69 changes: 67 additions & 2 deletions news-bundle/src/Resources/contao/dca/tl_news.php
Expand Up @@ -996,14 +996,49 @@ public function toggleFeatured($intId, $blnVisible, DataContainer $dc=null)
Input::setGet('id', $intId);
Input::setGet('act', 'feature');

$this->checkPermission();
if ($dc)
{
$dc->id = $intId; // see #8043
}

// Trigger the onload_callback
if (is_array($GLOBALS['TL_DCA']['tl_news']['config']['onload_callback']))
{
foreach ($GLOBALS['TL_DCA']['tl_news']['config']['onload_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($dc);
}
elseif (is_callable($callback))
{
$callback($dc);
}
}
}

// Check permissions to feature
if (!$this->User->hasAccess('tl_news::featured', 'alexf'))
{
throw new AccessDeniedException('Not enough permissions to feature/unfeature news item ID ' . $intId . '.');
}

$objRow = $this->Database->prepare("SELECT * FROM tl_news WHERE id=?")
->limit(1)
->execute($intId);

if ($objRow->numRows < 1)
{
throw new AccessDeniedException('Invalid news item ID ' . $intId . '.');
}

// Set the current record
if ($dc)
{
$dc->activeRecord = $objRow;
}

$objVersions = new Versions('tl_news', $intId);
$objVersions->initialize();

Expand All @@ -1024,11 +1059,41 @@ public function toggleFeatured($intId, $blnVisible, DataContainer $dc=null)
}
}

$time = time();

// Update the database
$this->Database->prepare("UPDATE tl_news SET tstamp=" . time() . ", featured='" . ($blnVisible ? 1 : '') . "' WHERE id=?")
$this->Database->prepare("UPDATE tl_news SET tstamp=$time, featured='" . ($blnVisible ? 1 : '') . "' WHERE id=?")
->execute($intId);

if ($dc)
{
$dc->activeRecord->tstamp = $time;
$dc->activeRecord->published = ($blnVisible ? '1' : '');
}

// Trigger the onsubmit_callback
if (is_array($GLOBALS['TL_DCA']['tl_news']['config']['onsubmit_callback']))
{
foreach ($GLOBALS['TL_DCA']['tl_news']['config']['onsubmit_callback'] as $callback)
{
if (is_array($callback))
{
$this->import($callback[0]);
$this->{$callback[0]}->{$callback[1]}($dc);
}
elseif (is_callable($callback))
{
$callback($dc);
}
}
}

$objVersions->create();

if ($dc)
{
$dc->invalidateCacheTags();
}
}

/**
Expand Down

0 comments on commit bc8447d

Please sign in to comment.