BxSEMigProfilesFields.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 SocialEngineMigration SocialEngine Migration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxSEMigData.php');
  12. bx_import('BxDolStudioLanguagesUtils');
  13. class BxSEMigProfilesFields extends BxSEMigData
  14. {
  15. private $_aProfileFields = array();
  16. private $_aFiltered = array();
  17. public function BxSEMigProfilesFields($oMigrationModule, &$seDb)
  18. {
  19. parent::BxSEMigData($oMigrationModule, $seDb);
  20. // dont transfer these fields
  21. $this -> _aFiltered = array(
  22. 'username', 'email', 'password', 'salt', 'language', 'creation_date', 'modified_dat', 'lastlogin_date', 'user_id',
  23. 'profile_type', 'first_name', 'last_name', 'about_me', 'heading'
  24. );
  25. }
  26. public function getTotalRecords(){
  27. $aFields = $this -> _seDb -> getAll("SELECT * FROM `{$this -> _sEnginePrefix}user_fields_meta`");
  28. foreach($aFields as $iKey => $aValue){
  29. if (in_array($aValue['type'], $this -> _aFiltered)) continue;
  30. $this -> _aProfileFields[$aValue['type']] = array(
  31. 'name' => $aValue['type'],
  32. 'caption' => $aValue['label'],
  33. 'id' => $aValue['field_id'],
  34. );
  35. }
  36. return count($this -> _aProfileFields);
  37. }
  38. public function runMigration(){
  39. if (!$this -> getTotalRecords()){
  40. $this -> setResultStatus(_t('_bx_se_migration_no_data_to_transfer'));
  41. return BX_SEMIG_SUCCESSFUL;
  42. }
  43. $this -> setResultStatus(_t('_bx_se_migration_started_migration_profile_fields'));
  44. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  45. $i = 0;
  46. foreach($this -> _aProfileFields as $sKey => $aItem){
  47. if (!$this -> _oDb -> isFieldExists('bx_persons_data', $sKey)){
  48. $sQuery = $this -> _oDb -> prepare("ALTER TABLE `bx_persons_data` ADD $sKey varchar(255) default ''");
  49. if (!$this -> _oDb -> query($sQuery)){
  50. $this -> setResultStatus(_t('_bx_se_migration_started_migration_profile_field_can_not_be_transferred'));
  51. return BX_SEMIG_FAILED;
  52. }
  53. }
  54. if ($this -> isFieldTransfered($sKey)) continue;
  55. $iKeyID = time() + $i++; // unique language keys postfix
  56. $sQuery = $this -> _oDb -> prepare("
  57. INSERT INTO `sys_form_inputs` SET
  58. `object` = 'bx_person',
  59. `module` = 'custom',
  60. `name` = ?,
  61. `type` = 'text',
  62. `caption_system` = ?,
  63. `caption` = ?", $sKey, '_sys_form_txt_field_caption_system_' . $iKeyID, '_sys_form_txt_field_caption_' . $iKeyID);
  64. if (!$this -> _oDb -> query($sQuery)){
  65. $this -> setResultStatus(_t('_bx_se_migration_started_migration_profile_field_can_not_be_transferred'));
  66. return BX_SEMIG_FAILED;
  67. }
  68. // create form fields
  69. $this -> _oDb -> query($sQuery);
  70. // add display for view and add forms
  71. $this -> _oDb -> query("INSERT INTO `sys_form_display_inputs` (`id`, `display_name`, `input_name`, `visible_for_levels`, `active`, `order`) VALUES
  72. (NULL, 'bx_person_add', '{$sKey}', 2147483647, 1, " . $i . "),
  73. (NULL, 'bx_person_view', '{$sKey}', 2147483647, 1, " . $i . ")");
  74. // add lanungage keys with translations
  75. $oLanguage -> addLanguageString('_sys_form_txt_field_caption_system_' . $iKeyID, $aItem['caption'], 0, 0, false);
  76. $oLanguage -> addLanguageString('_sys_form_txt_field_caption_' . $iKeyID, $aItem['caption']);
  77. $this -> _iTransferred++;
  78. }
  79. $this -> migrateProfileFieldsInfo();
  80. $this -> setResultStatus(_t('_bx_se_migration_started_migration_profile_fields_finished', $this -> _iTransferred));
  81. return BX_SEMIG_SUCCESSFUL;
  82. }
  83. private function isFieldTransfered($sName){
  84. $sQuery = $this -> _oDb -> prepare("SELECT COUNT(*) FROM `sys_form_inputs` WHERE `object` = 'bx_person' AND `module` = 'custom' AND `name` = ? LIMIT 1", $sName);
  85. return (int)$this -> _oDb -> getOne($sQuery) > 0;
  86. }
  87. /**
  88. * Returns social engine profile fields list
  89. * @param int $iUserId social engine's user ID
  90. * @return array
  91. */
  92. private function getFieldsValues($iUserId){
  93. return $this -> _seDb -> getPairs("SELECT * FROM `{$this -> _sEnginePrefix}user_fields_values` WHERE `item_id` = {$iUserId} AND `value` != ''", 'field_id', 'value');
  94. }
  95. /**
  96. * Returns Una profile fields list
  97. * @param int $iProfileId profile ID
  98. * @return array
  99. */
  100. private function getUsersFieldsValues($iProfileId){
  101. return $this -> _oDb -> getRow("SELECT * FROM `bx_persons_data` WHERE `id` = {$iProfileId} LIMIT 1");
  102. }
  103. /**
  104. * Transfers profile fields values for the all profiles
  105. */
  106. private function migrateProfileFieldsInfo()
  107. {
  108. $sQuery = "SELECT * FROM `{$this -> _sEnginePrefix}users`";
  109. $aFields = $this -> _seDb -> getAll($sQuery);
  110. foreach($aFields as $iKey => $aValue){
  111. if (!($iProfileId = $this -> getContentId($aValue['user_id']))) continue;
  112. if (!empty($aValue['wp_data'])) $aValues = @unserialize($aValue['wp_data']);
  113. $sUpdateField = '';
  114. $aFieldsValues = $this -> getFieldsValues($aValue['user_id']);
  115. $aCurrentMembersValues = $this -> getUsersFieldsValues($iProfileId);
  116. foreach($this -> _aProfileFields as $sKey => $aItem)
  117. if (empty($aCurrentMembersValues[$sKey]))
  118. $sUpdateField .= "`{$sKey}` = '". addslashes(isset($aValues[$sKey]) && $aValues[$sKey] ? $aValues[$sKey] :
  119. (isset($aFieldsValues[$aItem['id']]) ? $aFieldsValues[$aItem['id']] : '')) . "',";
  120. if ($sUpdateField){
  121. $sUpdateField = trim($sUpdateField, ',');
  122. $sQuery =
  123. "
  124. UPDATE
  125. `bx_persons_data`
  126. SET
  127. {$sUpdateField}
  128. WHERE
  129. `id` = {$iProfileId}
  130. ";
  131. $this -> _oDb -> query($sQuery);
  132. }
  133. }
  134. }
  135. }
  136. /** @} */