/install-dev/upgrade/php/migrate_block_info_to_cms_block.php

https://gitlab.com/jslee1/PrestaShop · PHP · 133 lines · 86 code · 11 blank · 36 comment · 8 complexity · 154b377f6eae8e1f6211c2a8572ae749 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. function migrate_block_info_to_cms_block()
  27. {
  28. $res = true;
  29. $languages = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'lang`');
  30. //get ids cms of block information
  31. $id_blockinfos = Db::getInstance()->getValue('SELECT id_module FROM `'._DB_PREFIX_.'module` WHERE name = \'blockinfos\'');
  32. //get ids cms of block information
  33. $ids_cms = Db::getInstance()->executeS('SELECT id_cms FROM `'._DB_PREFIX_.'block_cms` WHERE `id_block` = '.(int)$id_blockinfos);
  34. //check if block info is installed and active
  35. if (is_array($ids_cms)) {
  36. //install module blockcms
  37. // Module::getInstanceByName('blockcms')->install()
  38. // 1) from module
  39. $ps_lang_default = Db::getInstance()->getValue('SELECT value
  40. FROM `'._DB_PREFIX_.'configuration`
  41. WHERE name="PS_LANG_DEFAULT"');
  42. // 2) parent::install()
  43. $result = Db::getInstance()->insert('module',
  44. array('name' => 'blockcms', 'active' => 1));
  45. $id_module = Db::getInstance()->insert_Id();
  46. // 3) hooks
  47. $hooks = array('leftColumn', 'rightColumn', 'footer', 'header');
  48. foreach ($hooks as $hook_name) {
  49. // do not pSql hook_name
  50. $row = Db::getInstance()->getRow('SELECT h.id_hook, '.$id_module.' as id_module, MAX(hm.position)+1 as position
  51. FROM `'._DB_PREFIX_.'hook_module` hm
  52. LEFT JOIN `'._DB_PREFIX_.'hook` h on hm.id_hook=h.id_hook
  53. WHERE h.name = "'.$hook_name.'" group by id_hook');
  54. $res &= Db::getInstance()->insert('hook_module', $row);
  55. }
  56. // module install
  57. $res &= Db::getInstance()->execute('
  58. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'cms_block`(
  59. `id_cms_block` int(10) unsigned NOT NULL auto_increment,
  60. `id_cms_category` int(10) unsigned NOT NULL,
  61. `location` tinyint(1) unsigned NOT NULL,
  62. `position` int(10) unsigned NOT NULL default \'0\',
  63. `display_store` tinyint(1) unsigned NOT NULL default \'1\',
  64. PRIMARY KEY (`id_cms_block`)
  65. ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
  66. $res &= Db::getInstance()->execute('
  67. INSERT INTO `'._DB_PREFIX_.'cms_block` (`id_cms_category`, `location`, `position`) VALUES(1, 0, 0)');
  68. $res &= Db::getInstance()->execute('
  69. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'cms_block_lang`(
  70. `id_cms_block` int(10) unsigned NOT NULL,
  71. `id_lang` int(10) unsigned NOT NULL,
  72. `name` varchar(40) NOT NULL default \'\',
  73. PRIMARY KEY (`id_cms_block`, `id_lang`)
  74. ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
  75. $query_lang = 'INSERT INTO `'._DB_PREFIX_.'cms_block_lang` (`id_cms_block`, `id_lang`) VALUES';
  76. foreach ($languages as $language) {
  77. $query_lang .= '(1, '.(int)($language['id_lang']).'),';
  78. }
  79. $query_lang = rtrim($query_lang, ',');
  80. $res &= Db::getInstance()->execute($query_lang);
  81. $res &= Db::getInstance()->execute('
  82. CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'cms_block_page`(
  83. `id_cms_block_page` int(10) unsigned NOT NULL auto_increment,
  84. `id_cms_block` int(10) unsigned NOT NULL,
  85. `id_cms` int(10) unsigned NOT NULL,
  86. `is_category` tinyint(1) unsigned NOT NULL,
  87. PRIMARY KEY (`id_cms_block_page`)
  88. ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
  89. $exist = Db::getInstance()->getValue('SELECT `id_configuration` FROM `'._DB_PREFIX_.'configuration` WHERE `name` = \'FOOTER_CMS\'');
  90. if ($exist) {
  91. $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'configuration` SET value = "" WHERE `name` = \'FOOTER_CMS\'');
  92. } else {
  93. $res &= Db::getInstance()->getValue('INSERT INTO `'._DB_PREFIX_.'configuration` (name, value) VALUES ("FOOTER_CMS", "")');
  94. }
  95. $exist = Db::getInstance()->getValue('SELECT `id_configuration` FROM `'._DB_PREFIX_.'configuration` WHERE `name` = \'FOOTER_BLOCK_ACTIVATION\'');
  96. if ($exist) {
  97. $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'configuration` SET value = "1" WHERE `name` = \'FOOTER_BLOCK_ACTIVATION\'');
  98. } else {
  99. $res &= Db::getInstance()->getValue('INSERT INTO `'._DB_PREFIX_.'configuration` (name, value) VALUES ("FOOTER_BLOCK_ACTIVATION", "1")');
  100. }
  101. $exist = Db::getInstance()->getValue('SELECT `id_configuration` FROM `'._DB_PREFIX_.'configuration` WHERE `name` = \'FOOTER_POWEREDBY\'');
  102. if ($exist) {
  103. $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'configuration` SET value = "1" WHERE `name` = \'FOOTER_POWEREDBY\'');
  104. } else {
  105. $res &= Db::getInstance()->getValue('INSERT INTO `'._DB_PREFIX_.'configuration` (name, value) VALUES ("FOOTER_POWEREDBY", "1")');
  106. }
  107. //add new block in new cms block
  108. $res &= Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cms_block`
  109. (`id_cms_category`, `name`, `location`, `position`)
  110. VALUES( 1, "", 0, 0)');
  111. $id_block = Db::getInstance()->insert_id();
  112. foreach ($languages as $language) {
  113. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cms_block_lang` (`id_cms_block`, `id_lang`, `name`) VALUES ('.(int)$id_block.', '.(int)$language['id_lang'].', \'Information\')');
  114. }
  115. //save ids cms of block information in new module cms bloc
  116. foreach ($ids_cms as $id_cms) {
  117. Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'cms_block_page` (`id_cms_block`, `id_cms`, `is_category`) VALUES ('.(int)$id_block.', '.(int)$id_cms['id_cms'].', 0)');
  118. }
  119. } else {
  120. return true;
  121. }
  122. }