Commit 8b0464ec authored by Tino Goratsch's avatar Tino Goratsch

Merge commit '4b3a884c' as 'ACP3/Modules/ACP3/Comments'

parents d9cd854d 4b3a884c
<?php
namespace ACP3\Modules\ACP3\Comments\Controller\Admin;
use ACP3\Core;
use ACP3\Modules\ACP3\Comments;
use ACP3\Modules\ACP3\Emoticons;
use ACP3\Modules\ACP3\System;
/**
* Class Details
* @package ACP3\Modules\ACP3\Comments\Controller\Admin
*/
class Details extends Core\Modules\Controller\Admin
{
/**
* @var \ACP3\Modules\ACP3\Comments\Model
*/
protected $commentsModel;
/**
* @var \ACP3\Modules\ACP3\Comments\Validator
*/
protected $commentsValidator;
/**
* @var \ACP3\Modules\ACP3\System\Model
*/
protected $systemModel;
/**
* @var \ACP3\Core\Helpers\Secure
*/
protected $secureHelper;
/**
* @var \ACP3\Modules\ACP3\Emoticons\Helpers
*/
protected $emoticonsHelpers;
/**
* @param \ACP3\Core\Context\Admin $context
* @param \ACP3\Modules\ACP3\Comments\Model $commentsModel
* @param \ACP3\Modules\ACP3\Comments\Validator $commentsValidator
* @param \ACP3\Modules\ACP3\System\Model $systemModel
* @param \ACP3\Core\Helpers\Secure $secureHelper
*/
public function __construct(
Core\Context\Admin $context,
Comments\Model $commentsModel,
Comments\Validator $commentsValidator,
System\Model $systemModel,
Core\Helpers\Secure $secureHelper)
{
parent::__construct($context);
$this->commentsModel = $commentsModel;
$this->commentsValidator = $commentsValidator;
$this->systemModel = $systemModel;
$this->secureHelper = $secureHelper;
}
/**
* @param \ACP3\Modules\ACP3\Emoticons\Helpers $emoticonsHelpers
*
* @return $this
*/
public function setEmoticonsHelpers(Emoticons\Helpers $emoticonsHelpers)
{
$this->emoticonsHelpers = $emoticonsHelpers;
return $this;
}
public function actionDelete()
{
$items = $this->_deleteItem(null, 'acp/comments');
if ($this->request->action === 'confirmed') {
$bool = false;
// Get the module-ID of the first item
$moduleId = 0;
if (isset($items[0])) {
$comment = $this->commentsModel->getOneById($items[0]);
if (!empty($comment)) {
$moduleId = $comment['module_id'];
}
}
foreach ($items as $item) {
$bool = $this->commentsModel->delete($item);
}
// If there are no comments for the given module, redirect to the general comments admin panel page
if ($this->commentsModel->countAll($moduleId) == 0) {
$this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool !== false ? 'delete_success' : 'delete_error'), 'acp/comments');
} else {
$this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool !== false ? 'delete_success' : 'delete_error'), 'acp/comments/details/index/id_' . $moduleId);
}
} elseif (is_string($items)) {
throw new Core\Exceptions\ResultNotExists();
}
}
public function actionEdit()
{
$comment = $this->commentsModel->getOneById((int)$this->request->id);
if (empty($comment) === false) {
$this->breadcrumb
->append($this->lang->t($comment['module'], $comment['module']), 'acp/comments/details/index/id_' . $comment['module_id'])
->append($this->lang->t('comments', 'admin_details_edit'))
->setTitlePostfix($comment['name']);
if (empty($_POST) === false) {
$this->_editPost($_POST, $comment);
}
if ($this->emoticonsHelpers) {
// Emoticons im Formular anzeigen
$this->view->assign('emoticons', $this->emoticonsHelpers->emoticonsList());
}
$this->view->assign('form', array_merge($comment, $_POST));
$this->view->assign('module_id', (int)$comment['module_id']);
$this->secureHelper->generateFormToken($this->request->query);
} else {
throw new Core\Exceptions\ResultNotExists();
}
}
public function actionIndex()
{
$comments = $this->commentsModel->getAllByModuleInAcp((int)$this->request->id);
if (empty($comments) === false) {
$moduleName = $this->systemModel->getModuleNameById($this->request->id);
//Brotkrümelspur
$this->breadcrumb->append($this->lang->t($moduleName, $moduleName));
$c_comments = count($comments);
if ($c_comments > 0) {
$canDelete = $this->acl->hasPermission('admin/comments/details/delete');
$config = [
'element' => '#acp-table',
'sort_col' => $canDelete === true ? 5 : 4,
'sort_dir' => 'asc',
'hide_col_sort' => $canDelete === true ? 0 : '',
'records_per_page' => $this->auth->entries
];
$this->view->assign('datatable_config', $config);
$settings = $this->config->getSettings('comments');
// Emoticons einbinden
$emoticonsActive = ($settings['emoticons'] == 1 && $this->emoticonsHelpers);
for ($i = 0; $i < $c_comments; ++$i) {
if (!empty($comments[$i]['user_id']) && empty($comments[$i]['name'])) {
$comments[$i]['name'] = $this->lang->t('users', 'deleted_user');
}
if ($emoticonsActive === true) {
$comments[$i]['message'] = $this->emoticonsHelpers->emoticonsReplace($comments[$i]['message']);
}
}
$this->view->assign('comments', $comments);
$this->view->assign('can_delete', $canDelete);
}
}
}
/**
* @param array $formData
* @param array $comment
*/
protected function _editPost(array $formData, array $comment)
{
try {
$this->commentsValidator->validateEdit($formData);
$updateValues = [];
$updateValues['message'] = Core\Functions::strEncode($formData['message']);
if ((empty($comment['user_id']) || $this->get('core.validator.rules.misc')->isNumber($comment['user_id']) === false) && !empty($formData['name'])) {
$updateValues['name'] = Core\Functions::strEncode($formData['name']);
}
$bool = $this->commentsModel->update($updateValues, $this->request->id);
$this->secureHelper->unsetFormToken($this->request->query);
$this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool !== false ? 'edit_success' : 'edit_error'), 'acp/comments/details/index/id_' . $comment['module_id']);
} catch (Core\Exceptions\InvalidFormToken $e) {
$this->redirectMessages()->setMessage(false, $e->getMessage(), 'acp/comments/details/index/id_' . $comment['module_id']);
} catch (Core\Exceptions\ValidationFailed $e) {
$this->view->assign('error_msg', $this->get('core.helpers.alerts')->errorBox($e->getMessage()));
}
}
}
<?php
namespace ACP3\Modules\ACP3\Comments\Controller\Admin;
use ACP3\Core;
use ACP3\Modules\ACP3\Comments;
/**
* Class Index
* @package ACP3\Modules\ACP3\Comments\Controller\Admin
*/
class Index extends Core\Modules\Controller\Admin
{
/**
* @var \ACP3\Core\Date
*/
protected $date;
/**
* @var \ACP3\Modules\ACP3\Comments\Model
*/
protected $commentsModel;
/**
* @var \ACP3\Modules\ACP3\Comments\Validator
*/
protected $commentsValidator;
/**
* @var \ACP3\Core\Helpers\Secure
*/
protected $secureHelper;
/**
* @param \ACP3\Core\Context\Admin $context
* @param \ACP3\Core\Date $date
* @param \ACP3\Modules\ACP3\Comments\Model $commentsModel
* @param \ACP3\Modules\ACP3\Comments\Validator $commentsValidator
* @param \ACP3\Core\Helpers\Secure $secureHelper
*/
public function __construct(
Core\Context\Admin $context,
Core\Date $date,
Comments\Model $commentsModel,
Comments\Validator $commentsValidator,
Core\Helpers\Secure $secureHelper)
{
parent::__construct($context);
$this->date = $date;
$this->commentsModel = $commentsModel;
$this->commentsValidator = $commentsValidator;
$this->secureHelper = $secureHelper;
}
public function actionDelete()
{
$items = $this->_deleteItem();
if ($this->request->action === 'confirmed') {
$bool = false;
foreach ($items as $item) {
$bool = $this->commentsModel->delete($item, 'module_id');
}
$this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool !== false ? 'delete_success' : 'delete_error'));
} elseif (is_string($items)) {
throw new Core\Exceptions\ResultNotExists();
}
}
public function actionIndex()
{
$comments = $this->commentsModel->getCommentsGroupedByModule();
$c_comments = count($comments);
if ($c_comments > 0) {
$canDelete = $this->acl->hasPermission('admin/comments/index/delete');
$config = [
'element' => '#acp-table',
'sort_col' => $canDelete === true ? 1 : 0,
'sort_dir' => 'desc',
'hide_col_sort' => $canDelete === true ? 0 : '',
'records_per_page' => $this->auth->entries
];
$this->view->assign('datatable_config', $config);
for ($i = 0; $i < $c_comments; ++$i) {
$comments[$i]['name'] = $this->lang->t($comments[$i]['module'], $comments[$i]['module']);
}
$this->view->assign('comments', $comments);
$this->view->assign('can_delete', $canDelete);
}
}
public function actionSettings()
{
if (empty($_POST) === false) {
$this->_settingsPost($_POST);
}
$settings = $this->config->getSettings('comments');
$this->view->assign('dateformat', $this->date->dateFormatDropdown($settings['dateformat']));
// Emoticons erlauben
if ($this->modules->isActive('emoticons') === true) {
$lang_allowEmoticons = [$this->lang->t('system', 'yes'), $this->lang->t('system', 'no')];
$this->view->assign('allow_emoticons', $this->get('core.helpers.forms')->selectGenerator('emoticons', [1, 0], $lang_allowEmoticons, $settings['emoticons'], 'checked'));
}
$this->secureHelper->generateFormToken($this->request->query);
}
/**
* @param array $formData
*/
protected function _settingsPost(array $formData)
{
try {
$this->commentsValidator->validateSettings($formData);
$data = [
'dateformat' => Core\Functions::strEncode($formData['dateformat']),
'emoticons' => $formData['emoticons'],
];
$bool = $this->config->setSettings($data, 'comments');
$this->secureHelper->unsetFormToken($this->request->query);
$this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool === true ? 'settings_success' : 'settings_error'));
} catch (Core\Exceptions\InvalidFormToken $e) {
$this->redirectMessages()->setMessage(false, $e->getMessage());
} catch (Core\Exceptions\ValidationFailed $e) {
$this->view->assign('error_msg', $this->get('core.helpers.alerts')->errorBox($e->getMessage()));
}
}
}
<?php
namespace ACP3\Modules\ACP3\Comments\Controller;
use ACP3\Core;
use ACP3\Modules\ACP3\Captcha;
use ACP3\Modules\ACP3\Comments;
use ACP3\Modules\ACP3\Emoticons;
/**
* Class Index
* @package ACP3\Modules\ACP3\Comments\Controller
*/
class Index extends Core\Modules\Controller\Frontend
{
/**
* @var \ACP3\Core\Date
*/
protected $date;
/**
* @var \ACP3\Core\Pagination
*/
protected $pagination;
/**
* @var \ACP3\Core\Helpers\Secure
*/
protected $secureHelper;
/**
* @var \ACP3\Modules\ACP3\Comments\Model
*/
protected $commentsModel;
/**
* @var \ACP3\Modules\ACP3\Comments\Validator
*/
protected $commentsValidator;
/**
* @var \ACP3\Modules\ACP3\Emoticons\Helpers
*/
protected $emoticonsHelpers;
/**
* @var \ACP3\Modules\ACP3\Captcha\Helpers
*/
protected $captchaHelpers;
/**
* @var string
*/
protected $module;
/**
* @var int
*/
protected $entryId;
/**
* @var bool
*/
protected $emoticonsActive;
/**
* @var array
*/
protected $commentsSettings;
/**
* @param \ACP3\Core\Context\Frontend $context
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Pagination $pagination
* @param \ACP3\Modules\ACP3\Comments\Model $commentsModel
* @param \ACP3\Modules\ACP3\Comments\Validator $commentsValidator
* @param \ACP3\Core\Helpers\Secure $secureHelper
*/
public function __construct(
Core\Context\Frontend $context,
Core\Date $date,
Core\Pagination $pagination,
Comments\Model $commentsModel,
Comments\Validator $commentsValidator,
Core\Helpers\Secure $secureHelper)
{
parent::__construct($context);
$this->date = $date;
$this->pagination = $pagination;
$this->commentsModel = $commentsModel;
$this->commentsValidator = $commentsValidator;
$this->secureHelper = $secureHelper;
}
public function preDispatch()
{
parent::preDispatch();
$this->commentsSettings = $this->config->getSettings('comments');
$this->emoticonsActive = ($this->commentsSettings['emoticons'] == 1);
}
/**
* @param \ACP3\Modules\ACP3\Emoticons\Helpers $emoticonsHelpers
*
* @return $this
*/
public function setEmoticonsHelpers(Emoticons\Helpers $emoticonsHelpers)
{
$this->emoticonsHelpers = $emoticonsHelpers;
return $this;
}
/**
* @param \ACP3\Modules\ACP3\Captcha\Helpers $captchaHelpers
*
* @return $this
*/
public function setCaptchaHelpers(Captcha\Helpers $captchaHelpers)
{
$this->captchaHelpers = $captchaHelpers;
return $this;
}
/**
* @param $module
*
* @return $this
*/
public function setModule($module)
{
$this->module = $module;
return $this;
}
/**
* @param $entryId
*
* @return $this
*/
public function setEntryId($entryId)
{
$this->entryId = $entryId;
return $this;
}
public function actionIndex()
{
// Auflistung der Kommentare
$comments = $this->commentsModel->getAllByModule($this->modules->getModuleId($this->module), $this->entryId, POS, $this->auth->entries);
$c_comments = count($comments);
if ($c_comments > 0) {
$this->pagination->setTotalResults($this->commentsModel->countAllByModule($this->modules->getModuleId($this->module), $this->entryId));
$this->pagination->display();
for ($i = 0; $i < $c_comments; ++$i) {
if (empty($comments[$i]['user_name']) && empty($comments[$i]['name'])) {
$comments[$i]['name'] = $this->lang->t('users', 'deleted_user');
$comments[$i]['user_id'] = 0;
}
$comments[$i]['name'] = !empty($comments[$i]['user_name']) ? $comments[$i]['user_name'] : $comments[$i]['name'];
if ($this->emoticonsActive === true && $this->emoticonsHelpers) {
$comments[$i]['message'] = $this->emoticonsHelpers->emoticonsReplace($comments[$i]['message']);
}
}
$this->view->assign('comments', $comments);
$this->view->assign('dateformat', $this->commentsSettings['dateformat']);
}
if ($this->acl->hasPermission('frontend/comments/index/create') === true) {
$this->view->assign('comments_create_form', $this->actionCreate());
}
return $this->view->fetchTemplate('Comments/Frontend/index.index.tpl');
}
public function actionCreate()
{
if (empty($_POST) === false) {
$this->_createPost($_POST);
}
// Emoticons einbinden, falls diese aktiv sind
if ($this->emoticonsActive === true && $this->emoticonsHelpers) {
// Emoticons im Formular anzeigen
$this->view->assign('emoticons', $this->emoticonsHelpers->emoticonsList());
}
$defaults = [
'name' => '',
'name_disabled' => '',
'message' => ''
];
// Falls Benutzer eingeloggt ist, Formular schon teilweise ausfüllen
if ($this->auth->isUser() === true) {
$user = $this->auth->getUserInfo();
$disabled = ' readonly="readonly"';
$defaults['name'] = $user['nickname'];
$defaults['name_disabled'] = $disabled;
$defaults['message'] = '';
}
$this->view->assign('form', array_merge($defaults, $_POST));
if ($this->acl->hasPermission('frontend/captcha/index/image') === true) {
$this->view->assign('captcha', $this->captchaHelpers->captcha());
}
$this->secureHelper->generateFormToken($this->request->query);
return $this->view->fetchTemplate('Comments/Frontend/index.create.tpl');
}
/**
* @param array $formData
*/
protected function _createPost(array $formData)
{
try {
$ip = $_SERVER['REMOTE_ADDR'];
$this->commentsValidator->validateCreate($formData, $ip);
$insertValues = [
'id' => '',
'date' => $this->date->toSQL(),
'ip' => $ip,
'name' => Core\Functions::strEncode($formData['name']),
'user_id' => $this->auth->isUser() === true && $this->get('core.validator.rules.misc')->isNumber($this->auth->getUserId() === true) ? $this->auth->getUserId() : '',
'message' => Core\Functions::strEncode($formData['message']),
'module_id' => $this->modules->getModuleId($this->module),
'entry_id' => $this->entryId,
];
$bool = $this->commentsModel->insert($insertValues);
$this->secureHelper->unsetFormToken($this->request->query);
$this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool !== false ? 'create_success' : 'create_error'), $this->request->query);
} catch (Core\Exceptions\InvalidFormToken $e) {
$this->redirectMessages()->setMessage(false, $e->getMessage(), $this->request->query);
} catch (Core\Exceptions\ValidationFailed $e) {
$this->view->assign('error_msg', $this->get('core.helpers.alerts')->errorBox($e->getMessage()));
}
}
}
<?php
namespace ACP3\Modules\ACP3\Comments;
use ACP3\Core;
/**
* Class Helpers
* @package ACP3\Modules\ACP3\Comments
*/
class Helpers
{
/**
* @var \ACP3\Core\Modules
*/
protected $modules;
/**
* @var \ACP3\Modules\ACP3\Comments\Model
*/
protected $commentsModel;
/**
* @param \ACP3\Core\Modules $modules
* @param \ACP3\Modules\ACP3\Comments\Model $commentsModel
*/
public function __construct(
Core\Modules $modules,
Model $commentsModel)
{
$this->modules = $modules;
$this->commentsModel = $commentsModel;
}
/**
* Zählt die Anzahl der Kommentare für einen bestimmten Eintrag eines Modules zusammen
*
* @param string $moduleName
* @param integer $resultId
*
* @return integer
*/
public function commentsCount($moduleName, $resultId)
{
return $this->commentsModel->countAllByModule($this->modules->getModuleId($moduleName), $resultId);
}
/**
* @param $moduleId
* @param $resultId
*
* @return int
*/
public function deleteCommentsByModuleAndResult($moduleId, $resultId)
{
return $this->commentsModel->delete(['module_id' => $moduleId, 'entry_id' => $resultId]);
}
}
<?php
namespace ACP3\Modules\ACP3\Comments;
use ACP3\Core\Modules;
/**
* Class Installer
* @package ACP3\Modules\ACP3\Comments
*/
class Installer extends Modules\AbstractInstaller
{
const MODULE_NAME = 'comments';
const SCHEMA_VERSION = 33;
/**
* @inheritdoc
*/
public function createTables()
{
return [
"CREATE TABLE `{pre}comments` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip` VARCHAR(40) NOT NULL,
`date` DATETIME NOT NULL,
`name` VARCHAR(20) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`message` TEXT NOT NULL,
`module_id` INT(10) UNSIGNED NOT NULL,
`entry_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`), INDEX (`module_id`, `entry_id`)
) {engine} {charset};"
];
}
/**
* @inheritdoc
*/
public function removeTables()
{
return ["DROP TABLE `{pre}comments`;"];
}
/**
* @inheritdoc
*/
public function settings()
{
return [
'dateformat' => 'long',
'emoticons' => 1,
];
}
/**
* @inheritdoc
*/
public function schemaUpdates()
{
return [
31 => [