PageRenderTime 963ms CodeModel.GetById 161ms app.highlight 588ms RepoModel.GetById 139ms app.codeStats 2ms

/controllers/admin/AdminCategoriesController.php

https://bitbucket.org/marcenuc/prestashop
PHP | 718 lines | 607 code | 63 blank | 48 comment | 146 complexity | 760c70dfe1fa67156ab309cb605ae2ff MD5 | raw file
  1<?php
  2/*
  3* 2007-2012 PrestaShop
  4*
  5* NOTICE OF LICENSE
  6*
  7* This source file is subject to the Open Software License (OSL 3.0)
  8* that is bundled with this package in the file LICENSE.txt.
  9* It is also available through the world-wide-web at this URL:
 10* http://opensource.org/licenses/osl-3.0.php
 11* If you did not receive a copy of the license and are unable to
 12* obtain it through the world-wide-web, please send an email
 13* to license@prestashop.com so we can send you a copy immediately.
 14*
 15* DISCLAIMER
 16*
 17* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 18* versions in the future. If you wish to customize PrestaShop for your
 19* needs please refer to http://www.prestashop.com for more information.
 20*
 21*  @author PrestaShop SA <contact@prestashop.com>
 22*  @copyright  2007-2012 PrestaShop SA
 23*  @version  Release: $Revision: 8971 $
 24*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 25*  International Registered Trademark & Property of PrestaShop SA
 26*/
 27
 28class AdminCategoriesControllerCore extends AdminController
 29{
 30	/**
 31	 *  @var object Category() instance for navigation
 32	 */
 33	protected $_category = null;
 34	protected $position_identifier = 'id_category_to_move';
 35	
 36	/** @var boolean does the product have to be removed during the delete process */
 37	public $remove_products = true;
 38
 39	/** @var boolean does the product have to be disable during the delete process */
 40	public $disable_products = false;
 41
 42	public function __construct()
 43	{
 44		$this->table = 'category';
 45		$this->className = 'Category';
 46		$this->lang = true;
 47		$this->deleted = false;
 48		$this->explicitSelect = true;
 49
 50		$this->context = Context::getContext();
 51
 52 		$this->fieldImageSettings = array(
 53 			'name' => 'image',
 54 			'dir' => 'c'
 55 		);
 56
 57		$this->fields_list = array(
 58			'id_category' => array(
 59				'title' => $this->l('ID'),
 60				'align' => 'center',
 61				'width' => 20
 62			),
 63			'name' => array(
 64				'title' => $this->l('Name'),
 65				'width' => 'auto'
 66			),
 67			'description' => array(
 68				'title' => $this->l('Description'),
 69				'width' => 500,
 70				'maxlength' => 90,
 71				'callback' => 'getDescriptionClean',
 72				'orderby' => false
 73			),
 74			'position' => array(
 75				'title' => $this->l('Position'),
 76				'width' => 40,
 77				'filter_key' => 'sa!position',
 78				'align' => 'center',
 79				'position' => 'position'
 80			),
 81			'active' => array(
 82				'title' => $this->l('Displayed'),
 83				'active' => 'status',
 84				'align' => 'center',
 85				'type' => 'bool',
 86				'width' => 70,
 87				'orderby' => false
 88			)
 89		);
 90
 91		$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected')));
 92		$this->specificConfirmDelete = false;
 93		
 94		parent::__construct();
 95	}
 96
 97	public function init()
 98	{
 99		parent::init();
100
101		// context->shop is set in the init() function, so we move the _category instanciation after that
102		if (($id_category = Tools::getvalue('id_category')) && $this->action != 'select_delete')
103			$this->_category = new Category($id_category);
104		else
105			if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP)
106				$this->_category = new Category($this->context->shop->id_category);
107			else if (count(Category::getCategoriesWithoutParent()) > 1 && Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && count(Shop::getShops(true, null, true)) != 1)
108				$this->_category = Category::getTopCategory();
109			else
110				$this->_category = new Category(Configuration::get('PS_HOME_CATEGORY'));
111		// if we are not in a shop context, we remove the position column
112		if (Shop::isFeatureActive() && Shop::getContext() != Shop::CONTEXT_SHOP)
113			unset($this->fields_list['position']);
114		// shop restriction : if category is not available for current shop, we redirect to the list from default category
115		if (!$this->_category->isAssociatedToShop() && Shop::getContext() == Shop::CONTEXT_SHOP)
116		{
117			$this->redirect_after = self::$currentIndex.'&id_category='.(int)$this->context->shop->getCategory().'&token='.$this->token;
118			$this->redirect();
119		}
120	}
121	
122	public function initContent()
123	{
124		if ($this->action == 'select_delete')
125			$this->context->smarty->assign(array(
126				'delete_form' => true,
127				'url_delete' => htmlentities($_SERVER['REQUEST_URI']),
128				'boxes' => $this->boxes,
129			));
130
131		parent::initContent();
132	}
133
134	public function setMedia()
135	{
136		parent::setMedia();
137		$this->addJqueryUi('ui.widget');
138		$this->addJqueryPlugin('tagify');
139	}
140
141	public function renderList()
142	{
143		$this->addRowAction('edit');
144		$this->addRowAction('delete');
145		$this->addRowAction('add');
146		$this->addRowAction('view');
147
148		$count_categories_without_parent = count(Category::getCategoriesWithoutParent());
149		$is_multishop = Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE');
150		$top_category = Category::getTopCategory();
151
152		if (Tools::isSubmit('id_category'))
153			$id_parent = $this->_category->id;
154		else if (!$is_multishop && $count_categories_without_parent > 1)
155			$id_parent = $top_category->id;
156		else if ($is_multishop && $count_categories_without_parent == 1)
157			$id_parent = Configuration::get('PS_HOME_CATEGORY');
158		else if ($is_multishop && $count_categories_without_parent > 1 && Shop::getContext() != Shop::CONTEXT_SHOP)
159			if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') && count(Shop::getShops(true, null, true)) == 1)
160				$id_parent = $this->context->shop->id_category;
161			else
162				$id_parent = $top_category->id;
163		else
164			$id_parent = $this->context->shop->id_category;
165
166		$this->_select = 'sa.position position';
167		$this->_filter .= ' AND `id_parent` = '.(int)$id_parent.' ';
168
169		if (Shop::getContext() == Shop::CONTEXT_SHOP)
170			$this->_join .= ' LEFT JOIN `'._DB_PREFIX_.'category_shop` sa ON (a.`id_category` = sa.`id_category` AND sa.id_shop = '.(int)$this->context->shop->id.') ';
171		else
172				$this->_join .= ' LEFT JOIN `'._DB_PREFIX_.'category_shop` sa ON (a.`id_category` = sa.`id_category` AND sa.id_shop = a.id_shop_default) ';
173
174
175		// we add restriction for shop
176		if (Shop::getContext() == Shop::CONTEXT_SHOP && $is_multishop)
177			$this->_where = ' AND sa.`id_shop` = '.(int)Context::getContext()->shop->id;
178
179		$categories_tree = $this->_category->getParentsCategories();
180		if (empty($categories_tree)
181			&& ($this->_category->id_category != 1 || Tools::isSubmit('id_category'))
182			&& (Shop::getContext() == Shop::CONTEXT_SHOP && !$is_multishop && $count_categories_without_parent > 1))
183			$categories_tree = array(array('name' => $this->_category->name[$this->context->language->id]));
184
185		asort($categories_tree);
186		$this->tpl_list_vars['categories_tree'] = $categories_tree;
187
188		if (Tools::isSubmit('submitBulkdelete'.$this->table) || Tools::isSubmit('delete'.$this->table))
189		{
190			$category = new Category(Tools::getValue('id_category'));
191			if ($category->is_root_category)
192				$this->tpl_list_vars['need_delete_mode'] = false;
193			else
194				$this->tpl_list_vars['need_delete_mode'] = true;
195			$this->tpl_list_vars['delete_category'] = true;
196			$this->tpl_list_vars['REQUEST_URI'] = $_SERVER['REQUEST_URI'];
197			$this->tpl_list_vars['POST'] = $_POST;
198		}
199
200		return parent::renderList();
201	}
202
203	public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
204	{
205		$alias = 'sa';
206		parent::getList($id_lang, $alias.'.position', $order_way, $start, $limit, Context::getContext()->shop->id);
207		// Check each row to see if there are combinations and get the correct action in consequence
208
209		$nb_items = count($this->_list);
210		for ($i = 0; $i < $nb_items; $i++)
211		{
212			$item = &$this->_list[$i];
213			$category_tree = Category::getChildren((int)$item['id_category'], $this->context->language->id);
214			if (!count($category_tree))
215				$this->addRowActionSkipList('view', array($item['id_category']));
216		}
217	}
218
219	public function renderView()
220	{
221		$this->initToolbar();
222		return $this->renderList();
223	}
224
225	public function initToolbar()
226	{
227		if (empty($this->display))
228		{
229			if (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE'))
230				$this->toolbar_btn['new-url'] = array(
231					'href' => self::$currentIndex.'&amp;add'.$this->table.'root&amp;token='.$this->token,
232					'desc' => $this->l('Add new root category')
233				);
234			$this->toolbar_btn['new'] = array(
235				'href' => self::$currentIndex.'&amp;add'.$this->table.'&amp;token='.$this->token,
236				'desc' => $this->l('Add new')
237			);
238		}
239		// be able to edit the Home category
240		if (count(Category::getCategoriesWithoutParent()) == 1 && !Tools::isSubmit('id_category')
241			&& ($this->display == 'view' || empty($this->display)))
242			$this->toolbar_btn['edit'] = array(
243				'href' => self::$currentIndex.'&amp;update'.$this->table.'&amp;id_category='.(int)$this->_category->id.'&amp;token='.$this->token,
244				'desc' => $this->l('Edit')
245			);
246		if (Tools::getValue('id_category') && !Tools::isSubmit('updatecategory'))
247		{
248			$this->toolbar_btn['edit'] = array(
249				'href' => self::$currentIndex.'&amp;update'.$this->table.'&amp;id_category='.(int)Tools::getValue('id_category').'&amp;token='.$this->token,
250				'desc' => $this->l('Edit')
251			);
252		}
253
254		if ($this->display == 'view')
255			$this->toolbar_btn['new'] = array(
256				'href' => self::$currentIndex.'&amp;add'.$this->table.'&amp;id_parent='.(int)Tools::getValue('id_category').'&amp;token='.$this->token,
257				'desc' => $this->l('Add new')
258			);
259		parent::initToolbar();
260		if ($this->_category->id == Category::getTopCategory()->id && isset($this->toolbar_btn['new']))
261			unset($this->toolbar_btn['new']);
262		// after adding a category
263		if (empty($this->display))
264		{
265			$id_category = (Tools::isSubmit('id_category')) ? '&amp;id_parent='.(int)Tools::getValue('id_category') : '';
266			$this->toolbar_btn['new'] = array(
267				'href' => self::$currentIndex.'&amp;add'.$this->table.'&amp;token='.$this->token.$id_category,
268				'desc' => $this->l('Add new')
269			);
270
271			if (Tools::isSubmit('id_category'))
272			{
273				$back = Tools::safeOutput(Tools::getValue('back', ''));
274				if (empty($back))
275					$back = self::$currentIndex.'&token='.$this->token;
276				$this->toolbar_btn['back'] = array(
277					'href' => $back,
278					'desc' => $this->l('Back to list')
279				);
280			}
281		}
282	}
283
284	public function initProcess()
285	{
286		if (Tools::isSubmit('add'.$this->table.'root'))
287		{
288			if ($this->tabAccess['add'])
289			{
290				$this->action = 'add'.$this->table.'root';
291				$this->display = 'edit';
292			}
293			else
294				$this->errors[] = Tools::displayError('You do not have permission to edit here.');
295		}
296
297		parent::initProcess();
298
299		if ($this->action == 'delete' || $this->action == 'bulkdelete')
300			if (Tools::getIsset('cancel'))
301				Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminCategories'));
302			elseif (Tools::getValue('deleteMode') == 'link' || Tools::getValue('deleteMode') == 'linkanddisable' || Tools::getValue('deleteMode') == 'delete')
303				$this->delete_mode = Tools::getValue('deleteMode');
304			else
305				$this->action = 'select_delete';
306	}
307
308	public function renderForm()
309	{
310		$this->initToolbar();
311		$obj = $this->loadObject(true);
312		$id_shop = Context::getContext()->shop->id;
313		$selected_cat = array((isset($obj->id_parent) && $obj->isParentCategoryAvailable($id_shop))? (int)$obj->id_parent : (int)Tools::getValue('id_parent', Category::getRootCategory()->id));
314		$unidentified = new Group(Configuration::get('PS_UNIDENTIFIED_GROUP'));
315		$guest = new Group(Configuration::get('PS_GUEST_GROUP'));
316		$default = new Group(Configuration::get('PS_CUSTOMER_GROUP'));
317
318		$unidentified_group_information = sprintf($this->l('%s - All people without a validated customer account.'), '<b>'.$unidentified->name[$this->context->language->id].'</b>');
319		$guest_group_information = sprintf($this->l('%s - Customer who placed an order with the Guest Checkout.'), '<b>'.$guest->name[$this->context->language->id].'</b>');
320		$default_group_information = sprintf($this->l('%s - All people who have created an account on this site.'), '<b>'.$default->name[$this->context->language->id].'</b>');
321		$root_category = Category::getRootCategory();
322		$root_category = array('id_category' => $root_category->id_category, 'name' => $root_category->name);
323		$this->fields_form = array(
324			'tinymce' => true,
325			'legend' => array(
326				'title' => $this->l('Category'),
327				'image' => '../img/admin/tab-categories.gif'
328			),
329			'input' => array(
330				array(
331					'type' => 'text',
332					'label' => $this->l('Name:'),
333					'name' => 'name',
334					'lang' => true,
335					'size' => 48,
336					'required' => true,
337					'class' => 'copy2friendlyUrl',
338					'hint' => $this->l('Invalid characters:').' <>;=#{}',
339				),
340				array(
341					'type' => 'radio',
342					'label' => $this->l('Displayed:'),
343					'name' => 'active',
344					'required' => false,
345					'class' => 't',
346					'is_bool' => true,
347					'values' => array(
348						array(
349							'id' => 'active_on',
350							'value' => 1,
351							'label' => $this->l('Enabled')
352						),
353						array(
354							'id' => 'active_off',
355							'value' => 0,
356							'label' => $this->l('Disabled')
357						)
358					)
359				),
360				array(
361					'type' => 'categories',
362					'label' => $this->l('Parent category:'),
363					'name' => 'id_parent',
364					'values' => array(
365						'trads' => array(
366							 'Root' => $root_category,
367							 'selected' => $this->l('selected'),
368							 'Collapse All' => $this->l('Collapse All'),
369							 'Expand All' => $this->l('Expand All')
370						),
371						'selected_cat' => $selected_cat,
372						'input_name' => 'id_parent',
373						'use_radio' => true,
374						'use_search' => false,
375						'disabled_categories' => array(4),
376						'top_category' => Category::getTopCategory(),
377						'use_context' => true,
378					)
379				),
380				array(
381					'type' => 'radio',
382					'label' => $this->l('Root Category:'),
383					'name' => 'is_root_category',
384					'required' => false,
385					'is_bool' => true,
386					'class' => 't',
387					'values' => array(
388						array(
389							'id' => 'is_root_on',
390							'value' => 1,
391							'label' => $this->l('Yes')
392						),
393						array(
394							'id' => 'is_root_off',
395							'value' => 0,
396							'label' => $this->l('No')
397						)
398					)
399				),
400				array(
401					'type' => 'textarea',
402					'label' => $this->l('Description:'),
403					'name' => 'description',
404					'lang' => true,
405					'rows' => 10,
406					'cols' => 100,
407					'hint' => $this->l('Invalid characters:').' <>;=#{}'
408				),
409				array(
410					'type' => 'file',
411					'label' => $this->l('Image:'),
412					'name' => 'image',
413					'display_image' => true,
414					'desc' => $this->l('Upload category logo from your computer')
415				),
416				array(
417					'type' => 'text',
418					'label' => $this->l('Meta title:'),
419					'name' => 'meta_title',
420					'lang' => true,
421					'hint' => $this->l('Forbidden characters:').' <>;=#{}'
422				),
423				array(
424					'type' => 'text',
425					'label' => $this->l('Meta description:'),
426					'name' => 'meta_description',
427					'lang' => true,
428					'hint' => $this->l('Forbidden characters:').' <>;=#{}'
429				),
430				array(
431					'type' => 'tags',
432					'label' => $this->l('Meta keywords:'),
433					'name' => 'meta_keywords',
434					'lang' => true,
435					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
436					'desc' => $this->l('To add "tags" click in the field, write something, then press "Enter"')
437				),
438				array(
439					'type' => 'text',
440					'label' => $this->l('Friendly URL:'),
441					'name' => 'link_rewrite',
442					'lang' => true,
443					'required' => true,
444					'hint' => $this->l('Only letters and the minus (-) character are allowed')
445				),
446				array(
447					'type' => 'group',
448					'label' => $this->l('Group access:'),
449					'name' => 'groupBox',
450					'values' => Group::getGroups(Context::getContext()->language->id),
451					'info_introduction' => $this->l('You now have three default customer groups.'),
452					'unidentified' => $unidentified_group_information,
453					'guest' => $guest_group_information,
454					'customer' => $default_group_information,
455					'desc' => $this->l('Mark all customer groups you want to give access to this category')
456				)
457			),
458			'submit' => array(
459				'title' => $this->l('Save'),
460				'class' => 'button'
461			)
462		);
463		
464		$this->tpl_form_vars['shared_category'] = Validate::isLoadedObject($obj) && $obj->hasMultishopEntries(); 
465		$this->tpl_form_vars['PS_ALLOW_ACCENTED_CHARS_URL'] = (int)Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');
466		if (Shop::isFeatureActive())
467			$this->fields_form['input'][] = array(
468				'type' => 'shop',
469				'label' => $this->l('Shop association:'),
470				'name' => 'checkBoxShopAsso',
471			);
472		// remove category tree and radio button "is_root_category" if this category has the root category as parent category to avoid any conflict
473		if ($this->_category->id_parent == Category::getTopCategory()->id && Tools::isSubmit('updatecategory'))
474			foreach ($this->fields_form['input'] as $k => $input)
475				if (in_array($input['name'], array('id_parent', 'is_root_category')))
476					unset($this->fields_form['input'][$k]);
477
478		if (Tools::isSubmit('add'.$this->table.'root'))
479			unset($this->fields_form['input'][2],$this->fields_form['input'][3]);
480
481		if (!($obj = $this->loadObject(true)))
482			return;
483
484		$image = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true);
485
486		$this->fields_value = array(
487			'image' => $image ? $image : false,
488			'size' => $image ? filesize(_PS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg') / 1000 : false
489		);
490
491		// Added values of object Group
492		$category_groups_ids = $obj->getGroups();
493
494		$groups = Group::getGroups($this->context->language->id);
495		// if empty $carrier_groups_ids : object creation : we set the default groups
496		if (empty($category_groups_ids))
497		{
498			$preselected = array(Configuration::get('PS_UNIDENTIFIED_GROUP'), Configuration::get('PS_GUEST_GROUP'), Configuration::get('PS_CUSTOMER_GROUP'));
499			$category_groups_ids = array_merge($category_groups_ids, $preselected);
500		}
501		foreach ($groups as $group)
502			$this->fields_value['groupBox_'.$group['id_group']] = Tools::getValue('groupBox_'.$group['id_group'], (in_array($group['id_group'], $category_groups_ids)));
503
504		return parent::renderForm();
505	}
506	
507	public function postProcess()
508	{
509		if (!in_array($this->display, array('edit', 'add')))
510			$this->multishop_context_group = false;
511		if (Tools::isSubmit('forcedeleteImage') || (isset($_FILES['image']) && $_FILES['image']['size'] > 0))
512		{
513			$this->processForceDeleteImage();
514			if (Tools::isSubmit('forcedeleteImage'))
515				Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=7');
516		}
517		
518		return parent::postProcess();
519	}
520	
521	public function processForceDeleteImage()
522	{
523		$category = $this->loadObject(true);
524		if (Validate::isLoadedObject($category))
525			$category->deleteImage(true);
526	}
527	
528	public function processAdd()
529	{
530		$id_category = (int)Tools::getValue('id_category');
531		$id_parent = (int)Tools::getValue('id_parent');
532		// if true, we are in a root category creation
533		if (!$id_parent && !Tools::isSubmit('is_root_category'))
534			$_POST['is_root_category'] = $_POST['level_depth'] = $_POST['id_parent'] = $id_parent = 1;
535		if ($id_category)
536		{
537			if ($id_category != $id_parent)
538			{
539				if (!Category::checkBeforeMove($id_category, $id_parent))
540					$this->errors[] = Tools::displayError($this->l('Category cannot be moved here'));
541			}
542			else
543				$this->errors[] = Tools::displayError($this->l('Category cannot be parent of itself.'));
544		}
545		$object = parent::processAdd();
546		
547		//if we create a you root category you have to associate to a shop before to add sub categories in. So we redirect to AdminCategories listing
548		if ($object && Tools::getValue('is_root_category'))
549			Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminCategories').'&conf=3');
550		return $object;
551	}
552	
553	protected function setDeleteMode()
554	{
555		if ($this->delete_mode == 'link' || $this->delete_mode == 'linkanddisable')
556		{
557			$this->remove_products = false;
558			if ($this->delete_mode == 'linkanddisable')
559				$this->disable_products = true;
560		}
561		else if ($this->delete_mode != 'delete')
562			$this->errors[] = Tools::displayError('Unknown delete mode:'.' '.$this->deleted);
563		
564	}
565	
566	protected function processBulkDelete()
567	{
568		if ($this->tabAccess['delete'] === '1')
569		{
570			$cats_ids = array();
571			foreach (Tools::getValue($this->table.'Box') as $id_category)
572			{
573				$category = new Category((int)$id_category);
574				if (!$category->isRootCategoryForAShop())
575					$cats_ids[$category->id] = $category->id_parent;
576			}
577	
578			if (parent::processBulkDelete())
579			{
580					$this->setDeleteMode();
581					foreach ($cats_ids as $id => $id_parent)
582						$this->processFatherlessProducts((int)$id_parent);
583					return true;
584			}
585			else
586				return false;
587		}
588		else
589			$this->errors[] = Tools::displayError('You do not have permission to delete here.');
590	}
591	
592	public function processDelete()
593	{
594		$category = $this->loadObject();
595		if ($this->tabAccess['delete'] === '1')
596		{
597			if ($category->isRootCategoryForAShop())
598				$this->errors[] = Tools::displayError('You cannot remove this category because a shop uses this category as a root category.');
599			else if (parent::processDelete())
600			{
601				$this->setDeleteMode();
602				$this->processFatherlessProducts((int)$category->id_parent);
603				return true;
604			}
605			else
606				return false;
607		}
608		else
609			$this->errors[] = Tools::displayError('You do not have permission to delete here.');
610	}
611	
612	public function processFatherlessProducts($id_parent)
613	{
614		/* Delete or link products which were not in others categories */
615		$fatherless_products = Db::getInstance()->executeS('
616			SELECT p.`id_product` FROM `'._DB_PREFIX_.'product` p
617			'.Shop::addSqlAssociation('product', 'p').'
618			WHERE p.`id_product` NOT IN (SELECT DISTINCT(cp.`id_product`) FROM `'._DB_PREFIX_.'category_product` cp)');
619			
620		foreach ($fatherless_products as $id_poor_product)
621		{
622			$poor_product = new Product((int)$id_poor_product);
623			if (Validate::isLoadedObject($poor_product))
624			{
625				if ($this->remove_products || $id_parent == 0)
626					$poor_product->delete();
627				else
628				{
629					if ($this->disable_products)
630						$poor_product->active = 0;
631					$poor_product->id_category_default = (int)$id_parent;
632					$poor_product->addToCategories((int)$id_parent);
633					$poor_product->save();
634				}
635			}
636		}
637	}
638
639	public function processPosition()
640	{
641		if ($this->tabAccess['edit'] !== '1')
642			$this->errors[] = Tools::displayError('You do not have permission to edit here.');
643		else if (!Validate::isLoadedObject($object = new Category((int)Tools::getValue($this->identifier, Tools::getValue('id_category_to_move', 1)))))
644			$this->errors[] = Tools::displayError('An error occurred while updating status for object.').' <b>'.
645				$this->table.'</b> '.Tools::displayError('(cannot load object)');
646		if (!$object->updatePosition((int)Tools::getValue('way'), (int)Tools::getValue('position')))
647			$this->errors[] = Tools::displayError('Failed to update the position.');
648		else
649		{
650			$object->regenerateEntireNtree();
651			Tools::redirectAdmin(self::$currentIndex.'&'.$this->table.'Orderby=position&'.$this->table.'Orderway=asc&conf=5'.(($id_category = (int)Tools::getValue($this->identifier, Tools::getValue('id_category_parent', 1))) ? ('&'.$this->identifier.'='.$id_category) : '').'&token='.Tools::getAdminTokenLite('AdminCategories'));
652		}
653	}
654
655	protected function postImage($id)
656	{
657		$ret = parent::postImage($id);
658		if (($id_category = (int)Tools::getValue('id_category')) &&
659			isset($_FILES) && count($_FILES) && $_FILES['image']['name'] != null &&
660			file_exists(_PS_CAT_IMG_DIR_.$id_category.'.jpg'))
661		{
662			$images_types = ImageType::getImagesTypes('categories');
663			foreach ($images_types as $k => $image_type)
664			{
665				ImageManager::resize(
666					_PS_CAT_IMG_DIR_.$id_category.'.jpg',
667					_PS_CAT_IMG_DIR_.$id_category.'-'.stripslashes($image_type['name']).'.jpg',
668					(int)$image_type['width'], (int)$image_type['height']
669				);
670			}
671		}
672
673		return $ret;
674	}
675
676	/**
677	  * Allows to display the category description without HTML tags and slashes
678	  *
679	  * @return string
680	  */
681	public static function getDescriptionClean($description)
682	{
683		return strip_tags(stripslashes($description));
684	}
685
686	public function ajaxProcessUpdatePositions()
687	{
688		$id_category_to_move = (int)(Tools::getValue('id_category_to_move'));
689		$id_category_parent = (int)(Tools::getValue('id_category_parent'));
690		$way = (int)(Tools::getValue('way'));
691		$positions = Tools::getValue('category');
692		if (is_array($positions))
693			foreach ($positions as $key => $value)
694			{
695				$pos = explode('_', $value);
696				if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category_parent && $pos[2] == $id_category_to_move))
697				{
698					$position = $key + 1;
699					break;
700				}
701			}
702
703		$category = new Category($id_category_to_move);
704		if (Validate::isLoadedObject($category))
705		{
706			if (isset($position) && $category->updatePosition($way, $position))
707			{
708				Hook::exec('actionCategoryUpdate');
709				die(true);
710			}
711			else
712				die('{"hasError" : true, errors : "Can not update categories position"}');
713		}
714		else
715			die('{"hasError" : true, "errors" : "This category can not be loaded"}');
716	}
717}
718