Index.php 14.8 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();
134 135
    }

136 137 138 139 140 141
    /**
     * @param string $action
     *
     * @throws \ACP3\Core\Exceptions\ResultNotExists
     */
    public function actionDelete($action = '')
142
    {
143 144 145 146 147 148 149 150 151 152 153 154 155
        $this->handleDeleteAction(
            $action,
            function($items) {
                $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));
156 157
                }

158
                return $bool;
159
            }
160
        );
161 162
    }

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

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

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

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

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

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

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

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

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

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

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

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

253
        $this->view->assign('readmore_chars', $this->request->getPost()->get('readmore_chars', $settings['readmore_chars']));
254 255 256

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

        $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')];
263
        $this->view->assign('category_in_breadcrumb', $this->get('core.helpers.forms')->checkboxGenerator('category_in_breadcrumb', [1, 0], $lang_category_in_breadcrumb, $settings['category_in_breadcrumb']));
264

265
        $this->formTokenHelper->generateFormToken();
266 267 268 269 270 271 272 273
    }

    /**
     * @param array $formData
     * @param array $settings
     */
    protected function _createPost(array $formData, array $settings)
    {
274
        $this->handleCreatePostAction(function() use ($formData, $settings) {
275 276 277 278 279 280 281 282
            $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
283 284 285
                'readmore' => $this->useReadMore($formData, $settings),
                'comments' => $this->useComments($formData, $settings),
                'category_id' => $this->fetchCategoryIdForSave($formData),
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                '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']
            );

302
            $this->formTokenHelper->unsetFormToken();
303

304 305
            return $lastId;
        });
306 307 308 309 310
    }

    /**
     * @param array $formData
     * @param array $settings
311
     * @param int   $id
312
     */
313
    protected function _editPost(array $formData, array $settings, $id)
314
    {
315
        $this->handleEditPostAction(function() use ($formData, $settings, $id) {
316 317
            $this->newsValidator->validate(
                $formData,
318
                sprintf(News\Helpers::URL_KEY_PATTERN, $id)
319 320 321 322 323 324 325
            );

            $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
326 327 328
                'readmore' => $this->useReadMore($formData, $settings),
                'comments' => $this->useComments($formData, $settings),
                'category_id' => $this->fetchCategoryIdForSave($formData),
329 330 331 332 333 334
                'uri' => Core\Functions::strEncode($formData['uri'], true),
                'target' => (int)$formData['target'],
                'link_title' => Core\Functions::strEncode($formData['link_title']),
                'user_id' => $this->auth->getUserId(),
            ];

335
            $bool = $this->newsModel->update($updateValues, $id);
336 337

            $this->seo->insertUriAlias(
338
                sprintf(News\Helpers::URL_KEY_PATTERN, $id),
339 340 341 342 343 344
                $formData['alias'],
                $formData['seo_keywords'],
                $formData['seo_description'],
                (int)$formData['seo_robots']
            );

345
            $this->newsCache->saveCache($id);
346

347
            $this->formTokenHelper->unsetFormToken();
348

349 350
            return $bool;
        });
351 352 353 354 355 356 357
    }

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

            $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'],
            ];
368 369 370 371 372

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

373
            $this->formTokenHelper->unsetFormToken();
374

375 376
            return $this->config->setSettings($data, 'news');
        });
377
    }
Tino Goratsch's avatar
Tino Goratsch committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

    /**
     * @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;
    }
410
}