Classes

File content/category/controllers/AbstractCategoriesManageController.class.php

File content/category/controllers/AbstractCategoriesManageController.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  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: 131: 132: 133: 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: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 
<?php
/**
 * @package     Content
 * @subpackage  Category\controllers
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2017 08 22
 * @since       PHPBoost 4.0 - 2013 02 11
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
 * @contributor janus57 <janus57@janus57.fr>
*/

abstract class AbstractCategoriesManageController extends ModuleController
{
    protected $lang;
    protected $tpl;

    public function execute(HTTPRequestCustom $request)
    {
        $this->check_authorizations();

        $this->init();

        $this->update_positions($request);

        $this->build_view();

        return $this->generate_response($this->tpl);
    }

    private function init()
    {
        $this->lang = LangLoader::get('categories-common');
        $this->tpl = new FileTemplate('default/framework/content/categories/manage.tpl');
        $this->tpl->add_lang($this->lang);
    }

    private function build_view()
    {
        $categories_cache = $this->get_categories_manager()->get_categories_cache()->get_class();
        $categories = $categories_cache::load()->get_categories();

        $number_categories = count($categories);

        $this->tpl->put_all(array(
            'C_NO_CATEGORIES' => $number_categories <= 1,
            'C_MORE_THAN_ONE_CATEGORY' => $number_categories > 2, // Root category is not displayed, but taken into account in the calculation
            'FIELDSET_TITLE' => $this->get_title()
        ));

        $this->build_children_view($this->tpl, $categories, Category::ROOT_CATEGORY);
    }

    private function build_children_view($template, $categories, $id_parent)
    {
        foreach ($categories as $id => $category)
        {
            if ($category->get_id_parent() == $id_parent && $id != Category::ROOT_CATEGORY)
            {
                $description = '';
                if (method_exists($category, 'get_description'))
                {
                    $description = FormatingHelper::second_parse($category->get_description());
                    $description = TextHelper::strlen($description) > 250 ? TextHelper::cut_string(@strip_tags($description, '<br><br/>'), 250) . '...' : $description;
                }

                $description_exists = method_exists($category, 'get_description');
                $category_view = new FileTemplate('default/framework/content/categories/category.tpl');
                $category_view->add_lang($this->lang);
                $category_view->put_all(array(
                    'C_DESCRIPTION' => !empty($description),
                    'C_ALLOWED_TO_HAVE_CHILDS' => $category->is_allowed_to_have_childs(),
                    'U_DISPLAY' => $this->get_display_category_url($category)->rel(),
                    'U_EDIT' => $this->get_edit_category_url($category)->rel(),
                    'U_DELETE' => $this->get_delete_category_url($category)->rel(),
                    'ID' => $id,
                    'NAME' => $category->get_name(),
                    'DESCRIPTION' => $description,
                    'DELETE_CONFIRMATION_MESSAGE' => StringVars::replace_vars($this->get_delete_confirmation_message(), array('name' => $category->get_name()))
                ));

                $this->build_children_view($category_view, $categories, $id);

                $template->assign_block_vars('children', array('child' => $category_view->render()));
            }
        }
    }

    private function update_positions(HTTPRequestCustom $request)
    {
        if ($request->get_postvalue('submit', false))
        {
            $categories = json_decode(TextHelper::html_entity_decode($request->get_value('tree')));
            $categories_cache = $this->get_categories_manager()->get_categories_cache();

            foreach ($categories as $position => $tree)
            {
                $id = $tree->id;
                $children = $tree->children[0];
                $category = $categories_cache->get_category($id);

                $this->get_categories_manager()->update_position($category, Category::ROOT_CATEGORY, ($position +1));

                $this->update_children_positions($children, $category->get_id());
            }

            $categories_cache::invalidate();

            $this->tpl->put('MSG', MessageHelper::display(LangLoader::get_message('message.success.position.update', 'status-messages-common'), MessageHelper::SUCCESS, 5));
        }
    }

    private function update_children_positions($categories, $id_parent)
    {
        if (!empty($categories))
        {
            foreach ($categories as $position => $tree)
            {
                if (is_int($position))
                {
                    $id = $tree->id;
                    $children = $tree->children[0];
                    $category = $this->get_categories_manager()->get_categories_cache()->get_category($id);

                    $this->get_categories_manager()->update_position($category, $id_parent, ($position +1));

                    $this->update_children_positions($children, $category->get_id());
                }
            }
        }
    }

    /**
     * @return string Page title
     */
    protected function get_title()
    {
        return $this->lang['categories.management'];
    }

    /**
     * @return string Delete category confirmation message
     */
    protected function get_delete_confirmation_message()
    {
        return $this->lang['category.message.delete_confirmation'];
    }

    /**
     * @param View $view
     * @return Response
     */
    protected function generate_response(View $view)
    {
        $response = new SiteDisplayResponse($view);

        $graphical_environment = $response->get_graphical_environment();
        $graphical_environment->set_page_title($this->get_title(), $this->get_module_home_page_title());
        $graphical_environment->get_seo_meta_data()->set_canonical_url($this->get_categories_management_url());

        $breadcrumb = $graphical_environment->get_breadcrumb();
        $breadcrumb->add($this->get_module_home_page_title(), $this->get_module_home_page_url());

        $breadcrumb->add($this->get_title(), $this->get_categories_management_url());

        return $response;
    }

    /**
     * @return CategoriesManager
     */
    abstract protected function get_categories_manager();

    /**
     * @param int $category Category
     * @return Url
     */
    abstract protected function get_display_category_url(Category $category);

    /**
     * @param int $category Category
     * @return Url
     */
    abstract protected function get_edit_category_url(Category $category);

    /**
     * @param int $category Category
     * @return Url
     */
    abstract protected function get_delete_category_url(Category $category);

    /**
     * @return Url
     */
    abstract protected function get_categories_management_url();

    /**
     * @return Url
     */
    abstract protected function get_module_home_page_url();

    /**
     * @return string module home page title
     */
    abstract protected function get_module_home_page_title();

    /**
     * @return boolean Authorization to manage categories
     */
    abstract protected function check_authorizations();
}
?>
Annonces