BxDolMEvents.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 BxDolMEvents extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'events';
  19. $this -> _sTableWithTransKey = 'bx_events_data';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `Status` = 'approved'");
  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_events'));
  33. $this -> createMIdField();
  34. $iEventId = $this -> getLastMIDField();
  35. $sStart = '';
  36. if ($iEventId)
  37. $sStart = " AND `ID` >= {$iEventId}";
  38. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `Status` = 'approved' {$sStart} ORDER BY `ID`");
  39. $iCmts = 0;
  40. foreach($aResult as $iKey => $aValue)
  41. {
  42. $iProfileId = $this -> getProfileId((int)$aValue['ResponsibleID']);
  43. $iEventId = $this -> isItemExisted($aValue['ID']);
  44. if (!$iProfileId)
  45. continue;
  46. if (!$iEventId)
  47. {
  48. $sQuery = $this -> _oDb -> prepare(
  49. "
  50. INSERT INTO
  51. `{$this -> _sTableWithTransKey}`
  52. SET
  53. `author` = ?,
  54. `added` = ?,
  55. `changed` = ?,
  56. `event_name` = ?,
  57. `event_desc` = ?,
  58. `event_cat` = ?,
  59. `views` = ?,
  60. `featured` = ?,
  61. `allow_view_to` = ?,
  62. `join_confirmation` = ?,
  63. `date_start` = ?,
  64. `date_end` = ?
  65. ",
  66. $iProfileId,
  67. isset($aValue['Date']) ? $aValue['Date'] : time(),
  68. isset($aValue['Date']) ? $aValue['Date'] : time(),
  69. isset($aValue['Title']) ? $aValue['Title'] : '',
  70. isset($aValue['Description']) ? $aValue['Description'] : '',
  71. $this -> transferCategory($aValue['Categories']),
  72. isset($aValue['Views']) ? (int)$aValue['Views'] : 0,
  73. isset($aValue['Featured']) ? (int)$aValue['Featured'] : 0,
  74. $this -> getPrivacy((int)$aValue['ResponsibleID'], isset($aValue['allow_view_event_to']) ? (int)$aValue['allow_view_event_to'] : 0, 'events', 'view_event'),
  75. isset($aValue['JoinConfirmation']) ? (int)$aValue['JoinConfirmation'] : 0,
  76. isset($aValue['EventStart']) ? $aValue['EventStart'] : time(),
  77. isset($aValue['EventEnd']) ? $aValue['EventEnd'] : time()
  78. );
  79. $this -> _oDb -> query($sQuery);
  80. $iEventId = $this -> _oDb -> lastId();
  81. if (!$iEventId)
  82. {
  83. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_events_error', (int)$aValue['ID']));
  84. return BX_MIG_FAILED;
  85. }
  86. $this -> setMID($iEventId, $aValue['ID']);
  87. $this -> exportFans($aValue['ID'], $this-> addEventToProfiles($iProfileId, $iEventId));
  88. }
  89. $iCmts = $this -> transferComments($iEventId, (int)$aValue['ID'], 'events');
  90. $iPic = $this -> exportPictures($aValue['ID'], $iProfileId);
  91. $this -> _iTransferred++;
  92. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}`
  93. SET
  94. `picture` = :pic,
  95. `comments` = :cmts
  96. WHERE `id` = :id", array('pic' => $iPic, 'id' => $iEventId, 'cmts' => $iCmts));
  97. $this -> transferTags((int)$aValue['ID'], $iEventId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  98. }
  99. // set as finished;
  100. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_events_finished', $this -> _iTransferred, $iCmts));
  101. return BX_MIG_SUCCESSFUL;
  102. }
  103. protected function transferCategory($sCategory, $sPrefix = 'bx_events', $sPreValueCateg = 'bx_events_cats', $iValue = 0, $sData = '')
  104. {
  105. return parent::transferCategory($sCategory, $sPrefix, $sPreValueCateg);
  106. }
  107. /**
  108. * Transfers all Dolphin's events pictures
  109. *
  110. * @param int $iEventId Dolphin's event's id
  111. * @param int $iProfileId event's owner id in UNA events table
  112. * @return int first transferred events image's id
  113. */
  114. private function exportPictures($iEventId, $iProfileId)
  115. {
  116. $iFirstImage = 0;
  117. $iProfileId = (int)$iProfileId;
  118. $aPhotos = $this -> _mDb -> getAll("SELECT `p`.* FROM `bx_photos_main` as `p`
  119. LEFT JOIN `bx_events_images` as `e` ON `p`.`ID` = `e`.`media_id`
  120. WHERE `e`.`entry_id` = :id", array('id' => $iEventId));
  121. if (empty($aPhotos))
  122. return false;
  123. foreach($aPhotos as $iKey => $aFile)
  124. {
  125. $sImageFile = $this -> _sImagePhotoFiles . "{$aFile['ID']}.{$aFile['Ext']}";
  126. if (file_exists($sImageFile))
  127. {
  128. $oStorage = BxDolStorage::getObjectInstance('bx_events_pics');
  129. $iId = $oStorage->storeFileFromPath($sImageFile, false, $iProfileId);
  130. $oStorage -> afterUploadCleanup($iId, $iProfileId);
  131. $iFirstImage = $iFirstImage ? $iFirstImage : $iId;
  132. }
  133. }
  134. return $iFirstImage;
  135. }
  136. /**
  137. * Add event to sys_profiles table in una
  138. *
  139. * @param int $iProfileId profile id
  140. * @param int $iEventId event id
  141. * @return int last insert id
  142. */
  143. private function addEventToProfiles($iProfileId, $iEventId)
  144. {
  145. $sQuery = $this -> _oDb -> prepare("
  146. INSERT INTO
  147. `sys_profiles`
  148. SET
  149. `account_id` = ?,
  150. `type` = 'bx_events',
  151. `content_id` = ?,
  152. `status` = 'active'
  153. ", (int)BxDolProfile::getInstance($iProfileId)->getAccountId(), $iEventId);
  154. $this -> _oDb -> query($sQuery);
  155. return $this -> _oDb -> lastId();
  156. }
  157. /**
  158. * Transfer Event's participants lists
  159. *
  160. * @param int $iMID event's id in Dolphin
  161. * @param int $iEventProfileId events id from sys_profile
  162. * @return int participants number
  163. */
  164. private function exportFans($iMID, $iEventProfileId)
  165. {
  166. $aFans = $this -> _mDb -> getAll("SELECT `p` . * , IF( `a`.`id_entry` IS NULL , 0, 1 ) AS `admin`
  167. FROM `bx_events_participants` AS `p`
  168. LEFT JOIN `bx_events_admins` AS `a` ON `p`.`id_entry` = `a`.`id_entry` AND `p`.`id_profile` = `a`.`id_profile`
  169. WHERE `p`.`id_entry`=:id", array('id' => $iMID));
  170. foreach($aFans as $iKey => $aValue)
  171. {
  172. $iProfileId = $this -> getProfileId($aValue['id_profile']);
  173. $sQuery = $this -> _oDb -> prepare("
  174. INSERT IGNORE INTO
  175. `bx_events_fans`
  176. SET
  177. `initiator` = ?,
  178. `content` = ?,
  179. `mutual` = ?,
  180. `added` = ?
  181. ", $iProfileId, $iEventProfileId, $aValue['confirmed'], $aValue['when']);
  182. if(!$this -> _oDb -> query($sQuery))
  183. return _t('_bx_dolphin_migration_friends_exports_error');
  184. if ($aValue['confirmed']) {
  185. $sQuery = $this->_oDb->prepare("
  186. INSERT IGNORE INTO
  187. `bx_events_fans`
  188. SET
  189. `initiator` = ?,
  190. `content` = ?,
  191. `mutual` = ?,
  192. `added` = ?
  193. ", $iEventProfileId, $iProfileId, 1, $aValue['when']);
  194. if (!$this->_oDb->query($sQuery))
  195. return _t('_bx_dolphin_migration_friends_exports_error');
  196. }
  197. if ((int)$aValue['admin']) {
  198. $this->_oDb->query("REPLACE INTO `bx_events_admins` SET `fan_id` = :fan, `group_profile_id` = :group",
  199. array('fan' => $iProfileId, 'group' => $iEventProfileId));
  200. }
  201. }
  202. }
  203. public function removeContent()
  204. {
  205. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  206. return false;
  207. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0 ");
  208. $iNumber = 0;
  209. if (!empty($aRecords))
  210. {
  211. foreach($aRecords as $iKey => $aValue)
  212. {
  213. BxDolService::call('bx_events', 'delete_entity_service', array($aValue['id'], true));
  214. $iNumber++;
  215. }
  216. }
  217. parent::removeContent();
  218. return $iNumber;
  219. }
  220. }
  221. /** @} */