From fc7abfbf4af92ac8aa461fbdb292d0095aab4477 Mon Sep 17 00:00:00 2001 From: Hamid Abbaszadeh Date: Wed, 5 Jun 2019 00:20:52 +0430 Subject: [PATCH] Add comments to Products --- src/dca/tl_module.php | 8 ++ src/dca/tl_product_catalog.php | 78 ++++++++++++++++++- .../Frontend/Module/ModuleProductDetail.php | 57 +++++++++++++- .../modules/mod_product_detail.html5 | 10 +++ 4 files changed, 149 insertions(+), 4 deletions(-) diff --git a/src/dca/tl_module.php b/src/dca/tl_module.php index 7b80168..7eda668 100644 --- a/src/dca/tl_module.php +++ b/src/dca/tl_module.php @@ -157,6 +157,14 @@ 'sql' => "varchar(64) NOT NULL default ''" ); +$bundles = Contao\System::getContainer()->getParameter('kernel.bundles'); + +// Add the comments template drop-down menu +if (isset($bundles['ContaoCommentsBundle'])) +{ + $GLOBALS['TL_DCA']['tl_module']['palettes']['product_detail'] = str_replace('{protected_legend:hide}', '{comment_legend:hide},com_template;{protected_legend:hide}', $GLOBALS['TL_DCA']['tl_module']['palettes']['product_detail']); +} + /** diff --git a/src/dca/tl_product_catalog.php b/src/dca/tl_product_catalog.php index 5fda1a1..5c4d15a 100644 --- a/src/dca/tl_product_catalog.php +++ b/src/dca/tl_product_catalog.php @@ -104,14 +104,15 @@ // Palettes 'palettes' => array ( - '__selector__' => array('protected'), - 'default' => '{title_legend},title;{redirect_legend},jumpTo;{protected_legend:hide},protected;' + '__selector__' => array('protected', 'allowComments'), + 'default' => '{title_legend},title;{redirect_legend},jumpTo;{protected_legend:hide},protected;{comments_legend:hide},allowComments' ), // Subpalettes 'subpalettes' => array ( 'protected' => 'groups', + 'allowComments' => 'notify,sortOrder,perPage,moderate,bbcode,requireLogin,disableCaptcha' ), // Fields @@ -161,6 +162,77 @@ 'eval' => array('mandatory'=>true, 'multiple'=>true), 'sql' => "blob NULL", 'relation' => array('type'=>'hasMany', 'load'=>'lazy') + ), + 'allowComments' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['allowComments'], + 'exclude' => true, + 'filter' => true, + 'inputType' => 'checkbox', + 'eval' => array('submitOnChange'=>true), + 'sql' => "char(1) NOT NULL default ''" + ), + 'notify' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['notify'], + 'default' => 'notify_admin', + 'exclude' => true, + 'inputType' => 'select', + 'options' => array('notify_admin', 'notify_author', 'notify_both'), + 'eval' => array('tl_class'=>'w50'), + 'reference' => &$GLOBALS['TL_LANG']['tl_product_catalog'], + 'sql' => "varchar(16) NOT NULL default ''" + ), + 'sortOrder' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['sortOrder'], + 'default' => 'ascending', + 'exclude' => true, + 'inputType' => 'select', + 'options' => array('ascending', 'descending'), + 'reference' => &$GLOBALS['TL_LANG']['MSC'], + 'eval' => array('tl_class'=>'w50 clr'), + 'sql' => "varchar(32) NOT NULL default ''" + ), + 'perPage' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['perPage'], + 'exclude' => true, + 'inputType' => 'text', + 'eval' => array('rgxp'=>'natural', 'tl_class'=>'w50'), + 'sql' => "smallint(5) unsigned NOT NULL default '0'" + ), + 'moderate' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['moderate'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class'=>'w50'), + 'sql' => "char(1) NOT NULL default ''" + ), + 'bbcode' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['bbcode'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class'=>'w50'), + 'sql' => "char(1) NOT NULL default ''" + ), + 'requireLogin' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['requireLogin'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class'=>'w50'), + 'sql' => "char(1) NOT NULL default ''" + ), + 'disableCaptcha' => array + ( + 'label' => &$GLOBALS['TL_LANG']['tl_product_catalog']['disableCaptcha'], + 'exclude' => true, + 'inputType' => 'checkbox', + 'eval' => array('tl_class'=>'w50'), + 'sql' => "char(1) NOT NULL default ''" ) ) ); @@ -179,7 +251,7 @@ public function __construct() } /** - * Check permissions to edit table tl_news_archive + * Check permissions to edit table tl_product_catalog */ public function checkPermission() { diff --git a/src/library/Frontend/Module/ModuleProductDetail.php b/src/library/Frontend/Module/ModuleProductDetail.php index 2b75998..6e019b4 100644 --- a/src/library/Frontend/Module/ModuleProductDetail.php +++ b/src/library/Frontend/Module/ModuleProductDetail.php @@ -138,7 +138,62 @@ protected function compile() $this->Template->relateds = $this->parseRelateds($objProducts); } - } + } + + $bundles = \System::getContainer()->getParameter('kernel.bundles'); + + // HOOK: comments extension required + if ($objProduct->noComments || !isset($bundles['ContaoCommentsBundle'])) + { + $this->Template->allowComments = false; + + return; + } + + /** @var NewsArchiveModel $objCatalog */ + $objCatalog = $objProduct->getRelated('pid'); + $this->Template->allowComments = $objCatalog->allowComments; + + // Comments are not allowed + if (!$objCatalog->allowComments) + { + return; + } + + // Adjust the comments headline level + $intHl = min((int) str_replace('h', '', $this->hl), 5); + $this->Template->hlc = 'h' . ($intHl + 1); + + $this->import(\Comments::class, 'Comments'); + $arrNotifies = array(); + + // Notify the system administrator + if ($objCatalog->notify != 'notify_author') + { + $arrNotifies[] = $GLOBALS['TL_ADMIN_EMAIL']; + } + + // Notify the author + if ($objCatalog->notify != 'notify_admin') + { + /** @var UserModel $objAuthor */ + if (($objAuthor = $objProduct->getRelated('author')) instanceof UserModel && $objAuthor->email != '') + { + $arrNotifies[] = $objAuthor->email; + } + } + + $objConfig = new \stdClass(); + + $objConfig->perPage = $objCatalog->perPage; + $objConfig->order = $objCatalog->sortOrder; + $objConfig->template = $this->com_template; + $objConfig->requireLogin = $objCatalog->requireLogin; + $objConfig->disableCaptcha = $objCatalog->disableCaptcha; + $objConfig->bbcode = $objCatalog->bbcode; + $objConfig->moderate = $objCatalog->moderate; + + $this->Comments->addCommentsToTemplate($this->Template, $objConfig, 'tl_news', $objProduct->id, $arrNotifies); } } diff --git a/src/templates/modules/mod_product_detail.html5 b/src/templates/modules/mod_product_detail.html5 index 7bcfc39..fae1ea2 100644 --- a/src/templates/modules/mod_product_detail.html5 +++ b/src/templates/modules/mod_product_detail.html5 @@ -18,4 +18,14 @@

back; ?>

+ allowComments): ?> +
+ <hlc ?>>hlcText ?>hlc ?>> + comments) ?> + pagination ?> + <hlc ?>>addComment ?>hlc ?>> + insert('mod_comment_form', $this->arrData); ?> +
+ + endblock(); ?>