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

/components/bitrix/sale.crm.site.master/wizard/crmgroupstep.php

https://gitlab.com/alexprowars/bitrix
PHP | 429 lines | 285 code | 60 blank | 84 comment | 17 complexity | f828bd067f3cdd0659ff2d18a03413d2 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Sale\CrmSiteMaster\Steps;
  3. if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true)
  4. {
  5. die();
  6. }
  7. use Bitrix\Main,
  8. Bitrix\Main\Application,
  9. Bitrix\Main\Localization\Loc,
  10. Bitrix\Sale\CrmSiteMaster\Tools\SitePatcher;
  11. Loc::loadMessages(__FILE__);
  12. /**
  13. * Class CrmGroupStep
  14. * @package Bitrix\Sale\CrmSiteMaster\Steps
  15. */
  16. class CrmGroupStep extends \CWizardStep
  17. {
  18. const ADMIN_USER_GROUP_ID = 1;
  19. const SELECTED_USER_GROUPS = "~SELECTED_USER_GROUPS";
  20. const EMPLOYEE_USER_GROUP_ID = "~EMPLOYEE_USER_GROUP_ID";
  21. private $currentStepName = __CLASS__;
  22. /** @var \SaleCrmSiteMaster */
  23. private $component = null;
  24. /** @var Main\Request */
  25. private $request;
  26. /**
  27. * Check step errors
  28. */
  29. private function setStepErrors()
  30. {
  31. $errors = $this->component->getWizardStepErrors($this->currentStepName);
  32. if ($errors)
  33. {
  34. foreach ($errors as $error)
  35. {
  36. $this->SetError($error);
  37. }
  38. }
  39. }
  40. /**
  41. * Prepare next/prev buttons
  42. *
  43. * @throws \ReflectionException
  44. */
  45. private function prepareButtons()
  46. {
  47. $steps = $this->component->getSteps($this->currentStepName);
  48. $shortClassName = (new \ReflectionClass($this))->getShortName();
  49. if (isset($steps["NEXT_STEP"]))
  50. {
  51. $this->SetNextStep($steps["NEXT_STEP"]);
  52. $this->SetNextCaption(Loc::getMessage("SALE_CSM_WIZARD_".mb_strtoupper($shortClassName)."_NEXT"));
  53. }
  54. }
  55. /**
  56. * Initialization step id, title and next step
  57. *
  58. * @throws Main\SystemException
  59. * @throws \ReflectionException
  60. */
  61. public function initStep()
  62. {
  63. $this->component = $this->GetWizard()->GetVar("component");
  64. $this->SetStepID($this->currentStepName);
  65. $this->SetTitle(Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_TITLE"));
  66. $this->request = Application::getInstance()->getContext()->getRequest();
  67. $this->prepareButtons();
  68. $this->setStepErrors();
  69. }
  70. /**
  71. * Show step content
  72. *
  73. * @return bool
  74. * @throws Main\ArgumentException
  75. * @throws Main\ObjectPropertyException
  76. * @throws Main\SystemException
  77. */
  78. public function showStep()
  79. {
  80. ob_start();
  81. ?>
  82. <div class="adm-crm-site-master-paragraph"><?=Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_DESCR1")?></div>
  83. <div class="adm-crm-site-master-paragraph"><?=Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_DESCR2")?></div>
  84. <div class="adm-crm-site-master-paragraph"><?=Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_DESCR3")?></div>
  85. <div class="ui-alert ui-alert-danger ui-alert-inline ui-alert-icon-danger">
  86. <span class="ui-alert-message"><?=Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_NOTE")?></span>
  87. </div>
  88. <div class="adm-crm-group-master-form">
  89. <div class="adm-crm-group-master-form-select-manager">
  90. {#USER_GROUPS_SELECT_MANAGER#}
  91. </div>
  92. <div class="adm-crm-group-master-form-select-admin">
  93. {#USER_GROUPS_SELECT_ADMIN#}
  94. </div>
  95. </div>
  96. <?
  97. $content = ob_get_contents();
  98. ob_end_clean();
  99. $this->content = $content;
  100. $this->showSelectUserGroupManagerHtml();
  101. $this->showSelectUserGroupAdminHtml();
  102. return true;
  103. }
  104. /**
  105. * @return bool
  106. * @throws Main\ArgumentException
  107. * @throws Main\ArgumentNullException
  108. * @throws Main\ArgumentOutOfRangeException
  109. * @throws Main\LoaderException
  110. * @throws Main\ObjectPropertyException
  111. * @throws Main\SystemException
  112. */
  113. public function onPostForm()
  114. {
  115. Main\Loader::includeModule("crm");
  116. $this->saveSelectedGroups();
  117. $employeeGroupId = $this->getEmployeesGroupId();
  118. if ($employeeGroupId)
  119. {
  120. Main\Config\Option::set("crm", self::EMPLOYEE_USER_GROUP_ID, $employeeGroupId);
  121. }
  122. $crmRoleId = $this->getCrmRoleId(
  123. Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_CRM_ROLE_ADM")
  124. );
  125. if ($crmRoleId === null)
  126. {
  127. $crmRoleId = $this->getCrmRoleId(
  128. Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_CRM_ROLE_MAN")
  129. );
  130. }
  131. if ($crmRoleId && $employeeGroupId)
  132. {
  133. $this->setCrmRole($crmRoleId, [$employeeGroupId]);
  134. }
  135. $this->setIntranetUserGroups();
  136. $sitePatcher = SitePatcher::getInstance();
  137. $sitePatcher->addEmployeesToCompanyStructure();
  138. return true;
  139. }
  140. /**
  141. * @throws Main\ArgumentOutOfRangeException
  142. */
  143. private function saveSelectedGroups()
  144. {
  145. $selectedGroups = [];
  146. $groupManagerList = $this->request->get("USER_GROUPS_MANAGER");
  147. if ($groupManagerList)
  148. {
  149. $selectedGroups["MANAGER"] = $groupManagerList;
  150. }
  151. $groupAdminList = $this->request->get("USER_GROUPS_ADMIN");
  152. if ($groupAdminList)
  153. {
  154. $selectedGroups["ADMIN"] = $groupAdminList;
  155. }
  156. if ($selectedGroups)
  157. {
  158. Main\Config\Option::set("sale", self::SELECTED_USER_GROUPS, serialize($selectedGroups));
  159. }
  160. unset($groupManagerList, $groupAdminList, $selectedGroups);
  161. }
  162. /**
  163. * @throws Main\ArgumentException
  164. * @throws Main\ArgumentNullException
  165. * @throws Main\ArgumentOutOfRangeException
  166. * @throws Main\LoaderException
  167. * @throws Main\ObjectPropertyException
  168. * @throws Main\SystemException
  169. */
  170. private function setIntranetUserGroups()
  171. {
  172. $groups = [self::ADMIN_USER_GROUP_ID];
  173. $groupManagerList = $this->request->get("USER_GROUPS_MANAGER");
  174. if ($groupManagerList)
  175. {
  176. $groups = array_merge($groupManagerList, $groups);
  177. }
  178. $groupAdminList = $this->request->get("USER_GROUPS_ADMIN");
  179. if ($groupAdminList)
  180. {
  181. $groups = array_merge($groupAdminList, $groups);
  182. }
  183. $employeesGroupId = $this->getEmployeesGroupId();
  184. if ($employeesGroupId)
  185. {
  186. $sitePatcher = SitePatcher::getInstance();
  187. $userList = $sitePatcher->getUserIdList($groups);
  188. $sitePatcher->addNewGroup($userList, [$employeesGroupId]);
  189. }
  190. }
  191. /**
  192. * @throws Main\ArgumentException
  193. * @throws Main\ObjectPropertyException
  194. * @throws Main\SystemException
  195. */
  196. private function showSelectUserGroupManagerHtml()
  197. {
  198. $groupList = $this->getUserGroupList();
  199. ob_start();
  200. ?>
  201. <div class="adm-crm-site-master-form-row">
  202. <label for="USER_GROUPS_MANAGER" class="adm-crm-site-master-form-label">
  203. <?=Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_SELECT_TITLE_MAN")?>
  204. </label>
  205. <div class="adm-crm-site-master-form-control">
  206. <div class="ui-ctl-multiple-select ui-ctl-w75">
  207. <select class="ui-ctl-element ui-ctl-element-auto"
  208. name="USER_GROUPS_MANAGER[]" id="USER_GROUPS_MANAGER" title="" multiple size="5"
  209. >
  210. <?
  211. foreach ($groupList as $group)
  212. {
  213. ?><option value="<?=$group["ID"]?>"><?=$group["NAME"]?></option><?
  214. }
  215. ?>
  216. </select>
  217. </div>
  218. </div>
  219. </div>
  220. <?
  221. $content = ob_get_contents();
  222. ob_end_clean();
  223. $this->content = str_replace("{#USER_GROUPS_SELECT_MANAGER#}", $content, $this->content);
  224. }
  225. /**
  226. * @throws Main\ArgumentException
  227. * @throws Main\ObjectPropertyException
  228. * @throws Main\SystemException
  229. */
  230. private function showSelectUserGroupAdminHtml()
  231. {
  232. $groupList = $this->getUserGroupList();
  233. ob_start();
  234. ?>
  235. <div class="adm-crm-site-master-form-row">
  236. <label for="USER_GROUPS_ADMIN" class="adm-crm-site-master-form-label">
  237. <?=Loc::getMessage("SALE_CSM_WIZARD_CRMGROUPSTEP_SELECT_TITLE_ADM")?>
  238. </label>
  239. <div class="adm-crm-site-master-form-control">
  240. <div class="ui-ctl-multiple-select ui-ctl-w75">
  241. <select class="ui-ctl-element ui-ctl-element-auto"
  242. name="USER_GROUPS_ADMIN[]" id="USER_GROUPS_ADMIN" title="" multiple size="5">
  243. <?
  244. foreach ($groupList as $group)
  245. {
  246. ?><option value="<?=$group["ID"]?>"><?=$group["NAME"]?></option><?
  247. }
  248. ?>
  249. </select>
  250. </div>
  251. </div>
  252. </div>
  253. <?
  254. $content = ob_get_contents();
  255. ob_end_clean();
  256. $this->content = str_replace("{#USER_GROUPS_SELECT_ADMIN#}", $content, $this->content);
  257. }
  258. /**
  259. * @return array
  260. * @throws Main\ArgumentException
  261. * @throws Main\ObjectPropertyException
  262. * @throws Main\SystemException
  263. */
  264. private function getUserGroupList()
  265. {
  266. $groupList = [];
  267. $groupIterator = Main\GroupTable::getList([
  268. "select" => ["ID", "NAME"],
  269. "filter" => [
  270. 'LOGIC' => 'AND',
  271. ["!%STRING_ID" => "CRM_SHOP_"],
  272. ["!STRING_ID" => "ADMIN_SECTION"],
  273. ["!STRING_ID" => "SUPPORT"],
  274. ["!STRING_ID" => "CREATE_GROUPS"],
  275. ["!STRING_ID" => "PERSONNEL_DEPARTMENT"],
  276. ["!STRING_ID" => "DIRECTION"],
  277. ["!STRING_ID" => "MARKETING_AND_SALES"],
  278. ["!%STRING_ID" => "EMPLOYEES_"],
  279. ["!%STRING_ID" => "PORTAL_ADMINISTRATION_"],
  280. ],
  281. ]);
  282. while ($group = $groupIterator->fetch())
  283. {
  284. $groupList[] = $group;
  285. }
  286. return $groupList;
  287. }
  288. /**
  289. * @return array
  290. */
  291. public function showButtons()
  292. {
  293. ob_start();
  294. if ($this->GetNextStepID() !== null)
  295. {
  296. ?>
  297. <input type="hidden" name="<?=$this->GetWizard()->nextStepHiddenID?>" value="<?=$this->GetNextStepID()?>">
  298. <button type="submit" class="ui-btn ui-btn-primary" name="<?=$this->GetWizard()->nextButtonID?>">
  299. <?=$this->GetNextCaption()?>
  300. </button>
  301. <?
  302. }
  303. $content = ob_get_contents();
  304. ob_end_clean();
  305. return [
  306. "CONTENT" => $content,
  307. "NEED_WRAPPER" => true,
  308. "CENTER" => true,
  309. ];
  310. }
  311. /**
  312. * @return int|null
  313. * @throws Main\ArgumentException
  314. * @throws Main\ArgumentNullException
  315. * @throws Main\ArgumentOutOfRangeException
  316. * @throws Main\LoaderException
  317. * @throws Main\ObjectPropertyException
  318. * @throws Main\SystemException
  319. */
  320. private function getEmployeesGroupId()
  321. {
  322. $employeeGroupId = Main\GroupTable::getList([
  323. "select" => ["ID"],
  324. "filter" => ["=STRING_ID" => "EMPLOYEES_".SitePatcher::getInstance()->getCrmSiteId()],
  325. ])->fetch();
  326. return $employeeGroupId ? (int)$employeeGroupId["ID"] : null;
  327. }
  328. /**
  329. * @param $crmRoleId
  330. * @param array $groups
  331. */
  332. private function setCrmRole($crmRoleId, array $groups)
  333. {
  334. $arPerms = [];
  335. foreach ($groups as $group)
  336. {
  337. $arPerms["G".$group][] = $crmRoleId;
  338. }
  339. $crmRole = new \CcrmRole();
  340. $crmRole->SetRelation($arPerms);
  341. try
  342. {
  343. \CCrmSaleHelper::addUserToShopGroup();
  344. $cache = new \CPHPCache;
  345. $cache->CleanDir("/crm/list_crm_roles/");
  346. }
  347. catch (\Exception $ex)
  348. {
  349. $this->SetError($ex->getMessage());
  350. }
  351. }
  352. /**
  353. * @param $name
  354. * @return int|null
  355. */
  356. private function getCrmRoleId($name)
  357. {
  358. $crmRoleId = null;
  359. $crmRoleIterator = \CCrmRole::GetList(
  360. ["ID" => "DESC"],
  361. ["NAME" => $name]
  362. );
  363. if ($crmRole = $crmRoleIterator->Fetch())
  364. {
  365. $crmRoleId = (int)$crmRole["ID"];
  366. }
  367. return $crmRoleId;
  368. }
  369. }