Advertisement
Guest User

AdminCategoriesController.php

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