Index.php 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?php

namespace ACP3\Modules\ACP3\News\Controller\Admin;

use ACP3\Core;
use ACP3\Modules\ACP3\Categories;
use ACP3\Modules\ACP3\Comments;
use ACP3\Modules\ACP3\News;

/**
 * Class Index
 * @package ACP3\Modules\ACP3\News\Controller\Admin
 */
class Index extends Core\Modules\Controller\Admin
{
    /**
     * @var \ACP3\Core\Date
     */
    protected $date;
    /**
21
     * @var \ACP3\Core\Helpers\FormToken
22
     */
23
    protected $formTokenHelper;
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    /**
     * @var \ACP3\Modules\ACP3\News\Model
     */
    protected $newsModel;
    /**
     * @var \ACP3\Modules\ACP3\News\Cache
     */
    protected $newsCache;
    /**
     * @var \ACP3\Modules\ACP3\News\Validator
     */
    protected $newsValidator;
    /**
     * @var \ACP3\Modules\ACP3\Categories\Helpers
     */
    protected $categoriesHelpers;
    /**
     * @var \ACP3\Modules\ACP3\Comments\Helpers
     */
    protected $commentsHelpers;

    /**
46 47 48
     * @param \ACP3\Core\Context\Admin              $context
     * @param \ACP3\Core\Date                       $date
     * @param \ACP3\Core\Helpers\FormToken          $formTokenHelper
49 50 51 52 53 54 55 56
     * @param \ACP3\Modules\ACP3\News\Model         $newsModel
     * @param \ACP3\Modules\ACP3\News\Cache         $newsCache
     * @param \ACP3\Modules\ACP3\News\Validator     $newsValidator
     * @param \ACP3\Modules\ACP3\Categories\Helpers $categoriesHelpers
     */
    public function __construct(
        Core\Context\Admin $context,
        Core\Date $date,
57
        Core\Helpers\FormToken $formTokenHelper,
58 59 60 61 62 63 64 65
        News\Model $newsModel,
        News\Cache $newsCache,
        News\Validator $newsValidator,
        Categories\Helpers $categoriesHelpers)
    {
        parent::__construct($context);

        $this->date = $date;
66
        $this->formTokenHelper = $formTokenHelper;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        $this->newsModel = $newsModel;
        $this->newsCache = $newsCache;
        $this->newsValidator = $newsValidator;
        $this->categoriesHelpers = $categoriesHelpers;
    }

    /**
     * @param \ACP3\Modules\ACP3\Comments\Helpers $commentsHelpers
     *
     * @return $this
     */
    public function setCommentsHelpers(Comments\Helpers $commentsHelpers)
    {
        $this->commentsHelpers = $commentsHelpers;

        return $this;
    }

    public function actionCreate()
    {
        $settings = $this->config->getSettings('news');

        if (empty($_POST) === false) {
            $this->_createPost($_POST, $settings);
        }

        // Datumsauswahl
        $this->view->assign('publication_period', $this->date->datepicker(['start', 'end']));

        // Kategorien
        $this->view->assign('categories', $this->categoriesHelpers->categoriesList('news', '', true));

        // Weiterlesen & Kommentare
        if ($settings['readmore'] == 1 || ($settings['comments'] == 1 && $this->modules->isActive('comments') === true)) {
            $options = [];
            if ($settings['readmore'] == 1) {
                $options[] = [
                    'name' => 'readmore',
                    'checked' => $this->get('core.helpers.forms')->selectEntry('readmore', '1', '0', 'checked'),
                    'lang' => $this->lang->t('news', 'activate_readmore')
                ];
            }
            if ($settings['comments'] == 1 && $this->modules->isActive('comments') === true) {
                $options[] = [
                    'name' => 'comments',
                    'checked' => $this->get('core.helpers.forms')->selectEntry('comments', '1', '0', 'checked'),
                    'lang' => $this->lang->t('system', 'allow_comments')
                ];
            }
            $this->view->assign('options', $options);
        }

        // Linkziel
        $lang_target = [$this->lang->t('system', 'window_self'), $this->lang->t('system', 'window_blank')];
        $this->view->assign('target', $this->get('core.helpers.forms')->selectGenerator('target', [1, 2], $lang_target));

        $this->view->assign('SEO_FORM_FIELDS', $this->seo->formFields());

        $defaults = [
            'title' => '',
            'text' => '',
            'uri' => '',
            'link_title' => ''
        ];
131
        $this->view->assign('form', array_merge($defaults, $this->request->getPost()->getAll()));
132

133
        $this->formTokenHelper->generateFormToken($this->request->getQuery());
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    }

    public function actionDelete()
    {
        $items = $this->_deleteItem();

        if ($this->request->action === 'confirmed') {
            $bool = false;

            foreach ($items as $item) {
                $bool = $this->newsModel->delete($item);
                if ($this->commentsHelpers) {
                    $this->commentsHelpers->deleteCommentsByModuleAndResult('news', $item);
                }

                $this->newsCache->getCacheDriver()->delete(News\Cache::CACHE_ID . $item);
                $this->seo->deleteUriAlias(sprintf(News\Helpers::URL_KEY_PATTERN, $item));
            }

            $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 actionEdit()
    {
        $news = $this->newsModel->getOneById((int)$this->request->id);

        if (empty($news) === false) {
            $this->breadcrumb->setTitlePostfix($news['title']);

            $settings = $this->config->getSettings('news');

            if (empty($_POST) === false) {
                $this->_editPost($_POST, $settings);
            }

            // Datumsauswahl
            $this->view->assign('publication_period', $this->date->datepicker(['start', 'end'], [$news['start'], $news['end']]));

            // Kategorien
            $this->view->assign('categories', $this->categoriesHelpers->categoriesList('news', $news['category_id'], true));

            // Weiterlesen & Kommentare
            if ($settings['readmore'] == 1 || ($settings['comments'] == 1 && $this->modules->isActive('comments') === true)) {
                $options = [];
                if ($settings['readmore'] == 1) {
                    $options[] = [
                        'name' => 'readmore',
                        'checked' => $this->get('core.helpers.forms')->selectEntry('readmore', '1', $news['readmore'], 'checked'),
                        'lang' => $this->lang->t('news', 'activate_readmore')
                    ];
                }
                if ($settings['comments'] == 1 && $this->modules->isActive('comments') === true) {
                    $options[] = [
                        'name' => 'comments',
                        'checked' => $this->get('core.helpers.forms')->selectEntry('comments', '1', $news['comments'], 'checked'),
                        'lang' => $this->lang->t('system', 'allow_comments')
                    ];
                }
                $this->view->assign('options', $options);
            }

            // Linkziel
            $lang_target = [$this->lang->t('system', 'window_self'), $this->lang->t('system', 'window_blank')];
            $this->view->assign('target', $this->get('core.helpers.forms')->selectGenerator('target', [1, 2], $lang_target, $news['target']));

            $this->view->assign('SEO_FORM_FIELDS', $this->seo->formFields(sprintf(News\Helpers::URL_KEY_PATTERN, $this->request->id)));

204
            $this->view->assign('form', array_merge($news, $this->request->getPost()->getAll()));
205

206
            $this->formTokenHelper->generateFormToken($this->request->getQuery());
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        } else {
            throw new Core\Exceptions\ResultNotExists();
        }
    }

    public function actionIndex()
    {
        $news = $this->newsModel->getAllInAcp();

        if (count($news) > 0) {
            $canDelete = $this->acl->hasPermission('admin/news/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);
            $this->view->assign('news', $news);
            $this->view->assign('can_delete', $canDelete);
        }
    }

    public function actionSettings()
    {
        if (empty($_POST) === false) {
            $this->_settingsPost($_POST);
        }

        $settings = $this->config->getSettings('news');

        $this->view->assign('dateformat', $this->date->dateFormatDropdown($settings['dateformat']));

        $lang_readmore = [$this->lang->t('system', 'yes'), $this->lang->t('system', 'no')];
        $this->view->assign('readmore', $this->get('core.helpers.forms')->selectGenerator('readmore', [1, 0], $lang_readmore, $settings['readmore'], 'checked'));

        $this->view->assign('readmore_chars', empty($_POST) === false ? $_POST['readmore_chars'] : $settings['readmore_chars']);

        if ($this->modules->isActive('comments') === true) {
            $lang_allow_comments = [$this->lang->t('system', 'yes'), $this->lang->t('system', 'no')];
            $this->view->assign('allow_comments', $this->get('core.helpers.forms')->selectGenerator('comments', [1, 0], $lang_allow_comments, $settings['comments'], 'checked'));
        }

        $this->view->assign('sidebar_entries', $this->get('core.helpers.forms')->recordsPerPage((int)$settings['sidebar'], 1, 10));

        $lang_category_in_breadcrumb = [$this->lang->t('system', 'yes'), $this->lang->t('system', 'no')];
        $this->view->assign('category_in_breadcrumb', $this->get('core.helpers.forms')->selectGenerator('category_in_breadcrumb', [1, 0], $lang_category_in_breadcrumb, $settings['category_in_breadcrumb'], 'checked'));

256
        $this->formTokenHelper->generateFormToken($this->request->getQuery());
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    }

    /**
     * @param array $formData
     * @param array $settings
     */
    protected function _createPost(array $formData, array $settings)
    {
        try {
            $this->newsValidator->validate($formData);

            $insertValues = [
                'id' => '',
                'start' => $this->date->toSQL($formData['start']),
                'end' => $this->date->toSQL($formData['end']),
                'title' => Core\Functions::strEncode($formData['title']),
                'text' => Core\Functions::strEncode($formData['text'], true),
                'readmore' => $settings['readmore'] == 1 && isset($formData['readmore']) ? 1 : 0,
                'comments' => $settings['comments'] == 1 && isset($formData['comments']) ? 1 : 0,
                'category_id' => !empty($formData['cat_create']) ? $this->categoriesHelpers->categoriesCreate($formData['cat_create'], 'news') : $formData['cat'],
                'uri' => Core\Functions::strEncode($formData['uri'], true),
                'target' => (int)$formData['target'],
                'link_title' => Core\Functions::strEncode($formData['link_title']),
                'user_id' => $this->auth->getUserId(),
            ];

            $lastId = $this->newsModel->insert($insertValues);

            $this->seo->insertUriAlias(
                sprintf(News\Helpers::URL_KEY_PATTERN, $lastId),
                $formData['alias'],
                $formData['seo_keywords'],
                $formData['seo_description'],
                (int)$formData['seo_robots']
            );

293
            $this->formTokenHelper->unsetFormToken($this->request->getQuery());
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

            $this->redirectMessages()->setMessage($lastId, $this->lang->t('system', $lastId !== false ? 'create_success' : 'create_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()));
        }
    }

    /**
     * @param array $formData
     * @param array $settings
     */
    protected function _editPost(array $formData, array $settings)
    {
        try {
            $this->newsValidator->validate(
                $formData,
                sprintf(News\Helpers::URL_KEY_PATTERN, $this->request->id)
            );

            $updateValues = [
                'start' => $this->date->toSQL($formData['start']),
                'end' => $this->date->toSQL($formData['end']),
                'title' => Core\Functions::strEncode($formData['title']),
                'text' => Core\Functions::strEncode($formData['text'], true),
                'readmore' => $settings['readmore'] == 1 && isset($formData['readmore']) ? 1 : 0,
                'comments' => $settings['comments'] == 1 && isset($formData['comments']) ? 1 : 0,
                'category_id' => strlen($formData['cat_create']) >= 3 ? $this->categoriesHelpers->categoriesCreate($formData['cat_create'], 'news') : $formData['cat'],
                'uri' => Core\Functions::strEncode($formData['uri'], true),
                'target' => (int)$formData['target'],
                'link_title' => Core\Functions::strEncode($formData['link_title']),
                'user_id' => $this->auth->getUserId(),
            ];

            $bool = $this->newsModel->update($updateValues, $this->request->id);

            $this->seo->insertUriAlias(
                sprintf(News\Helpers::URL_KEY_PATTERN, $this->request->id),
                $formData['alias'],
                $formData['seo_keywords'],
                $formData['seo_description'],
                (int)$formData['seo_robots']
            );

            $this->newsCache->setCache($this->request->id);

341
            $this->formTokenHelper->unsetFormToken($this->request->getQuery());
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

            $this->redirectMessages()->setMessage($bool, $this->lang->t('system', $bool !== false ? 'edit_success' : 'edit_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()));
        }
    }

    /**
     * @param array $formData
     */
    protected function _settingsPost(array $formData)
    {
        try {
Tino Goratsch's avatar
Tino Goratsch committed
357
            $this->newsValidator->validateSettings($formData);
358 359 360 361 362 363 364 365

            $data = [
                'dateformat' => Core\Functions::strEncode($formData['dateformat']),
                'sidebar' => (int)$formData['sidebar'],
                'readmore' => $formData['readmore'],
                'readmore_chars' => (int)$formData['readmore_chars'],
                'category_in_breadcrumb' => $formData['category_in_breadcrumb'],
            ];
366 367 368 369 370

            if ($this->commentsHelpers) {
                $data['comments'] = $formData['comments'];
            }

371 372
            $bool = $this->config->setSettings($data, 'news');

373
            $this->formTokenHelper->unsetFormToken($this->request->getQuery());
374 375 376 377 378 379 380 381 382

            $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()));
        }
    }
}