PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Tag.php

https://gitlab.com/staging06/myproject
PHP | 275 lines | 209 code | 28 blank | 38 comment | 29 complexity | 42a8698dc88c319dda6d9ee0ffa7710b MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 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-2015 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 TagCore extends ObjectModel
  27. {
  28. /** @var int Language id */
  29. public $id_lang;
  30. /** @var string Name */
  31. public $name;
  32. /**
  33. * @see ObjectModel::$definition
  34. */
  35. public static $definition = array(
  36. 'table' => 'tag',
  37. 'primary' => 'id_tag',
  38. 'fields' => array(
  39. 'id_lang' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  40. 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),
  41. ),
  42. );
  43. protected $webserviceParameters = array(
  44. 'fields' => array(
  45. 'id_lang' => array('xlink_resource' => 'languages'),
  46. ),
  47. );
  48. public function __construct($id = null, $name = null, $id_lang = null)
  49. {
  50. $this->def = Tag::getDefinition($this);
  51. $this->setDefinitionRetrocompatibility();
  52. if ($id) {
  53. parent::__construct($id);
  54. } elseif ($name && Validate::isGenericName($name) && $id_lang && Validate::isUnsignedId($id_lang)) {
  55. $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
  56. SELECT *
  57. FROM `'._DB_PREFIX_.'tag` t
  58. WHERE `name` = \''.pSQL($name).'\' AND `id_lang` = '.(int)$id_lang);
  59. if ($row) {
  60. $this->id = (int)$row['id_tag'];
  61. $this->id_lang = (int)$row['id_lang'];
  62. $this->name = $row['name'];
  63. }
  64. }
  65. }
  66. public function add($autodate = true, $null_values = false)
  67. {
  68. if (!parent::add($autodate, $null_values)) {
  69. return false;
  70. } elseif (isset($_POST['products'])) {
  71. return $this->setProducts(Tools::getValue('products'));
  72. }
  73. return true;
  74. }
  75. /**
  76. * Add several tags in database and link it to a product
  77. *
  78. * @param int $id_lang Language id
  79. * @param int $id_product Product id to link tags with
  80. * @param string|array $tag_list List of tags, as array or as a string with comas
  81. * @return bool Operation success
  82. */
  83. public static function addTags($id_lang, $id_product, $tag_list, $separator = ',')
  84. {
  85. if (!Validate::isUnsignedId($id_lang)) {
  86. return false;
  87. }
  88. if (!is_array($tag_list)) {
  89. $tag_list = array_filter(array_unique(array_map('trim', preg_split('#\\'.$separator.'#', $tag_list, null, PREG_SPLIT_NO_EMPTY))));
  90. }
  91. $list = array();
  92. if (is_array($tag_list)) {
  93. foreach ($tag_list as $tag) {
  94. if (!Validate::isGenericName($tag)) {
  95. return false;
  96. }
  97. $tag = trim(Tools::substr($tag, 0, self::$definition['fields']['name']['size']));
  98. $tag_obj = new Tag(null, $tag, (int)$id_lang);
  99. /* Tag does not exist in database */
  100. if (!Validate::isLoadedObject($tag_obj)) {
  101. $tag_obj->name = $tag;
  102. $tag_obj->id_lang = (int)$id_lang;
  103. $tag_obj->add();
  104. }
  105. if (!in_array($tag_obj->id, $list)) {
  106. $list[] = $tag_obj->id;
  107. }
  108. }
  109. }
  110. $data = '';
  111. foreach ($list as $tag) {
  112. $data .= '('.(int)$tag.','.(int)$id_product.','.(int)$id_lang.'),';
  113. }
  114. $data = rtrim($data, ',');
  115. $result = Db::getInstance()->execute('
  116. INSERT INTO `'._DB_PREFIX_.'product_tag` (`id_tag`, `id_product`, `id_lang`)
  117. VALUES '.$data);
  118. if ($list != array()) {
  119. self::updateTagCount($list);
  120. }
  121. return $result;
  122. }
  123. public static function updateTagCount($tag_list = null)
  124. {
  125. if (!Module::getBatchMode()) {
  126. if ($tag_list != null) {
  127. $tag_list_query = ' AND pt.id_tag IN ('.implode(',', $tag_list).')';
  128. Db::getInstance()->execute('DELETE pt FROM `'._DB_PREFIX_.'tag_count` pt WHERE 1=1 '.$tag_list_query);
  129. } else {
  130. $tag_list_query = '';
  131. }
  132. Db::getInstance()->execute('REPLACE INTO `'._DB_PREFIX_.'tag_count` (id_group, id_tag, id_lang, id_shop, counter)
  133. SELECT cg.id_group, pt.id_tag, pt.id_lang, id_shop, COUNT(pt.id_tag) AS times
  134. FROM `'._DB_PREFIX_.'product_tag` pt
  135. INNER JOIN `'._DB_PREFIX_.'product_shop` product_shop
  136. USING (id_product)
  137. JOIN (SELECT DISTINCT id_group FROM `'._DB_PREFIX_.'category_group`) cg
  138. WHERE product_shop.`active` = 1
  139. AND EXISTS(SELECT 1 FROM `'._DB_PREFIX_.'category_product` cp
  140. LEFT JOIN `'._DB_PREFIX_.'category_group` cgo ON (cp.`id_category` = cgo.`id_category`)
  141. WHERE cgo.`id_group` = cg.id_group AND product_shop.`id_product` = cp.`id_product`)
  142. '.$tag_list_query.'
  143. GROUP BY pt.id_tag, pt.id_lang, cg.id_group, id_shop ORDER BY NULL');
  144. Db::getInstance()->execute('REPLACE INTO `'._DB_PREFIX_.'tag_count` (id_group, id_tag, id_lang, id_shop, counter)
  145. SELECT 0, pt.id_tag, pt.id_lang, id_shop, COUNT(pt.id_tag) AS times
  146. FROM `'._DB_PREFIX_.'product_tag` pt
  147. INNER JOIN `'._DB_PREFIX_.'product_shop` product_shop
  148. USING (id_product)
  149. WHERE product_shop.`active` = 1
  150. '.$tag_list_query.'
  151. GROUP BY pt.id_tag, pt.id_lang, id_shop ORDER BY NULL');
  152. }
  153. }
  154. public static function getMainTags($id_lang, $nb = 10)
  155. {
  156. $context = Context::getContext();
  157. if (Group::isFeatureActive()) {
  158. $groups = FrontController::getCurrentCustomerGroups();
  159. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  160. SELECT t.name, counter AS times
  161. FROM `'._DB_PREFIX_.'tag_count` pt
  162. LEFT JOIN `'._DB_PREFIX_.'tag` t ON (t.id_tag = pt.id_tag)
  163. WHERE pt.`id_group` '.(count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1').'
  164. AND pt.`id_lang` = '.(int)$id_lang.' AND pt.`id_shop` = '.(int)$context->shop->id.'
  165. ORDER BY times DESC
  166. LIMIT '.(int)$nb);
  167. } else {
  168. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  169. SELECT t.name, counter AS times
  170. FROM `'._DB_PREFIX_.'tag_count` pt
  171. LEFT JOIN `'._DB_PREFIX_.'tag` t ON (t.id_tag = pt.id_tag)
  172. WHERE pt.id_group = 0 AND pt.`id_lang` = '.(int)$id_lang.' AND pt.`id_shop` = '.(int)$context->shop->id.'
  173. ORDER BY times DESC
  174. LIMIT '.(int)$nb);
  175. }
  176. }
  177. public static function getProductTags($id_product)
  178. {
  179. if (!$tmp = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  180. SELECT t.`id_lang`, t.`name`
  181. FROM '._DB_PREFIX_.'tag t
  182. LEFT JOIN '._DB_PREFIX_.'product_tag pt ON (pt.id_tag = t.id_tag)
  183. WHERE pt.`id_product`='.(int)$id_product)) {
  184. return false;
  185. }
  186. $result = array();
  187. foreach ($tmp as $tag) {
  188. $result[$tag['id_lang']][] = $tag['name'];
  189. }
  190. return $result;
  191. }
  192. public function getProducts($associated = true, Context $context = null)
  193. {
  194. if (!$context) {
  195. $context = Context::getContext();
  196. }
  197. $id_lang = $this->id_lang ? $this->id_lang : $context->language->id;
  198. if (!$this->id && $associated) {
  199. return array();
  200. }
  201. $in = $associated ? 'IN' : 'NOT IN';
  202. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  203. SELECT pl.name, pl.id_product
  204. FROM `'._DB_PREFIX_.'product` p
  205. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON p.id_product = pl.id_product'.Shop::addSqlRestrictionOnLang('pl').'
  206. '.Shop::addSqlAssociation('product', 'p').'
  207. WHERE pl.id_lang = '.(int)$id_lang.'
  208. AND product_shop.active = 1
  209. '.($this->id ? ('AND p.id_product '.$in.' (SELECT pt.id_product FROM `'._DB_PREFIX_.'product_tag` pt WHERE pt.id_tag = '.(int)$this->id.')') : '').'
  210. ORDER BY pl.name');
  211. }
  212. public function setProducts($array)
  213. {
  214. $result = Db::getInstance()->delete('product_tag', 'id_tag = '.(int)$this->id);
  215. if (is_array($array)) {
  216. $array = array_map('intval', $array);
  217. $result &= ObjectModel::updateMultishopTable('Product', array('indexed' => 0), 'a.id_product IN ('.implode(',', $array).')');
  218. $ids = array();
  219. foreach ($array as $id_product) {
  220. $ids[] = '('.(int)$id_product.','.(int)$this->id.','.(int)$this->id_lang.')';
  221. }
  222. if ($result) {
  223. $result &= Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'product_tag (id_product, id_tag, id_lang) VALUES '.implode(',', $ids));
  224. if (Configuration::get('PS_SEARCH_INDEXATION')) {
  225. $result &= Search::indexation(false);
  226. }
  227. }
  228. }
  229. self::updateTagCount(array((int)$this->id));
  230. return $result;
  231. }
  232. public static function deleteTagsForProduct($id_product)
  233. {
  234. $tags_removed = Db::getInstance()->executeS('SELECT id_tag FROM '._DB_PREFIX_.'product_tag WHERE id_product='.(int)$id_product);
  235. $result = Db::getInstance()->delete('product_tag', 'id_product = '.(int)$id_product);
  236. Db::getInstance()->delete('tag', 'NOT EXISTS (SELECT 1 FROM '._DB_PREFIX_.'product_tag
  237. WHERE '._DB_PREFIX_.'product_tag.id_tag = '._DB_PREFIX_.'tag.id_tag)');
  238. $tag_list = array();
  239. foreach($tags_removed as $tag_removed) {
  240. $tag_list[] = $tag_removed['id_tag'];
  241. }
  242. if ($tag_list != array()) {
  243. self::updateTagCount($tag_list);
  244. }
  245. return $result;
  246. }
  247. }