BxDolMPolls.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup DolphinMigration Dolphin Migration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxDolMData.php');
  12. bx_import('BxDolStorage');
  13. class BxDolMPolls extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'polls';
  19. $this -> _sTableWithTransKey = 'bx_polls_entries';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "`");
  24. }
  25. public function runMigration()
  26. {
  27. if (!$this -> getTotalRecords())
  28. {
  29. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  30. return BX_MIG_SUCCESSFUL;
  31. }
  32. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_polls'));
  33. $this -> createMIdField();
  34. $iPollId = $this -> getLastMIDField();
  35. $sStart = '';
  36. if ($iPollId)
  37. $sStart = " WHERE `id_poll` >= {$iPollId}";
  38. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` {$sStart} ORDER BY `id_poll`");
  39. $iCmts = 0;
  40. foreach($aResult as $iKey => $aValue)
  41. {
  42. $iProfileId = $this -> getProfileId((int)$aValue['id_profile']);
  43. if (!$iProfileId)
  44. continue;
  45. $iPollId = $this -> isItemExisted($aValue['id_poll']);
  46. if (!$iPollId)
  47. {
  48. $sQuery = $this -> _oDb -> prepare(
  49. "
  50. INSERT INTO
  51. `{$this -> _sTableWithTransKey}`
  52. SET
  53. `author` = ?,
  54. `added` = ?,
  55. `changed` = ?,
  56. `text` = ?,
  57. `cat` = ?,
  58. `featured` = ?,
  59. `allow_view_to` = ?,
  60. `status_admin` = ?
  61. ",
  62. $iProfileId,
  63. isset($aValue['poll_date']) ? $aValue['poll_date'] : time(),
  64. isset($aValue['poll_date']) ? $aValue['poll_date'] : time(),
  65. isset($aValue['poll_question']) ? $aValue['poll_question'] : '',
  66. $this -> transferCategory($aValue['poll_categories']),
  67. isset($aValue['poll_featured']) ? (int)$aValue['poll_featured'] : 0,
  68. $this -> getPrivacy((int)$aValue['id_profile'], isset($aValue['allow_view_to']) ? (int)$aValue['allow_view_to'] : 0, 'poll', 'view'),
  69. $aValue['poll_status'] = 'active' && $aValue['poll_approval'] ? 'active' : 'hidden'
  70. );
  71. $this -> _oDb -> query($sQuery);
  72. $iPollId = $this -> _oDb -> lastId();
  73. if (!$iPollId)
  74. {
  75. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_polls_error', (int)$aValue['id_poll']));
  76. return BX_MIG_FAILED;
  77. }
  78. $this -> setMID($iPollId, $aValue['id_poll']);
  79. $this -> exportAnswers($aValue['poll_results'], $aValue['poll_answers'], $iPollId);
  80. }
  81. $iCmts = $this -> transferComments($iPollId, (int)$aValue['id_poll'], 'polls');
  82. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}`
  83. SET
  84. `comments` = :cmts
  85. WHERE `id` = :id", array('id' => $iPollId, 'cmts' => $iCmts));
  86. $this -> _iTransferred++;
  87. $this -> transferTags($aValue['id_poll'], $iPollId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  88. }
  89. // set as finished;
  90. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_polls_finished', $this -> _iTransferred));
  91. return BX_MIG_SUCCESSFUL;
  92. }
  93. protected function transferCategory($sCategory, $sPrefix = 'bx_polls', $sPreValueCateg = 'bx_polls_cats', $iValue = 0, $sData = '')
  94. {
  95. return parent::transferCategory($sCategory, $sPrefix, $sPreValueCateg);
  96. }
  97. private function exportAnswers($sResults, $sQuestions, $iPollId)
  98. {
  99. $aResults = preg_split('/[;]/', $sResults, -1, PREG_SPLIT_NO_EMPTY);
  100. $aQuestions = preg_split('/<delim>/', $sQuestions, -1, PREG_SPLIT_NO_EMPTY);
  101. if (empty($aQuestions))
  102. return false;
  103. $aObjects = array();
  104. foreach($aQuestions as $iKey => $sQ)
  105. {
  106. $sQuery = $this -> _oDb -> prepare("
  107. INSERT INTO
  108. `bx_polls_subentries`
  109. SET
  110. `entry_id` = ?,
  111. `title` = ?,
  112. `rate` = ?,
  113. `votes` = ?,
  114. `order` = ?
  115. ", $iPollId, $sQ, (int)($aResults[$iKey] != 0), $aResults[$iKey], $iKey);
  116. $this -> _oDb -> query($sQuery);
  117. $aObjects[$this -> _oDb -> lastId()] = $aResults[$iKey];
  118. }
  119. foreach($aObjects as $iKey => $iValue)
  120. {
  121. $sQuery = $this -> _oDb -> prepare("
  122. INSERT INTO
  123. `bx_polls_votes_subentries`
  124. SET
  125. `object_id` = ?,
  126. `count` = ?,
  127. `sum` = ?
  128. ", $iKey, $iValue, $iValue);
  129. $this -> _oDb -> query($sQuery);
  130. }
  131. return true;
  132. }
  133. public function removeContent()
  134. {
  135. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  136. return false;
  137. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  138. $iNumber = 0;
  139. if (!empty($aRecords))
  140. {
  141. foreach($aRecords as $iKey => $aValue)
  142. {
  143. BxDolService::call('bx_polls', 'delete_entity', array($aValue['id']));
  144. $iNumber++;
  145. }
  146. }
  147. parent::removeContent();
  148. return $iNumber;
  149. }
  150. }
  151. /** @} */