rate-it/src/Resources/contao/classes/RateItFaq.php

164 lines
5.2 KiB
PHP
Raw Normal View History

2014-10-01 07:19:14 +02:00
<?php
/**
* Contao Open Source CMS
* Copyright (C) 2005-2011 Leo Feyer
*
* Formerly known as TYPOlight Open Source CMS.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright cgo IT, 2013
* @author Carsten Götzinger (info@cgo-it.de)
* @package rateit
* @license GNU/LGPL
* @filesource
*/
namespace cgoIT\rateit;
2018-02-14 08:30:39 +01:00
use cgoIT\rateit\RateItRating;
class RateItFaq extends RateItFrontend {
var $rateItRating;
2014-10-01 07:19:14 +02:00
/**
* Initialize the controller
*/
public function __construct() {
parent::__construct();
2018-02-14 08:30:39 +01:00
$this->rateItRating = new RateItRating();
2014-10-01 07:19:14 +02:00
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
public function getContentElementRateIt($objRow, $strBuffer) {
if ($objRow->type == 'module') {
$objModule = $this->Database->prepare("SELECT * FROM tl_module WHERE id=? AND type IN ('faqpage', 'faqreader')")
->limit(1)
->execute($objRow->module);
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($objModule->numRows == 1) {
$this->faq_categories = deserialize($objModule->faq_categories);
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($objModule->type == 'faqreader') {
$strBuffer = $this->generateForFaqReader($objModule, $strBuffer);
} else {
$strBuffer = $this->generateForFaqPage($objModule, $strBuffer);
}
2018-02-14 08:30:39 +01:00
2018-02-14 16:17:58 +01:00
$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/cgoitrateit/js/onReadyRateIt.js|static';
$GLOBALS['TL_JAVASCRIPT'][] = 'bundles/cgoitrateit/js/rateit.js|static';
$GLOBALS['TL_CSS'][] = 'bundles/cgoitrateit/css/rateit.min.css||static';
2014-10-01 07:19:14 +02:00
switch ($GLOBALS['TL_CONFIG']['rating_type']) {
case 'hearts' :
2018-02-14 16:17:58 +01:00
$GLOBALS['TL_CSS'][] = 'bundles/cgoitrateit/css/heart.min.css||static';
2014-10-01 07:19:14 +02:00
break;
default:
2018-02-14 16:17:58 +01:00
$GLOBALS['TL_CSS'][] = 'bundles/cgoitrateit/css/star.min.css||static';
2014-10-01 07:19:14 +02:00
}
}
}
return $strBuffer;
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
private function generateForFaqPage($objModule, $strBuffer) {
$objFaq = $this->Database
->execute("SELECT *, author AS authorId, (SELECT headline FROM tl_faq_category WHERE tl_faq_category.id=tl_faq.pid) AS category, (SELECT name FROM tl_user WHERE tl_user.id=tl_faq.author) AS author FROM tl_faq WHERE pid IN(" . implode(',', array_map('intval', $this->faq_categories)) . ")" . (!BE_USER_LOGGED_IN ? " AND published=1" : ""));
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($objFaq->numRows < 1) {
return $strBuffer;
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
$htmlBuffer = new \simple_html_dom();
$htmlBuffer->load($strBuffer);
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
$arrFaqs = $objFaq->fetchAllAssoc();
foreach ($arrFaqs as $arrFaq) {
$rating = $this->generateSingle($arrFaq, $strBuffer);
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
$h3 = $htmlBuffer->find('#'.$arrFaq['alias']);
2018-02-14 08:30:39 +01:00
if (is_array($h3) && count($h3) == 1) {
2014-10-01 07:19:14 +02:00
$section = $h3[0]->parent();
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($arrFaq['rateit_position'] == 'before') {
$section->innertext = $rating.$section->innertext;
} else if ($arrFaq['rateit_position'] == 'after') {
$section->innertext = $section->innertext.$rating;
}
}
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
$strBuffer = $htmlBuffer->save();
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
// Aufräumen
$htmlBuffer->clear();
unset($htmlBuffer);
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
return $strBuffer;
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
private function generateForFaqReader($objModule, $strBuffer) {
// Set the item from the auto_item parameter
if ($GLOBALS['TL_CONFIG']['useAutoItem'] && isset($_GET['auto_item'])) {
$this->Input->setGet('items', $this->Input->get('auto_item'));
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
// Do not index or cache the page if no FAQ has been specified
if (!$this->Input->get('items')) {
return $strBuffer;
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
$objFaq = $this->Database->prepare("SELECT *, author AS authorId, (SELECT title FROM tl_faq_category WHERE tl_faq_category.id=tl_faq.pid) AS category, (SELECT name FROM tl_user WHERE tl_user.id=tl_faq.author) AS author FROM tl_faq WHERE pid IN(" . implode(',', array_map('intval', $this->faq_categories)) . ") AND (id=? OR alias=?)" . (!BE_USER_LOGGED_IN ? " AND published=1" : ""))
->limit(1)
->execute((is_numeric($this->Input->get('items')) ? $this->Input->get('items') : 0), $this->Input->get('items'));
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($objFaq->numRows == 1) {
$arrFaq = $objFaq->fetchAssoc();
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
$rating = $this->generateSingle($arrFaq, $strBuffer);
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($arrFaq['rateit_position'] == 'before') {
$strBuffer = $rating.$strBuffer;
} else if ($arrFaq['rateit_position'] == 'after') {
$strBuffer = $strBuffer.$rating;
}
return $strBuffer;
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
private function generateSingle($arrFaq, $strBuffer) {
$rating = '';
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($arrFaq['addRating']) {
$actRecord = $this->Database->prepare("SELECT * FROM tl_rateit_items WHERE rkey=? and typ='faq'")
->execute($arrFaq['id'])
->fetchAssoc();
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
if ($actRecord['active']) {
2018-02-14 08:30:39 +01:00
$this->rateItRating->rkey = $arrFaq['id'];
$this->rateItRating->ratingType = 'faq';
$this->rateItRating->generate();
$rating = $this->rateItRating->output();
2014-10-01 07:19:14 +02:00
}
}
2018-02-14 08:30:39 +01:00
2014-10-01 07:19:14 +02:00
return $rating;
}
}
2018-02-14 08:30:39 +01:00
?>