BxDolMPhotos.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 BxDolMPhotos extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'photos';
  19. $this -> _sTableWithTransKey = 'bx_photos_entries';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return $this -> _mDb -> getOne("SELECT SUM(`ObjCount`)
  24. FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_photos'] ."`
  25. WHERE `Type` = 'bx_photos' AND `Uri` <> 'Hidden'");
  26. }
  27. public function runMigration()
  28. {
  29. if (!$this -> getTotalRecords())
  30. {
  31. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  32. return BX_MIG_SUCCESSFUL;
  33. }
  34. $sWhereCount = '';
  35. if ($this -> _oConfig -> _bTransferEmpty)
  36. $sWhereCount = " AND `ObjCount` <> 0";
  37. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_photos'));
  38. $this -> createMIdField();
  39. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name_photos'] ."`
  40. WHERE `Type` = 'bx_photos' AND `Uri` <> 'Hidden' {$sWhereCount} ORDER BY `ID` ASC");
  41. foreach($aResult as $iKey => $aValue)
  42. {
  43. $iProfileId = $this -> getProfileId((int)$aValue['Owner']);
  44. if (!$iProfileId)
  45. continue;
  46. $this -> migratePhoto($aValue['ID'], $iProfileId);
  47. }
  48. // set as finished;
  49. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_photos_finished', $this -> _iTransferred));
  50. return BX_MIG_SUCCESSFUL;
  51. }
  52. /**
  53. * Migrates all photo albums and users photos
  54. * @param int $iAlbumId original albums id
  55. * @param int $iProfileId una profile ID
  56. * @param int $iNewPhotoId created una Album
  57. * @return Integer
  58. */
  59. private function migratePhoto($iAlbumId, $iProfileId){
  60. $aResult = $this -> _mDb -> getAll("SELECT `m`.*, `a`.`AllowAlbumView`, `a`.`Status` as `admin_status`, `m`.`Status` as `status`
  61. FROM `sys_albums_objects` as `o`
  62. LEFT JOIN `sys_albums` as `a` ON `o`.`id_album` = `a`.`ID`
  63. LEFT JOIN `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] ."` as `m` ON `o`.`id_object` = `m`.`ID`
  64. WHERE `o`.`id_album` = :album ORDER BY `o`.`id_object` ASC", array('album' => $iAlbumId));
  65. $iTransferred = 0;
  66. foreach($aResult as $iKey => $aValue)
  67. {
  68. $iPhotoId = $this -> isItemExisted($aValue['ID']);
  69. if ($iPhotoId)
  70. continue;
  71. $sPhotoTitle = $aValue['Title'];
  72. $sQuery = $this -> _oDb -> prepare(
  73. "
  74. INSERT INTO
  75. `bx_photos_entries`
  76. SET
  77. `author` = ?,
  78. `added` = ?,
  79. `changed` = ?,
  80. `thumb` = 0,
  81. `title` = ?,
  82. `allow_view_to` = ?,
  83. `text` = ?,
  84. `status_admin` = ?,
  85. `status` = ?,
  86. `cat` = ?
  87. ",
  88. $iProfileId,
  89. $aValue['Date'] ? $aValue['Date'] : time(),
  90. $aValue['Date'] ? $aValue['Date'] : time(),
  91. $sPhotoTitle,
  92. $this -> getPrivacy($aValue['Owner'], (int)$aValue['AllowAlbumView'], 'photos', 'album_view'),
  93. $aValue['Desc'],
  94. $aValue['admin_status'] == 'active' ? 'active' : 'hidden',
  95. $aValue['status'] == 'approved' ? 'active' : 'hidden',
  96. $this -> transferCategory($aValue['Categories'], 'bx_photos', 'bx_photos_cats')
  97. );
  98. $this -> _oDb -> query($sQuery);
  99. if ($iPhotoId = $this -> _oDb -> lastId())
  100. $this -> setMID($iPhotoId, $aValue['ID']);
  101. else
  102. continue;
  103. $sFileName = "{$aValue['ID']}.{$aValue['Ext']}";
  104. $sImagePath = $this -> _sImagePhotoFiles . $sFileName;
  105. if (file_exists($sImagePath))
  106. {
  107. $oStorage = BxDolStorage::getObjectInstance('bx_photos_photos');
  108. $iId = $oStorage -> storeFileFromPath($sImagePath, false, $iProfileId, $iPhotoId);
  109. if ($iId)
  110. {
  111. $this -> _iTransferred++;
  112. $iTransferred++;
  113. $this->updateExif($iPhotoId, array('thumb' => $iId, 'id' => $iPhotoId));
  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_photos_entries` SET `comments` = :comments, `thumb`=:thumb WHERE `id` = :id", array('id' => $iPhotoId, 'thumb' => $iId, 'comments' => $this->transferComments($iPhotoId, $aValue['ID'])));
  118. }
  119. }
  120. }
  121. return $iTransferred;
  122. }
  123. private function transferFavorites($iPhotoId, $iNewID){
  124. $aData = $this->_mDb->getRow("SELECT * FROM `bx_photos_favorites` WHERE `ID`=:id LIMIT 1", array('id' => $iPhotoId));
  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_photos_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($iPhotoId, $iNewID){
  134. $aData = $this->_mDb->getRow("SELECT * FROM `bx_photos_rating` WHERE `gal_id`=:id LIMIT 1", array('id' => $iPhotoId));
  135. if (empty($aData))
  136. return false;
  137. $sQuery = $this->_oDb->prepare("INSERT INTO `bx_photos_svotes` SET `object_id` = ?, `count` = ?, `sum` = ?", $iNewID, $aData['gal_rating_count'], $aData['gal_rating_sum']);
  138. $this->_oDb->query("UPDATE `bx_photos_entries` SET `svotes` = :votes WHERE `id` = :id", array('id' => $iPhotoId, '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_photos', 'delete_entity', array($aValue['id']));
  152. $iNumber++;
  153. }
  154. }
  155. parent::removeContent();
  156. return $iNumber;
  157. }
  158. function updateExif($iPhotoId, $aContentInfo)
  159. {
  160. $CNF = &$this->_oConfig->CNF;
  161. $oStorage = BxDolStorage::getObjectInstance('bx_photos_photos');
  162. $oTranscoder = BxDolTranscoderImage::getObjectInstance('bx_photos_preview');
  163. if (!$oStorage || !$oTranscoder)
  164. return false;
  165. $aInfo = bx_get_image_exif_and_size($oStorage, $oTranscoder, $aContentInfo['thumb']);
  166. $this->_oDb->query("UPDATE `bx_photos_entries` SET `exif`=:exif WHERE `id`=:id", array('exif' => $aInfo['exif'], 'id' => $iPhotoId));
  167. $aExif = unserialize($aInfo['exif']);
  168. if ($aContentInfo && isset($aExif['Make']) && !empty($CNF['OBJECT_METATAGS_MEDIA_CAMERA'])) {
  169. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS_MEDIA_CAMERA']);
  170. if ($oMetatags->keywordsIsEnabled()){
  171. $oMetatags->keywordsAddOne($aContentInfo['id'], $oMetatags->keywordsCameraModel($aExif));
  172. }
  173. }
  174. }
  175. }
  176. /** @} */