PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/CMS.php

https://github.com/netplayer/PrestaShop
PHP | 225 lines | 166 code | 28 blank | 31 comment | 16 complexity | fbd3a8be76542ed81413cd3f0ebef0fe 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 CMSCore extends ObjectModel
  27. {
  28. /** @var string Name */
  29. public $meta_title;
  30. public $meta_description;
  31. public $meta_keywords;
  32. public $content;
  33. public $link_rewrite;
  34. public $id_cms_category;
  35. public $position;
  36. public $indexation;
  37. public $active;
  38. /**
  39. * @see ObjectModel::$definition
  40. */
  41. public static $definition = array(
  42. 'table' => 'cms',
  43. 'primary' => 'id_cms',
  44. 'multilang' => true,
  45. 'fields' => array(
  46. 'id_cms_category' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
  47. 'position' => array('type' => self::TYPE_INT),
  48. 'indexation' => array('type' => self::TYPE_BOOL),
  49. 'active' => array('type' => self::TYPE_BOOL),
  50. // Lang fields
  51. 'meta_description' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
  52. 'meta_keywords' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
  53. 'meta_title' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
  54. 'link_rewrite' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
  55. 'content' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml', 'size' => 3999999999999),
  56. ),
  57. );
  58. protected $webserviceParameters = array(
  59. 'objectNodeName' => 'content',
  60. 'objectsNodeName' => 'content_management_system',
  61. );
  62. public function add($autodate = true, $null_values = false)
  63. {
  64. $this->position = CMS::getLastPosition((int)$this->id_cms_category);
  65. return parent::add($autodate, true);
  66. }
  67. public function update($null_values = false)
  68. {
  69. if (parent::update($null_values))
  70. return $this->cleanPositions($this->id_cms_category);
  71. return false;
  72. }
  73. public function delete()
  74. {
  75. if (parent::delete())
  76. return $this->cleanPositions($this->id_cms_category);
  77. return false;
  78. }
  79. public static function getLinks($id_lang, $selection = null, $active = true, Link $link = null)
  80. {
  81. if (!$link)
  82. $link = Context::getContext()->link;
  83. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  84. SELECT c.id_cms, cl.link_rewrite, cl.meta_title
  85. FROM '._DB_PREFIX_.'cms c
  86. LEFT JOIN '._DB_PREFIX_.'cms_lang cl ON (c.id_cms = cl.id_cms AND cl.id_lang = '.(int)$id_lang.')
  87. '.Shop::addSqlAssociation('cms', 'c').'
  88. WHERE 1
  89. '.(($selection !== null) ? ' AND c.id_cms IN ('.implode(',', array_map('intval', $selection)).')' : '').
  90. ($active ? ' AND c.`active` = 1 ' : '').
  91. 'GROUP BY c.id_cms
  92. ORDER BY c.`position`');
  93. $links = array();
  94. if ($result)
  95. foreach ($result as $row)
  96. {
  97. $row['link'] = $link->getCMSLink((int)$row['id_cms'], $row['link_rewrite']);
  98. $links[] = $row;
  99. }
  100. return $links;
  101. }
  102. public static function listCms($id_lang = null, $id_block = false, $active = true)
  103. {
  104. if (empty($id_lang))
  105. $id_lang = (int)Configuration::get('PS_LANG_DEFAULT');
  106. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  107. SELECT c.id_cms, l.meta_title
  108. FROM '._DB_PREFIX_.'cms c
  109. JOIN '._DB_PREFIX_.'cms_lang l ON (c.id_cms = l.id_cms)
  110. '.Shop::addSqlAssociation('cms', 'c').'
  111. '.(($id_block) ? 'JOIN '._DB_PREFIX_.'block_cms b ON (c.id_cms = b.id_cms)' : '').'
  112. WHERE l.id_lang = '.(int)$id_lang.(($id_block) ? ' AND b.id_block = '.(int)$id_block : '').($active ? ' AND c.`active` = 1 ' : '').'
  113. GROUP BY c.id_cms
  114. ORDER BY c.`position`');
  115. }
  116. public function updatePosition($way, $position)
  117. {
  118. if (!$res = Db::getInstance()->executeS('
  119. SELECT cp.`id_cms`, cp.`position`, cp.`id_cms_category`
  120. FROM `'._DB_PREFIX_.'cms` cp
  121. WHERE cp.`id_cms_category` = '.(int)$this->id_cms_category.'
  122. ORDER BY cp.`position` ASC'
  123. ))
  124. return false;
  125. foreach ($res as $cms)
  126. if ((int)$cms['id_cms'] == (int)$this->id)
  127. $moved_cms = $cms;
  128. if (!isset($moved_cms) || !isset($position))
  129. return false;
  130. // < and > statements rather than BETWEEN operator
  131. // since BETWEEN is treated differently according to databases
  132. return (Db::getInstance()->execute('
  133. UPDATE `'._DB_PREFIX_.'cms`
  134. SET `position`= `position` '.($way ? '- 1' : '+ 1').'
  135. WHERE `position`
  136. '.($way
  137. ? '> '.(int)$moved_cms['position'].' AND `position` <= '.(int)$position
  138. : '< '.(int)$moved_cms['position'].' AND `position` >= '.(int)$position).'
  139. AND `id_cms_category`='.(int)$moved_cms['id_cms_category'])
  140. && Db::getInstance()->execute('
  141. UPDATE `'._DB_PREFIX_.'cms`
  142. SET `position` = '.(int)$position.'
  143. WHERE `id_cms` = '.(int)$moved_cms['id_cms'].'
  144. AND `id_cms_category`='.(int)$moved_cms['id_cms_category']));
  145. }
  146. public static function cleanPositions($id_category)
  147. {
  148. $sql = '
  149. SELECT `id_cms`
  150. FROM `'._DB_PREFIX_.'cms`
  151. WHERE `id_cms_category` = '.(int)$id_category.'
  152. ORDER BY `position`';
  153. $result = Db::getInstance()->executeS($sql);
  154. for ($i = 0, $total = count($result); $i < $total; ++$i)
  155. {
  156. $sql = 'UPDATE `'._DB_PREFIX_.'cms`
  157. SET `position` = '.(int)$i.'
  158. WHERE `id_cms_category` = '.(int)$id_category.'
  159. AND `id_cms` = '.(int)$result[$i]['id_cms'];
  160. Db::getInstance()->execute($sql);
  161. }
  162. return true;
  163. }
  164. public static function getLastPosition($id_category)
  165. {
  166. $sql = '
  167. SELECT MAX(position) + 1
  168. FROM `'._DB_PREFIX_.'cms`
  169. WHERE `id_cms_category` = '.(int)$id_category;
  170. return (Db::getInstance()->getValue($sql));
  171. }
  172. public static function getCMSPages($id_lang = null, $id_cms_category = null, $active = true, $id_shop = null)
  173. {
  174. $sql = new DbQuery();
  175. $sql->select('*');
  176. $sql->from('cms', 'c');
  177. if ($id_lang)
  178. $sql->innerJoin('cms_lang', 'l', 'c.id_cms = l.id_cms AND l.id_lang = '.(int)$id_lang);
  179. if ($id_shop)
  180. $sql->innerJoin('cms_shop', 'cs', 'c.id_cms = cs.id_cms AND cs.id_shop = '.(int)$id_shop);
  181. if ($active)
  182. $sql->where('c.active = 1');
  183. if ($id_cms_category)
  184. $sql->where('c.id_cms_category = '.(int)$id_cms_category);
  185. $sql->orderBy('position');
  186. return Db::getInstance()->executeS($sql);
  187. }
  188. public static function getUrlRewriteInformations($id_cms)
  189. {
  190. $sql = 'SELECT l.`id_lang`, c.`link_rewrite`
  191. FROM `'._DB_PREFIX_.'cms_lang` AS c
  192. LEFT JOIN `'._DB_PREFIX_.'lang` AS l ON c.`id_lang` = l.`id_lang`
  193. WHERE c.`id_cms` = '.(int)$id_cms.'
  194. AND l.`active` = 1';
  195. return Db::getInstance()->executeS($sql);
  196. }
  197. }