BxDataMigrationProfilesCustomFields.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. require_once 'BxDataMigrationData.php';
  3. require_once( BX_DIRECTORY_PATH_CLASSES . 'BxDolPFM.php' );
  4. require_once( BX_DIRECTORY_PATH_INC . 'prof.inc.php' );
  5. class BxDataMigrationProfilesCustomFields extends BxDataMigrationData
  6. {
  7. var $iTransffered = 0;
  8. var $aOldProfileTable = array();
  9. var $aNewProfileTable = array();
  10. var $aFiltered = array();
  11. /**
  12. * Class constructor;
  13. *
  14. * @param : $oMigrationModule (object) - object instance of migration class;
  15. * @param : $rOldDb (resourse) - connect to old dolphin's database;
  16. * @param : $oDolModule (object);
  17. * @return : void;
  18. */
  19. function BxDataMigrationProfilesCustomFields(&$oMigrationModule, &$rOldDb, $oDolModule = '')
  20. {
  21. parent::BxDataMigrationData($oMigrationModule, $rOldDb, $oDolModule);
  22. $this -> aFiltered = array(
  23. 'PrimPhoto', 'Picture',
  24. );
  25. }
  26. /**
  27. * Function migrate profiles custom fields;
  28. *
  29. * @return : (string) operation result;
  30. */
  31. function getMigration()
  32. {
  33. // set new status;
  34. $this -> setResultStatus('Profiles custom fields transfer now');
  35. mysql_query('SET NAMES utf8', $this->rOldDb);
  36. //-- get old table structure --//
  37. $sQuery = "DESCRIBE `Profiles`";
  38. $rResult = mysql_query($sQuery, $this -> rOldDb);
  39. while( $aRow = mysql_fetch_assoc($rResult) )
  40. {
  41. $this -> aOldProfileTable[$aRow['Field']] = array (
  42. 'Type' => $aRow['Type'],
  43. 'Null' => $aRow['Null'],
  44. 'Key' => $aRow['Key'],
  45. 'Default' => $aRow['Default'],
  46. 'Extra' => $aRow['Extra'],
  47. );
  48. }
  49. //--
  50. //-- get new table structure --//
  51. $sQuery = "DESCRIBE `Profiles`";
  52. $aRow = $this -> oMigrationModule -> _oDb -> getAll($sQuery);
  53. foreach($aRow as $iKey => $aItems)
  54. {
  55. $this -> aNewProfileTable[$aItems['Field']] = array (
  56. 'Type' => $aItems['Type'],
  57. 'Null' => $aItems['Null'],
  58. 'Key' => $aItems['Key'],
  59. 'Default' => $aItems['Default'],
  60. 'Extra' => $aItems['Extra'],
  61. );
  62. }
  63. //--
  64. //-- compare the recived array --//
  65. foreach($this -> aOldProfileTable as $sKey => $aItems)
  66. {
  67. if(array_key_exists($sKey, $this -> aNewProfileTable) || in_array($sKey, $this -> aFiltered)) {
  68. unset($this -> aOldProfileTable[$sKey]);
  69. }
  70. }
  71. //--
  72. //-- alter new table --//
  73. foreach($this -> aOldProfileTable as $sKey => $aItems)
  74. {
  75. // define dafault value;
  76. $sDefault = $aItems['Default']
  77. ? "DEFAULT '{$aItems['Default']}'"
  78. : '';
  79. $sNull = $aItems['Null'] == 'NO'
  80. ? 'NOT NULL'
  81. : '';
  82. $sQuery =
  83. "
  84. ALTER TABLE `Profiles` ADD `{$sKey}` {$aItems['Type']} {$sNull} {$sDefault}
  85. ";
  86. // add new field
  87. $this -> oMigrationModule -> _oDb -> query($sQuery);
  88. // transfer into sys_profile_fields
  89. $this -> _transferProfileFields($sKey);
  90. // transfer into sys_pre_values
  91. $this -> _transferPreValues($sKey);
  92. $this -> iTransffered++;
  93. }
  94. //--
  95. //-- transfer all data --//
  96. $this -> _transformData();
  97. //--
  98. //-- Recompile all needed cache files --//
  99. bx_import('BxDolInstallerUtils');
  100. $oInstallerUtils = new BxDolInstallerUtils();
  101. $oInstallerUtils->updateProfileFieldsHtml();
  102. $oCacher = new BxDolPFMCacher();
  103. $oCacher -> createCache();
  104. $this -> compilePreValues();
  105. //--
  106. // set as finished;
  107. $this -> setResultStatus('All profiles custom fields were transferred (' . $this -> iTransffered . ' items)');
  108. return MIGRATION_SUCCESSFUL;
  109. }
  110. /**
  111. * Transfer all linked values from prevalues
  112. *
  113. * @param $sFieldName strings
  114. * @return void
  115. */
  116. function _transferPreValues($sFieldName)
  117. {
  118. $sFieldName = $this -> escape($sFieldName);
  119. $sQuery = "SELECT * FROM `Prevalues` WHERE `Key` = '{$sFieldName}'";
  120. $rResult = mysql_query($sQuery, $this -> rOldDb);
  121. while( $aRow = mysql_fetch_assoc($rResult) )
  122. {
  123. // escape all data;
  124. $aRow = $this -> escapeData($aRow);
  125. $sQuery =
  126. "
  127. INSERT INTO
  128. `" . BX_SYS_PRE_VALUES_TABLE . "`
  129. SET
  130. `Key` = '{$aRow['Key']}',
  131. `Value` = '{$aRow['Value']}',
  132. `Order` = '{$aRow['Order']}',
  133. `LKey` = '{$aRow['LKey']}',
  134. `LKey2` = '{$aRow['LKey2']}',
  135. `LKey3` = '{$aRow['LKey3']}',
  136. `Extra` = '{$aRow['Extra']}',
  137. `Extra2` = '{$aRow['Extra2']}',
  138. `Extra3` = '{$aRow['Extra3']}'
  139. ";
  140. $this -> oMigrationModule -> _oDb -> query($sQuery);
  141. }
  142. }
  143. /**
  144. * Add new value in sys_profile_fields
  145. *
  146. * @param $sFieldName strings
  147. * @return void
  148. */
  149. function _transferProfileFields($sFieldName)
  150. {
  151. $sFieldName = $this -> escape($sFieldName);
  152. $sQuery = "SELECT * FROM `ProfileFields` WHERE `Name` = '{$sFieldName}'";
  153. $rResult = mysql_query($sQuery, $this -> rOldDb);
  154. while( $aRow = mysql_fetch_assoc($rResult) )
  155. {
  156. if( !$this -> isFieldExists($aRow['Name']) ) {
  157. // escape all data;
  158. $aRow = $this -> escapeData($aRow);
  159. // insert new
  160. $sQuery =
  161. "
  162. INSERT INTO
  163. `sys_profile_fields`
  164. SET
  165. `Name` = '{$aRow['Name']}',
  166. `Type` = '{$aRow['Type']}',
  167. `Control` = '{$aRow['Control']}',
  168. `Extra` = '{$aRow['Extra']}',
  169. `Min` = '{$aRow['Min']}',
  170. `Max` = '{$aRow['Max']}',
  171. `Values` = '{$aRow['Values']}',
  172. `UseLKey` = '{$aRow['UseLKey']}',
  173. `Check` = '{$aRow['Check']}',
  174. `Unique` = '{$aRow['Unique']}',
  175. `Default` = '{$aRow['Default']}',
  176. `Mandatory` = '{$aRow['Mandatory']}',
  177. `Deletable` = '{$aRow['Deletable']}',
  178. `JoinPage` = '{$aRow['JoinPage']}',
  179. `JoinBlock` = '{$aRow['JoinBlock']}',
  180. `JoinOrder` = '{$aRow['JoinOrder']}',
  181. `EditOwnBlock` = '{$aRow['EditOwnBlock']}',
  182. `EditOwnOrder` = '{$aRow['EditOwnOrder']}',
  183. `EditAdmBlock` = '{$aRow['EditAdmBlock']}',
  184. `EditAdmOrder` = '{$aRow['EditAdmOrder']}',
  185. `EditModBlock` = '{$aRow['EditModBlock']}',
  186. `EditModOrder` = '{$aRow['EditModOrder']}',
  187. `ViewMembBlock` = '{$aRow['ViewMembBlock']}',
  188. `ViewMembOrder` = '{$aRow['ViewMembOrder']}',
  189. `ViewAdmBlock` = '{$aRow['ViewAdmBlock']}',
  190. `ViewAdmOrder` = '{$aRow['ViewAdmOrder']}',
  191. `ViewModBlock` = '{$aRow['ViewModBlock']}',
  192. `ViewModOrder` = '{$aRow['ViewModOrder']}',
  193. `ViewVisBlock` = '{$aRow['ViewVisBlock']}',
  194. `ViewVisOrder` = '{$aRow['ViewVisOrder']}',
  195. `SearchParams` = '{$aRow['SearchParams']}',
  196. `SearchSimpleBlock` = '{$aRow['SearchSimpleBlock']}',
  197. `SearchSimpleOrder` = '{$aRow['SearchSimpleOrder']}',
  198. `SearchQuickBlock` = '{$aRow['SearchQuickBlock']}',
  199. `SearchQuickOrder` = '{$aRow['SearchQuickOrder']}',
  200. `SearchAdvBlock` = '{$aRow['SearchAdvBlock']}',
  201. `SearchAdvOrder` = '{$aRow['SearchAdvOrder']}',
  202. `MatchField` = '{$aRow['MatchField']}',
  203. `MatchPercent` = '{$aRow['MatchPercent']}'
  204. ";
  205. $this -> oMigrationModule -> _oDb -> query($sQuery);
  206. }
  207. }
  208. }
  209. /**
  210. * Function transfer field value
  211. *
  212. * @return void
  213. */
  214. function _transformData()
  215. {
  216. // get all data from old profiles table;
  217. $sQuery = "SELECT * FROM `Profiles`";
  218. $rResult = mysql_query($sQuery, $this -> rOldDb);
  219. while( $aRow = mysql_fetch_assoc($rResult) )
  220. {
  221. // escape all data;
  222. $aRow = $this -> escapeData($aRow);
  223. //-- update new profiles table --//
  224. $sUpdateField = '';
  225. foreach($this -> aOldProfileTable as $sKey => $aItems)
  226. {
  227. $sValue =
  228. $sUpdateField .= "`{$sKey}` = '{$aRow[$sKey]}',";
  229. }
  230. $sUpdateField = trim($sUpdateField, ',');
  231. $sQuery =
  232. "
  233. UPDATE
  234. `Profiles`
  235. SET
  236. {$sUpdateField}
  237. WHERE
  238. `ID` = {$aRow['ID']}
  239. ";
  240. $this -> oMigrationModule -> _oDb -> query($sQuery);
  241. //--
  242. }
  243. }
  244. /**
  245. * Check field exist in sys_profile_fields
  246. *
  247. * @param $sFieldName string
  248. * @return true if exist;
  249. */
  250. function isFieldExists($sFieldName)
  251. {
  252. $sFieldName = $this -> escape($sFieldName);
  253. $sQuery = sprintf("SELECT COUNT(*) FROM `sys_profile_fields` WHERE `Name` = '%s'", $sFieldName);
  254. return $this -> oMigrationModule -> _oDb -> getOne($sQuery) ? true : false;
  255. }
  256. /**
  257. * Compile all prevalues value;
  258. *
  259. * @return boolean - true if file was compiled;
  260. */
  261. function compilePreValues()
  262. {
  263. compilePreValues();
  264. }
  265. }