BxDolMVideos.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 BxDolMVideos extends BxDolMData
  14. {
  15. private $_sVideoFilesPath;
  16. public function __construct(&$oMigrationModule, &$oDb)
  17. {
  18. parent::__construct($oMigrationModule, $oDb);
  19. $this -> _sModuleName = 'videos';
  20. $this -> _sTableWithTransKey = 'bx_videos_entries';
  21. $this -> _sVideoFilesPath = $this -> _oDb -> getExtraParam('root') . 'flash' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . "video" . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR;
  22. }
  23. public function getTotalRecords()
  24. {
  25. return $this -> _mDb -> getOne("SELECT SUM(`ObjCount`) as `obj`
  26. FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_videos'] ."`
  27. WHERE `Type` = 'bx_videos' AND `Uri` <> 'Hidden'");
  28. }
  29. public function runMigration()
  30. {
  31. if (!$this -> getTotalRecords())
  32. {
  33. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  34. return BX_MIG_SUCCESSFUL;
  35. }
  36. $sWhereCount = '';
  37. if ($this -> _oConfig -> _bTransferEmpty)
  38. $sWhereCount = " AND `ObjCount` <> 0";
  39. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_videos'));
  40. $this -> createMIdField();
  41. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_videos'] ."`
  42. WHERE `Type` = 'bx_videos' AND `Uri` <> 'Hidden' {$sWhereCount} ORDER BY `ID` ASC");
  43. foreach($aResult as $iKey => $aValue)
  44. {
  45. $iProfileId = $this -> getProfileId((int)$aValue['Owner']);
  46. if (!$iProfileId)
  47. continue;
  48. $this -> migrateVideo($aValue['ID'], $iProfileId);
  49. }
  50. // set as finished;
  51. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_videos_finished', $this -> _iTransferred));
  52. return BX_MIG_SUCCESSFUL;
  53. }
  54. /**
  55. * Migrates all photo albums and users photos
  56. * @param int $iAlbumId original albums id
  57. * @param int $iProfileId una profile ID
  58. * @param int $iNewPhotoId created una Album
  59. * @return Integer
  60. */
  61. private function migrateVideo($iAlbumId, $iProfileId){
  62. $aResult = $this -> _mDb -> getAll("SELECT `m`.*, `a`.`AllowAlbumView`, `a`.`Status` as `admin_status`, `m`.`Status` as `status`
  63. FROM `sys_albums_objects` as `o`
  64. LEFT JOIN `sys_albums` as `a` ON `o`.`id_album` = `a`.`ID`
  65. LEFT JOIN `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] ."` as `m` ON `o`.`id_object` = `m`.`ID`
  66. WHERE `o`.`id_album` = :album ORDER BY `o`.`id_object` ASC", array('album' => $iAlbumId));
  67. $iTransferred = 0;
  68. foreach($aResult as $iKey => $aValue)
  69. {
  70. $iVideoId = $this -> isItemExisted($aValue['ID']);
  71. if ($iVideoId)
  72. continue;
  73. $sVideoTitle = $aValue['Title'];
  74. $sQuery = $this -> _oDb -> prepare(
  75. "
  76. INSERT INTO
  77. `bx_videos_entries`
  78. SET
  79. `author` = ?,
  80. `added` = ?,
  81. `changed` = ?,
  82. `video` = 0,
  83. `title` = ?,
  84. `allow_view_to` = ?,
  85. `text` = ?,
  86. `status_admin` = ?,
  87. `status` = ?,
  88. `cat` = ?
  89. ",
  90. $iProfileId,
  91. $aValue['Date'] ? $aValue['Date'] : time(),
  92. $aValue['Date'] ? $aValue['Date'] : time(),
  93. $sVideoTitle,
  94. $this -> getPrivacy($aValue['Owner'], (int)$aValue['AllowAlbumView'], 'videos', 'album_view'),
  95. $aValue['Description'],
  96. $aValue['admin_status'] == 'active' ? 'active' : 'hidden',
  97. $aValue['status'] == 'approved' ? 'active' : 'hidden',
  98. $this -> transferCategory($aValue['Categories'], 'bx_videos', 'bx_videos_cats')
  99. );
  100. $this -> _oDb -> query($sQuery);
  101. if ($iVideoId = $this -> _oDb -> lastId())
  102. $this -> setMID($iVideoId, $aValue['ID']);
  103. else
  104. continue;
  105. $sFileName = "{$aValue['ID']}.m4v";
  106. $sVideoPath = $this -> _sVideoFilesPath . $sFileName;
  107. if (file_exists($sVideoPath))
  108. {
  109. $oStorage = BxDolStorage::getObjectInstance('bx_videos_videos');
  110. $iId = $oStorage -> storeFileFromPath($sVideoPath, false, $iProfileId, $iVideoId);
  111. if ($iId)
  112. {
  113. $this -> _iTransferred++;
  114. $this->transferTags((int)$aValue['ID'], $iId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  115. $this->transferFavorites((int)$aValue['ID'], $iId);
  116. $this->transferSVotes((int)$aValue['ID'], $iId);
  117. $this->_oDb->query("UPDATE `bx_videos_entries` SET `comments` = :comments, `video`=:video WHERE `id` = :id", array('id' => $iVideoId, 'video' => $iId, 'comments' => $this->transferComments($iVideoId, $aValue['ID'], 'videos')));
  118. }
  119. }
  120. }
  121. return $iTransferred;
  122. }
  123. private function transferFavorites($iItemId, $iNewID){
  124. $aData = $this->_mDb->getRow("SELECT * FROM `bx_videos_favorites` WHERE `ID`=:id LIMIT 1", array('id' => $iItemId));
  125. if (empty($aData))
  126. return false;
  127. $iProfileId = $this -> getProfileId((int)$aData['Profile']);
  128. if (!$iProfileId)
  129. return false;
  130. $sQuery = $this -> _oDb -> prepare("INSERT INTO `bx_videos_favorites_track` SET `object_id` = ?, `author_id` = ?, `date` = ?", $iNewID, $iProfileId, ($aData['Date'] ? $aData['Date'] : time()));
  131. return $this -> _oDb -> query($sQuery);
  132. }
  133. private function transferSVotes($iItemId, $iNewID){
  134. $aData = $this->_mDb->getRow("SELECT * FROM `bx_videos_rating` WHERE `gal_id`=:id LIMIT 1", array('id' => $iItemId));
  135. if (empty($aData))
  136. return false;
  137. $sQuery = $this->_oDb->prepare("INSERT INTO `bx_videos_svotes` SET `object_id` = ?, `count` = ?, `sum` = ?", $iNewID, $aData['gal_rating_count'], $aData['gal_rating_sum']);
  138. $this->_oDb->query("UPDATE `bx_videos_entries` SET `svotes` = :votes WHERE `id` = :id", array('id' => $iItemId, 'votes' => $aData['gal_rating_count']));
  139. return $this->_oDb->query($sQuery);
  140. }
  141. public function removeContent()
  142. {
  143. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  144. return false;
  145. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  146. $iNumber = 0;
  147. if (!empty($aRecords))
  148. {
  149. foreach($aRecords as $iKey => $aValue)
  150. {
  151. BxDolService::call('bx_videos', 'delete_entity', array($aValue['id']));
  152. $iNumber++;
  153. }
  154. }
  155. parent::removeContent();
  156. return $iNumber;
  157. }
  158. }
  159. /** @} */