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:
<?php
abstract class AbstractDeleteCategoryController extends ModuleController
{
protected $form;
private $submit_button;
private $lang;
public function execute(HTTPRequestCustom $request)
{
$this->check_authorizations();
$this->init();
try {
$category = $this->get_category();
} catch (CategoryNotFoundException $e) {
$controller = PHPBoostErrors::unexisting_page();
DispatchManager::redirect($controller);
}
$children = $this->get_category_children($category);
if (empty($children) && !$this->get_category_items_exists($category))
{
$this->get_categories_manager()->delete($this->get_category()->get_id());
$this->clear_cache();
AppContext::get_response()->redirect($this->get_categories_management_url(), StringVars::replace_vars($this->get_success_message(), array('name' => $this->get_category()->get_name())));
}
$this->build_form();
$tpl = new StringTemplate('# INCLUDE FORM #');
$tpl->add_lang($this->lang);
if ($this->submit_button->has_been_submited() && $this->form->validate())
{
if ($this->form->get_value('delete_category_and_content'))
{
$this->get_categories_manager()->delete($this->get_category()->get_id());
foreach ($children as $id => $category)
{
$this->get_categories_manager()->delete($id);
}
}
else
{
$id_parent = $this->form->get_value('move_in_other_cat')->get_raw_value();
$this->get_categories_manager()->move_items_into_another($category, $id_parent);
$children = $this->get_category_children($category, false);
foreach ($children as $id => $category)
{
$this->get_categories_manager()->move_into_another($category, $id_parent);
}
$this->get_categories_manager()->delete($this->get_category()->get_id());
$categories_cache = $this->get_categories_manager()->get_categories_cache()->get_class();
$categories_cache::invalidate();
}
$this->clear_cache();
AppContext::get_response()->redirect($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()
{
$this->lang = LangLoader::get('categories-common');
}
private function build_form()
{
$form = new HTMLForm(__CLASS__);
$fieldset = new FormFieldsetHTMLHeading('delete_category', $this->get_title());
$fieldset->set_description($this->get_description());
$form->add_fieldset($fieldset);
$fieldset->add_field(new FormFieldCheckbox('delete_category_and_content', $this->lang['delete.category_and_content'], FormFieldCheckbox::UNCHECKED, array('events' => array('click' => '
if (HTMLForms.getField("delete_category_and_content").getValue()) {
HTMLForms.getField("move_in_other_cat").disable();
} else {
HTMLForms.getField("move_in_other_cat").enable();
}')
)));
$options = new SearchCategoryChildrensOptions();
$options->add_category_in_excluded_categories($this->get_category()->get_id());
$fieldset->add_field($this->get_categories_manager()->get_select_categories_form_field('move_in_other_cat', $this->lang['delete.move_in_other_cat'], $this->get_category()->get_id_parent(), $options));
$this->submit_button = new FormButtonDefaultSubmit();
$form->add_button($this->submit_button);
$form->add_button(new FormButtonReset());
$this->form = $form;
}
private function get_category_children(Category $category, $enable_recursive_exploration = true)
{
$options = new SearchCategoryChildrensOptions();
$options->add_category_in_excluded_categories($category->get_id());
$options->set_enable_recursive_exploration($enable_recursive_exploration);
return $this->get_categories_manager()->get_children($category->get_id(), $options);
}
private function get_category_items_exists(Category $category)
{
return PersistenceContext::get_querier()->row_exists(
$this->get_categories_manager()->get_categories_items_parameters()->get_table_name_contains_items(),
'WHERE '.$this->get_categories_manager()->get_categories_items_parameters()->get_field_name_id_category().'=:id_category',
array('id_category' => $category->get_id()
));
}
private function get_category()
{
$id_category = $this->get_id_category();
if (!empty($id_category) && $this->get_categories_manager()->get_categories_cache()->category_exists($id_category))
{
return $this->get_categories_manager()->get_categories_cache()->get_category($id_category);
}
throw new CategoryNotFoundException($id_category);
}
protected function get_title()
{
return $this->lang['category.delete'];
}
protected function get_description()
{
return $this->lang['delete.description'];
}
protected function get_success_message()
{
return $this->lang['category.message.success.delete'];
}
protected function clear_cache()
{
return true;
}
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_delete_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->get_delete_category_url($this->get_category()));
return $response;
}
abstract protected function get_id_category();
abstract protected function get_categories_manager();
abstract protected function get_categories_management_url();
abstract protected function get_delete_category_url(Category $category);
abstract protected function get_module_home_page_url();
abstract protected function get_module_home_page_title();
abstract protected function check_authorizations();
}
?>