1<?php
2/**
3 * 2007-2016 PrestaShop
4 *
5 * thirty bees is an extension to the PrestaShop e-commerce software developed by PrestaShop SA
6 * Copyright (C) 2017-2018 thirty bees
7 *
8 * NOTICE OF LICENSE
9 *
10 * This source file is subject to the Open Software License (OSL 3.0)
11 * that is bundled with this package in the file LICENSE.txt.
12 * It is also available through the world-wide-web at this URL:
13 * http://opensource.org/licenses/osl-3.0.php
14 * If you did not receive a copy of the license and are unable to
15 * obtain it through the world-wide-web, please send an email
16 * to license@thirtybees.com so we can send you a copy immediately.
17 *
18 * DISCLAIMER
19 *
20 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
21 * versions in the future. If you wish to customize PrestaShop for your
22 * needs please refer to https://www.thirtybees.com for more information.
23 *
24 * @author    thirty bees <contact@thirtybees.com>
25 * @author    PrestaShop SA <contact@prestashop.com>
26 * @copyright 2017-2018 thirty bees
27 * @copyright 2007-2016 PrestaShop SA
28 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
29 *  PrestaShop is an internationally registered trademark & property of PrestaShop SA
30 */
31
32/**
33 * Class AdminCmsContentControllerCore
34 *
35 * @since 1.0.0
36 */
37class AdminCmsContentControllerCore extends AdminController
38{
39    // @codingStandardsIgnoreStart
40    /** @var CMSCategory Cms category instance for navigation */
41    protected static $category = null;
42    /** @var AdminCmsCategoriesController $admin_cms_categories */
43    protected $admin_cms_categories;
44    /** @var object adminCMS() instance */
45    protected $admin_cms;
46    // @codingStandardsIgnoreEnd
47
48    /**
49     * AdminCmsContentControllerCore constructor.
50     *
51     * @since 1.0.0
52     */
53    public function __construct()
54    {
55        $this->bootstrap = true;
56        /* Get current category */
57        $idCmsCategory = (int) Tools::getValue('id_cms_category', Tools::getValue('id_cms_category_parent', 1));
58        static::$category = new CMSCategory($idCmsCategory);
59        if (!Validate::isLoadedObject(static::$category)) {
60            die('Category cannot be loaded');
61        }
62
63        $this->table = 'cms';
64        $this->className = 'CMS';
65        $this->bulk_actions = [
66            'delete' => [
67                'text'    => $this->l('Delete selected'),
68                'confirm' => $this->l('Delete selected items?'),
69                'icon'    => 'icon-trash',
70            ],
71        ];
72        $this->admin_cms_categories = new AdminCmsCategoriesController();
73        $this->admin_cms_categories->init();
74        $this->admin_cms = new AdminCmsController();
75        $this->admin_cms->init();
76
77        parent::__construct();
78    }
79
80    /**
81     * Return current category
82     *
83     * @return CMSCategory
84     *
85     * @since 1.0.0
86     */
87    public static function getCurrentCMSCategory()
88    {
89        return static::$category;
90    }
91
92    /**
93     * Check if view access
94     *
95     * @param bool $disable
96     *
97     * @return bool
98     *
99     * @since 1.0.0
100     */
101    public function viewAccess($disable = false)
102    {
103        $result = parent::viewAccess($disable);
104        $this->admin_cms_categories->tabAccess = $this->tabAccess;
105        $this->admin_cms->tabAccess = $this->tabAccess;
106
107        return $result;
108    }
109
110    /**
111     * Initialize content
112     *
113     * @return void
114     *
115     * @since 1.0.0
116     */
117    public function initContent()
118    {
119        $this->initTabModuleList();
120        $this->renderPageHeaderToolbar();
121
122        $this->admin_cms_categories->token = $this->token;
123        $this->admin_cms->token = $this->token;
124
125        if ($this->display == 'edit_category') {
126            $this->content .= $this->admin_cms_categories->renderForm();
127        } elseif ($this->display == 'edit_page') {
128            $this->content .= $this->admin_cms->renderForm();
129        } elseif ($this->display == 'view_page') {
130            $fixme = 'fixme'; // FIXME
131        } else {
132            $idCmsCategory = (int) Tools::getValue('id_cms_category', 1);
133
134            // CMS categories breadcrumb
135            $cmsTabs = ['cms_category', 'cms'];
136            // Cleaning links
137            $catBarIndex = static::$currentIndex;
138            foreach ($cmsTabs as $tab) {
139                if (Tools::getValue($tab.'Orderby') && Tools::getValue($tab.'Orderway')) {
140                    $catBarIndex = preg_replace('/&'.$tab.'Orderby=([a-z _]*)&'.$tab.'Orderway=([a-z]*)/i', '', static::$currentIndex);
141                }
142            }
143            $this->context->smarty->assign(
144                [
145                    'cms_breadcrumb'            => getPath($catBarIndex, $idCmsCategory, '', '', 'cms'),
146                    'page_header_toolbar_btn'   => $this->page_header_toolbar_btn,
147                    'page_header_toolbar_title' => $this->toolbar_title,
148                ]
149            );
150
151            $this->content .= $this->admin_cms_categories->renderList();
152            $this->admin_cms->id_cms_category = $idCmsCategory;
153            $this->content .= $this->admin_cms->renderList();
154        }
155
156        $this->context->smarty->assign(
157            [
158                'content' => $this->content,
159            ]
160        );
161    }
162
163    /**
164     * Render toolbar in page header
165     *
166     * @return void
167     *
168     * @since 1.0.0
169     */
170    public function renderPageHeaderToolbar()
171    {
172        $idCmsCategory = (int) Tools::getValue('id_cms_category');
173        $idCmsPage = Tools::getValue('id_cms');
174
175        if (!$idCmsCategory) {
176            $idCmsCategory = 1;
177        }
178
179        $cmsCategory = new CMSCategory($idCmsCategory);
180
181        if ($this->display == 'edit_category') {
182            if (Tools::getValue('addcms_category') !== false) {
183                $this->toolbar_title[] = $this->l('Add new');
184            } else {
185                $this->toolbar_title[] = sprintf($this->l('Edit: %s'), $cmsCategory->name[$this->context->employee->id_lang]);
186            }
187        } elseif ($this->display == 'edit_page') {
188            $this->toolbar_title[] = $cmsCategory->name[$this->context->employee->id_lang];
189
190            if (Tools::getValue('addcms') !== false) {
191                $this->toolbar_title[] = $this->l('Add new');
192            } elseif ($idCmsPage) {
193                $cmsPage = new CMS($idCmsPage);
194                $this->toolbar_title[] = sprintf($this->l('Edit: %s'), $cmsPage->meta_title[$this->context->employee->id_lang]);
195            }
196        } else {
197            $this->toolbar_title[] = $this->l('CMS');
198        }
199
200        if ($this->display == 'list') {
201            $this->page_header_toolbar_btn['new_cms_category'] = [
202                'href' => static::$currentIndex.'&addcms_category&token='.$this->token,
203                'desc' => $this->l('Add new CMS category', null, null, false),
204                'icon' => 'process-icon-new',
205            ];
206            $this->page_header_toolbar_btn['new_cms_page'] = [
207                'href' => static::$currentIndex.'&addcms&id_cms_category='.(int) $idCmsCategory.'&token='.$this->token,
208                'desc' => $this->l('Add new CMS page', null, null, false),
209                'icon' => 'process-icon-new',
210            ];
211        }
212
213        $this->page_header_toolbar_title = implode(' '.Configuration::get('PS_NAVIGATION_PIPE').' ', $this->toolbar_title);
214
215        if (is_array($this->page_header_toolbar_btn)
216            && $this->page_header_toolbar_btn instanceof Traversable
217            || trim($this->page_header_toolbar_title) != ''
218        ) {
219            $this->show_page_header_toolbar = true;
220        }
221
222        // TODO: Check if we need this
223//        $template = $this->context->smarty->createTemplate(
224//            $this->context->smarty->getTemplateDir(0).DIRECTORY_SEPARATOR.'page_header_toolbar.tpl', $this->context->smarty
225//        );
226
227        $this->context->smarty->assign(
228            [
229                'show_page_header_toolbar'  => $this->show_page_header_toolbar,
230                'title'                     => $this->page_header_toolbar_title,
231                'toolbar_btn'               => $this->page_header_toolbar_btn,
232                'page_header_toolbar_btn'   => $this->page_header_toolbar_btn,
233                'page_header_toolbar_title' => $this->toolbar_title,
234            ]
235        );
236    }
237
238    /**
239     * Post process
240     *
241     * @return void
242     *
243     * @since 1.0.0
244     */
245    public function postProcess()
246    {
247        $this->admin_cms->postProcess();
248        $this->admin_cms_categories->postProcess();
249
250        parent::postProcess();
251
252        if (((Tools::isSubmit('submitAddcms_category') || Tools::isSubmit('submitAddcms_categoryAndStay')) && count($this->admin_cms_categories->errors))
253            || Tools::isSubmit('updatecms_category')
254            || Tools::isSubmit('addcms_category')
255        ) {
256            $this->display = 'edit_category';
257        } elseif (((Tools::isSubmit('submitAddcms') || Tools::isSubmit('submitAddcmsAndStay')) && count($this->admin_cms->errors))
258            || Tools::isSubmit('updatecms')
259            || Tools::isSubmit('addcms')
260        ) {
261            $this->display = 'edit_page';
262        } else {
263            $this->display = 'list';
264            $this->id_cms_category = (int) Tools::getValue('id_cms_category');
265        }
266
267        if (isset($this->admin_cms->errors)) {
268            $this->errors = array_merge($this->errors, $this->admin_cms->errors);
269        }
270
271        if (isset($this->admin_cms_categories->errors)) {
272            $this->errors = array_merge($this->errors, $this->admin_cms_categories->errors);
273        }
274    }
275
276    /**
277     * Set media
278     *
279     * @return void
280     *
281     * @since 1.0.0
282     */
283    public function setMedia()
284    {
285        parent::setMedia();
286        $this->addJqueryUi('ui.widget');
287        $this->addJqueryPlugin('tagify');
288    }
289
290    /**
291     * Ajax process update cms positions
292     *
293     * @return void
294     *
295     * @since 1.0.0
296     */
297    public function ajaxProcessUpdateCmsPositions()
298    {
299        if ($this->tabAccess['edit'] === '1') {
300            $idCms = (int) Tools::getValue('id_cms');
301            $idCategory = (int) Tools::getValue('id_cms_category');
302            $way = (int) Tools::getValue('way');
303            $positions = Tools::getValue('cms');
304            if (is_array($positions)) {
305                foreach ($positions as $key => $value) {
306                    $pos = explode('_', $value);
307                    if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $idCategory && $pos[2] == $idCms)) {
308                        $position = $key;
309                        break;
310                    }
311                }
312            }
313            $cms = new CMS($idCms);
314            if (Validate::isLoadedObject($cms)) {
315                if (isset($position) && $cms->updatePosition($way, $position)) {
316                    die(true);
317                } else {
318                    die('{"hasError" : true, "errors" : "Can not update cms position"}');
319                }
320            } else {
321                die('{"hasError" : true, "errors" : "This cms can not be loaded"}');
322            }
323        }
324    }
325
326    /**
327     * Ajax process update CMSCategory positions
328     *
329     * @return void
330     *
331     * @since 1.0.0
332     */
333    public function ajaxProcessUpdateCmsCategoriesPositions()
334    {
335        if ($this->tabAccess['edit'] === '1') {
336            $idCmsCategoryToMove = (int) Tools::getValue('id_cms_category_to_move');
337            $idCmsCategoryParent = (int) Tools::getValue('id_cms_category_parent');
338            $way = (int) Tools::getValue('way');
339            $positions = Tools::getValue('cms_category');
340            if (is_array($positions)) {
341                foreach ($positions as $key => $value) {
342                    $pos = explode('_', $value);
343                    if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $idCmsCategoryParent && $pos[2] == $idCmsCategoryToMove)) {
344                        $position = $key;
345                        break;
346                    }
347                }
348            }
349            $cmsCategory = new CMSCategory($idCmsCategoryToMove);
350            if (Validate::isLoadedObject($cmsCategory)) {
351                if (isset($position) && $cmsCategory->updatePosition($way, $position)) {
352                    die(true);
353                } else {
354                    die('{"hasError" : true, "errors" : "Can not update cms categories position"}');
355                }
356            } else {
357                die('{"hasError" : true, "errors" : "This cms category can not be loaded"}');
358            }
359        }
360    }
361
362    /**
363     * Ajax process publish CMS
364     *
365     * @return void
366     *
367     * @since 1.0.0
368     */
369    public function ajaxProcessPublishCMS()
370    {
371        if ($this->tabAccess['edit'] === '1') {
372            if ($idCms = (int) Tools::getValue('id_cms')) {
373                $boCmsUrl = $this->context->link->getAdminLink('AdminCmsContent', true).'&updatecms&id_cms='.(int) $idCms;
374                if (Tools::getValue('redirect')) {
375                    die($boCmsUrl);
376                }
377
378                $cms = new CMS((int) (Tools::getValue('id_cms')));
379                if (!Validate::isLoadedObject($cms)) {
380                    die('error: invalid id');
381                }
382
383                $cms->active = 1;
384                if ($cms->save()) {
385                    die($boCmsUrl);
386                } else {
387                    die('error: saving');
388                }
389            } else {
390                die('error: parameters');
391            }
392        }
393    }
394}
395