PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sender/lib/connector/manager.php

https://gitlab.com/alexprowars/bitrix
PHP | 315 lines | 207 code | 37 blank | 71 comment | 21 complexity | b168f00b5fd2c9d2e3d603a8e840a876 MD5 | raw file
  1. <?php
  2. /**
  3. * Bitrix Framework
  4. * @package bitrix
  5. * @subpackage sender
  6. * @copyright 2001-2012 Bitrix
  7. */
  8. namespace Bitrix\Sender\Connector;
  9. use Bitrix\Main\Event;
  10. use Bitrix\Main\EventResult;
  11. use Bitrix\Main\Web\Json;
  12. use Bitrix\Sender\Integration;
  13. /**
  14. * Class Manager
  15. * @package Bitrix\Sender\Connector
  16. */
  17. class Manager
  18. {
  19. /**
  20. * Return connector for contacts.
  21. *
  22. * @param array $data Event Data.
  23. * @return mixed
  24. * @deprecated
  25. */
  26. public static function onConnectorListContact($data)
  27. {
  28. return $data;
  29. }
  30. /**
  31. * Return connector for recipients.
  32. *
  33. * @param array $data Event Data.
  34. * @return mixed
  35. * @deprecated
  36. */
  37. public static function onConnectorListRecipient($data)
  38. {
  39. return $data;
  40. }
  41. /**
  42. * Return list of connectors.
  43. *
  44. * @param array $data Event Data.
  45. * @return array
  46. */
  47. public static function onConnectorList($data)
  48. {
  49. return Integration\EventHandler::onConnectorList($data);
  50. }
  51. /**
  52. * Get fields from endpoint.
  53. *
  54. * @param array $endpointList Endpoints.
  55. * @return array
  56. */
  57. public static function getFieldsFromEndpoint(array $endpointList)
  58. {
  59. $arResult = array();
  60. foreach($endpointList as $endpoint)
  61. {
  62. $arResult[$endpoint['MODULE_ID']][$endpoint['CODE']][] = $endpoint['FIELDS'];
  63. }
  64. return $arResult;
  65. }
  66. /**
  67. * Get endpoint from fields.
  68. *
  69. * @param array $postData Post data.
  70. * @return array|null
  71. */
  72. public static function getEndpointFromFields(array $postData)
  73. {
  74. $result = null;
  75. $fieldsTmp = array();
  76. foreach($postData as $moduleId => $settings)
  77. {
  78. if (is_numeric($moduleId))
  79. {
  80. $moduleId = '';
  81. }
  82. foreach($settings as $code => $items)
  83. {
  84. foreach($items as $num => $field)
  85. {
  86. if (isset($fieldsTmp[$moduleId][$code][$num]) && is_array($field))
  87. {
  88. foreach($field as $fieldName => $fieldValue)
  89. {
  90. if(!isset($fieldsTmp[$moduleId][$code][$num][$fieldName]))
  91. {
  92. $fieldsTmp[$moduleId][$code][$num][$fieldName] = $fieldValue;
  93. }
  94. else
  95. {
  96. if(!is_array($fieldsTmp[$moduleId][$code][$num][$fieldName]))
  97. {
  98. $fieldsTmp[$moduleId][$code][$num][$fieldName] = array(
  99. $fieldsTmp[$moduleId][$code][$num][$fieldName]
  100. );
  101. }
  102. if(is_array($fieldValue))
  103. {
  104. $fieldsTmp[$moduleId][$code][$num][$fieldName] = array_merge(
  105. $fieldsTmp[$moduleId][$code][$num][$fieldName],
  106. $fieldValue
  107. );
  108. }
  109. else
  110. {
  111. $fieldsTmp[$moduleId][$code][$num][$fieldName][] = $fieldValue;
  112. }
  113. }
  114. }
  115. }
  116. else
  117. {
  118. if ($field)
  119. {
  120. try
  121. {
  122. $field = Json::decode($field);
  123. }
  124. catch (\Exception $exception)
  125. {
  126. }
  127. }
  128. else
  129. {
  130. $field = null;
  131. }
  132. $fieldsTmp[$moduleId][$code][$num] = $field;
  133. }
  134. }
  135. }
  136. }
  137. foreach($fieldsTmp as $moduleId => $settings)
  138. {
  139. if(is_numeric($moduleId))
  140. {
  141. $moduleId = '';
  142. }
  143. foreach($settings as $code => $items)
  144. {
  145. foreach($items as $filter => $fields)
  146. {
  147. if (!is_array($result))
  148. {
  149. $result = array();
  150. }
  151. $result[] = array(
  152. 'MODULE_ID' => $moduleId,
  153. 'CODE' => $code,
  154. 'FIELDS' => $fields,
  155. 'FILTER_ID' => $moduleId . "_" . $code . "_" . $filter,
  156. );
  157. }
  158. }
  159. }
  160. return $result;
  161. }
  162. /**
  163. * Return instance of connector by endpoint array.
  164. *
  165. * @param array
  166. * @return Base|null
  167. */
  168. public static function getConnector(array $endpoint)
  169. {
  170. $connector = null;
  171. $connectors = static::getConnectorList(array($endpoint));
  172. foreach($connectors as $connector)
  173. {
  174. break;
  175. }
  176. return $connector;
  177. }
  178. /**
  179. * Return array of instances of connector by endpoints array.
  180. *
  181. * @param array
  182. * @return Base[]
  183. */
  184. public static function getConnectorList(array $endpointList = null)
  185. {
  186. $connectorList = array();
  187. $connectorClassList = static::getConnectorClassList($endpointList);
  188. foreach($connectorClassList as $connectorDescription)
  189. {
  190. /** @var Base $connector */
  191. $connector = new $connectorDescription['CLASS_NAME'];
  192. $connector->setModuleId($connectorDescription['MODULE_ID']);
  193. $connectorList[] = $connector;
  194. }
  195. return $connectorList;
  196. }
  197. /**
  198. * Return array of connectors information by endpoints array.
  199. *
  200. * @param array
  201. * @return array
  202. */
  203. public static function getConnectorClassList(array $endpointList = null)
  204. {
  205. $resultList = array();
  206. $moduleIdFilter = null;
  207. $moduleConnectorFilter = null;
  208. if($endpointList)
  209. {
  210. $moduleIdFilter = array();
  211. foreach($endpointList as $endpoint)
  212. {
  213. $moduleIdFilter[] = $endpoint['MODULE_ID'];
  214. $moduleConnectorFilter[$endpoint['MODULE_ID']][] = $endpoint['CODE'];
  215. }
  216. }
  217. $data = array();
  218. $event = new Event('sender', 'OnConnectorList', array($data), $moduleIdFilter);
  219. $event->send();
  220. foreach ($event->getResults() as $eventResult)
  221. {
  222. if ($eventResult->getType() == EventResult::ERROR)
  223. {
  224. continue;
  225. }
  226. $eventResultParameters = $eventResult->getParameters();
  227. if($eventResultParameters && array_key_exists('CONNECTOR', $eventResultParameters))
  228. {
  229. $connectorClassNameList = $eventResultParameters['CONNECTOR'];
  230. if (!is_array($eventResultParameters['CONNECTOR']))
  231. {
  232. $connectorClassNameList = array($connectorClassNameList);
  233. }
  234. foreach ($connectorClassNameList as $connectorClassName)
  235. {
  236. if(!is_subclass_of($connectorClassName, '\Bitrix\Sender\Connector'))
  237. {
  238. continue;
  239. }
  240. /**
  241. * @var \Bitrix\Sender\Connector $connectorInstance
  242. */
  243. $connectorInstance = new $connectorClassName;
  244. $connectorCode = $connectorInstance->getCode();
  245. if($moduleConnectorFilter && !in_array($connectorCode, $moduleConnectorFilter[$eventResult->getModuleId()]))
  246. {
  247. continue;
  248. }
  249. $connectorName = $connectorInstance->getName();
  250. $connectorRequireConfigure =$connectorInstance->requireConfigure();
  251. $resultList[] = array(
  252. 'MODULE_ID' => $eventResult->getModuleId(),
  253. 'CLASS_NAME' => $connectorClassName,
  254. 'CODE' => $connectorCode,
  255. 'NAME' => $connectorName,
  256. 'REQUIRE_CONFIGURE' => $connectorRequireConfigure,
  257. );
  258. }
  259. }
  260. }
  261. if(!empty($resultList))
  262. usort($resultList, array(__CLASS__, 'sort'));
  263. return $resultList;
  264. }
  265. /**
  266. * Sort.
  267. *
  268. * @param $a
  269. * @param $b
  270. * @return int
  271. */
  272. public static function sort($a, $b)
  273. {
  274. if ($a['NAME'] == $b['NAME'])
  275. return 0;
  276. return ($a['NAME'] < $b['NAME']) ? -1 : 1;
  277. }
  278. }