Skip to content

Commit

Permalink
Support auto-item
Browse files Browse the repository at this point in the history
  • Loading branch information
doishub committed Jul 24, 2023
1 parent 9f7faa7 commit 80dc9b7
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 9 deletions.
5 changes: 5 additions & 0 deletions contao/dca/tl_recommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@
'eval' => ['doNotCopy'=>true, 'maxlength'=>255, 'tl_class'=>'w100 clr'],
'sql' => "varchar(255) NOT NULL default ''"
],
'scope' => [
'inputType' => 'text',
'filter' => true,
'sql' => "varchar(255) NOT NULL default ''"
],
'cssClass' => [
'exclude' => true,
'inputType' => 'text',
Expand Down
8 changes: 7 additions & 1 deletion contao/dca/tl_recommendation_archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
// Palettes
'palettes' => [
'__selector__' => ['protected'],
'default' => '{title_legend},title,jumpTo;{protected_legend:hide},protected'
'default' => '{title_legend},title,jumpTo,useAutoItem;{protected_legend:hide},protected'
],

// Subpalettes
Expand Down Expand Up @@ -127,6 +127,12 @@
'eval' => ['submitOnChange'=>true],
'sql' => "char(1) NOT NULL default ''"
],
'useAutoItem' => [
'exclude' => true,
'filter' => true,
'inputType' => 'checkbox',
'sql' => "char(1) NOT NULL default ''"
],
'groups' => [
'exclude' => true,
'inputType' => 'checkbox',
Expand Down
4 changes: 4 additions & 0 deletions contao/languages/de/tl_recommendation_list.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
<source>Rating descending</source>
<target>Bewertung absteigend</target>
</trans-unit>
<trans-unit id="tl_recommendation_list.recommendation_count">
<source>%s Reviews</source>
<target>%s Bewertungen</target>
</trans-unit>
</body>
</file>
</xliff>
3 changes: 3 additions & 0 deletions contao/languages/en/tl_recommendation_list.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<trans-unit id="tl_recommendation_list.order_rating_desc">
<source>Rating descending</source>
</trans-unit>
<trans-unit id="tl_recommendation_list.recommendation_count">
<source>%s Reviews</source>
</trans-unit>
</body>
</file>
</xliff>
9 changes: 3 additions & 6 deletions contao/modules/ModuleRecommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,12 @@ protected function parseRecommendations(Collection $objRecommendations): array
$count = 0;
$arrRecommendations = [];

while ($objRecommendations->next())
foreach ($objRecommendations as $recommendation)
{
/** @var RecommendationModel $objRecommendation */
$objRecommendation = $objRecommendations->current();

/** @var RecommendationArchiveModel $objRecommendationArchive */
$objRecommendationArchive = $objRecommendation->getRelated('pid');
$objRecommendationArchive = $recommendation->getRelated('pid');

$arrRecommendations[] = $this->parseRecommendation($objRecommendation, $objRecommendationArchive, ((++$count == 1) ? ' first' : '') . (($count == $limit) ? ' last' : '') . ((($count % 2) == 0) ? ' odd' : ' even'), $count);
$arrRecommendations[] = $this->parseRecommendation($recommendation, $objRecommendationArchive, ((++$count == 1) ? ' first' : '') . (($count == $limit) ? ' last' : '') . ((($count % 2) == 0) ? ' odd' : ' even'), $count);
}

return $arrRecommendations;
Expand Down
15 changes: 15 additions & 0 deletions contao/modules/ModuleRecommendationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ protected function compile(): void
return;
}

// Get archive record
$archive = RecommendationArchiveModel::findMultipleByIds($this->recommendation_archives);
$archive = $archive[0] ?? null;

// Form fields
$arrFields = [
'author' => [
Expand Down Expand Up @@ -142,6 +146,16 @@ protected function compile(): void
],
];

// Add scope for auto alias archives
if($archive && $archive->useAutoItem)
{
$arrFields['scope'] = [
'name' => 'scope',
'inputType' => 'hidden',
'value' => Input::get('auto_item')
];
}

// Captcha
if (!$this->recommendation_disableCaptcha)
{
Expand Down Expand Up @@ -266,6 +280,7 @@ protected function compile(): void
'time' => $time,
'text' => $this->convertLineFeeds($strText),
'rating' => $arrWidgets['rating']->value,
'scope' => $arrWidgets['scope']->value ?? '',
'published' => $this->recommendation_moderate ? '' : 1
];

Expand Down
55 changes: 53 additions & 2 deletions contao/modules/ModuleRecommendationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

use Contao\BackendTemplate;
use Contao\Config;
use Contao\Controller;
use Contao\CoreBundle\Exception\PageNotFoundException;
use Contao\Environment;
use Contao\Input;
use Contao\Model\Collection;
use Contao\Pagination;
use Contao\StringUtil;
use Contao\System;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationArchiveModel;
use Oveleon\ContaoRecommendationBundle\Model\RecommendationModel;

/**
Expand Down Expand Up @@ -162,13 +164,34 @@ protected function compile()

$objRecommendations = $this->fetchItems($this->recommendation_archives, $blnFeatured, ($limit ?: 0), $offset, $minRating);

// Add summary details
$this->Template->totalCount = $intTotal;
$this->addSummary($objRecommendations);

// Add recommendations
if ($objRecommendations !== null)
{
$this->Template->recommendations = $this->parseRecommendations($objRecommendations);
}
}

protected function addSummary($objRecommendations): void
{
Controller::loadLanguageFile('tl_recommendation_list');

$ratings = $objRecommendations->fetchEach('rating');
$grouped = \array_count_values($ratings);
$average = 0;

\array_walk($grouped, static function ($count, $number) use (&$average){
$average += ($number * $count);
});

$this->Template->average = $average / $this->Template->totalCount;
$this->Template->averageRound = ceil($this->Template->average);
$this->Template->countLabel = sprintf($GLOBALS['TL_LANG']['tl_recommendation_list']['recommendation_count'], $this->Template->totalCount);
}

/**
* Count the total matching items
*
Expand Down Expand Up @@ -196,7 +219,8 @@ protected function countItems($recommendationArchives, $blnFeatured, $minRating)
}
}

return RecommendationModel::countPublishedByPids($recommendationArchives, $blnFeatured, $minRating);
//return RecommendationModel::countPublishedByPids($recommendationArchives, $blnFeatured, $minRating);
return $this->fetchItems($recommendationArchives, $blnFeatured, 0, 0, $minRating)?->count() ?? 0;
}

/**
Expand Down Expand Up @@ -247,7 +271,34 @@ protected function fetchItems($recommendationArchives, $blnFeatured, $limit, $of
$order .= "$t.date DESC";
}

return RecommendationModel::findPublishedByPids($recommendationArchives, $blnFeatured, $limit, $offset, $minRating, ['order'=>$order]);
// Get archives
$archives = RecommendationArchiveModel::findMultipleByIds($recommendationArchives);
$archives = array_combine(
$archives->fetchEach('id'),
$archives->fetchAll()
);

// Fetch items
$items = [];

// Get auto item
$autoItem = Input::get('auto_item');

foreach (RecommendationModel::findPublishedByPids($recommendationArchives, $blnFeatured, $limit, $offset, $minRating, ['order'=>$order]) ?? [] as $item)
{
if($archives[$item->pid]['useAutoItem'] ?? null)
{
// Add only if the scope match to the current auto_item
if($autoItem === $item->scope)
{
$items[] = $item;
}
}else{
$items[] = $item;
}
}

return new Collection($items, $t);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Model/RecommendationArchiveModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @property string $title
* @property integer $jumpTo
* @property boolean $protected
* @property boolean $useAutoItem
* @property string $groups
*
* @method static RecommendationArchiveModel|null findById($id, array $opt=array())
Expand Down

0 comments on commit 80dc9b7

Please sign in to comment.