PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/CMSCategory.php

https://gitlab.com/mtellezgalindo/PrestaShop
PHP | 626 lines | 423 code | 72 blank | 131 comment | 60 complexity | 828861ba6357a67145cafafe54fafb1b MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 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-2014 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. class CMSCategoryCore extends ObjectModel
  27. {
  28. public $id;
  29. /** @var integer CMSCategory ID */
  30. public $id_cms_category;
  31. /** @var string Name */
  32. public $name;
  33. /** @var boolean Status for display */
  34. public $active = 1;
  35. /** @var string Description */
  36. public $description;
  37. /** @var integer Parent CMSCategory ID */
  38. public $id_parent;
  39. /** @var integer category position */
  40. public $position;
  41. /** @var integer Parents number */
  42. public $level_depth;
  43. /** @var string string used in rewrited URL */
  44. public $link_rewrite;
  45. /** @var string Meta title */
  46. public $meta_title;
  47. /** @var string Meta keywords */
  48. public $meta_keywords;
  49. /** @var string Meta description */
  50. public $meta_description;
  51. /** @var string Object creation date */
  52. public $date_add;
  53. /** @var string Object last modification date */
  54. public $date_upd;
  55. protected static $_links = array();
  56. /**
  57. * @see ObjectModel::$definition
  58. */
  59. public static $definition = array(
  60. 'table' => 'cms_category',
  61. 'primary' => 'id_cms_category',
  62. 'multilang' => true,
  63. 'multilang_shop' => true,
  64. 'fields' => array(
  65. 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
  66. 'id_parent' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
  67. 'position' => array('type' => self::TYPE_INT),
  68. 'level_depth' => array('type' => self::TYPE_INT),
  69. 'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  70. 'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
  71. // Lang fields
  72. 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 64),
  73. 'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 64),
  74. 'description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml'),
  75. 'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 128),
  76. 'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
  77. 'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
  78. ),
  79. );
  80. public function add($autodate = true, $null_values = false)
  81. {
  82. $this->position = CMSCategory::getLastPosition((int)$this->id_parent);
  83. $this->level_depth = $this->calcLevelDepth();
  84. foreach ($this->name as $k => $value)
  85. if (preg_match('/^[1-9]\./', $value))
  86. $this->name[$k] = '0'.$value;
  87. $ret = parent::add($autodate, $null_values);
  88. $this->cleanPositions($this->id_parent);
  89. return $ret;
  90. }
  91. public function update($null_values = false)
  92. {
  93. $this->level_depth = $this->calcLevelDepth();
  94. foreach ($this->name as $k => $value)
  95. if (preg_match('/^[1-9]\./', $value))
  96. $this->name[$k] = '0'.$value;
  97. return parent::update($null_values);
  98. }
  99. /**
  100. * Recursive scan of subcategories
  101. *
  102. * @param integer $max_depth Maximum depth of the tree (i.e. 2 => 3 levels depth)
  103. * @param integer $currentDepth specify the current depth in the tree (don't use it, only for rucursivity!)
  104. * @param array $excluded_ids_array specify a list of ids to exclude of results
  105. * @param integer $idLang Specify the id of the language used
  106. *
  107. * @return array Subcategories lite tree
  108. */
  109. public function recurseLiteCategTree($max_depth = 3, $currentDepth = 0, $id_lang = null, $excluded_ids_array = null, Link $link = null)
  110. {
  111. if (!$link)
  112. $link = Context::getContext()->link;
  113. if (is_null($id_lang))
  114. $id_lang = Context::getContext()->language->id;
  115. // recursivity for subcategories
  116. $children = array();
  117. $subcats = $this->getSubCategories($id_lang, true);
  118. if (($max_depth == 0 || $currentDepth < $max_depth) && $subcats && count($subcats))
  119. foreach ($subcats as &$subcat)
  120. {
  121. if (!$subcat['id_cms_category'])
  122. break;
  123. elseif (!is_array($excluded_ids_array) || !in_array($subcat['id_cms_category'], $excluded_ids_array))
  124. {
  125. $categ = new CMSCategory($subcat['id_cms_category'], $id_lang);
  126. $categ->name = CMSCategory::hideCMSCategoryPosition($categ->name);
  127. $children[] = $categ->recurseLiteCategTree($max_depth, $currentDepth + 1, $id_lang, $excluded_ids_array);
  128. }
  129. }
  130. return array(
  131. 'id' => $this->id_cms_category,
  132. 'link' => $link->getCMSCategoryLink($this->id, $this->link_rewrite),
  133. 'name' => $this->name,
  134. 'desc'=> $this->description,
  135. 'children' => $children
  136. );
  137. }
  138. public static function getRecurseCategory($id_lang = null, $current = 1, $active = 1, $links = 0, Link $link = null)
  139. {
  140. if (!$link)
  141. $link = Context::getContext()->link;
  142. if (is_null($id_lang))
  143. $id_lang = Context::getContext()->language->id;
  144. $sql = 'SELECT c.`id_cms_category`, c.`id_parent`, c.`level_depth`, cl.`name`, cl.`link_rewrite`
  145. FROM `'._DB_PREFIX_.'cms_category` c
  146. JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON c.`id_cms_category` = cl.`id_cms_category`
  147. WHERE c.`id_cms_category` = '.(int)$current.'
  148. AND `id_lang` = '.(int)$id_lang;
  149. $category = Db::getInstance()->getRow($sql);
  150. $sql = 'SELECT c.`id_cms_category`
  151. FROM `'._DB_PREFIX_.'cms_category` c
  152. WHERE c.`id_parent` = '.(int)$current.
  153. ($active ? ' AND c.`active` = 1' : '');
  154. $result = Db::getInstance()->executeS($sql);
  155. foreach ($result as $row)
  156. $category['children'][] = CMSCategory::getRecurseCategory($id_lang, $row['id_cms_category'], $active, $links);
  157. $sql = 'SELECT c.`id_cms`, cl.`meta_title`, cl.`link_rewrite`
  158. FROM `'._DB_PREFIX_.'cms` c
  159. '.Shop::addSqlAssociation('cms', 'c').'
  160. JOIN `'._DB_PREFIX_.'cms_lang` cl ON c.`id_cms` = cl.`id_cms`
  161. WHERE `id_cms_category` = '.(int)$current.'
  162. AND cl.`id_lang` = '.(int)$id_lang.($active ? ' AND c.`active` = 1' : '').'
  163. GROUP BY c.id_cms
  164. ORDER BY c.`position`';
  165. $category['cms'] = Db::getInstance()->executeS($sql);
  166. if ($links == 1)
  167. {
  168. $category['link'] = $link->getCMSCategoryLink($current, $category['link_rewrite']);
  169. foreach ($category['cms'] as $key => $cms)
  170. $category['cms'][$key]['link'] = $link->getCMSLink($cms['id_cms'], $cms['link_rewrite']);
  171. }
  172. return $category;
  173. }
  174. public static function recurseCMSCategory($categories, $current, $id_cms_category = 1, $id_selected = 1, $is_html = 0)
  175. {
  176. $html = '<option value="'.$id_cms_category.'"'.(($id_selected == $id_cms_category) ? ' selected="selected"' : '').'>'
  177. .str_repeat('&nbsp;', $current['infos']['level_depth'] * 5)
  178. .CMSCategory::hideCMSCategoryPosition(stripslashes($current['infos']['name'])).'</option>';
  179. if ($is_html == 0)
  180. echo $html;
  181. if (isset($categories[$id_cms_category]))
  182. foreach (array_keys($categories[$id_cms_category]) as $key)
  183. $html .= CMSCategory::recurseCMSCategory($categories, $categories[$id_cms_category][$key], $key, $id_selected, $is_html);
  184. return $html;
  185. }
  186. /**
  187. * Recursively add specified CMSCategory childs to $toDelete array
  188. *
  189. * @param array &$toDelete Array reference where categories ID will be saved
  190. * @param array|int $id_cms_category Parent CMSCategory ID
  191. */
  192. protected function recursiveDelete(&$to_delete, $id_cms_category)
  193. {
  194. if (!is_array($to_delete) || !$id_cms_category)
  195. die(Tools::displayError());
  196. $result = Db::getInstance()->executeS('
  197. SELECT `id_cms_category`
  198. FROM `'._DB_PREFIX_.'cms_category`
  199. WHERE `id_parent` = '.(int)$id_cms_category);
  200. foreach ($result as $row)
  201. {
  202. $to_delete[] = (int)$row['id_cms_category'];
  203. $this->recursiveDelete($to_delete, (int)$row['id_cms_category']);
  204. }
  205. }
  206. public function delete()
  207. {
  208. if ($this->id == 1) return false;
  209. $this->clearCache();
  210. // Get children categories
  211. $to_delete = array((int)$this->id);
  212. $this->recursiveDelete($to_delete, (int)$this->id);
  213. $to_delete = array_unique($to_delete);
  214. // Delete CMS Category and its child from database
  215. $list = count($to_delete) > 1 ? implode(',', $to_delete) : (int)$this->id;
  216. $id_shop_list = Shop::getContextListShopID();
  217. if (count($this->id_shop_list))
  218. $id_shop_list = $this->id_shop_list;
  219. Db::getInstance()->delete($this->def['table'].'_shop', '`'.$this->def['primary'].'` IN ('.$list.') AND id_shop IN ('.implode(', ', $id_shop_list).')');
  220. $has_multishop_entries = $this->hasMultishopEntries();
  221. if (!$hasMultishopEntries)
  222. {
  223. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cms_category` WHERE `id_cms_category` IN ('.$list.')');
  224. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'cms_category_lang` WHERE `id_cms_category` IN ('.$list.')');
  225. }
  226. CMSCategory::cleanPositions($this->id_parent);
  227. // Delete pages which are in categories to delete
  228. $result = Db::getInstance()->executeS('
  229. SELECT `id_cms`
  230. FROM `'._DB_PREFIX_.'cms`
  231. WHERE `id_cms_category` IN ('.$list.')');
  232. foreach ($result as $p)
  233. {
  234. $product = new CMS($p['id_cms']);
  235. if (Validate::isLoadedObject($product))
  236. $product->delete();
  237. }
  238. return true;
  239. }
  240. /**
  241. * Delete several categories from database
  242. *
  243. * return boolean Deletion result
  244. */
  245. public function deleteSelection($categories)
  246. {
  247. $return = 1;
  248. foreach ($categories as $id_category_cms)
  249. {
  250. $category_cms = new CMSCategory($id_category_cms);
  251. $return &= $category_cms->delete();
  252. }
  253. return $return;
  254. }
  255. /**
  256. * Get the number of parent categories
  257. *
  258. * @return integer Level depth
  259. */
  260. public function calcLevelDepth()
  261. {
  262. $parentCMSCategory = new CMSCategory($this->id_parent);
  263. if (!$parentCMSCategory)
  264. die('parent CMS Category does not exist');
  265. return $parentCMSCategory->level_depth + 1;
  266. }
  267. /**
  268. * Return available categories
  269. *
  270. * @param integer $id_lang Language ID
  271. * @param boolean $active return only active categories
  272. * @return array Categories
  273. */
  274. public static function getCategories($id_lang, $active = true, $order = true)
  275. {
  276. if (!Validate::isBool($active))
  277. die(Tools::displayError());
  278. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  279. SELECT *
  280. FROM `'._DB_PREFIX_.'cms_category` c
  281. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON c.`id_cms_category` = cl.`id_cms_category`
  282. WHERE `id_lang` = '.(int)$id_lang.'
  283. '.($active ? 'AND `active` = 1' : '').'
  284. ORDER BY `name` ASC');
  285. if (!$order)
  286. return $result;
  287. $categories = array();
  288. foreach ($result as $row)
  289. $categories[$row['id_parent']][$row['id_cms_category']]['infos'] = $row;
  290. return $categories;
  291. }
  292. public static function getSimpleCategories($id_lang)
  293. {
  294. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  295. SELECT c.`id_cms_category`, cl.`name`
  296. FROM `'._DB_PREFIX_.'cms_category` c
  297. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category`)
  298. WHERE cl.`id_lang` = '.(int)$id_lang.'
  299. ORDER BY cl.`name`');
  300. }
  301. /**
  302. * Return current CMSCategory childs
  303. *
  304. * @param integer $id_lang Language ID
  305. * @param boolean $active return only active categories
  306. * @return array Categories
  307. */
  308. public function getSubCategories($id_lang, $active = true)
  309. {
  310. if (!Validate::isBool($active))
  311. die(Tools::displayError());
  312. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  313. SELECT c.*, cl.id_lang, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description
  314. FROM `'._DB_PREFIX_.'cms_category` c
  315. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category` AND `id_lang` = '.(int)$id_lang.')
  316. WHERE `id_parent` = '.(int)$this->id.'
  317. '.($active ? 'AND `active` = 1' : '').'
  318. GROUP BY c.`id_cms_category`
  319. ORDER BY `name` ASC');
  320. // Modify SQL result
  321. foreach ($result as &$row)
  322. $row['name'] = CMSCategory::hideCMSCategoryPosition($row['name']);
  323. return $result;
  324. }
  325. /**
  326. * Hide CMSCategory prefix used for position
  327. *
  328. * @param string $name CMSCategory name
  329. * @return string Name without position
  330. */
  331. public static function hideCMSCategoryPosition($name)
  332. {
  333. return preg_replace('/^[0-9]+\./', '', $name);
  334. }
  335. /**
  336. * Return main categories
  337. *
  338. * @param integer $id_lang Language ID
  339. * @param boolean $active return only active categories
  340. * @return array categories
  341. */
  342. public static function getHomeCategories($id_lang, $active = true)
  343. {
  344. return CMSCategory::getChildren(1, $id_lang, $active);
  345. }
  346. public static function getChildren($id_parent, $id_lang, $active = true)
  347. {
  348. if (!Validate::isBool($active))
  349. die(Tools::displayError());
  350. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  351. SELECT c.`id_cms_category`, cl.`name`, cl.`link_rewrite`
  352. FROM `'._DB_PREFIX_.'cms_category` c
  353. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON c.`id_cms_category` = cl.`id_cms_category`
  354. WHERE `id_lang` = '.(int)$id_lang.'
  355. AND c.`id_parent` = '.(int)$id_parent.'
  356. '.($active ? 'AND `active` = 1' : '').'
  357. ORDER BY `name` ASC');
  358. // Modify SQL result
  359. $results_array = array();
  360. foreach ($result as $row)
  361. {
  362. $row['name'] = CMSCategory::hideCMSCategoryPosition($row['name']);
  363. $results_array[] = $row;
  364. }
  365. return $results_array;
  366. }
  367. /**
  368. * Check if CMSCategory can be moved in another one
  369. *
  370. * @param integer $id_parent Parent candidate
  371. * @return boolean Parent validity
  372. */
  373. public static function checkBeforeMove($id_cms_category, $id_parent)
  374. {
  375. if ($id_cms_category == $id_parent) return false;
  376. if ($id_parent == 1) return true;
  377. $i = (int)$id_parent;
  378. while (42)
  379. {
  380. $result = Db::getInstance()->getRow('SELECT `id_parent` FROM `'._DB_PREFIX_.'cms_category` WHERE `id_cms_category` = '.(int)$i);
  381. if (!isset($result['id_parent'])) return false;
  382. if ($result['id_parent'] == $id_cms_category) return false;
  383. if ($result['id_parent'] == 1) return true;
  384. $i = $result['id_parent'];
  385. }
  386. }
  387. public static function getLinkRewrite($id_cms_category, $id_lang)
  388. {
  389. if (!Validate::isUnsignedId($id_cms_category) || !Validate::isUnsignedId($id_lang))
  390. return false;
  391. if (isset(self::$_links[$id_cms_category.'-'.$id_lang]))
  392. return self::$_links[$id_cms_category.'-'.$id_lang];
  393. $result = Db::getInstance()->getRow('
  394. SELECT cl.`link_rewrite`
  395. FROM `'._DB_PREFIX_.'cms_category` c
  396. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON c.`id_cms_category` = cl.`id_cms_category`
  397. WHERE `id_lang` = '.(int)$id_lang.'
  398. AND c.`id_cms_category` = '.(int)$id_cms_category);
  399. self::$_links[$id_cms_category.'-'.$id_lang] = $result['link_rewrite'];
  400. return $result['link_rewrite'];
  401. }
  402. public function getLink(Link $link = null)
  403. {
  404. if (!$link)
  405. $link = Context::getContext()->link;
  406. return $link->getCMSCategoryLink($this->id, $this->link_rewrite);
  407. }
  408. public function getName($id_lang = null)
  409. {
  410. $context = Context::getContext();
  411. if (!$id_lang)
  412. {
  413. if (isset($this->name[$context->language->id]))
  414. $id_lang = $context->language->id;
  415. else
  416. $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  417. }
  418. return isset($this->name[$id_lang]) ? $this->name[$id_lang] : '';
  419. }
  420. /**
  421. * Light back office search for categories
  422. *
  423. * @param integer $id_lang Language ID
  424. * @param string $query Searched string
  425. * @param boolean $unrestricted allows search without lang and includes first CMSCategory and exact match
  426. * @return array Corresponding categories
  427. */
  428. public static function searchByName($id_lang, $query, $unrestricted = false)
  429. {
  430. if ($unrestricted === true)
  431. return Db::getInstance()->getRow('
  432. SELECT c.*, cl.*
  433. FROM `'._DB_PREFIX_.'cms_category` c
  434. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category`)
  435. WHERE `name` LIKE \''.pSQL($query).'\'');
  436. else
  437. return Db::getInstance()->executeS('
  438. SELECT c.*, cl.*
  439. FROM `'._DB_PREFIX_.'cms_category` c
  440. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category` AND `id_lang` = '.(int)$id_lang.')
  441. WHERE `name` LIKE \'%'.pSQL($query).'%\' AND c.`id_cms_category` != 1');
  442. }
  443. /**
  444. * Retrieve CMSCategory by name and parent CMSCategory id
  445. *
  446. * @param integer $id_lang Language ID
  447. * @param string $CMSCategory_name Searched CMSCategory name
  448. * @param integer $id_parent_CMSCategory parent CMSCategory ID
  449. * @return array Corresponding CMSCategory
  450. * @deprecated
  451. */
  452. public static function searchByNameAndParentCMSCategoryId($id_lang, $CMSCategory_name, $id_parent_CMSCategory)
  453. {
  454. Tools::displayAsDeprecated();
  455. return Db::getInstance()->getRow('
  456. SELECT c.*, cl.*
  457. FROM `'._DB_PREFIX_.'cms_category` c
  458. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category` AND `id_lang` = '.(int)$id_lang.')
  459. WHERE `name` LIKE \''.pSQL($CMSCategory_name).'\'
  460. AND c.`id_cms_category` != 1
  461. AND c.`id_parent` = '.(int)$id_parent_CMSCategory);
  462. }
  463. /**
  464. * Get Each parent CMSCategory of this CMSCategory until the root CMSCategory
  465. *
  466. * @param integer $id_lang Language ID
  467. * @return array Corresponding categories
  468. */
  469. public function getParentsCategories($id_lang = null)
  470. {
  471. if (is_null($id_lang))
  472. $id_lang = Context::getContext()->language->id;
  473. $categories = null;
  474. $id_current = $this->id;
  475. while (true)
  476. {
  477. $query = '
  478. SELECT c.*, cl.*
  479. FROM `'._DB_PREFIX_.'cms_category` c
  480. LEFT JOIN `'._DB_PREFIX_.'cms_category_lang` cl ON (c.`id_cms_category` = cl.`id_cms_category` AND `id_lang` = '.(int)$id_lang.')
  481. WHERE c.`id_cms_category` = '.(int)$id_current.' AND c.`id_parent` != 0
  482. ';
  483. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
  484. $categories[] = $result[0];
  485. if (!$result || $result[0]['id_parent'] == 1)
  486. return $categories;
  487. $id_current = $result[0]['id_parent'];
  488. }
  489. }
  490. public function updatePosition($way, $position)
  491. {
  492. if (!$res = Db::getInstance()->executeS('
  493. SELECT cp.`id_cms_category`, cp.`position`, cp.`id_parent`
  494. FROM `'._DB_PREFIX_.'cms_category` cp
  495. WHERE cp.`id_parent` = '.(int)$this->id_parent.'
  496. ORDER BY cp.`position` ASC'
  497. ))
  498. return false;
  499. foreach ($res as $category)
  500. if ((int)$category['id_cms_category'] == (int)$this->id)
  501. $moved_category = $category;
  502. if (!isset($moved_category) || !isset($position))
  503. return false;
  504. // < and > statements rather than BETWEEN operator
  505. // since BETWEEN is treated differently according to databases
  506. return (Db::getInstance()->execute('
  507. UPDATE `'._DB_PREFIX_.'cms_category`
  508. SET `position`= `position` '.($way ? '- 1' : '+ 1').'
  509. WHERE `position`
  510. '.($way
  511. ? '> '.(int)$moved_category['position'].' AND `position` <= '.(int)$position
  512. : '< '.(int)$moved_category['position'].' AND `position` >= '.(int)$position).'
  513. AND `id_parent`='.(int)$moved_category['id_parent'])
  514. && Db::getInstance()->execute('
  515. UPDATE `'._DB_PREFIX_.'cms_category`
  516. SET `position` = '.(int)$position.'
  517. WHERE `id_parent` = '.(int)$moved_category['id_parent'].'
  518. AND `id_cms_category`='.(int)$moved_category['id_cms_category']));
  519. }
  520. public static function cleanPositions($id_category_parent)
  521. {
  522. $result = Db::getInstance()->executeS('
  523. SELECT `id_cms_category`
  524. FROM `'._DB_PREFIX_.'cms_category`
  525. WHERE `id_parent` = '.(int)$id_category_parent.'
  526. ORDER BY `position`');
  527. $sizeof = count($result);
  528. for ($i = 0; $i < $sizeof; ++$i)
  529. {
  530. $sql = '
  531. UPDATE `'._DB_PREFIX_.'cms_category`
  532. SET `position` = '.(int)$i.'
  533. WHERE `id_parent` = '.(int)$id_category_parent.'
  534. AND `id_cms_category` = '.(int)$result[$i]['id_cms_category'];
  535. Db::getInstance()->execute($sql);
  536. }
  537. return true;
  538. }
  539. public static function getLastPosition($id_category_parent)
  540. {
  541. return (Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_.'cms_category` WHERE `id_parent` = '.(int)$id_category_parent));
  542. }
  543. public static function getUrlRewriteInformations($id_category)
  544. {
  545. $sql = '
  546. SELECT l.`id_lang`, c.`link_rewrite`
  547. FROM `'._DB_PREFIX_.'cms_category_lang` AS c
  548. LEFT JOIN `'._DB_PREFIX_.'lang` AS l ON c.`id_lang` = l.`id_lang`
  549. WHERE c.`id_cms_category` = '.(int)$id_category.'
  550. AND l.`active` = 1';
  551. $arr_return = Db::getInstance()->executeS($sql);
  552. return $arr_return;
  553. }
  554. }