BxDolMMemLevels.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. bx_import('BxDolProfile');
  12. bx_import('BxDolTranscoderImage');
  13. bx_import('BxDolStorage');
  14. require_once('BxDolMData.php');
  15. class BxDolMMemLevels extends BxDolMData
  16. {
  17. public function __construct(&$oMigrationModule, &$mDb)
  18. {
  19. parent::__construct($oMigrationModule, $mDb);
  20. $this -> _sModuleName = 'membership_levels';
  21. $this -> _sTableWithTransKey = 'sys_acl_levels';
  22. }
  23. public function getTotalRecords()
  24. {
  25. $iPaidMemberships = $this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `Active` = 'yes' AND `Purchasable` = 'yes'");
  26. $iMemberWithSetMembership = $this -> _mDb -> getOne("SELECT COUNT(*) FROM `sys_acl_levels_members`");
  27. return array($iPaidMemberships, $iMemberWithSetMembership);
  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. $this -> setResultStatus(_t("_bx_dolphin_migration_started_migration_{$this -> _sModuleName}"));
  37. $iPaidCount = $this -> transferMembershipLevels();
  38. $aResult = $this -> _mDb -> getAll("SELECT * FROM `sys_acl_levels_members` ORDER BY `IDMember` ASC");
  39. foreach($aResult as $iKey => $aValue)
  40. {
  41. $iProfileId = $this -> getProfileId($aValue['IDMember']);
  42. if($iProfileId)
  43. {
  44. $sQuery = $this -> _oDb -> prepare(
  45. "INSERT INTO
  46. `sys_acl_levels_members`
  47. SET
  48. `IDMember` = ?,
  49. `IDLevel` = ?,
  50. `DateStarts` = ?,
  51. `DateExpires` = ?,
  52. `TransactionID` = ?
  53. ",
  54. $iProfileId,
  55. $this-> getMembershipById($aValue['IDLevel']),
  56. $aValue['DateStarts'],
  57. $aValue['DateExpires'],
  58. $aValue['TransactionID']
  59. );
  60. if ($this -> _oDb -> query($sQuery))
  61. $this -> _iTransferred++;
  62. }
  63. }
  64. $this -> setResultStatus(_t("_bx_dolphin_migration_started_migration_{$this -> _sModuleName}_finished", $iPaidCount, $this -> _iTransferred));
  65. return BX_MIG_SUCCESSFUL;
  66. }
  67. private function getMembershipById($iOldId){
  68. $aCommon = array(1 => 1, 2 => 3);
  69. if (isset($aCommon[$iOldId]))
  70. return $aCommon[$iOldId];
  71. return $this -> _oDb -> getOne("SELECT `ID` FROM `sys_acl_levels` WHERE `{$this -> _sTransferFieldIdent}` = :id LIMIT 1", array('id' => $iOldId));
  72. }
  73. private function transferMembershipIcon($sIconName)
  74. {
  75. if (!$sIconName)
  76. return 'user';
  77. $iId = 0;
  78. $sImagePath = $this -> _oDb -> getExtraParam('root') . 'media' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'membership' . DIRECTORY_SEPARATOR . $sIconName;
  79. if($sImagePath){
  80. $oStorage = BxDolStorage::getObjectInstance('sys_images');
  81. $iId = $oStorage->storeFileFromPath($sImagePath, false);
  82. }
  83. return $iId;
  84. }
  85. /**
  86. * Transfer all purchasable membership levels
  87. */
  88. private function transferMembershipLevels()
  89. {
  90. $iCounter = 0;
  91. $aMembershipLevels = $this -> _mDb -> getAll("SELECT * FROM `sys_acl_levels` WHERE `Active` = 'yes' AND `Purchasable` = 'yes'");
  92. if (empty($aMembershipLevels))
  93. return false;
  94. $this -> createMIdField();
  95. $iOrder = $this -> _oDb -> getOne("SELECT MAX(`Order`) FROM `sys_acl_levels` LIMIT 1");
  96. foreach($aMembershipLevels as $iKey => $aLevel)
  97. {
  98. if ($this -> isItemExisted($aLevel['ID'], 'ID'))
  99. continue;
  100. $sName = strtolower($aLevel['Name']);
  101. $sLangKeyName = "_adm_prm_txt_level_{$sName}";
  102. $sLangKeyDescription = '';
  103. foreach($this -> _aLanguages as $sLangKey => $sValue) {
  104. $this->_oLanguage->addLanguageString($sLangKeyName, $aLevel['Name'], $this->_aLanguages[$sLangKey], 1, !$aLevel['Description']);
  105. if ($aLevel['Description']) {
  106. $sLangKeyDescription = '_sys_form_input_description_' . (time() + $iCounter);
  107. $this->_oLanguage->addLanguageString($sLangKeyDescription, $aLevel['Description'], $this->_aLanguages[$sLangKey], 1, true);
  108. }
  109. }
  110. $sQuery = $this -> _oDb -> prepare("INSERT INTO `sys_acl_levels`
  111. SET
  112. `Name` = ?,
  113. `Icon` = ?,
  114. `Description` = ?,
  115. `Active` = 'yes',
  116. `Order` = ?
  117. ",
  118. $sLangKeyName,
  119. $this -> transferMembershipIcon($aLevel['Icon']),
  120. $sLangKeyDescription,
  121. $iOrder++
  122. );
  123. if ($this -> _oDb -> query($sQuery) && $iId = $this -> _oDb -> lastId()) {
  124. $this -> setMID($iId, $aLevel['ID']);
  125. $aPrices = $this -> _mDb -> getAll("SELECT * FROM `sys_acl_level_prices` WHERE `IDLevel` = :id", array('id' => $aLevel['ID']));
  126. foreach($aPrices as $iKey => $aPrice){
  127. $sQuery = $this -> _oDb -> prepare("REPLACE INTO `bx_acl_level_prices`
  128. SET
  129. `level_id` = ?,
  130. `name` = ?,
  131. `period` = ?,
  132. `period_unit`='day',
  133. `price` = ?,
  134. `order` = ?
  135. ",
  136. $iId,
  137. "{$sName}-{$aPrice['Days']}-day",
  138. $aPrice['Days'],
  139. $aPrice['Price'],
  140. $aPrice['id']
  141. );
  142. $this -> _oDb -> query($sQuery);
  143. }
  144. $iCounter++;
  145. }
  146. }
  147. return $iCounter;
  148. }
  149. public function removeContent()
  150. {
  151. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  152. return false;
  153. $iNumber = 0;
  154. $aLevels = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  155. if (!empty($aLevels) && $this -> _oDb -> isTableExists('bx_acl_level_prices'))
  156. {
  157. foreach($aLevels as $iKey => $aLevel){
  158. $this->_oDb->query("DELETE FROM `bx_acl_level_prices` WHERE `level_id` = :id", array('id' => $aLevel['ID']));
  159. if(is_numeric($aLevel['Icon']))
  160. BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES)->deleteFile((int)$aLevel['Icon'], 0);
  161. $this->_oLanguage->deleteLanguageString($aLevel['Name']);
  162. $this->_oLanguage->deleteLanguageString($aLevel['Description']);
  163. $this->_oDb->query("DELETE FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` = :id", array('id' => $aLevel[$this -> _sTransferFieldIdent]));
  164. $this->_oDb->query("DELETE FROM `sys_acl_levels_members` WHERE `IDLevel` = :id", array('id' => $aLevel['ID']));
  165. }
  166. $iNumber++;
  167. }
  168. parent::removeContent();
  169. return $iNumber;
  170. }
  171. }
  172. /** @} */