BxDolMPhotoAlbums.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 BxDolMPhotoAlbums extends BxDolMData
  14. {
  15. /**
  16. * @var $_iTransferredAlbums transferred albums number
  17. */
  18. private $_iTransferredAlbums;
  19. public function __construct(&$oMigrationModule, &$oDb)
  20. {
  21. parent::__construct($oMigrationModule, $oDb);
  22. $this -> _sModuleName = 'photos_albums';
  23. $this -> _sTableWithTransKey = 'bx_albums_albums';
  24. }
  25. public function getTotalRecords()
  26. {
  27. $aResult = $this -> _mDb -> getRow("SELECT COUNT(*) as `count`, SUM(`ObjCount`) as `obj` FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_albums'] ."` WHERE `Type` = 'bx_photos' AND `Uri` <> 'Hidden'");
  28. return !(int)$aResult['count'] && !(int)$aResult['obj'] ? 0 : $aResult;
  29. }
  30. public function runMigration()
  31. {
  32. if (!$this -> getTotalRecords())
  33. {
  34. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  35. return BX_MIG_SUCCESSFUL;
  36. }
  37. $sWhereCount = '';
  38. if ($this -> _oConfig -> _bTransferEmpty)
  39. $sWhereCount = " AND `ObjCount` <> 0";
  40. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_photos'));
  41. $this -> createMIdField();
  42. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_albums'] ."`
  43. WHERE `Type` = 'bx_photos' AND `Uri` <> 'Hidden' {$sWhereCount} ORDER BY `ID` ASC");
  44. foreach($aResult as $iKey => $aValue)
  45. {
  46. $iProfileId = $this -> getProfileId((int)$aValue['Owner']);
  47. if (!$iProfileId)
  48. continue;
  49. $iAlbumId = $this -> isItemExisted($aValue['ID']);
  50. if (!$iAlbumId)
  51. {
  52. $sAlbumTitle = isset($aValue['Caption']) && $aValue['Caption'] ? $aValue['Caption'] : 'Profile Photos';
  53. $sQuery = $this -> _oDb -> prepare(
  54. "
  55. INSERT INTO
  56. `bx_albums_albums`
  57. SET
  58. `author` = ?,
  59. `added` = ?,
  60. `changed` = ?,
  61. `thumb` = 0,
  62. `title` = ?,
  63. `allow_view_to` = ?,
  64. `text` = ?,
  65. `status_admin` = ?
  66. ",
  67. $iProfileId,
  68. $aValue['Date'] ? $aValue['Date'] : time(),
  69. $aValue['Date'] ? $aValue['Date'] : time(),
  70. $sAlbumTitle,
  71. $this -> getPrivacy($aValue['Owner'], (int)$aValue['AllowAlbumView'], 'photos', 'album_view'),
  72. $aValue['Description'],
  73. $aValue['Status'] == 'active' ? 'active' : 'hidden'
  74. );
  75. $this -> _oDb -> query($sQuery);
  76. $iAlbumId = $this -> _oDb -> lastId();
  77. if (!$iAlbumId)
  78. {
  79. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_photos_album_error'));
  80. return BX_MIG_FAILED;
  81. }
  82. $this -> setMID($iAlbumId, $aValue['ID']);
  83. }
  84. $iAlbumsCmts = $this -> transferComments($iAlbumId, $aValue['ID'], 'photo_albums');
  85. if ($iAlbumsCmts)
  86. $this -> _oDb -> query("UPDATE `bx_albums_albums` SET `comments` = :comments WHERE `id` = :id", array('id' => $iAlbumId, 'comments' => $iAlbumsCmts));
  87. $this -> migrateAlbumPhotos($aValue['ID'], $iProfileId, $iAlbumId);
  88. $this -> _iTransferredAlbums++;
  89. }
  90. // set as finished;
  91. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_photos_albums_finished', $this -> _iTransferredAlbums, $this -> _iTransferred));
  92. return BX_MIG_SUCCESSFUL;
  93. }
  94. /**
  95. * Migrates all photo albums and users photos
  96. * @param int $iAlbumId original albums id
  97. * @param int $iProfileId una profile ID
  98. * @param int $iNewAlbumID created una Album
  99. * @return Integer
  100. */
  101. private function migrateAlbumPhotos($iAlbumId, $iProfileId, $iNewAlbumID){
  102. $aResult = $this -> _mDb -> getAll("SELECT *
  103. FROM `sys_albums_objects`
  104. LEFT JOIN `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] ."` ON `id_object` = `ID`
  105. WHERE `id_album` = :album ORDER BY `id_object` ASC", array('album' => $iAlbumId));
  106. $iTransferred = 0;
  107. foreach($aResult as $iKey => $aValue)
  108. {
  109. $sFileName = "{$aValue['ID']}.{$aValue['Ext']}";
  110. if ($this -> isFileExisted($iProfileId, $sFileName, $aValue['Date']))
  111. continue;
  112. $sImagePath = $this -> _sImagePhotoFiles . $sFileName;
  113. if (file_exists($sImagePath))
  114. {
  115. $oStorage = BxDolStorage::getObjectInstance('bx_albums_files');
  116. $iId = $oStorage -> storeFileFromPath($sImagePath, false, $iProfileId, $iNewAlbumID);
  117. if ($iId)
  118. {
  119. $this -> updateFilesDate($iId, $aValue['Date']);
  120. $sQuery = $this -> _oDb -> prepare("INSERT INTO `bx_albums_files2albums` SET `content_id` = ?, `file_id` = ?, `data` = ?, `title` = ?", $iNewAlbumID, $iId, $aValue['Size'], $aValue['Title']);
  121. $this -> _oDb -> query($sQuery);
  122. $iCmts = $this -> transferComments($iItemId = $this -> _oDb -> lastId(), $aValue['ID'], 'photo_albums_items');
  123. if ($iCmts)
  124. $this -> _oDb -> query("UPDATE `bx_albums_files2albums` SET `comments` = :comments WHERE `id` = :id", array('id' => $iItemId, 'comments' => $iCmts));
  125. $this -> _iTransferred++;
  126. $iTransferred++;
  127. $this -> transferTags((int)$aValue['ID'], $iId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  128. $this -> transferFavorites((int)$aValue['ID'], $iId);
  129. }
  130. }
  131. }
  132. return $iTransferred;
  133. }
  134. private function transferFavorites($iPhotoId, $iNewID){
  135. $aData = $this->_mDb->getRow("SELECT * FROM `bx_photos_favorites` WHERE `ID`=:id LIMIT 1", array('id' => $iPhotoId));
  136. if (empty($aData))
  137. return false;
  138. $iProfileId = $this -> getProfileId((int)$aData['Profile']);
  139. if (!$iProfileId)
  140. return false;
  141. $sQuery = $this -> _oDb -> prepare("INSERT INTO `bx_albums_favorites_media_track` SET `object_id` = ?, `author_id` = ?, `date` = ?", $iNewID, $iProfileId, ($aData['Date'] ? $aData['Date'] : time()));
  142. return $this -> _oDb -> query($sQuery);
  143. }
  144. private function isFileExisted($iAuthor, $sTitle, $iDate){
  145. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `bx_albums_files` WHERE `profile_id` = ? AND `file_name` = ? AND `added` = ? LIMIT 1", $iAuthor, $sTitle, $iDate);
  146. return (bool)$this -> _oDb -> getOne($sQuery);
  147. }
  148. private function updateFilesDate($iId, $iDate){
  149. $sQuery = $this -> _oDb -> prepare("UPDATE `bx_albums_files` SET `added`=? WHERE `id` = ?", $iDate, $iId);
  150. return $this -> _oDb -> query($sQuery);
  151. }
  152. public function removeContent()
  153. {
  154. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  155. return false;
  156. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  157. $iNumber = 0;
  158. if (!empty($aRecords))
  159. {
  160. foreach($aRecords as $iKey => $aValue)
  161. {
  162. BxDolService::call('bx_albums', 'delete_entity', array($aValue['id']));
  163. $iNumber++;
  164. }
  165. }
  166. parent::removeContent();
  167. return $iNumber;
  168. }
  169. }
  170. /** @} */