PageRenderTime 381ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/CMSCategory.php

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