BxDolMData.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. define('BX_MIG_SUCCESSFUL', 1);
  12. define('BX_MIG_FAILED', 0);
  13. /**
  14. * Base class for all migration classes
  15. * contains general variables and function for modules' migration classes
  16. */
  17. class BxDolMData
  18. {
  19. /**
  20. * @var ref $_oMainModule the reference to the main module object
  21. */
  22. protected $_oMainModule;
  23. /**
  24. * @var ref $_mDb the reference to Dolphin's database connect
  25. */
  26. protected $_mDb;
  27. /**
  28. * @var string $_sPrefix migration module database prefix
  29. */
  30. protected $_sPrefix;
  31. /**
  32. * @var ref $_oDb connect to UNA database
  33. */
  34. protected $_oDb;
  35. /**
  36. * @var string $_sModuleName module name, it is set by inherited object and equal to value from {@link $_aMigrationModules}
  37. *@uses BxDolMConfig::_aMigrationModules modules array
  38. */
  39. protected $_sModuleName = '';
  40. /**
  41. * @var string $_sTableWithTransKey the main a module's table name into which transferring data from Dolphin's module
  42. * it is individual for each module and set in constructor of each class
  43. */
  44. protected $_sTableWithTransKey;
  45. /**
  46. * @var int $_iTransferred number of transferred records
  47. */
  48. protected $_iTransferred = 0;
  49. /**
  50. * @var ref $_oLanguage reference on @uses BxDolStudioLanguagesUtils::getInstance()
  51. */
  52. protected $_oLanguage = null;
  53. /**
  54. * @var array $_aLanguages list of the installed languages in appropriate format
  55. */
  56. protected $_aLanguages = array();
  57. /**
  58. * @var string $_sImagePhotoFiles path to the photo module's data files in Dolphin
  59. */
  60. protected $_sImagePhotoFiles = '';
  61. /**
  62. * @var string $_sTransferFieldIdent field name which is created in table into which records are added, it allows to set connect between ids from Dolphin and UNA's tables
  63. */
  64. protected $_sTransferFieldIdent = 'mig_id';
  65. /**
  66. * @var array $_aSysProfiles profiles cache, allows to find content_id and sys_profile ID by Dolphin's profile ID and avoiding to make request to database
  67. */
  68. protected $_aSysProfiles = array();
  69. /**
  70. * Base class constructor
  71. *
  72. * @param ref $oMainModule main module
  73. * @param ref $oDb connect with Dolphin database
  74. * @return void
  75. */
  76. public function __construct(&$oMainModule, &$oDb)
  77. {
  78. $this -> _sPrefix = $oMainModule -> _aModule['db_prefix'];
  79. $this -> _oMainModule = $oMainModule;
  80. $this -> _oConfig = $oMainModule -> _oConfig;
  81. $this -> _mDb = $oDb;
  82. $this -> _oDb = $this -> _oMainModule -> _oDb;
  83. $this -> _sTableWithTransKey = '';
  84. $this -> _oLanguage = BxDolStudioLanguagesUtils::getInstance();
  85. $this -> _aLanguages = $this -> _oDb -> getPairs("SELECT * FROM `sys_localization_languages` WHERE `Enabled`='1'", 'Name', 'ID');
  86. $this -> _sImagePhotoFiles = $this -> _oDb -> getExtraParam('root') . 'modules' . DIRECTORY_SEPARATOR . 'boonex' . DIRECTORY_SEPARATOR . "photos" . DIRECTORY_SEPARATOR . "data" . DIRECTORY_SEPARATOR . "files" . DIRECTORY_SEPARATOR;
  87. }
  88. /**
  89. * Main migration function, executes required operations for transfer
  90. * @return boolean
  91. */
  92. public function runMigration()
  93. {
  94. $this -> setResultStatus(_t('_bx_dolphin_migration_define_migration_method'));
  95. return BX_MIG_FAILED;
  96. }
  97. /**
  98. * Gets total records for transferring
  99. * @return boolean
  100. */
  101. public function getTotalRecords()
  102. {
  103. $this -> setResultStatus(_t('_bx_dolphin_migration_define_total_method'));
  104. return BX_MIG_FAILED;
  105. }
  106. /**
  107. * Set Migration Status
  108. * @param string $sStatus message
  109. */
  110. protected function setResultStatus($sStatus)
  111. {
  112. $sQuery = $this -> _oDb -> prepare("UPDATE `{$this -> _sPrefix}transfers` SET `status_text` = ? WHERE `module` = ? ", $sStatus, $this -> _sModuleName);
  113. $this -> _oDb -> query($sQuery);
  114. }
  115. private function createSysProfilesCache(){
  116. if (empty($this -> _aSysProfiles))
  117. $this->_aSysProfiles = $this->_oDb->getAllWithKey("SELECT `p`.`content_id`, `p`.`id`, `a`.`mig_id`
  118. FROM `sys_profiles` as `p`
  119. LEFT JOIN `sys_accounts` as `a` ON `a`.`id` = `p`.`account_id`
  120. WHERE `type` = 'bx_persons' AND `mig_id` <> 0", 'mig_id');
  121. }
  122. /**
  123. * Returns content id of transferred member from Dolphin
  124. * @param int $iId Dolphin's profile ID
  125. * @return int
  126. */
  127. protected function getContentId($iId){
  128. $this -> createSysProfilesCache();
  129. return isset($this->_aSysProfiles[$iId]) ? $this->_aSysProfiles[$iId]['content_id'] : false;
  130. }
  131. /**
  132. * Returns person id of transferred member from Dolphin
  133. * @param int $iId Dolphin's profile ID
  134. * @return int
  135. */
  136. protected function getProfileId($iId)
  137. {
  138. $this -> createSysProfilesCache();
  139. return isset($this->_aSysProfiles[$iId]) ? $this->_aSysProfiles[$iId]['id'] : false;
  140. }
  141. /**
  142. * Check if the module was transfered
  143. * @param string $sModule name from the list @uses BxDolMConfig::_aMigrationModules
  144. * @return int
  145. */
  146. protected function isModuleContentTransferred($sModule)
  147. {
  148. return isset($this -> _oConfig -> _aModulesAliases[$sModule])
  149. ? (int)$this -> _oDb -> getOne("SELECT `number` FROM `{$this -> _sPrefix}transfers` WHERE `status` != 'not_started' AND `module`=:module", array('module' => $this -> _oConfig -> _aModulesAliases[$sModule]))
  150. : false;
  151. }
  152. /**
  153. * Returns list of the fields which should be transferred to Persons module
  154. * @return array list of the fields names
  155. */
  156. protected function getAssocFields()
  157. {
  158. $aFields = array();
  159. $aExclude = array('Status', 'Couple', 'Captcha', 'aff_num', 'TermsOfUse', 'UserStatus', 'UserStatusMessage', 'UserStatusMessageWhen', 'Agree', 'Tags', 'Age', 'Featured', 'Keyword', 'Location', 'ProfilePhoto');
  160. $sWhere = "";
  161. if (!empty($aExclude))
  162. $sWhere = " AND `Name` NOT IN ('" . implode("','" , $aExclude) . "')";
  163. $aItems = $this -> _mDb -> getAll("SELECT * FROM `sys_profile_fields` WHERE `Type` != 'block' {$sWhere}");
  164. foreach($aItems as $iKey => $aValue)
  165. {
  166. $aFields[$aValue['Name']] = array(
  167. 'name' => $aValue['Name'],
  168. 'type' => $aValue['Type'],
  169. 'required' => $aValue['Mandatory'],
  170. 'title' => "_FieldCaption_{$aValue['Name']}_Edit",
  171. 'add' => (int)$aValue['JoinBlock'] !=0,
  172. 'edit' => (int)$aValue['EditOwnBlock'] !=0,
  173. 'view' => $aValue['ViewVisBlock'] !=0,
  174. 'values' => $aValue['Values']
  175. );
  176. }
  177. return $aFields;
  178. }
  179. /**
  180. * Check if the list already exists in una pre values list
  181. *
  182. * @param string $sName list name
  183. * @return Boolean
  184. */
  185. protected function isKeyPreKeyExits($sName)
  186. {
  187. return $this -> _oDb -> getOne("SELECT COUNT(*) FROM `sys_form_pre_lists` WHERE `key` = :key", array('key' => $sName)) == 1;
  188. }
  189. /**
  190. * Adds category to existed list
  191. *
  192. * @param string $sName category title
  193. * @param string $sPrefix prefix for the module which should be used for new category (depends on module)
  194. * @param int $sCategory module's category's pre values Name
  195. * @return string value of the added category
  196. */
  197. protected function transferCategory($sName, $sPrefix, $sCategory, $iValue = 0, $sData = '')
  198. {
  199. if (!$sName)
  200. return false;
  201. if (strpos($sName, ';') !== false)
  202. $sName = substr($sName, 0, strpos($sName, ';'));
  203. $sTitle = str_replace(' ', '_', $sName);
  204. $aValues = $this -> _oDb -> getRow("SELECT * FROM `sys_form_pre_values` WHERE `Key` = :cat AND `LKey` LIKE CONCAT('%', :value, '%') LIMIT 1", array('cat' => $sCategory, 'value' => $sTitle));
  205. if (!empty($aValues))
  206. return $aValues['Value'];
  207. $aCateg = $this->_oDb->getRow("SELECT MAX(CONVERT(`Value`, SIGNED INTEGER)) + 1 as `Value`, MAX(`Order`) + 1 as `Order` FROM `sys_form_pre_values` WHERE `Key` = :cat", array('cat' => $sCategory));
  208. if ($iValue)
  209. $aCateg['Value'] = $iValue;
  210. $sQuery = $this -> _oDb -> prepare("
  211. INSERT INTO `sys_form_pre_values` SET
  212. `Key` = ?,
  213. `Value` = ?,
  214. `Order` = ?,
  215. `LKey` = ?,
  216. `Data` = ?", $sCategory, $aCateg['Value'], $aCateg['Order'], "_{$sPrefix}_cat_{$sTitle}", $sData);
  217. $this -> _oDb -> query($sQuery);
  218. BxDolStudioLanguagesUtils::getInstance() -> addLanguageString("_{$sPrefix}_cat_{$sTitle}", $sName);
  219. return $aCateg['Value'];
  220. }
  221. /**
  222. * Transfer fields lists with translations
  223. *
  224. * @param string $sName field name
  225. * @param string $sTitle translation
  226. * @param mixed $mixedValues contains name of the list which already exists or array values for transfer
  227. * @param bool $bAdd don't add value to already exited fields list
  228. * @return string list name for db insert with #!
  229. */
  230. protected function transferPreValues($sName, $sTitle, $mixedValues, $bAdd = false)
  231. {
  232. if (empty($mixedValues))
  233. return '';
  234. if (is_string($mixedValues) && substr($mixedValues, 0, 2) == '#!')
  235. return $mixedValues;
  236. $i = 1;
  237. if (!$this -> isKeyPreKeyExits($sName))
  238. {
  239. $sQuery = $this -> _oDb -> prepare("INSERT INTO `sys_form_pre_lists` (`id`, `module`, `key`, `title`, `use_for_sets`, `extendable`) VALUES
  240. (NULL, 'bx_persons', ?, ?, 1, 1)", $sName, '_bx_' . $sName . '_pre_lists_cats');
  241. $this -> _oLanguage -> addLanguageString('_bx_' . $sName . '_pre_lists_cats', $sTitle);
  242. $this -> _oDb -> query($sQuery);
  243. $sQuery = $this -> _oDb -> prepare("
  244. INSERT INTO `sys_form_pre_values` SET
  245. `Key` = ?,
  246. `Value` = ?,
  247. `Order` = ?,
  248. `LKey` = ?", $sName, '', 0, '_sys_please_select');
  249. $this -> _oDb -> query($sQuery);
  250. }
  251. else
  252. {
  253. if (!$bAdd)
  254. return "#!{$sName}";
  255. $i = $this -> _oDb -> getOne("SELECT MAX(`Order`) FROM `sys_form_pre_values` WHERE `Key` =:key", array('key' => $sName)) + 1;
  256. }
  257. foreach($mixedValues as $mixedKey => $mixedValue)
  258. {
  259. $sLangKey = "_bx_fields_cat_{$sName}_value_{$mixedKey}";
  260. if (is_array($mixedValue))
  261. {
  262. foreach($mixedValue as $sLang => $sValue)
  263. {
  264. if (isset($this -> _aLanguages[$sLang]))
  265. $this -> _oLanguage -> addLanguageString($sLangKey, $sValue, $this -> _aLanguages[$sLang]);
  266. }
  267. }
  268. else
  269. $this -> _oLanguage -> addLanguageString($sLangKey, $mixedValue);
  270. $sQuery = $this -> _oDb -> prepare("
  271. INSERT INTO `sys_form_pre_values` SET
  272. `Key` = ?,
  273. `Value` = ?,
  274. `Order` = ?,
  275. `LKey` = ?", $sName, $mixedKey, $i++, $sLangKey);
  276. $this -> _oDb -> query($sQuery);
  277. }
  278. return "#!{$sName}";
  279. }
  280. /**
  281. * Returns pre values array by list name in pairs format
  282. *
  283. * @param string $sName name of the list
  284. * @param string $sField array's index field name
  285. * @param string $sValue array's value field name
  286. * @param int $iStart - allows to set start value and get values above it only
  287. * @return array
  288. */
  289. protected function getPreValuesBy($sName, $sField = 'Order', $sValue = 'LKey', $iStart = 0)
  290. {
  291. $sStart = '';
  292. if ($iStart)
  293. $sStart = "AND `$sField` > $iStart";
  294. $sQuery = $this -> _mDb -> prepare("SELECT * FROM `sys_pre_values` WHERE `Key` = ? {$sStart} ORDER BY `Order`", $sName);
  295. return $this -> _mDb -> getPairs($sQuery, $sField, $sValue);
  296. }
  297. /**
  298. * Returns default language name/id
  299. *
  300. * @param boolean $bName return language name, otherwise id
  301. * @return mixed default language
  302. */
  303. protected function getDefaultLang($bName = true)
  304. {
  305. $aLang = $this -> _mDb -> getPairs("SELECT * FROM `sys_localization_languages` ORDER BY `id`", 'Name', 'ID');
  306. $sDefultLang = $this -> _mDb -> getParam('lang_default');
  307. return $bName ? $sDefultLang : $aLang[$sDefultLang];
  308. }
  309. /**
  310. * Returns translations for all existed languages on Dolphin by language key
  311. *
  312. * @param string $LKey language key
  313. * @return array
  314. */
  315. protected function getLKeyTranslations($LKey)
  316. {
  317. return $this -> _mDb -> getPairs("SELECT `l`.`Name`, `s`.`String`, `l`.`ID`
  318. FROM `sys_localization_keys` as `k`
  319. LEFT JOIN `sys_localization_strings` as `s` ON `k`.`ID` = `s`.`IDKey`
  320. LEFT JOIN `sys_localization_languages` as `l` ON `s`.`IDLanguage` = `l`.`ID`
  321. WHERE `k`.`Key` = :key", 'Name', 'String', array('key' => $LKey));
  322. }
  323. /**
  324. * Transfer Tags from the module, to meta
  325. *
  326. * @param int $iObjectId module's item ID in Dolphin
  327. * @param int $iNewObjectId module's item ID in UNA
  328. * @param int $sType module's prefix in Dolphin
  329. * @param int $sTableKeywords table name in UNA for tags migration
  330. * @return void
  331. */
  332. protected function transferTags($iObjectId, $iNewObjectId, $sType, $sTableKeywords)
  333. {
  334. $aTags = $this -> _mDb -> getAll("SELECT `Tag` FROM `sys_tags` WHERE `ObjID`=:id AND `Type`=:type", array('id' => $iObjectId, 'type' => $sType));
  335. if (empty($aTags))
  336. return false;
  337. foreach($aTags as $aTag)
  338. $this -> _oDb -> query("INSERT IGNORE INTO `{$sTableKeywords}` SET `keyword`=:keyword, `object_id`=:id", array('keyword' => $aTag['Tag'], 'id' => $iNewObjectId));
  339. }
  340. private function addCmtsIds($sCmtsTable, $iId, $iRate){
  341. $iSystem = $this -> _oDb -> getOne("SELECT `ID` FROM `sys_objects_cmts` WHERE `Table`=:table LIMIT 1", array('table' => $sCmtsTable));
  342. if (!$iSystem)
  343. return false;
  344. return $this -> _oDb -> query("INSERT INTO `sys_cmts_ids` SET `system_id` = :id, `rate`=:rate, `cmt_id`=:cmt_id",
  345. array('id' => $iSystem, 'rate' => $iRate, 'cmt_id' => $iId));
  346. }
  347. /**
  348. * Transfer comments for module's items
  349. *
  350. * @param int $iObject object ID in UNA
  351. * @param int $iEntryId object ID in Dolphin
  352. * @param string $sType module name for transfer @uses BxDolMConfig::_aMigrationModules
  353. * @return int transferred comments number
  354. */
  355. protected function transferComments($iObject, $iEntryId, $sType = 'photos')
  356. {
  357. switch($sType)
  358. {
  359. case 'photos':
  360. $sCmtsTable = 'bx_photos_cmts';
  361. $sTable = 'bx_photos_cmts';
  362. break;
  363. case 'album_photo':
  364. $sCmtsTable = 'bx_photos_cmts';
  365. $sTable = 'bx_albums_cmts_media';
  366. break;
  367. case 'photo_albums':
  368. $sCmtsTable = 'bx_photos_cmts_albums';
  369. $sTable = 'bx_albums_cmts';
  370. break;
  371. case 'photo_albums_items':
  372. $sCmtsTable = 'bx_photos_cmts';
  373. $sTable = 'bx_albums_cmts_media';
  374. break;
  375. case 'videos':
  376. $sCmtsTable = 'bx_videos_cmts';
  377. $sTable = 'bx_videos_cmts';
  378. break;
  379. case 'video_albums_items':
  380. $sCmtsTable = 'bx_videos_cmts';
  381. $sTable = 'bx_albums_cmts_media';
  382. break;
  383. case 'video_albums':
  384. $sCmtsTable = 'bx_videos_cmts_albums';
  385. $sTable = 'bx_albums_cmts';
  386. break;
  387. case 'blogs':
  388. $sCmtsTable = 'bx_blogs_cmts';
  389. $sTable = 'bx_posts_cmts';
  390. break;
  391. case 'groups':
  392. $sCmtsTable = 'bx_groups_cmts';
  393. $sTable = 'bx_groups_cmts';
  394. break;
  395. case 'events':
  396. $sCmtsTable = 'bx_events_cmts';
  397. $sTable = 'bx_events_cmts';
  398. break;
  399. case 'polls':
  400. $sCmtsTable = 'bx_poll_cmts';
  401. $sTable = 'bx_polls_cmts';
  402. break;
  403. case 'files':
  404. $sCmtsTable = 'bx_files_cmts';
  405. $sTable = 'bx_files_cmts';
  406. break;
  407. case 'market':
  408. $sCmtsTable = 'bx_store_cmts';
  409. $sTable = 'bx_market_cmts';
  410. break;
  411. case 'timeline':
  412. $sCmtsTable = 'bx_wall_comments';
  413. $sTable = 'bx_timeline_comments';
  414. break;
  415. }
  416. $aComments = $this -> _mDb -> getAll("SELECT * FROM `{$sCmtsTable}` WHERE `cmt_object_id` = :id ORDER BY `cmt_id` ASC", array('id' => $iEntryId));
  417. $iComments = 0;
  418. $aCommentsArray = array();
  419. foreach($aComments as $iKey => $aValue)
  420. {
  421. $iProfileId = $this -> getProfileId($aValue['cmt_author_id']);
  422. if ($iProfileId)
  423. {
  424. $bIscmtsArray = $aValue['cmt_parent_id'] && isset($aCommentsArray[$aValue['cmt_parent_id']]);
  425. $this -> _oDb -> query("INSERT INTO `{$sTable}` (`cmt_id`, `cmt_parent_id`, `cmt_vparent_id`, `cmt_object_id`, `cmt_author_id`, `cmt_level`, `cmt_text`, `cmt_time`, `cmt_replies`)
  426. VALUES (NULL, :parent_id, :vparent_id, :object, :user, :level, :message, :time, :replies)",
  427. array(
  428. 'parent_id' => $bIscmtsArray ? $aCommentsArray[$aValue['cmt_parent_id']]['id'] : 0,
  429. 'vparent_id' => $bIscmtsArray ? ($aCommentsArray[$aValue['cmt_parent_id']]['vparent'] ? $aCommentsArray[$aValue['cmt_parent_id']]['vparent'] : $aCommentsArray[$aValue['cmt_parent_id']]['id']) : 0,
  430. 'object' => $iObject,
  431. 'user' => $iProfileId,
  432. 'message' => $aValue['cmt_text'],
  433. 'time' => strtotime($aValue['cmt_time']),
  434. 'replies' => $aValue['cmt_replies'],
  435. 'level' => isset($aCommentsArray[$aValue['cmt_parent_id']]) && $aCommentsArray[$aValue['cmt_parent_id']] ? $aCommentsArray[$aValue['cmt_parent_id']]['level'] + 1 : 0
  436. ));
  437. $iLastInsertId = $this -> _oDb -> lastId();
  438. $this -> addCmtsIds($sTable, $iLastInsertId, $aValue['cmt_rate'], $aValue['cmt_rate_count']);
  439. $aCommentsArray[$aValue['cmt_id']] = array(
  440. 'id' => $iLastInsertId,
  441. 'level' => $bIscmtsArray ? (int)$aCommentsArray[$aValue['cmt_parent_id']]['level'] + 1 : 0,
  442. 'vparent' =>
  443. $bIscmtsArray ?
  444. (
  445. $aCommentsArray[$aValue['cmt_parent_id']]['vparent'] ? $aCommentsArray[$aValue['cmt_parent_id']]['vparent'] : $aCommentsArray[$aValue['cmt_parent_id']]['id']
  446. ) : 0
  447. );
  448. $iComments++;
  449. }
  450. }
  451. return $iComments;
  452. }
  453. /**
  454. * Create migration field in main table for transferring content from Dolphin to UNA and contains id of the object in Dolphin
  455. * @param string $sIdentFieldName
  456. * @param string $sTableName name of the table to add the index field
  457. * @return mixed
  458. */
  459. protected function createMIDField($sIdentFieldName = '', $sTableName = '')
  460. {
  461. if (!$this -> _sTableWithTransKey)
  462. return false;
  463. $sIdentFieldName = $sIdentFieldName ? $sIdentFieldName : $this -> _sTransferFieldIdent;
  464. $sTableName = $sTableName ? $sTableName : $this -> _sTableWithTransKey;
  465. if ($this -> _oDb -> isFieldExists($sTableName, $sIdentFieldName))
  466. return true;
  467. return $this -> _oDb -> query("ALTER TABLE `{$sTableName}` ADD `{$sIdentFieldName}` int(11) unsigned NOT NULL default '0'");
  468. }
  469. /**
  470. * Returns last migration field value
  471. * @param string $sIdentFieldName
  472. * @param string $sTableName name of the table to add the index field
  473. * @return int
  474. */
  475. protected function getLastMIDField($sIdentFieldName = '', $sTableName = '')
  476. {
  477. if (!$this -> _sTableWithTransKey)
  478. return false;
  479. $sIdentFieldName = $sIdentFieldName ? $sIdentFieldName : $this -> _sTransferFieldIdent;
  480. $sTableName = $sTableName ? $sTableName : $this -> _sTableWithTransKey;
  481. return (int)$this -> _oDb -> getOne("SELECT `{$sIdentFieldName}` FROM `{$sTableName}` WHERE `{$sIdentFieldName}` <> 0 ORDER BY `{$sIdentFieldName}` DESC LIMIT 1");
  482. }
  483. /**
  484. * Check if this record was already transferred
  485. *
  486. * @param int $iItemId object id in Dolphin
  487. * @param string $sField field name with id values in Dolphin
  488. * @param string $sIdentFieldName
  489. * @param string $sTableName name of the table to add the index field
  490. * @return int
  491. */
  492. protected function isItemExisted($iItemId, $sField = 'id', $sIdentFieldName = '', $sTableName = '')
  493. {
  494. if (!$this -> _sTableWithTransKey)
  495. return false;
  496. $sIdentFieldName = $sIdentFieldName ? $sIdentFieldName : $this -> _sTransferFieldIdent;
  497. $sTableName = $sTableName ? $sTableName : $this -> _sTableWithTransKey;
  498. return (int)$this -> _oDb -> getOne("SELECT `{$sField}` FROM `{$sTableName}` WHERE `{$sIdentFieldName}` = :item LIMIT 1", array('item' => $iItemId));
  499. }
  500. /**
  501. * Set value for migrated record to migration field
  502. *
  503. * @param int $iId object id in UNA
  504. * @param int $iItemId object id in Dolphin
  505. * @param string $sField id field name
  506. * @param string $sIdentFieldName
  507. * @return int affected rows
  508. */
  509. protected function setMID($iId, $iItemId, $sField ='id', $sIdentFieldName = '')
  510. {
  511. if (!$this -> _sTableWithTransKey)
  512. return false;
  513. $sIdentFieldName = $sIdentFieldName ? $sIdentFieldName : $this -> _sTransferFieldIdent;
  514. return (int)$this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}` SET `{$sIdentFieldName}` = :item WHERE `{$sField}` = :id", array('id' => $iId, 'item' => $iItemId));
  515. }
  516. /**
  517. * Drop migration field from the table
  518. * @param string $sIdentFieldName field name to store id from original table (in case if you don't want to use default field)
  519. * @param string $sTableName table name in which to create the field
  520. * @return int affected rows
  521. */
  522. public function dropMID($sIdentFieldName = '', $sTableName = '')
  523. {
  524. $sIdentFieldName = $sIdentFieldName ? $sIdentFieldName : $this -> _sTransferFieldIdent;
  525. $sTableName = $sTableName ? $sTableName : $this -> _sTableWithTransKey;
  526. if (!$sTableName || !$this -> _oDb -> isFieldExists($sTableName, $sIdentFieldName))
  527. return false;
  528. return (int)$this -> _oDb -> query("ALTER TABLE `{$sTableName}` DROP `{$sIdentFieldName}`");
  529. }
  530. /**
  531. * Removes all transferred content from UNA
  532. * @return void
  533. */
  534. public function removeContent()
  535. {
  536. $this -> dropMID();
  537. $this -> _oDb -> updateTransferStatus($this -> _sModuleName, 'not_started');
  538. $this -> setResultStatus('');
  539. }
  540. protected function getDefaultPrivacy($iDolProfileId, $sModule, $sAction){
  541. $iPrivacy = $this->_mDb->getOne("SELECT IF (`d`.`group_id` IS NULL, `a`.`default_group`, `d`.`group_id`)
  542. FROM `sys_privacy_actions` as `a`
  543. LEFT JOIN `sys_privacy_defaults` as `d` ON `a`.`id` = `d`.`action_id` AND `d`.`owner_id` = :owner
  544. WHERE `a`.`module_uri`=:module AND `a`.`name`=:action
  545. ", array('owner' => $iDolProfileId, 'module' => $sModule, 'action' => $sAction));
  546. $aProfileInfo = $this->_mDb->getRow("SELECT * FROM `Profiles` WHERE `ID`=:id LIMIT 1", array('id' => $iDolProfileId));
  547. return $iPrivacy == 1 && isset($aProfileInfo['PrivacyDefaultGroup']) ? $this -> getValidPrivacy((int)$aProfileInfo['PrivacyDefaultGroup']) : $this -> getValidPrivacy($iPrivacy);
  548. }
  549. protected function getValidPrivacy($iPrivacy)
  550. {
  551. return $iPrivacy > 1 && $iPrivacy <= 5 ? $iPrivacy : $this->_oConfig->_iDefaultPrivacyGroup;
  552. }
  553. protected function getPrivacy($iDolProfileId, $iPrivacy, $sModule = '', $sAction = '', $iDefaultPrivacy = 0){
  554. if ((int)$iPrivacy == 1)
  555. return $iDefaultPrivacy ? $this -> getValidPrivacy($iDefaultPrivacy) : $this -> getDefaultPrivacy($iDolProfileId, $sModule, $sAction);
  556. return $this -> getValidPrivacy($iPrivacy);
  557. }
  558. function getFieldType($sFieldName){
  559. return $this->_mDb->getOne("SELECT `Type` FROM `sys_profile_fields` WHERE `Name`=:name LIMIT 1", array('name' => $sFieldName));
  560. }
  561. }
  562. /** @} */