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

/components/bitrix/main.urlpreview/class.php

https://gitlab.com/alexprowars/bitrix
PHP | 228 lines | 192 code | 22 blank | 14 comment | 49 complexity | 9255e924bd2bb367f34976b282e1e6a9 MD5 | raw file
  1. <?php
  2. use Bitrix\Main;
  3. use Bitrix\Main\Application;
  4. use Bitrix\Main\Loader;
  5. use Bitrix\Main\Localization\Loc;
  6. use Bitrix\Main\UrlPreview\UrlMetadataTable;
  7. if (!defined('B_PROLOG_INCLUDED') || B_PROLOG_INCLUDED !== true) die();
  8. Loc::loadMessages(__FILE__);
  9. class UrlPreviewComponent extends \CBitrixComponent
  10. {
  11. protected $editMode = false;
  12. protected $checkAccess = false;
  13. protected $multiple = false;
  14. protected $metadataId;
  15. protected $mobileApp = false;
  16. protected function prepareParams()
  17. {
  18. $this->editMode = ($this->arParams['EDIT'] === 'Y');
  19. $this->mobileApp = ($this->arParams['PARAMS']['MOBILE'] === 'Y');
  20. if($this->mobileApp)
  21. $this->setTemplateName('mobile');
  22. else
  23. $this->setTemplateName('.default');
  24. return $this;
  25. }
  26. /**
  27. * Sets component arResult array
  28. */
  29. protected function prepareData()
  30. {
  31. $this->arResult['METADATA'] = $this->arParams['METADATA'];
  32. $this->setDynamicPreview();
  33. $this->arResult['FIELD_NAME'] = $this->arParams['PARAMS']['arUserField']['FIELD_NAME'];
  34. if($this->arResult['METADATA']['ID'] > 0)
  35. $this->arResult['FIELD_VALUE'] = Main\UrlPreview\UrlPreview::sign($this->arResult['METADATA']['ID']);
  36. else
  37. $this->arResult['FIELD_VALUE'] = null;
  38. $this->arResult['FIELD_ID'] = $this->arParams['PARAMS']['arUserField']['ID'];
  39. $this->arResult['ELEMENT_ID'] = $this->arParams['PARAMS']['urlPreviewId'];
  40. if(isset($this->arParams['~METADATA']['EMBED']) && $this->arParams['~METADATA']['EMBED'] != '')
  41. {
  42. $this->arResult['METADATA']['EMBED'] = $this->arParams['~METADATA']['EMBED'];
  43. if(mb_strpos($this->arResult['METADATA']['EMBED'], '<iframe') !== 0)
  44. {
  45. $this->arResult['METADATA']['EMBED'] = '<iframe class="urlpreview-iframe-html-embed" src="'.Main\UrlPreview\UrlPreview::getInnerFrameUrl($this->arResult['METADATA']['ID']).'" allowfullscreen="" width="'.Main\UrlPreview\UrlPreview::IFRAME_MAX_WIDTH.'" height="'.Main\UrlPreview\UrlPreview::IFRAME_MAX_HEIGHT.'" frameborder="0" onload="BXUrlPreview.adjustFrameHeight(this);"></iframe>';
  46. }
  47. $this->arResult['METADATA']['EMBED'] = $this->prepareFrame($this->arResult['METADATA']['EMBED']);
  48. }
  49. else
  50. {
  51. if($this->arParams['METADATA']['EXTRA']['VIDEO'])
  52. {
  53. $this->arResult['METADATA']['EMBED'] = $this->invokePlayer();
  54. }
  55. else
  56. {
  57. $this->arResult['METADATA']['EMBED'] = null;
  58. }
  59. }
  60. $this->arResult['SELECT_IMAGE'] = (
  61. $this->editMode
  62. && empty($this->arResult['METADATA']['EMBED'])
  63. && is_array($this->arResult['METADATA']['EXTRA'])
  64. && is_array($this->arResult['METADATA']['EXTRA']['IMAGES'])
  65. );
  66. if($this->arResult['SELECT_IMAGE'])
  67. {
  68. $this->arResult['SELECTED_IMAGE'] = $this->arResult['METADATA']['EXTRA']['SELECTED_IMAGE'] ?: 0;
  69. }
  70. else
  71. {
  72. $this->arResult['METADATA']['CONTAINER']['CLASSES'] = "";
  73. if ($this->arResult['METADATA']['IMAGE_ID'] > 0
  74. && $imageFile = \CFile::GetFileArray($this->arResult['METADATA']['IMAGE_ID']))
  75. {
  76. $this->arResult['METADATA']['IMAGE'] = $imageFile['SRC'];
  77. if($imageFile['HEIGHT'] > $imageFile['WIDTH'] * 1.5)
  78. {
  79. $this->arResult['METADATA']['CONTAINER']['CLASSES'] .= " urlpreview__container-left";
  80. }
  81. }
  82. $this->arResult['SHOW_CONTAINER'] = isset($this->arResult['METADATA']['IMAGE']) && $this->arResult['METADATA']['IMAGE'] != ''
  83. || isset($this->arResult['METADATA']['EMBED']) && $this->arResult['METADATA']['EMBED'] != '';
  84. if( isset($this->arResult['METADATA']['IMAGE'])
  85. && $this->arResult['METADATA']['IMAGE'] != ''
  86. && isset($this->arResult['METADATA']['EMBED'])
  87. && $this->arResult['METADATA']['EMBED'] != ''
  88. )
  89. {
  90. $this->arResult['METADATA']['CONTAINER']['CLASSES'] .= " urlpreview__container-switchable";
  91. $this->arResult['METADATA']['CONTAINER']['CLASSES'] .= " urlpreview__container-hide-embed";
  92. }
  93. }
  94. }
  95. protected function prepareFrame($embed)
  96. {
  97. if($this->mobileApp)
  98. {
  99. $document = new Main\UrlPreview\HtmlDocument($embed, new Main\Web\Uri('/'));
  100. $attributes = $document->extractElementAttributes('iframe');
  101. if(count($attributes) > 0)
  102. {
  103. $attributes = $attributes[0];
  104. $attributes['height'] = '100%';
  105. $attributes['width'] = '100%';
  106. $attributes['class'] = isset($attributes['class']) ? $attributes['class'].' ' : '';
  107. $attributes['class'] .= 'bx-mobile-video-frame';
  108. $embed = '<iframe';
  109. foreach($attributes as $name => $value)
  110. {
  111. $embed .= ' '.$name.'="'.$value.'"';
  112. }
  113. $embed.= '></iframe>';
  114. }
  115. }
  116. return $embed;
  117. }
  118. /**
  119. * Sets main element style
  120. */
  121. protected function prepareStyle()
  122. {
  123. $this->arResult['STYLE'] = '';
  124. if(!isset($this->arResult['METADATA']['ID']))
  125. {
  126. $this->arResult['STYLE'] .= "display:none; ";
  127. }
  128. if(isset($this->arParams['PARAMS']['STYLE']))
  129. {
  130. $this->arResult['STYLE'] .= $this->arParams['PARAMS']['STYLE']."; ";
  131. }
  132. }
  133. protected function setDynamicPreview()
  134. {
  135. if ($this->arParams['METADATA']['TYPE'] == UrlMetadataTable::TYPE_DYNAMIC)
  136. {
  137. if (is_array($this->arParams['METADATA']['HANDLER']))
  138. {
  139. $module = $this->arParams['METADATA']['HANDLER']['MODULE'];
  140. $className = $this->arParams['METADATA']['HANDLER']['CLASS'];
  141. $buildMethod = $this->arParams['METADATA']['HANDLER']['BUILD_METHOD'];
  142. $parameters = $this->arParams['METADATA']['HANDLER']['PARAMETERS'];
  143. if (Loader::includeModule($module) && method_exists($className, $buildMethod))
  144. {
  145. $this->arResult['DYNAMIC_PREVIEW'] = $className::$buildMethod($parameters);
  146. }
  147. } else
  148. {
  149. $this->arResult['METADATA']['ID'] = null;
  150. }
  151. }
  152. }
  153. /**
  154. * Include component bitrix:player to view html5 player. Returns html.
  155. *
  156. * @return string
  157. */
  158. protected function invokePlayer()
  159. {
  160. global $APPLICATION;
  161. $params = array(
  162. 'PATH' => $this->arParams['METADATA']['EXTRA']['VIDEO'],
  163. 'PLAYER_TYPE' => 'videojs',
  164. 'WIDTH' => '600',
  165. 'HEIGHT' => '340',
  166. );
  167. if(isset($this->arParams['METADATA']['EXTRA']['VIDEO_TYPE']))
  168. {
  169. $params['TYPE'] = $this->arParams['METADATA']['EXTRA']['VIDEO_TYPE'];
  170. }
  171. if(isset($this->arParams['METADATA']['IMAGE']))
  172. {
  173. $params['PREVIEW'] = $this->arParams['METADATA']['IMAGE'];
  174. }
  175. $playerComponent = 'bitrix:player';
  176. if($this->mobileApp)
  177. {
  178. $playerComponent = 'bitrix:mobile.player';
  179. }
  180. ob_start();
  181. $APPLICATION->IncludeComponent($playerComponent, '', $params);
  182. return ob_get_clean();
  183. }
  184. /**
  185. * Executes component
  186. */
  187. public function executeComponent()
  188. {
  189. $this->prepareParams();
  190. if(
  191. !isset($this->arParams['METADATA']['ID'])
  192. || $this->arParams['METADATA']['TYPE'] == UrlMetadataTable::TYPE_STATIC
  193. || (
  194. $this->arParams['METADATA']['TYPE'] == UrlMetadataTable::TYPE_DYNAMIC
  195. && !$this->mobileApp
  196. )
  197. )
  198. {
  199. $this->prepareData();
  200. $this->prepareStyle();
  201. if($this->arParams['METADATA']['TYPE'] == UrlMetadataTable::TYPE_DYNAMIC && $this->arResult['DYNAMIC_PREVIEW'] == '')
  202. return;
  203. $this->includeComponentTemplate($this->editMode ? 'edit' : 'show');
  204. }
  205. }
  206. }