PageRenderTime 35ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sender/lib/segment/tileview.php

https://gitlab.com/alexprowars/bitrix
PHP | 314 lines | 216 code | 40 blank | 58 comment | 19 complexity | e1010553f64321ec21f62821b04cf5bc 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\Segment;
  9. use Bitrix\Main\Localization\Loc;
  10. use Bitrix\Sender\GroupConnectorTable;
  11. use Bitrix\Sender\GroupTable;
  12. use Bitrix\Sender\Entity;
  13. use Bitrix\Sender\Dispatch;
  14. use Bitrix\Sender\Integration\Sender\Connectors\Contact;
  15. use Bitrix\Sender\Message;
  16. Loc::loadMessages(__FILE__);
  17. /**
  18. * Class TileView
  19. * @package Bitrix\Sender\Entity
  20. * @internal
  21. */
  22. class TileView
  23. {
  24. const MAX_COUNT = 4;
  25. /** @var bool $isInclude Is include. */
  26. protected $isInclude = true;
  27. /** @var Message\Adapter $message Message. */
  28. protected $message;
  29. /**
  30. * Create instance.
  31. *
  32. * @param bool $isInclude Get stat by including or excluding segments in letters.
  33. * @return static
  34. */
  35. public static function create($isInclude = true)
  36. {
  37. return new static($isInclude);
  38. }
  39. /**
  40. * Constructor.
  41. *
  42. * @param bool $isInclude Get stat by including or excluding segments in letters.
  43. */
  44. public function __construct($isInclude = true)
  45. {
  46. $this->isInclude = $isInclude;
  47. }
  48. /**
  49. * Set message.
  50. *
  51. * @param Message\Adapter|null $message Message.
  52. *
  53. * @return $this
  54. */
  55. public function setMessage(Message\Adapter $message = null)
  56. {
  57. $this->message = $message;
  58. return $this;
  59. }
  60. /**
  61. * Get segments as tiles array.
  62. *
  63. * @return array
  64. */
  65. public function getSections()
  66. {
  67. $list = array(
  68. 'last' => array(
  69. 'id' => 'last',
  70. 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_LAST'),
  71. 'items' => array()
  72. ),
  73. 'freq' => array(
  74. 'id' => 'freq',
  75. 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_FREQ'),
  76. 'items' => array()
  77. ),
  78. 'system' => array(
  79. 'id' => 'system',
  80. 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_SYSTEM'),
  81. 'items' => array()
  82. ),
  83. 'case' => array(
  84. 'id' => 'case',
  85. 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_CASE'),
  86. 'items' => array()
  87. ),
  88. 'my' => array(
  89. 'id' => 'my',
  90. 'name' => Loc::getMessage('SENDER_SEGMENT_TILEVIEW_SECTION_MY'),
  91. 'items' => array()
  92. ),
  93. );
  94. $tiles = $this->getTiles(array('filter' => array('=HIDDEN' => false)));
  95. foreach ($tiles as $tile)
  96. {
  97. // set last
  98. if ($tile['data']['last'])
  99. {
  100. /** @var \Bitrix\Main\Type\DateTime $last */
  101. $last = $tile['data']['last'];
  102. $tile['data']['last'] = $last->getTimestamp();
  103. $list['last']['items'][] = $tile;
  104. }
  105. // set freq
  106. if ($tile['data']['freq'])
  107. {
  108. $list['freq']['items'][] = $tile;
  109. }
  110. // set cases
  111. if ($tile['data']['case'])
  112. {
  113. $list['case']['items'][] = $tile;
  114. }
  115. // set system or my
  116. $key = $tile['data']['system'] ? 'system' : 'my';
  117. $list[$key]['items'][] = $tile;
  118. }
  119. // sort & cut last
  120. usort(
  121. $list['last']['items'],
  122. function ($a, $b)
  123. {
  124. return ($a['data']['last'] > $b['data']['last']) ? -1 : 1;
  125. }
  126. );
  127. $list['last']['items'] = array_slice($list['last']['items'], 0, self::MAX_COUNT);
  128. // sort freq
  129. usort(
  130. $list['freq']['items'],
  131. function ($a, $b)
  132. {
  133. return ($a['data']['freq'] > $b['data']['freq']) ? -1 : 1;
  134. }
  135. );
  136. $list['freq']['items'] = array_slice($list['freq']['items'], 0, self::MAX_COUNT);
  137. // remove empty sections
  138. foreach ($list as $sectionId => $section)
  139. {
  140. if (count($section['items']) > 0)
  141. {
  142. continue;
  143. }
  144. unset($list[$sectionId]);
  145. }
  146. $list = array_values($list);
  147. return $list;
  148. }
  149. /**
  150. * Get segments as tiles array.
  151. *
  152. * @param integer $segmentId Segment ID.
  153. * @return array|null
  154. */
  155. public function getTile($segmentId)
  156. {
  157. $tiles = $this->getTiles(array('filter' => array('=ID' => $segmentId)));
  158. $tile = current($tiles) ?: null;
  159. return $tile;
  160. }
  161. /**
  162. * Get segments as tiles array.
  163. *
  164. * @param array $parameters Parameters.
  165. * @return array
  166. */
  167. public function getTiles(array $parameters = array())
  168. {
  169. $result = [];
  170. $ids = [];
  171. $prefix = $this->isInclude ? '' : '_EXCLUDE';
  172. $fieldDateUse = "DATE_USE$prefix";
  173. $fieldUseCount = "USE_COUNT$prefix";
  174. if (!isset($parameters['order']))
  175. {
  176. $parameters['order'] = array(
  177. 'SORT' => 'ASC',
  178. 'NAME' => 'ASC',
  179. $fieldDateUse => 'DESC'
  180. );
  181. }
  182. $segments = GroupTable::getList($parameters);
  183. foreach ($segments as $segment)
  184. {
  185. $item = array(
  186. 'id' => $segment['ID'],
  187. 'name' => $segment['NAME'],
  188. 'data' => array(
  189. 'last' => $segment[$fieldDateUse],
  190. 'freq' => (int) $segment[$fieldUseCount],
  191. 'case' => mb_substr($segment['CODE'], 0, 5) == 'case_',
  192. 'hidden' => $segment['HIDDEN'] == 'Y',
  193. 'system' => $segment['IS_SYSTEM'] == 'Y',
  194. 'hasStatic' => false,
  195. 'count' => array()
  196. ),
  197. );
  198. $item['bgcolor'] = self::getBackgroundColor($item['data']);
  199. $result[] = $item;
  200. $ids[] = $item['id'];
  201. }
  202. if (count($ids) > 0)
  203. {
  204. $connectors = GroupConnectorTable::getList([
  205. 'filter' => [
  206. '@GROUP_ID' => $ids
  207. ]
  208. ]);
  209. $hasStatic = [];
  210. foreach ($connectors as $connector)
  211. {
  212. if (!is_array($connector['ENDPOINT']))
  213. {
  214. $hasStatic[$connector['GROUP_ID']] = false;
  215. continue;
  216. }
  217. $entityConnector = \Bitrix\Sender\Connector\Manager::getConnector($connector['ENDPOINT']);
  218. $hasStatic[$connector['GROUP_ID']] = $entityConnector instanceof Contact && $connector['ADDRESS_COUNT'] > 0;
  219. }
  220. $duration = null;
  221. $messageTypes = array();
  222. if ($this->message)
  223. {
  224. $duration = Dispatch\DurationCountBased::create($this->message);
  225. $messageTypes = $this->message->getSupportedRecipientTypes();
  226. }
  227. $counters = Entity\Segment::getAddressCounters($ids);
  228. foreach ($result as $index => $item)
  229. {
  230. if (!isset($counters[$item['id']]))
  231. {
  232. continue;
  233. }
  234. // set count
  235. $item['data']['count'] = $counters[$item['id']];
  236. $item['data']['hasStatic'] = $hasStatic[$item['id']];
  237. // set duration
  238. foreach ($item['data']['count'] as $typeId => $count)
  239. {
  240. if (isset($item['data']['duration']) && $item['data']['duration'])
  241. {
  242. continue;
  243. }
  244. if (!$duration || !$this->message)
  245. {
  246. continue;
  247. }
  248. if (!in_array($typeId, $messageTypes))
  249. {
  250. continue;
  251. }
  252. $item['data']['duration'] = $duration->getIntervalDefault($count);
  253. }
  254. $result[$index] = $item;
  255. }
  256. }
  257. return $result;
  258. }
  259. protected static function getBackgroundColor(array $data)
  260. {
  261. if ($data['system'])
  262. {
  263. return null;//'#d3ffcd';
  264. }
  265. elseif ($data['hidden'])
  266. {
  267. return '#eef2f4';
  268. }
  269. else
  270. {
  271. return null;
  272. }
  273. }
  274. }