UpgradeSchema.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Setup;
  9. use Magento\Framework\Setup\UpgradeSchemaInterface;
  10. use Magento\Framework\Setup\ModuleContextInterface;
  11. use Magento\Framework\Setup\SchemaSetupInterface;
  12. /**
  13. * Blog schema update
  14. */
  15. class UpgradeSchema implements UpgradeSchemaInterface
  16. {
  17. /**
  18. * {@inheritdoc}
  19. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  20. */
  21. public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
  22. {
  23. $installer = $setup;
  24. $setup->startSetup();
  25. $version = $context->getVersion();
  26. $connection = $setup->getConnection();
  27. if (version_compare($version, '2.0.1') < 0) {
  28. foreach (['magefan_blog_post_relatedpost', 'magefan_blog_post_relatedproduct'] as $tableName) {
  29. // Get module table
  30. $tableName = $setup->getTable($tableName);
  31. // Check if the table already exists
  32. if ($connection->isTableExists($tableName) == true) {
  33. $columns = [
  34. 'position' => [
  35. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  36. 'nullable' => false,
  37. 'comment' => 'Position',
  38. ],
  39. ];
  40. foreach ($columns as $name => $definition) {
  41. $connection->addColumn($tableName, $name, $definition);
  42. }
  43. }
  44. }
  45. $connection->addColumn(
  46. $setup->getTable('magefan_blog_post'),
  47. 'featured_img',
  48. [
  49. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  50. 'length' => 255,
  51. 'nullable' => true,
  52. 'comment' => 'Thumbnail Image',
  53. ]
  54. );
  55. }
  56. if (version_compare($version, '2.2.0') < 0) {
  57. /* Add author field to posts tabel */
  58. $connection->addColumn(
  59. $setup->getTable('magefan_blog_post'),
  60. 'author_id',
  61. [
  62. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  63. 'nullable' => true,
  64. 'comment' => 'Author ID',
  65. ]
  66. );
  67. $connection->addIndex(
  68. $setup->getTable('magefan_blog_post'),
  69. $setup->getIdxName($setup->getTable('magefan_blog_post'), ['author_id']),
  70. ['author_id']
  71. );
  72. }
  73. if (version_compare($version, '2.2.5') < 0) {
  74. /* Add layout field to posts and category tabels */
  75. foreach (['magefan_blog_post', 'magefan_blog_category'] as $table) {
  76. $table = $setup->getTable($table);
  77. $connection->addColumn(
  78. $setup->getTable($table),
  79. 'page_layout',
  80. [
  81. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  82. 'length' => 255,
  83. 'nullable' => true,
  84. 'comment' => 'Post Layout',
  85. ]
  86. );
  87. $connection->addColumn(
  88. $setup->getTable($table),
  89. 'layout_update_xml',
  90. [
  91. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  92. 'length' => '64k',
  93. 'nullable' => true,
  94. 'comment' => 'Post Layout Update Content',
  95. ]
  96. );
  97. $connection->addColumn(
  98. $setup->getTable($table),
  99. 'custom_theme',
  100. [
  101. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  102. 'length' => 100,
  103. 'nullable' => true,
  104. 'comment' => 'Post Custom Theme',
  105. ]
  106. );
  107. $connection->addColumn(
  108. $setup->getTable($table),
  109. 'custom_layout',
  110. [
  111. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  112. 'length' => 255,
  113. 'nullable' => true,
  114. 'comment' => 'Post Custom Template',
  115. ]
  116. );
  117. $connection->addColumn(
  118. $setup->getTable($table),
  119. 'custom_layout_update_xml',
  120. [
  121. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  122. 'length' => '64k',
  123. 'nullable' => true,
  124. 'comment' => 'Post Custom Layout Update Content',
  125. ]
  126. );
  127. $connection->addColumn(
  128. $setup->getTable($table),
  129. 'custom_theme_from',
  130. [
  131. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
  132. 'nullable' => true,
  133. 'comment' => 'Post Custom Theme Active From Date',
  134. ]
  135. );
  136. $connection->addColumn(
  137. $setup->getTable($table),
  138. 'custom_theme_to',
  139. [
  140. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_DATE,
  141. 'nullable' => true,
  142. 'comment' => 'Post Custom Theme Active To Date',
  143. ]
  144. );
  145. }
  146. }
  147. if (version_compare($version, '2.3.0') < 0) {
  148. /* Add meta title field to posts tabel */
  149. $connection->addColumn(
  150. $setup->getTable('magefan_blog_post'),
  151. 'meta_title',
  152. [
  153. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  154. 'length' => 255,
  155. 'nullable' => true,
  156. 'comment' => 'Post Meta Title',
  157. 'after' => 'title'
  158. ]
  159. );
  160. /* Add og tags fields to post tabel */
  161. foreach (['type', 'img', 'description', 'title'] as $type) {
  162. $connection->addColumn(
  163. $setup->getTable('magefan_blog_post'),
  164. 'og_' . $type,
  165. [
  166. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  167. 'length' => 255,
  168. 'nullable' => true,
  169. 'comment' => 'Post OG ' . ucfirst($type),
  170. 'after' => 'identifier'
  171. ]
  172. );
  173. }
  174. /* Add meta title field to category tabel */
  175. $connection->addColumn(
  176. $setup->getTable('magefan_blog_category'),
  177. 'meta_title',
  178. [
  179. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  180. 'length' => 255,
  181. 'nullable' => true,
  182. 'comment' => 'Category Meta Title',
  183. 'after' => 'title'
  184. ]
  185. );
  186. /**
  187. * Create table 'magefan_blog_tag'
  188. */
  189. $table = $setup->getConnection()->newTable(
  190. $setup->getTable('magefan_blog_tag')
  191. )->addColumn(
  192. 'tag_id',
  193. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  194. null,
  195. ['identity' => true, 'nullable' => false, 'primary' => true],
  196. 'Tag ID'
  197. )->addColumn(
  198. 'title',
  199. \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  200. 255,
  201. ['nullable' => true],
  202. 'Tag Title'
  203. )->addColumn(
  204. 'identifier',
  205. \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  206. 100,
  207. ['nullable' => true, 'default' => null],
  208. 'Tag String Identifier'
  209. )->addIndex(
  210. $setup->getIdxName('magefan_blog_tag', ['identifier']),
  211. ['identifier']
  212. )->setComment(
  213. 'Magefan Blog Tag Table'
  214. );
  215. $setup->getConnection()->createTable($table);
  216. /**
  217. * Create table 'magefan_blog_post_tag'
  218. */
  219. $table = $setup->getConnection()->newTable(
  220. $setup->getTable('magefan_blog_post_tag')
  221. )->addColumn(
  222. 'post_id',
  223. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  224. null,
  225. ['nullable' => false, 'primary' => true],
  226. 'Post ID'
  227. )->addColumn(
  228. 'tag_id',
  229. \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  230. null,
  231. ['nullable' => false, 'primary' => true],
  232. 'Tag ID'
  233. )->addIndex(
  234. $setup->getIdxName('magefan_blog_post_tag', ['tag_id']),
  235. ['tag_id']
  236. )->addForeignKey(
  237. $setup->getFkName('magefan_blog_post_tag', 'post_id', 'magefan_blog_post', 'post_id'),
  238. 'post_id',
  239. $setup->getTable('magefan_blog_post'),
  240. 'post_id',
  241. \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
  242. )->addForeignKey(
  243. $setup->getFkName('magefan_blog_post_tag', 'tag_id', 'magefan_blog_tag', 'tag_id'),
  244. 'tag_id',
  245. $setup->getTable('magefan_blog_tag'),
  246. 'tag_id',
  247. \Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
  248. )->setComment(
  249. 'Magefan Blog Post To Category Linkage Table'
  250. );
  251. $setup->getConnection()->createTable($table);
  252. }
  253. if (version_compare($version, '2.4.4') < 0) {
  254. $connection->addColumn(
  255. $setup->getTable('magefan_blog_post'),
  256. 'media_gallery',
  257. [
  258. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  259. 'length' => '2M',
  260. 'nullable' => true,
  261. 'comment' => 'Media Gallery',
  262. ]
  263. );
  264. }
  265. if (version_compare($version, '2.5.2') < 0) {
  266. $connection->addColumn(
  267. $setup->getTable('magefan_blog_post'),
  268. 'secret',
  269. [
  270. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  271. 'length' => '32',
  272. 'nullable' => true,
  273. 'comment' => 'Post Secret',
  274. ]
  275. );
  276. $connection->addColumn(
  277. $setup->getTable('magefan_blog_post'),
  278. 'views_count',
  279. [
  280. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
  281. null,
  282. 'nullable' => true,
  283. 'comment' => 'Post Views Count',
  284. ]
  285. );
  286. $connection->addColumn(
  287. $setup->getTable('magefan_blog_post'),
  288. 'is_recent_posts_skip',
  289. [
  290. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT,
  291. null,
  292. 'nullable' => true,
  293. 'comment' => 'Is Post Skipped From Recent Posts',
  294. ]
  295. );
  296. $connection->addIndex(
  297. $setup->getTable('magefan_blog_post'),
  298. $setup->getIdxName($setup->getTable('magefan_blog_post'), ['views_count']),
  299. ['views_count']
  300. );
  301. $connection->addIndex(
  302. $setup->getTable('magefan_blog_post'),
  303. $setup->getIdxName($setup->getTable('magefan_blog_post'), ['is_recent_posts_skip']),
  304. ['is_recent_posts_skip']
  305. );
  306. }
  307. if (version_compare($version, '2.5.3') < 0) {
  308. $connection->addColumn(
  309. $setup->getTable('magefan_blog_post'),
  310. 'short_content',
  311. [
  312. 'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
  313. 'length' => '2M',
  314. 'nullable' => true,
  315. 'comment' => 'Post Short Content',
  316. ]
  317. );
  318. }
  319. $setup->endSetup();
  320. }
  321. }