BxDolMDb.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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('BxDolDb');
  12. class BxDolMDb extends BxBaseModGeneralDb
  13. {
  14. public function __construct(&$oConfig)
  15. {
  16. parent::__construct($oConfig);
  17. }
  18. /**
  19. * Save Dolphin configuration
  20. * @param array $aConfig parameters
  21. * @return boolean if requests are successful
  22. */
  23. public function saveConfig($aConfig)
  24. {
  25. if(is_array($aConfig))
  26. {
  27. $bRecords = true;
  28. foreach($aConfig as $sName => $mValue)
  29. {
  30. $mValue = bx_process_input($mValue);
  31. $sName = bx_process_input($sName);
  32. $sQuery = $this -> prepare("REPLACE INTO `{$this -> _sPrefix}config` SET `name` = ?, `value` = ?", $sName, $mValue);
  33. if (!$this -> res($sQuery))
  34. $bRecords &= false;
  35. }
  36. }
  37. return $bRecords;
  38. }
  39. /**
  40. * Removes Dolphin's transfer settings
  41. * @return boolean if requests are successful
  42. */
  43. public function removeConfig()
  44. {
  45. $this -> query("TRUNCATE TABLE `bx_dolphin_config`");
  46. $this -> query("TRUNCATE TABLE `bx_dolphin_transfers`");
  47. }
  48. /**
  49. * Get config parameter value
  50. * @param string $sConfigName param's name
  51. * @return string
  52. */
  53. public function getExtraParam($sConfigName){
  54. $sQuery = $this -> prepare("SELECT `value` FROM `{$this -> _sPrefix}config` WHERE `name` = ?", $sConfigName);
  55. return $this -> getOne($sQuery);
  56. }
  57. /**
  58. * Creates/edits migration list
  59. * @param array $aConfig Dolphin config info
  60. */
  61. public function createConfig($aConfig){
  62. if( is_array($aConfig) ) {
  63. foreach($aConfig as $sName => $mValue)
  64. {
  65. $mValue = $this -> escape($mValue);
  66. $sName = $this -> escape($sName);
  67. // add new;
  68. if(!$this -> getExtraParam($sName))
  69. {
  70. $sQuery = $this -> prepare("INSERT INTO `{$this -> _sPrefix}config` SET `name` = ?, `value` = ?", $sName, $mValue);
  71. $this -> query($sQuery);
  72. }
  73. else
  74. {
  75. // update exsisting;
  76. $sQuery = $this -> prepare("UPDATE `{$this -> _sPrefix}config` SET `value` = ? WHERE `name` = ?", $mValue, $sName);
  77. $this -> query($sQuery);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * Returns Migration Result information
  84. * @param string $sModule name
  85. * @return string
  86. */
  87. public function getTransferStatusText($sModule)
  88. {
  89. $sQuery = $this -> prepare("SELECT `status_text` FROM `{$this -> _sPrefix}transfers` WHERE `module` = ? LIMIT 1", $sModule);
  90. return $this -> getOne($sQuery);
  91. }
  92. /**
  93. * Returns current migration status
  94. * @param string $sModule name @uses BxDolMConfig::_aMigrationModules
  95. * @return string
  96. */
  97. public function getTransferStatus($sModule)
  98. {
  99. $sQuery = $this -> prepare("SELECT `status` FROM `{$this -> _sPrefix}transfers` WHERE `module` = ? LIMIT 1", $sModule);
  100. return $this -> getOne($sQuery);
  101. }
  102. public function updateTransferStatus($sModule, $sStatus)
  103. {
  104. $this -> query("UPDATE `{$this -> _sPrefix}transfers` SET `status` = :status WHERE `module` = :module", array('module' => $sModule, 'status' => $sStatus));
  105. }
  106. /**
  107. * Check if configuration already exists
  108. * @return Boolean
  109. */
  110. public function isConfigInstalled()
  111. {
  112. return $this -> getOne('SELECT COUNT(*) FROM `bx_dolphin_config`');
  113. }
  114. /**
  115. * Adds module for migration to migration list
  116. * @param string $sName modules name
  117. * @param string $mixedNumber records number
  118. * @return mixed
  119. */
  120. public function addToTransferList($sName, $mixedNumber)
  121. {
  122. $sNumber = $mixedNumber;
  123. if (is_array($mixedNumber))
  124. $sNumber = implode(',', $mixedNumber);
  125. return $this -> query("INSERT INTO `{$this -> _sPrefix}transfers` SET `module` = :module, `status` = 'not_started', `number` = :number", array('module' => $sName, 'number' => $sNumber));
  126. }
  127. /**
  128. * Check whether required Una plug-in installed for transferring
  129. * @param string $sName pugin name
  130. * @return Boolean
  131. */
  132. public function isPluginInstalled($sName)
  133. {
  134. $sQuery = $this -> prepare("SELECT COUNT(*) FROM `sys_modules` WHERE `type` = 'module' AND `name` = ? LIMIT 1" , $sName);
  135. return (int)$this -> getOne($sQuery) == 1;
  136. }
  137. public function cleanTransfersTable()
  138. {
  139. return $this -> query("TRUNCATE TABLE `{$this -> _sPrefix}transfers`");
  140. }
  141. public function isFinished($aElements){
  142. if (empty($aElements))
  143. return false;
  144. $sList = implode("','", $aElements);
  145. return $this -> getOne("SELECT COUNT(*) FROM `{$this -> _sPrefix}transfers` WHERE `module` IN (:list) AND `status` != 'finished'", array('list' => $sList)) == 0;
  146. }
  147. public function getPrivacyList()
  148. {
  149. return $this -> getAll("SELECT
  150. `id`,
  151. `title`,
  152. `check`,
  153. `active`
  154. FROM `sys_privacy_groups`
  155. WHERE `active`='1' AND `visible`='1'");
  156. }
  157. }
  158. /** @} */