Index.php 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?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
 */
Tino Goratsch's avatar
Tino Goratsch committed
14
class Index extends Core\Modules\AdminController
15 16 17 18 19 20
{
    /**
     * @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;

    /**
Tino Goratsch's avatar
Tino Goratsch committed
46 47 48 49 50 51 52
     * @param \ACP3\Core\Modules\Controller\AdminContext $context
     * @param \ACP3\Core\Date                            $date
     * @param \ACP3\Core\Helpers\FormToken               $formTokenHelper
     * @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
53 54
     */
    public function __construct(
Tino Goratsch's avatar
Tino Goratsch committed
55
        Core\Modules\Controller\AdminContext $context,
56
        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
        $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');

89 90
        if ($this->request->getPost()->isEmpty() === false) {
            $this->_createPost($this->request->getPost()->getAll(), $settings);
91 92 93
        }

        // Datumsauswahl
94
        $this->view->assign('publication_period', $this->get('core.helpers.date')->datepicker(['start', 'end']));
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

        // 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
    /**
     * @param string $action
     *
     * @throws \ACP3\Core\Exceptions\ResultNotExists
     */
    public function actionDelete($action = '')
142 143 144
    {
        $items = $this->_deleteItem();

145
        if ($action === 'confirmed') {
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
            $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();
        }
    }

164
    /**
165
     * @param int $id
166 167 168 169
     *
     * @throws \ACP3\Core\Exceptions\ResultNotExists
     */
    public function actionEdit($id)
170
    {
171
        $news = $this->newsModel->getOneById($id);
172 173 174 175 176 177

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

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

178
            if ($this->request->getPost()->isEmpty() === false) {
179
                $this->_editPost($this->request->getPost()->getAll(), $settings, $id);
180 181 182
            }

            // Datumsauswahl
183
            $this->view->assign('publication_period', $this->get('core.helpers.date')->datepicker(['start', 'end'], [$news['start'], $news['end']]));
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

            // 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']));

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

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

216
            $this->formTokenHelper->generateFormToken($this->request->getQuery());
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
        } 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()
    {
243 244
        if ($this->request->getPost()->isEmpty() === false) {
            $this->_settingsPost($this->request->getPost()->getAll());
245 246 247 248
        }

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

249
        $this->view->assign('dateformat', $this->get('core.helpers.date')->dateFormatDropdown($settings['dateformat']));
250 251 252 253

        $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'));

254
        $this->view->assign('readmore_chars', $this->request->getPost()->get('readmore_chars', $settings['readmore_chars']));
255 256 257 258 259 260 261 262 263 264 265

        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'));

266
        $this->formTokenHelper->generateFormToken($this->request->getQuery());
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
    }

    /**
     * @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),
Tino Goratsch's avatar
Tino Goratsch committed
284 285 286
                'readmore' => $this->useReadMore($formData, $settings),
                'comments' => $this->useComments($formData, $settings),
                'category_id' => $this->fetchCategoryIdForSave($formData),
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
                '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']
            );

303
            $this->formTokenHelper->unsetFormToken($this->request->getQuery());
304 305 306 307 308 309 310 311 312 313 314 315

            $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
316
     * @param int   $id
317
     */
318
    protected function _editPost(array $formData, array $settings, $id)
319 320 321 322
    {
        try {
            $this->newsValidator->validate(
                $formData,
323
                sprintf(News\Helpers::URL_KEY_PATTERN, $id)
324 325 326 327 328 329 330
            );

            $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),
Tino Goratsch's avatar
Tino Goratsch committed
331 332 333
                'readmore' => $this->useReadMore($formData, $settings),
                'comments' => $this->useComments($formData, $settings),
                'category_id' => $this->fetchCategoryIdForSave($formData),
334 335 336 337 338 339
                'uri' => Core\Functions::strEncode($formData['uri'], true),
                'target' => (int)$formData['target'],
                'link_title' => Core\Functions::strEncode($formData['link_title']),
                'user_id' => $this->auth->getUserId(),
            ];

340
            $bool = $this->newsModel->update($updateValues, $id);
341 342

            $this->seo->insertUriAlias(
343
                sprintf(News\Helpers::URL_KEY_PATTERN, $id),
344 345 346 347 348 349
                $formData['alias'],
                $formData['seo_keywords'],
                $formData['seo_description'],
                (int)$formData['seo_robots']
            );

350
            $this->newsCache->saveCache($id);
351

352
            $this->formTokenHelper->unsetFormToken($this->request->getQuery());
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

            $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
368
            $this->newsValidator->validateSettings($formData);
369 370 371 372 373 374 375 376

            $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'],
            ];
377 378 379 380 381

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

382 383
            $bool = $this->config->setSettings($data, 'news');

384
            $this->formTokenHelper->unsetFormToken($this->request->getQuery());
385 386 387 388 389 390 391 392

            $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()));
        }
    }
Tino Goratsch's avatar
Tino Goratsch committed
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

    /**
     * @param array $formData
     *
     * @return int
     */
    protected function fetchCategoryIdForSave(array $formData)
    {
        return !empty($formData['cat_create']) ? $this->categoriesHelpers->categoriesCreate($formData['cat_create'], 'news') : $formData['cat'];
    }

    /**
     * @param array $formData
     * @param array $settings
     *
     * @return int
     */
    protected function useReadMore(array $formData, array $settings)
    {
        return $settings['readmore'] == 1 && isset($formData['readmore']) ? 1 : 0;
    }

    /**
     * @param array $formData
     * @param array $settings
     *
     * @return int
     */
    protected function useComments(array $formData, array $settings)
    {
        return $settings['comments'] == 1 && isset($formData['comments']) ? 1 : 0;
    }
425
}