PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/report/lib/visualconstructor/fields/base.php

https://gitlab.com/alexprowars/bitrix
PHP | 539 lines | 280 code | 55 blank | 204 comment | 9 complexity | 86b945ae2658238c055a890eca61478d MD5 | raw file
  1. <?php
  2. namespace Bitrix\Report\VisualConstructor\Fields;
  3. use Bitrix\Main\Page\Asset;
  4. use Bitrix\Report\VisualConstructor\Form;
  5. /**
  6. * Class Base
  7. * @package Bitrix\Report\VisualConstructor\Fields
  8. */
  9. abstract class Base
  10. {
  11. const FIELDS_COMPONENT_NAME = 'bitrix:report.visualconstructor.config.fields';
  12. private $key;
  13. private $classList = array();
  14. private $dataAttributes = array();
  15. private $id;
  16. private $label = '';
  17. private $isDisplayLabel = true;
  18. private $prefix;
  19. private $postfix;
  20. private $weight = 0;
  21. private $assets = array();
  22. private $js = array();
  23. private $css = array();
  24. private $inline = array();
  25. private $jsEvents = array();
  26. private $jsEventListeners = array();
  27. private $form;
  28. private $compatibleViewTypes;
  29. private $inlineStyle;
  30. private $display = true;
  31. /**
  32. * @return string
  33. */
  34. public static function getClassName()
  35. {
  36. return get_called_class();
  37. }
  38. /**
  39. * @return Base
  40. */
  41. public function getPrefix()
  42. {
  43. return $this->prefix;
  44. }
  45. /**
  46. * Setter for string which render before field content.
  47. *
  48. * @param string|Base $prefix String for render before content.
  49. * @return void
  50. */
  51. public function setPrefix($prefix)
  52. {
  53. if (is_string($prefix))
  54. {
  55. $prefix = new Html($prefix);
  56. }
  57. $this->prefix = $prefix;
  58. }
  59. /**
  60. * @return Base
  61. */
  62. public function getPostfix()
  63. {
  64. return $this->postfix;
  65. }
  66. /**
  67. * Setter for string which render after field content.
  68. *
  69. * @param string|Base $postfix String for render after content.
  70. * @return void
  71. */
  72. public function setPostfix($postfix)
  73. {
  74. if (is_string($postfix))
  75. {
  76. $postfix = new Html($postfix);
  77. }
  78. $this->postfix = $postfix;
  79. }
  80. /**
  81. * @return array
  82. */
  83. public function getAssets()
  84. {
  85. return $this->assets;
  86. }
  87. /**
  88. * Attach js, css, or inline assets to field.
  89. *
  90. * @param array $assets Array of assets which attach to field.
  91. * @return void
  92. */
  93. public function addAssets($assets)
  94. {
  95. foreach ($assets as $key => $assetList)
  96. {
  97. switch ($key)
  98. {
  99. case 'js':
  100. $this->js = array_merge($this->js, $assetList);
  101. break;
  102. case 'css':
  103. $this->css = array_merge($this->css, $assetList);
  104. break;
  105. case 'inline':
  106. $this->inline = array_merge($this->inline, $assetList);
  107. }
  108. }
  109. $this->assets = $assets;
  110. }
  111. /**
  112. * @return Form
  113. */
  114. public function getForm()
  115. {
  116. return $this->form;
  117. }
  118. /**
  119. * Setter for form context.
  120. *
  121. * @param Form $form Form where render field.
  122. * @return void
  123. */
  124. public function setForm($form)
  125. {
  126. $this->form = $form;
  127. }
  128. /**
  129. * Check is displayable this field, if yes collect all assets,
  130. * And print content.
  131. *
  132. * @return void
  133. */
  134. public function render()
  135. {
  136. if (!$this->isDisplay())
  137. {
  138. return;
  139. }
  140. foreach ($this->js as $jsPath)
  141. {
  142. Asset::getInstance()->addJs($jsPath);
  143. }
  144. foreach ($this->css as $cssPath)
  145. {
  146. Asset::getInstance()->addCss($cssPath);
  147. }
  148. foreach ($this->inline as $inline)
  149. {
  150. //TODO
  151. Asset::getInstance()->addString($inline);
  152. }
  153. if ($this->getPrefix())
  154. {
  155. $this->getPrefix()->render();
  156. }
  157. $this->printContent();
  158. if ($this->getPostfix())
  159. {
  160. $this->getPostfix()->render();
  161. }
  162. }
  163. /**
  164. * Print field html/string ore somthing else printable.
  165. *
  166. * @return void
  167. */
  168. abstract public function printContent();
  169. /**
  170. * @return array
  171. */
  172. public function getJsEventListeners()
  173. {
  174. return $this->jsEventListeners;
  175. }
  176. /**
  177. * @return array
  178. */
  179. public function getJsEvents()
  180. {
  181. return $this->jsEvents;
  182. }
  183. /**
  184. * Add js event handler to field on some event which fire on $field in first parameter of this function.
  185. *
  186. * @param self|null $field Field which fire event.
  187. * @param string $eventKey Event key which listen this field.
  188. * @param array $jsParams JS parameters which pass to handler.
  189. * @return $this
  190. */
  191. public function addJsEventListener(Base $field = null, $eventKey, $jsParams)
  192. {
  193. $field->jsEvents[$eventKey][] = array(
  194. 'behaviourOwner' => $this,
  195. 'handlerParams' => $jsParams
  196. );
  197. $this->jsEventListeners[$eventKey][] = array(
  198. 'eventOwner' => $field,
  199. 'handlerParams' => $jsParams,
  200. );
  201. return $this;
  202. }
  203. /**
  204. * @return string
  205. */
  206. public function getLabel()
  207. {
  208. return $this->label;
  209. }
  210. /**
  211. * Setter for field label.
  212. *
  213. * @param string $label String to set as label for field.
  214. * @return void
  215. */
  216. public function setLabel($label)
  217. {
  218. $this->label = $label;
  219. }
  220. /**
  221. * @return array
  222. */
  223. public function getCompatibleViewTypes()
  224. {
  225. return $this->compatibleViewTypes;
  226. }
  227. /**
  228. * Setter of compatible widget view types.
  229. *
  230. * @param array $compatibleViewTypes Compatible widget view type list.
  231. * @return void
  232. */
  233. public function setCompatibleViewTypes($compatibleViewTypes)
  234. {
  235. $this->compatibleViewTypes = $compatibleViewTypes;
  236. }
  237. /**
  238. * @return string
  239. */
  240. public function getId()
  241. {
  242. return $this->id;
  243. }
  244. /**
  245. * Setter for id.
  246. *
  247. * @param string $id Unique id.
  248. * @return void
  249. */
  250. public function setId($id)
  251. {
  252. $this->id = $id;
  253. }
  254. /**
  255. * @return int
  256. */
  257. public function getWeight()
  258. {
  259. return $this->weight;
  260. }
  261. /**
  262. * Setter for Weight.
  263. *
  264. * @param int $weight Integer of weightof Field.
  265. * @return void
  266. */
  267. public function setWeight($weight)
  268. {
  269. $this->weight = $weight;
  270. }
  271. /**
  272. * Add to class list class string.
  273. *
  274. * @param string $class Class string.
  275. * @return void
  276. */
  277. public function addClass($class)
  278. {
  279. $this->classList[] = $class;
  280. }
  281. /**
  282. * @return array
  283. */
  284. public function getClasses()
  285. {
  286. return $this->classList;
  287. }
  288. /**
  289. * Get data attribute by key.
  290. *
  291. * @param string $key Key for data attribute which will return.
  292. * @return mixed|null
  293. */
  294. public function getDataAttribute($key)
  295. {
  296. return !empty($this->dataAttributes[$key]) ? $this->dataAttributes[$key] : null;
  297. }
  298. /**
  299. * @return array
  300. */
  301. public function getDataAttributes()
  302. {
  303. return $this->dataAttributes;
  304. }
  305. /**
  306. * Set Data attributes by array of pair key => values.
  307. *
  308. * @param array $dataAttributes Array of pair key=>value.
  309. * @return void
  310. */
  311. public function setDataAttributes($dataAttributes)
  312. {
  313. $this->dataAttributes = $dataAttributes;
  314. }
  315. /**
  316. * @param string $key Key for data attribute. ('role').
  317. * @param string $value Value for data attribute. ('widget').
  318. * @return void
  319. */
  320. public function addDataAttribute($key, $value)
  321. {
  322. $this->dataAttributes[$key] = $value;
  323. }
  324. /**
  325. * Conver id property to string for render as html in element.
  326. *
  327. * @return string
  328. */
  329. public function getRenderedIdAttribute()
  330. {
  331. $result = '';
  332. if ($this->getId() !== null)
  333. {
  334. $result = ' id="' . $this->getId() . '"';
  335. }
  336. return $result;
  337. }
  338. /**
  339. * @return string
  340. */
  341. public function getKey()
  342. {
  343. return $this->key;
  344. }
  345. /**
  346. * Seter for key.
  347. *
  348. * @param string $key Unique key for field.
  349. * @return void
  350. */
  351. public function setKey($key)
  352. {
  353. $this->key = $key;
  354. }
  355. /**
  356. * Convert class list property collection to string for render as html in field element.
  357. *
  358. * @return string
  359. */
  360. public function getRenderedClassAttributes()
  361. {
  362. $classes = $this->getClasses();
  363. $classes = array_filter($classes);
  364. $result = '';
  365. if (!empty($classes))
  366. {
  367. $result = !empty($classes) ? ' class="' . implode(' ', $classes) . '"' : '';
  368. }
  369. return $result;
  370. }
  371. /**
  372. * Conver data atttribute property collection to string for render as html in field element.
  373. *
  374. * @return string
  375. */
  376. public function getRenderedDataAttributes()
  377. {
  378. $dataAttributes = $this->getDataAttributes();
  379. $result = '';
  380. foreach ($dataAttributes as $key => $value)
  381. {
  382. $result .= ' data-' . $key . '="' . $value . '"';
  383. }
  384. return $result;
  385. }
  386. /**
  387. * Convert inline style propery collection to string for render as html.
  388. *
  389. * @return string
  390. */
  391. public function getRenderedInlineStyle()
  392. {
  393. $inlineStyles = $this->getInlineStyle();
  394. $result = '';
  395. if ($inlineStyles)
  396. {
  397. $result = 'style="';
  398. foreach ($inlineStyles as $key => $value)
  399. {
  400. $result .= ' ' . $key . ': ' . $value . ';';
  401. }
  402. $result .= '"';
  403. }
  404. return $result;
  405. }
  406. /**
  407. * @return bool
  408. */
  409. public function isDisplayLabel()
  410. {
  411. return $this->isDisplayLabel;
  412. }
  413. /**
  414. * Setter for display mode of label of field.
  415. *
  416. * @param bool $isDisplayLabel True if label must render else false.
  417. * @return void
  418. */
  419. public function setIsDisplayLabel($isDisplayLabel)
  420. {
  421. $this->isDisplayLabel = $isDisplayLabel;
  422. }
  423. /**
  424. * Add inline style by key and value.
  425. *
  426. * @param string $key Key of inline style. ('background-color').
  427. * @param string $value Value Of inline style. ('red').
  428. * @return void
  429. */
  430. public function addInlineStyle($key, $value)
  431. {
  432. $this->inlineStyle[$key] = $value;
  433. }
  434. /**
  435. * @return array
  436. */
  437. public function getInlineStyle()
  438. {
  439. return $this->inlineStyle;
  440. }
  441. /**
  442. * Setter dor inline style.
  443. *
  444. * @param array $inlineStyle Inline style string.
  445. * @return void
  446. */
  447. public function setInlineStyle($inlineStyle)
  448. {
  449. $this->inlineStyle = $inlineStyle;
  450. }
  451. /**
  452. * @return bool
  453. */
  454. public function isDisplay()
  455. {
  456. return $this->display;
  457. }
  458. /**
  459. * Setter for display mode.
  460. *
  461. * @param bool $display Render or not this field marker.
  462. * @return void
  463. */
  464. public function setDisplay($display)
  465. {
  466. $this->display = $display;
  467. }
  468. /**
  469. * Include component for field.
  470. *
  471. * @param string $templateName Template name string.
  472. * @param array $params Parameters pass to component.
  473. * @return void
  474. */
  475. protected function includeFieldComponent($templateName, $params = array())
  476. {
  477. global $APPLICATION;
  478. $defaultParams = array(
  479. 'CONFIGURATION_FIELD' => $this,
  480. );
  481. $params = array_merge($defaultParams, $params);
  482. $APPLICATION->IncludeComponent(self::FIELDS_COMPONENT_NAME, $templateName, $params);
  483. }
  484. }