PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/components/bitrix/sale.bsm.site.master/wizard/moduleinstallstep.php

https://gitlab.com/alexprowars/bitrix
PHP | 431 lines | 314 code | 62 blank | 55 comment | 32 complexity | 78986677009cc81f5fa33cd80831e3d2 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sale\BsmSiteMaster\Steps;
  3. use Bitrix\Main,
  4. Bitrix\Main\Localization\Loc,
  5. Bitrix\Sale\BsmSiteMaster\Tools\ModuleChecker,
  6. Bitrix\Sale\CrmSiteMaster\Tools\PushChecker;
  7. Loc::loadMessages(__FILE__);
  8. /**
  9. * Class ModuleInstallStep
  10. * Install required modules
  11. *
  12. * @package Bitrix\Sale\BsmSiteMaster\Steps
  13. */
  14. class ModuleInstallStep extends \CWizardStep
  15. {
  16. private $currentStepName = __CLASS__;
  17. /** @var \SaleBsmSiteMaster */
  18. private $component = null;
  19. /** @var ModuleChecker */
  20. private $moduleChecker;
  21. /** @var array */
  22. private $modules = [];
  23. /**
  24. * Prepare next/prev buttons
  25. *
  26. * @throws \ReflectionException
  27. */
  28. private function prepareButtons()
  29. {
  30. $steps = $this->component->getSteps($this->currentStepName);
  31. $shortClassName = (new \ReflectionClass($this))->getShortName();
  32. if (isset($steps["NEXT_STEP"]))
  33. {
  34. $this->SetNextStep($steps["NEXT_STEP"]);
  35. $this->SetNextCaption(Loc::getMessage("SALE_BSM_WIZARD_".mb_strtoupper($shortClassName)."_NEXT"));
  36. }
  37. if (isset($steps["PREV_STEP"]))
  38. {
  39. $this->SetPrevStep($steps["PREV_STEP"]);
  40. $this->SetPrevCaption(Loc::getMessage("SALE_BSM_WIZARD_".mb_strtoupper($shortClassName)."_PREV"));
  41. }
  42. }
  43. /**
  44. * Initialization step id, title and next/prev step
  45. *
  46. * @throws \ReflectionException
  47. */
  48. public function initStep()
  49. {
  50. $this->component = $this->GetWizard()->GetVar("component");
  51. $this->moduleChecker = $this->component->getModuleChecker();
  52. $this->SetStepID($this->currentStepName);
  53. $this->SetTitle(Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_TITLE"));
  54. $this->prepareButtons();
  55. }
  56. /**
  57. * Show step content
  58. *
  59. * @return bool
  60. * @throws Main\ArgumentOutOfRangeException
  61. */
  62. public function showStep()
  63. {
  64. $wizard =& $this->GetWizard();
  65. if ($this->GetErrors())
  66. {
  67. return false;
  68. }
  69. $this->modules = $this->GetWizard()->GetVar("modules");
  70. $this->moduleChecker->setInstallStatus();
  71. ob_start();
  72. ?>
  73. <div class="adm-bsm-site-master-progress-container" id="result">
  74. <div class="adm-bsm-site-master-progress-counter">
  75. <div class="adm-bsm-site-master-progress-container-num" id="progressBar_percent"></div>
  76. <div class="adm-bsm-site-master-progress-container-per">%</div>
  77. </div>
  78. <img src="<?=$this->component->getPath()?>/wizard/images/install-complete-icon.svg" alt="" class="adm-bsm-site-master-progress-complete">
  79. <div class="adm-bsm-site-master-progress-complete-text">
  80. <?=Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_INSTALL_FINISH")?>
  81. </div>
  82. </div>
  83. <div class="adm-bsm-site-master-progress">
  84. <div class="ui-progressbar ui-progressbar-lg ui-progressbar-success">
  85. <div class="ui-progressbar-track">
  86. <div class="ui-progressbar-bar" id="progressBar" style="width: 0%;"></div>
  87. </div>
  88. </div>
  89. </div>
  90. <div class="adm-bsm-site-master-progress-description" id="progress_description">
  91. <?=Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_INSTALL_WAIT1")?><br>
  92. <?=Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_INSTALL_WAIT2")?>
  93. </div>
  94. <div class="adm-bsm-slider-buttons" id="button_submit_wrap" style="display: none">
  95. <div class="ui-btn-container ui-btn-container-center">
  96. <button type="submit" class="ui-btn ui-btn-primary">
  97. <?=Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_NEXT")?>
  98. </button>
  99. </div>
  100. </div>
  101. <div id="error_container" style="display: none; margin-top: 25px">
  102. <div class="ui-alert ui-alert-danger ui-alert-inline ui-alert-icon-danger">
  103. <span class="ui-alert-message" id="error_text"></span>
  104. </div>
  105. <div class="adm-bsm-slider-buttons" id="error_buttons">
  106. <div class="ui-btn-container ui-btn-container-center">
  107. <button type="button" id="error_retry_button" class="ui-btn ui-btn-primary" onclick="">
  108. <?=Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_RETRY_BUTTON")?>
  109. </button>
  110. <button type="button" id="error_skip_button" class="ui-btn ui-btn-primary" onclick="">
  111. <?=Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_SKIP_BUTTON")?>
  112. </button>
  113. </div>
  114. </div>
  115. </div>
  116. <?
  117. $modulesName = array_keys($this->modules);
  118. echo $this->ShowHiddenField("nextStep", $modulesName[0]);
  119. echo $this->ShowHiddenField("nextStepStage", "");
  120. ?><iframe style="display:none;" id="iframe-post-form" name="iframe-post-form" src="javascript:''"></iframe><?
  121. list($firstModule, $stage) = $this->getFirstModule();
  122. $formName = $wizard->GetFormName();
  123. $nextStepVarName = $wizard->GetRealName("nextStep");
  124. $messages = Loc::loadLanguageFile(__FILE__);
  125. ?>
  126. <script type="text/javascript">
  127. BX.message(<?=\CUtil::PhpToJSObject($messages)?>);
  128. var moduleWizardForm = new CModuleWizardForm("<?=$formName?>", "iframe-post-form", "<?=$nextStepVarName?>");
  129. moduleWizardForm.Post("<?=$firstModule?>", "<?=$stage?>");
  130. </script>
  131. <?
  132. $content = ob_get_contents();
  133. ob_end_clean();
  134. $this->content = $content;
  135. return true;
  136. }
  137. /**
  138. * @return bool
  139. * @throws Main\ArgumentNullException
  140. * @throws Main\SystemException
  141. */
  142. public function onPostForm()
  143. {
  144. $wizard =& $this->GetWizard();
  145. if ($wizard->IsPrevButtonClick())
  146. {
  147. return false;
  148. }
  149. $this->modules = $this->GetWizard()->GetVar("modules");
  150. $moduleId = $wizard->GetVar("nextStep");
  151. $moduleStage = $wizard->GetVar("nextStepStage");
  152. if ($moduleId === "finish")
  153. {
  154. $modulesRequired = $wizard->GetVar("modulesRequired");
  155. $this->moduleChecker->setRequiredModules($modulesRequired);
  156. $checkModules = $this->moduleChecker->checkInstalledModules();
  157. if ($checkModules["NOT_INSTALL"])
  158. {
  159. $wizard->SetCurrentStep("Bitrix\Sale\BsmSiteMaster\Steps\ModuleStep");
  160. }
  161. else
  162. {
  163. $this->moduleChecker->deleteInstallStatus();
  164. $wizard->SetCurrentStep("Bitrix\Sale\BsmSiteMaster\Steps\SiteInstructionStep");
  165. }
  166. return true;
  167. }
  168. if ($moduleStage != "skip")
  169. {
  170. try
  171. {
  172. $this->installModule($moduleId);
  173. if ($moduleId === "pull")
  174. {
  175. $pushChecker = new PushChecker();
  176. $registerResult = $pushChecker->registerSharedServer();
  177. if (!$registerResult->isSuccess())
  178. {
  179. $this->SetError(implode("<br />", $registerResult->getErrorMessages()));
  180. }
  181. }
  182. }
  183. catch (\Exception $ex)
  184. {
  185. $this->SetError($ex->getMessage());
  186. }
  187. $cacheManager = Main\Application::getInstance()->getManagedCache();
  188. $cacheManager->clean("b_module");
  189. $cacheManager->clean("b_module_to_module");
  190. }
  191. if ($errors = $this->GetErrors())
  192. {
  193. $arError[] = Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_ERROR_OCCURED", [
  194. "#MODULE_NAME#" => $this->modules[$moduleId]["name"]
  195. ]);
  196. foreach ($errors as $error)
  197. {
  198. $arError[] = $error[0];
  199. }
  200. $strError = implode("<br />", $arError);
  201. $strError = addslashes(str_replace(["\r\n", "\r", "\n"], "<br />", $strError));
  202. if (mb_strpos($moduleId, ".") !== false)
  203. {
  204. $strError .= "<br /><br />".Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_ERROR_MARKETPLACE", [
  205. "#UPDATE_SYSTEM_PARTNER_LINK#" => "/bitrix/admin/update_system_partner.php?lang=".LANGUAGE_ID
  206. ]);
  207. }
  208. else
  209. {
  210. $strError .= "<br /><br />".Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_ERROR_UPDATESYSTEM", [
  211. "#MODULES_LINK#" => "/bitrix/admin/module_admin.php?lang=".LANGUAGE_ID
  212. ]);
  213. }
  214. $response = "window.moduleWizardForm.ShowError('".$strError."')";
  215. $this->sendResponse($response);
  216. }
  217. list($nextModule, $nextModuleStage, $stepsComplete) = $this->getModuleStep($moduleId);
  218. if ($nextModule == "finish")
  219. {
  220. $response = "window.moduleWizardForm.StopAjax();";
  221. $response .= "window.moduleWizardForm.SetStatus('100');";
  222. $response .= "window.moduleWizardForm.Post('".$nextModule."', '".$nextModuleStage."');";
  223. }
  224. else
  225. {
  226. $percent = round($stepsComplete);
  227. $response = "window.moduleWizardForm.SetStatus('".$percent."');";
  228. $response .= "window.moduleWizardForm.Post('".$nextModule."', '".$nextModuleStage."');";
  229. }
  230. $this->sendResponse($response);
  231. return true;
  232. }
  233. /**
  234. * @return array
  235. */
  236. private function getFirstModule()
  237. {
  238. $modules = array_keys($this->modules);
  239. foreach ($modules as $module)
  240. {
  241. $stage = "";
  242. return [
  243. $module,
  244. $stage
  245. ];
  246. }
  247. return [
  248. "module_not_found",
  249. "finish"
  250. ];
  251. }
  252. /**
  253. * Get next module for installation
  254. *
  255. * @param $moduleId
  256. * @return array
  257. */
  258. private function getModuleStep($moduleId)
  259. {
  260. $modules = array_keys($this->modules);
  261. $nextService = $nextServiceStage = "finish";
  262. $key = array_search($moduleId, $modules);
  263. if ($key !== false)
  264. {
  265. if (isset($modules[$key+1]))
  266. {
  267. $nextService = $nextServiceStage = $modules[$key+1];
  268. }
  269. }
  270. if (!in_array($moduleId, $modules) || $nextService == "finish")
  271. {
  272. return [
  273. $nextService,
  274. $nextServiceStage,
  275. 100
  276. ];
  277. }
  278. $wizard =& $this->GetWizard();
  279. $nextServiceStage = "";
  280. $modulesCount = $wizard->GetVar("modulesCount");
  281. $stepsComplete = round((($key + 1) * 100) / $modulesCount);
  282. $wizard->SetVar("modules", $modules);
  283. return [
  284. $nextService,
  285. $nextServiceStage,
  286. $stepsComplete
  287. ];
  288. }
  289. /**
  290. * @param $moduleId
  291. */
  292. private function onModuleInstalledEvent($moduleId)
  293. {
  294. foreach (GetModuleEvents("main", "OnModuleInstalled", true) as $arEvent)
  295. {
  296. \ExecuteModuleEventEx($arEvent, array($moduleId));
  297. }
  298. }
  299. /**
  300. * Install required modules
  301. *
  302. * @param $moduleId
  303. * @return bool
  304. */
  305. private function installModule($moduleId)
  306. {
  307. /** @noinspection PhpVariableNamingConventionInspection */
  308. global $DB, $APPLICATION;
  309. if ($DB->type == "MYSQL" && defined("MYSQL_TABLE_TYPE") && MYSQL_TABLE_TYPE <> '')
  310. {
  311. $res = $DB->Query("SET storage_engine = '".MYSQL_TABLE_TYPE."'", true);
  312. if(!$res)
  313. {
  314. //mysql 5.7 removed storage_engine variable
  315. $DB->Query("SET default_storage_engine = '".MYSQL_TABLE_TYPE."'", true);
  316. }
  317. }
  318. $this->onModuleInstalledEvent($moduleId);
  319. if (!Main\ModuleManager::isModuleInstalled($moduleId))
  320. {
  321. $module = \CModule::CreateModuleObject($moduleId);
  322. if (!is_object($module))
  323. {
  324. $this->SetError(Loc::getMessage("SALE_BSM_WIZARD_MODULEINSTALLSTEP_INSTALL_ERROR",
  325. ["#MODULE_NAME#" => $moduleId]
  326. ));
  327. return false;
  328. }
  329. if (method_exists($module, "CheckModules"))
  330. {
  331. $module->CheckModules();
  332. if ($ex = $APPLICATION->GetException())
  333. {
  334. $this->SetError($ex->GetString());
  335. return false;
  336. }
  337. }
  338. if (!$module->InstallDB())
  339. {
  340. if ($ex = $APPLICATION->GetException())
  341. {
  342. $this->SetError($ex->GetString());
  343. }
  344. return false;
  345. }
  346. $module->InstallEvents();
  347. /** @noinspection PhpVoidFunctionResultUsedInspection */
  348. if (!$module->InstallFiles())
  349. {
  350. if ($ex = $APPLICATION->GetException())
  351. {
  352. $this->SetError($ex->GetString());
  353. }
  354. return false;
  355. }
  356. }
  357. return true;
  358. }
  359. /**
  360. * @param $response
  361. */
  362. private function sendResponse($response)
  363. {
  364. /** @noinspection PhpVariableNamingConventionInspection */
  365. global $APPLICATION;
  366. $APPLICATION->RestartBuffer();
  367. die("[response]".$response."[/response]");
  368. }
  369. }