BxDolMVideoAlbums.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 BxDolMVideoAlbums extends BxDolMData {
  14. /**
  15. * @var $_iTransferredAlbums transferred albums number
  16. */
  17. private $_iTransferredAlbums;
  18. /**
  19. * @var $_sVideoFilesPath path to the video albums files
  20. */
  21. private $_sVideoFilesPath;
  22. private $_sVideoMigField = 'vid_id';
  23. public function __construct(&$oMigrationModule, &$oDb)
  24. {
  25. parent::__construct($oMigrationModule, $oDb);
  26. $this -> _sModuleName = 'videos_albums';
  27. $this -> _sTableWithTransKey = 'bx_albums_albums';
  28. $this -> _sVideoFilesPath = $this -> _oDb -> getExtraParam('root') . 'flash' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . "video" . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR;
  29. }
  30. public function getTotalRecords()
  31. {
  32. $aResult = $this -> _mDb -> getRow("SELECT COUNT(*) as `count`, SUM(`ObjCount`) as `obj` FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_albums'] ."` WHERE `Type` = 'bx_videos' AND `Uri` <> 'Hidden'");
  33. return !(int)$aResult['count'] && !(int)$aResult['obj'] ? 0 : $aResult;
  34. }
  35. public function runMigration()
  36. {
  37. if (!$this -> getTotalRecords())
  38. {
  39. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  40. return BX_MIG_SUCCESSFUL;
  41. }
  42. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_videos'));
  43. $this -> createMIdField($this -> _sVideoMigField);
  44. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_albums'] ."` WHERE `Type` = 'bx_videos' AND `Uri` <> 'Hidden' ORDER BY `ID` ASC");
  45. foreach($aResult as $iKey => $aValue)
  46. {
  47. $iProfileId = $this -> getProfileId((int)$aValue['Owner']);
  48. if (!$iProfileId)
  49. continue;
  50. $iAlbumId = $this -> isItemExisted($aValue['ID'], 'id', $this -> _sVideoMigField);
  51. if (!$iAlbumId)
  52. {
  53. $sAlbumTitle = isset($aValue['Caption']) && $aValue['Caption'] ? $aValue['Caption'] : 'Profile Videos';
  54. $sQuery = $this -> _oDb -> prepare(
  55. "
  56. INSERT INTO
  57. `bx_albums_albums`
  58. SET
  59. `author` = ?,
  60. `added` = ?,
  61. `changed` = ?,
  62. `thumb` = 0,
  63. `title` = ?,
  64. `allow_view_to` = ?,
  65. `text` = ?,
  66. `status_admin` = ?
  67. ",
  68. $iProfileId,
  69. $aValue['Date'] ? $aValue['Date'] : time(),
  70. $aValue['Date'] ? $aValue['Date'] : time(),
  71. $sAlbumTitle,
  72. $this -> getPrivacy($aValue['Owner'], (int)$aValue['AllowAlbumView'], 'videos', 'album_view'),
  73. $aValue['Description'],
  74. $aValue['Status'] == 'active' ? 'active' : 'hidden'
  75. );
  76. $this -> _oDb -> query($sQuery);
  77. $iAlbumId = $this -> _oDb -> lastId();
  78. if (!$iAlbumId)
  79. {
  80. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_videos_album_error'));
  81. return BX_MIG_FAILED;
  82. }
  83. $this -> setMID($iAlbumId, $aValue['ID'], 'id', $this -> _sVideoMigField);
  84. }
  85. $iAlbumsCmts = $this -> transferComments($iAlbumId, $aValue['ID'], 'video_albums');
  86. if ($iAlbumsCmts)
  87. $this -> _oDb -> query("UPDATE `bx_albums_albums` SET `comments` = :comments WHERE `id` = :id", array('id' => $iAlbumId, 'comments' => $iAlbumsCmts));
  88. $this -> migrateAlbumVideos($aValue['ID'], $iProfileId, $iAlbumId);
  89. $this -> _iTransferredAlbums++;
  90. }
  91. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_videos_albums_finished', $this -> _iTransferredAlbums, $this -> _iTransferred));
  92. return BX_MIG_SUCCESSFUL;
  93. }
  94. /**
  95. * Migrates all photo albums and users videos
  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 migrateAlbumVideos($iAlbumId, $iProfileId, $iNewAlbumID)
  102. {
  103. $aResult = $this -> _mDb -> getAll("SELECT *
  104. FROM `sys_albums_objects`
  105. LEFT JOIN `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] ."` ON `id_object` = `ID`
  106. WHERE `id_album` = :album ORDER BY `id_object` ASC", array('album' => $iAlbumId));
  107. $iTransferred = 0;
  108. foreach($aResult as $iKey => $aValue)
  109. {
  110. if ($this -> isFileExisted($iProfileId, $aValue['Title'], $aValue['Date']))
  111. continue;
  112. $sVideoPath = $this -> _sVideoFilesPath . "{$aValue['ID']}.m4v";
  113. if (file_exists($sVideoPath))
  114. {
  115. $oStorage = BxDolStorage::getObjectInstance('bx_albums_files');
  116. $iId = $oStorage -> storeFileFromPath($sVideoPath, false, $iProfileId);
  117. if ($iId)
  118. {
  119. $oStorage -> afterUploadCleanup($iId, $iProfileId);
  120. $sQuery = $this -> _oDb -> prepare("INSERT INTO `bx_albums_files2albums` SET `content_id` = ?, `file_id` = ?, `title` = ?", $iNewAlbumID, $iId, $aValue['Title']);
  121. $this -> _oDb -> query($sQuery);
  122. $iCmts = $this -> transferComments($iItemId = $this -> _oDb -> lastId(), $aValue['ID'], 'video_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. }
  129. }
  130. }
  131. return $iTransferred;
  132. }
  133. public function dropMID($sIdentFieldName = '', $sTableName = '')
  134. {
  135. return parent::dropMID($this -> _sVideoMigField);
  136. }
  137. private function isFileExisted($iAuthor, $sTitle, $iDate)
  138. {
  139. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `bx_albums_files` WHERE `profile_id` = ? AND `file_name` = ? AND `added` = ? LIMIT 1", $iAuthor, $sTitle, $iDate);
  140. return (bool)$this -> _oDb -> getOne($sQuery);
  141. }
  142. public function removeContent()
  143. {
  144. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sVideoMigField))
  145. return false;
  146. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sVideoMigField}` !=0");
  147. $iNumber = 0;
  148. if (!empty($aRecords))
  149. {
  150. foreach($aRecords as $iKey => $aValue)
  151. {
  152. BxDolService::call('bx_albums', 'delete_entity', array($aValue['id']));
  153. $iNumber++;
  154. }
  155. }
  156. parent::removeContent();
  157. return $iNumber;
  158. }
  159. }
  160. /** @} */