Classes

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

File content/category/controllers/AbstractCategoriesFormController.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: 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: 256: 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: 293: 294: 295: 296: 297: 298: 
<?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: 2018 11 06
 * @since       PHPBoost 4.0 - 2013 02 06
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
 * @contributor mipel <mipel@phpboost.com>
*/

abstract class AbstractCategoriesFormController extends ModuleController
{
    /**
     * @var HTMLForm
     */
    protected $form;
    /**
     * @var FormButtonSubmit
     */
    protected $submit_button;

    protected static $lang;
    protected static $common_lang;

    /**
     * @var Category
     */
    private $category;
    protected $is_new_category;

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

        $tpl = new StringTemplate('# INCLUDE FORM #');
        $tpl->add_lang(self::$lang);

        if ($this->submit_button->has_been_submited() && $this->form->validate())
        {
            $this->set_properties();
            $this->save();
            if ($this->is_new_category)
                AppContext::get_response()->redirect($this->get_categories_management_url(), StringVars::replace_vars($this->get_success_message(), array('name' => $this->get_category()->get_name())));
            else
                AppContext::get_response()->redirect($this->form->get_value('referrer') ? $this->form->get_value('referrer') : $this->get_categories_management_url(), StringVars::replace_vars($this->get_success_message(), array('name' => $this->get_category()->get_name())));
        }

        $tpl->put('FORM', $this->form->display());

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

    private function init()
    {
        self::$lang = LangLoader::get('categories-common');
        self::$common_lang = LangLoader::get('common');
    }

    protected function build_form(HTTPRequestCustom $request)
    {
        $form = new HTMLForm(__CLASS__);

        $fieldset = new FormFieldsetHTML('category', $this->get_title());
        $form->add_fieldset($fieldset);

        $fieldset->add_field(new FormFieldTextEditor('name', self::$common_lang['form.name'], $this->get_category()->get_name(), array('required' => true)));

        $fieldset->add_field(new FormFieldCheckbox('personalize_rewrited_name', self::$common_lang['form.rewrited_name.personalize'], $this->get_category()->rewrited_name_is_personalized(), array(
        'events' => array('click' => '
        if (HTMLForms.getField("personalize_rewrited_name").getValue()) {
            HTMLForms.getField("rewrited_name").enable();
        } else {
            HTMLForms.getField("rewrited_name").disable();
        }'
        ))));

        $fieldset->add_field(new FormFieldTextEditor('rewrited_name', self::$common_lang['form.rewrited_name'], $this->get_category()->get_rewrited_name(), array(
            'description' => self::$common_lang['form.rewrited_name.description'],
            'hidden' => !$this->get_category()->rewrited_name_is_personalized()
        ), array(new FormFieldConstraintRegex('`^[a-z0-9\-]+$`iu'))));

        if ($this->get_category()->is_allowed_to_have_childs())
        {
            $search_category_children_options = new SearchCategoryChildrensOptions();

            if ($this->get_category()->get_id())
                $search_category_children_options->add_category_in_excluded_categories($this->get_category()->get_id());

            $fieldset->add_field($this->get_categories_manager()->get_select_categories_form_field('id_parent', self::$common_lang['form.category'], $this->get_category()->get_id_parent(), $search_category_children_options));
        }

        $this->build_fieldset_options($form);

        $fieldset_authorizations = new FormFieldsetHTML('authorizations_fieldset', self::$common_lang['authorizations']);
        $form->add_fieldset($fieldset_authorizations);

        $root_auth = $this->get_categories_manager()->get_categories_cache()->get_category(Category::ROOT_CATEGORY)->get_authorizations();

        $fieldset_authorizations->add_field(new FormFieldCheckbox('special_authorizations', self::$common_lang['authorizations'], !$this->get_category()->auth_is_equals($root_auth),
        array('description' => self::$lang['category.form.authorizations.description'], 'events' => array('click' => '
        if (HTMLForms.getField("special_authorizations").getValue()) {
            jQuery("#' . __CLASS__ . '_authorizations").show();
        } else {
            jQuery("#' . __CLASS__ . '_authorizations").hide();
        }')
        )));

        $auth_settings = new AuthorizationsSettings($this->get_authorizations_settings());
        $auth_settings->build_from_auth_array($this->get_category()->get_authorizations());
        $fieldset_authorizations->add_field(new FormFieldAuthorizationsSetter('authorizations', $auth_settings, array('hidden' => $this->get_category()->auth_is_equals($root_auth))));

        $fieldset->add_field(new FormFieldHidden('referrer', $request->get_url_referrer()));

        $this->submit_button = new FormButtonDefaultSubmit();
        $form->add_button($this->submit_button);
        $form->add_button(new FormButtonReset());

        $this->form = $form;
    }

    protected function set_properties()
    {
        $this->get_category()->set_name($this->form->get_value('name'));
        $rewrited_name = $this->form->get_value('rewrited_name', '');
        $rewrited_name = $this->form->get_value('personalize_rewrited_name') && !empty($rewrited_name) ? $rewrited_name : Url::encode_rewrite($this->get_category()->get_name());
        $this->get_category()->set_rewrited_name($rewrited_name);
        if ($this->get_category()->is_allowed_to_have_childs() && $this->form->get_value('id_parent'))
            $this->get_category()->set_id_parent($this->form->get_value('id_parent')->get_raw_value());
        else
            $this->get_category()->set_id_parent(Category::ROOT_CATEGORY);

        if ($this->form->get_value('special_authorizations'))
        {
            $this->get_category()->set_special_authorizations(true);
            $autorizations = $this->form->get_value('authorizations')->build_auth_array();
        }
        else
        {
            $this->get_category()->set_special_authorizations(false);
            $autorizations = array();
        }

        $this->get_category()->set_authorizations($autorizations);
    }

    private function build_fieldset_options(HTMLForm $form)
    {
        $fieldset = new FormFieldsetHTML('options_fieldset', LangLoader::get_message('form.options', 'common'));
        $this->get_options_fields($fieldset);
        if ($fieldset->get_fields())
        {
            $form->add_fieldset($fieldset);
        }
    }

    protected function get_options_fields(FormFieldset $fieldset)
    {

    }

    /**
     * Update or add category
     */
    private function save()
    {
        $category = $this->get_category();
        if ($category->get_id())
        {
            $this->get_categories_manager()->update($category);
        }
        else
        {
            $this->get_categories_manager()->add($category);
        }
    }

    /**
     * @return Category
     */
    protected function get_category()
    {
        if ($this->category === null)
        {
            $id_category = $this->get_id_category();
            if (!empty($id_category))
            {
                $this->category = $this->get_categories_manager()->get_categories_cache()->get_category($id_category);
            }
            else
            {
                $category_class = $this->get_categories_manager()->get_categories_cache()->get_category_class();
                $this->is_new_category = true;
                $this->category = new $category_class();
                $this->category->set_id_parent(AppContext::get_request()->get_getint('id_parent', Category::ROOT_CATEGORY));
                $this->category->set_authorizations($this->get_categories_manager()->get_categories_cache()->get_root_category()->get_authorizations());
            }
        }
        return $this->category;
    }

    /**
     * @return mixed[] Array of ActionAuthorization for AuthorizationsSettings
     */
    public function get_authorizations_settings()
    {
        return array(
            new ActionAuthorization(self::$common_lang['authorizations.read'], Category::READ_AUTHORIZATIONS),
            new VisitorDisabledActionAuthorization(self::$common_lang['authorizations.write'], Category::WRITE_AUTHORIZATIONS),
            new VisitorDisabledActionAuthorization(self::$common_lang['authorizations.contribution'], Category::CONTRIBUTION_AUTHORIZATIONS),
            new MemberDisabledActionAuthorization(self::$common_lang['authorizations.moderation'], Category::MODERATION_AUTHORIZATIONS)
        );
    }

    /**
     * @return string Page title
     */
    protected function get_title()
    {
        return $this->get_id_category() == 0 ? self::$lang['category.add'] : self::$lang['category.edit'];
    }

    /**
     * @return string the appropriate success message
     */
    protected function get_success_message()
    {
        return $this->is_new_category ? self::$lang['category.message.success.add'] : self::$lang['category.message.success.edit'];
    }

    /**
     * @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->is_new_category ? $this->get_add_category_url() : $this->get_edit_category_url($this->get_category()));

        $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->is_new_category ? $this->get_add_category_url() : $this->get_edit_category_url($this->get_category()));

        return $response;
    }

    /**
     * @return string id of the category to edit / delete
     */
    abstract protected function get_id_category();

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

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

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

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

    /**
     * @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();
}
?>