/components/bitrix/bizproc.globalfield.edit/class.php

https://gitlab.com/alexprowars/bitrix · PHP · 241 lines · 199 code · 39 blank · 3 comment · 23 complexity · ca185b6a24fad671f80b8e252a51189c MD5 · raw file

  1. <?php
  2. use Bitrix\Main\LoaderException;
  3. if (!defined("B_PROLOG_INCLUDED") || B_PROLOG_INCLUDED !== true)
  4. {
  5. die();
  6. }
  7. class BizprocGlobalFieldEditComponent extends CBitrixComponent
  8. {
  9. private const VAR_MODE = 'variable';
  10. private const CONST_MODE = 'constant';
  11. private $mode;
  12. public function onPrepareComponentParams($arParams): array
  13. {
  14. if (isset($arParams['DOCUMENT_TYPE_SIGNED']))
  15. {
  16. $arParams['DOCUMENT_TYPE_SIGNED'] = htmlspecialcharsback($arParams['DOCUMENT_TYPE_SIGNED']);
  17. $arParams['DOCUMENT_TYPE'] = CBPDocument::unSignDocumentType($arParams['DOCUMENT_TYPE_SIGNED']);
  18. }
  19. $arParams['FIELD_ID'] = $arParams['FIELD_ID'] ? htmlspecialcharsback($arParams['FIELD_ID']) : null;
  20. $arParams['MODE'] = in_array($arParams['MODE'], ['constant', 'variable']) ? $arParams['MODE'] : null;
  21. $arParams['SET_TITLE'] = ($arParams["SET_TITLE"] === 'N' ? 'N' : 'Y');
  22. $arParams['NAME'] = ($arParams['NAME']) ? htmlspecialcharsback($arParams['NAME']) : null;
  23. return $arParams;
  24. }
  25. private function getTitle(string $id): ?string
  26. {
  27. if ($this->mode === self::VAR_MODE)
  28. {
  29. if (!\Bitrix\Bizproc\Workflow\Type\GlobalVar::getById($id))
  30. {
  31. return \Bitrix\Main\Localization\Loc::getMessage('BIZPROC_GLOBALFIELDS_EDIT_TITLE_VARIABLE_CREATE');
  32. }
  33. return \Bitrix\Main\Localization\Loc::getMessage('BIZPROC_GLOBALFIELDS_EDIT_TITLE_VARIABLE_EDIT');
  34. }
  35. elseif ($this->mode === self::CONST_MODE)
  36. {
  37. if (!\Bitrix\Bizproc\Workflow\Type\GlobalConst::getById($id))
  38. {
  39. return \Bitrix\Main\Localization\Loc::getMessage('BIZPROC_GLOBALFIELDS_EDIT_TITLE_CONSTANT_CREATE');
  40. }
  41. return \Bitrix\Main\Localization\Loc::getMessage('BIZPROC_GLOBALFIELDS_EDIT_TITLE_CONSTANT_EDIT');
  42. }
  43. else
  44. {
  45. return '';
  46. }
  47. }
  48. /**
  49. * @throws LoaderException
  50. */
  51. public function executeComponent()
  52. {
  53. global $APPLICATION;
  54. if (!\Bitrix\Main\Loader::includeModule('bizproc'))
  55. {
  56. static::showError(\Bitrix\Main\Localization\Loc::getMessage('BIZPROC_MODULE_NOT_INSTALLED'));
  57. return false;
  58. }
  59. $this->initMode();
  60. if ($this->arParams['SET_TITLE'] === 'Y')
  61. {
  62. $id = (string)$this->arParams['~FIELD_ID'];
  63. $title = static::getTitle($id);
  64. $APPLICATION->SetTitle($title);
  65. }
  66. $this->arResult = [
  67. 'fieldTypes' => $this->getFieldsTypes(),
  68. 'fieldInfo' => $this->getFieldInfo(),
  69. 'visibilityTypes' => $this->getVisibilityTypes(),
  70. 'disabled' => $this->arParams['FIELD_ID'] ? 'disabled' : '',
  71. 'mode' => $this->mode,
  72. ];
  73. return $this->includeComponentTemplate();
  74. }
  75. private static function showError(string $message)
  76. {
  77. echo <<<HTML
  78. <div class="ui-alert ui-alert-danger ui-alert-icon-danger">
  79. <span class="ui-alert-message">{$message}</span>
  80. </div>
  81. HTML;
  82. }
  83. private function initMode()
  84. {
  85. if ($this->arParams['MODE'] === self::CONST_MODE)
  86. {
  87. $this->mode = self::CONST_MODE;
  88. }
  89. elseif ($this->arParams['MODE'] === self::VAR_MODE)
  90. {
  91. $this->mode = self::VAR_MODE;
  92. }
  93. else
  94. {
  95. $this->mode = null;
  96. }
  97. }
  98. private function getFieldsTypes(): array
  99. {
  100. $baseTypes = \Bitrix\Bizproc\FieldType::getBaseTypesMap();
  101. unset($baseTypes[\Bitrix\Bizproc\FieldType::INTERNALSELECT]);
  102. unset($baseTypes[\Bitrix\Bizproc\FieldType::FILE]);
  103. $runtime = CBPRuntime::GetRuntime();
  104. $runtime->StartRuntime();
  105. $documentService = $runtime->GetService("DocumentService");
  106. $documentType = $this->arParams['DOCUMENT_TYPE'];
  107. $documentTypes = $documentService->GetDocumentFieldTypes($documentType);
  108. $fieldTypes = [];
  109. foreach ($documentTypes as $key => $value)
  110. {
  111. if ($key == 'UF:date')
  112. {
  113. $key = 'date';
  114. }
  115. if (!isset($baseTypes[$key]))
  116. {
  117. continue;
  118. }
  119. $fieldTypes[$key] = $value['Name'];
  120. }
  121. $availableTypes = $this->arParams['TYPES'];
  122. if (!$availableTypes)
  123. {
  124. return $fieldTypes;
  125. }
  126. if (!is_array($availableTypes))
  127. {
  128. $availableTypes = [$availableTypes];
  129. }
  130. $types = [];
  131. foreach ($availableTypes as $type)
  132. {
  133. if (array_key_exists($type, $fieldTypes))
  134. {
  135. $types[$type] = $fieldTypes[$type];
  136. }
  137. }
  138. return $types;
  139. }
  140. private function getVisibilityTypes(): array
  141. {
  142. $documentType = $this->arParams['DOCUMENT_TYPE'];
  143. if ($this->mode === self::VAR_MODE)
  144. {
  145. return \Bitrix\Bizproc\Workflow\Type\GlobalVar::getVisibilityShortNames($documentType);
  146. }
  147. elseif ($this->mode === self::CONST_MODE)
  148. {
  149. return \Bitrix\Bizproc\Workflow\Type\GlobalConst::getVisibilityShortNames($documentType);
  150. }
  151. else
  152. {
  153. return [];
  154. }
  155. }
  156. private function getFieldInfo(): array
  157. {
  158. $id = (string)$this->arParams['~FIELD_ID'];
  159. if (!$id)
  160. {
  161. $newProperty = [];
  162. if ($this->arParams['~NAME'])
  163. {
  164. $newProperty['Name'] = (string)$this->arParams['~NAME'];
  165. }
  166. if ($this->arParams['VISIBILITY'])
  167. {
  168. $newProperty['Visibility'] = (string)$this->arParams['VISIBILITY'];
  169. }
  170. return $newProperty;
  171. }
  172. if ($this->mode === self::VAR_MODE)
  173. {
  174. $table = \Bitrix\Bizproc\Workflow\Type\GlobalVar::class;
  175. }
  176. elseif ($this->mode === self::CONST_MODE)
  177. {
  178. $table = \Bitrix\Bizproc\Workflow\Type\GlobalConst::class;
  179. }
  180. else
  181. {
  182. return [];
  183. }
  184. if (method_exists($table, 'getById'))
  185. {
  186. $property = $table::getById($id);
  187. if ($property)
  188. {
  189. if ($property['Type'] === 'user')
  190. {
  191. $property['Default'] = CBPHelper::UsersArrayToString(
  192. $property['Default'],
  193. null,
  194. $this->arParams['DOCUMENT_TYPE']
  195. );
  196. }
  197. return array_merge($property, ['id' => $id]);
  198. }
  199. }
  200. return [];
  201. }
  202. }