BxBaseModGeneralTemplate.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Module representation.
  13. */
  14. class BxBaseModGeneralTemplate extends BxDolModuleTemplate
  15. {
  16. protected $MODULE;
  17. protected $_bIsApi;
  18. public $aMethodsToCallAddJsCss = array('entry', 'unit');
  19. public function __construct(&$oConfig, &$oDb)
  20. {
  21. parent::__construct($oConfig, $oDb);
  22. $this->_bIsApi = bx_is_api();
  23. }
  24. public function addLocationBase()
  25. {
  26. parent::addLocationBase();
  27. $this->addLocation('mod_general', BX_DIRECTORY_PATH_MODULES . 'base' . DIRECTORY_SEPARATOR . 'general' . DIRECTORY_SEPARATOR, BX_DOL_URL_MODULES . 'base/general/');
  28. }
  29. public function getJsCode($sType, $aParams = array(), $mixedWrap = true)
  30. {
  31. $sMask = "{var} {object} = new {class}({params});";
  32. $aMaskMarkers = array();
  33. if(is_array($mixedWrap)) {
  34. if(!empty($mixedWrap['mask']))
  35. $sMask = $mixedWrap['mask'];
  36. if(!empty($mixedWrap['mask_markers']) && is_array($mixedWrap['mask_markers']))
  37. $aMaskMarkers = $mixedWrap['mask_markers'];
  38. }
  39. $sJsClass = $this->_oConfig->getJsClass($sType);
  40. if(!empty($aParams['js_class'])) {
  41. $sJsClass = $aParams['js_class'];
  42. unset($aParams['js_class']);
  43. }
  44. $sJsObject = $this->_oConfig->getJsObject($sType);
  45. if(!empty($aParams['js_object'])) {
  46. $sJsObject = $aParams['js_object'];
  47. unset($aParams['js_object']);
  48. }
  49. $sBaseUri = $this->_oConfig->getBaseUri();
  50. $aParams = array_merge([
  51. 'sActionUri' => $sBaseUri,
  52. 'sActionUrl' => BX_DOL_URL_ROOT . $sBaseUri,
  53. 'sObjName' => $sJsObject,
  54. 'aHtmlIds' => [],
  55. 'oRequestParams' => []
  56. ], $aParams);
  57. $sContent = false;
  58. /**
  59. * @hooks
  60. * @hookdef hook-system-get_js_code 'system', 'get_js_code' - hook to override an instance of JavaScript class associate with the page
  61. * - $unit_name - equals `system`
  62. * - $action - equals `get_js_code`
  63. * - $object_id - not used
  64. * - $sender_id - not used
  65. * - $extra_params - array of additional params with the following array keys:
  66. * - `mask` - [string] by ref, a mask to generate JS code, can be overridden in hook processing
  67. * - `mask_markers` - [array] by ref, an array of markers to be parsed in the mask, can be overridden in hook processing
  68. * - `object` - [string] by ref, a string with JS object name, can be overridden in hook processing
  69. * - `class` - [string] by ref, a string with JS class name, can be overridden in hook processing
  70. * - `params` - [array] by ref, an array of parameters as key&value pairs which will be passed to constructor, can be overridden in hook processing
  71. * - `override_result` - [string] or [boolean] by ref, if string is returned then it will be used as resulting JS code, can be overridden in hook processing
  72. * @hook @ref hook-system-get_js_code
  73. */
  74. bx_alert('system', 'get_js_code', 0, 0, [
  75. 'mask' => &$sMask,
  76. 'mask_markers' => &$aMaskMarkers,
  77. 'object' => &$sJsObject,
  78. 'class' => &$sJsClass,
  79. 'params' => &$aParams,
  80. 'override_result' => &$sContent,
  81. ]);
  82. if($sContent === false)
  83. $sContent = bx_replace_markers($sMask, array_merge([
  84. 'var' => 'var',
  85. 'object' => $sJsObject,
  86. 'class' => $sJsClass,
  87. 'params' => json_encode($aParams)
  88. ], $aMaskMarkers));
  89. return ($mixedWrap === true || (is_array($mixedWrap) && isset($mixedWrap['wrap']) && $mixedWrap['wrap'] === true)) ? $this->_wrapInTagJsCode($sContent) : $sContent;
  90. }
  91. public function getTitleAuto($aData, $iMaxLen = 20, $sEllipsisSign = '...')
  92. {
  93. $CNF = &$this->getModule()->_oConfig->CNF;
  94. if(isset($aData[$CNF['FIELD_TITLE']]))
  95. return $this->getTitle($aData);
  96. $sResult = $this->getText($aData);
  97. if(strlen($sResult) > 0 && $iMaxLen > 0)
  98. $sResult = strmaxtextlen($sResult, $iMaxLen, $sEllipsisSign);
  99. return $sResult;
  100. }
  101. public function getTitle($aData, $mixedProcessOutput = BX_DATA_TEXT)
  102. {
  103. $CNF = &$this->getModule()->_oConfig->CNF;
  104. if(!isset($aData[$CNF['FIELD_TITLE']]))
  105. return '';
  106. $sResult = $aData[$CNF['FIELD_TITLE']];
  107. if($mixedProcessOutput !== false && !empty($sResult))
  108. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  109. return $sResult;
  110. }
  111. public function getAbstract($aData, $mixedProcessOutput = BX_DATA_TEXT)
  112. {
  113. $CNF = &$this->getModule()->_oConfig->CNF;
  114. if(!isset($CNF['FIELD_ABSTRACT']) || !isset($aData[$CNF['FIELD_ABSTRACT']]))
  115. return '';
  116. $sResult = $aData[$CNF['FIELD_ABSTRACT']];
  117. if($mixedProcessOutput !== false && !empty($sResult))
  118. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  119. return $sResult;
  120. }
  121. public function getText($aData, $mixedProcessOutput = BX_DATA_HTML)
  122. {
  123. $CNF = &$this->getModule()->_oConfig->CNF;
  124. if(!isset($CNF['FIELD_TEXT']) || !isset($aData[$CNF['FIELD_TEXT']]))
  125. return '';
  126. $sResult = $aData[$CNF['FIELD_TEXT']];
  127. if($mixedProcessOutput !== false && !empty($sResult))
  128. $sResult = bx_process_output($sResult, (int)$mixedProcessOutput);
  129. return $sResult;
  130. }
  131. protected function getSummary($aData, $sTitle = '', $sText = '', $sUrl = '')
  132. {
  133. $sAbstract = $this->getAbstract($aData);
  134. if(!empty($sAbstract))
  135. return $sAbstract;
  136. $CNF = &$this->getModule()->_oConfig->CNF;
  137. if(empty($CNF['PARAM_CHARS_SUMMARY']))
  138. return '';
  139. // get summary
  140. $sLinkMore = ' <a title="' . bx_html_attribute(_t('_sys_read_more', $sTitle)) . '" href="' . $sUrl . '"><i class="sys-icon ellipsis-h"></i></a>';
  141. return strmaxtextlen($sText, (int)getParam($CNF['PARAM_CHARS_SUMMARY']), $sLinkMore);
  142. }
  143. public function getProfileLink($mixedProfile)
  144. {
  145. if(!is_array($mixedProfile))
  146. $mixedProfile = $this->getModule()->getProfileInfo((int)$mixedProfile);
  147. return $this->getLink('link', array(
  148. 'href' => $mixedProfile['link'],
  149. 'title' => bx_html_attribute(!empty($mixedProfile['title']) ? $mixedProfile['title'] : $mixedProfile['name']),
  150. 'content' => $mixedProfile['name']
  151. ));
  152. }
  153. public function getLink($sTemplate, $aParams)
  154. {
  155. return $this->parseHtmlByName($sTemplate . '.html', array(
  156. 'href' => $aParams['href'],
  157. 'title' => $aParams['title'],
  158. 'content' => $aParams['content']
  159. ));
  160. }
  161. function getContextAddon ($aData, $oProfile)
  162. {
  163. $CNF = &$this->getModule()->_oConfig->CNF;
  164. $sUrl = 'page.php?i=' . $CNF['URI_ENTRIES_BY_CONTEXT'] . '&profile_id=' . $oProfile->id();
  165. $bActAsProfile = BxDolService::call($oProfile->getModule(), 'act_as_profile');
  166. if ($bActAsProfile)
  167. $sUrl = 'page.php?i=' . $CNF['URI_AUTHOR_ENTRIES'] . '&profile_id=' . $oProfile->id();
  168. $sUrl = BxDolPermalinks::getInstance()->permalink($sUrl);
  169. return _t($CNF['T']['txt_all_entries_in'], $sUrl, $oProfile->getDisplayName(), $this->getModule()->_oDb->getEntriesNumByContext($oProfile->id()));
  170. }
  171. function getContextDesc ($aData)
  172. {
  173. return '';
  174. }
  175. public function getTmplVarsText($aData)
  176. {
  177. $CNF = &$this->getModule()->_oConfig->CNF;
  178. $aVars = $aData;
  179. $aVars['entry_title'] = $this->getTitle($aData);
  180. $aVars['entry_abstract'] = $this->getAbstract($aData);
  181. $aVars['bx_if:show_entry_abstract'] = [
  182. 'condition' => !empty($aVars['entry_abstract']),
  183. 'content' => [
  184. 'entry_abstract' => $aVars['entry_abstract']
  185. ]
  186. ];
  187. $aVars['entry_text'] = $this->getText($aData);
  188. $aVars['badges'] = $this->getModule()->serviceGetBadges($aData[$CNF['FIELD_ID']]);
  189. if (!empty($CNF['OBJECT_METATAGS'])) {
  190. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  191. // keywords
  192. if ($oMetatags->keywordsIsEnabled()) {
  193. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']), array('entry_title', 'entry_text'));
  194. foreach ($aFields as $sField)
  195. $aVars[$sField] = $oMetatags->keywordsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  196. }
  197. // mentions
  198. if ($oMetatags->mentionsIsEnabled()) {
  199. $aFields = array_merge($oMetatags->metaFields($aData, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'], true), array('entry_text'));
  200. foreach ($aFields as $sField)
  201. $aVars[$sField] = $oMetatags->mentionsParse($aData[$CNF['FIELD_ID']], $aVars[$sField]);
  202. }
  203. // location
  204. $aVars['location'] = $oMetatags->locationsIsEnabled() ? $oMetatags->locationsString($aData[$CNF['FIELD_ID']]) : '';
  205. }
  206. unset($aVars['recipients']);
  207. return $aVars;
  208. }
  209. public function entryBreadcrumb($aContentInfo, $aTmplVarsItems = array())
  210. {
  211. $CNF = &BxDolModule::getInstance($this->MODULE)->_oConfig->CNF;
  212. return bx_is_api() ? ['id' => 2, 'type' => 'breadcrumb', 'data' => ['name' => BxDolModule::getInstance($this->MODULE)->_aModule['title'], 'url' => BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME'])]] : $this->parseHtmlByName('breadcrumb.html', [
  213. 'url_home' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME'])),
  214. 'icon_home' => $CNF['ICON'],
  215. 'bx_repeat:items' => $aTmplVarsItems
  216. ]);
  217. }
  218. public function entryText ($aData, $sTemplateName = 'entry-text.html')
  219. {
  220. $aVars = $this->getTmplVarsText($aData);
  221. if (empty($aVars['entry_text']))
  222. return false;
  223. return bx_is_api() ? $aVars : $this->parseHtmlByName($sTemplateName, $aVars);
  224. }
  225. public function entryLocation ($iContentId)
  226. {
  227. $CNF = &$this->getModule()->_oConfig->CNF;
  228. if (empty($CNF['OBJECT_METATAGS']))
  229. return '';
  230. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  231. if (!($sLocationString = $oMetatags->locationsString($iContentId)))
  232. return '';
  233. $aVars = array (
  234. 'location' => $sLocationString
  235. );
  236. return $this->parseHtmlByName('entry-location.html', $aVars);
  237. }
  238. public function entryInfo($aData, $aValues = array())
  239. {
  240. $CNF = $this->_oConfig->CNF;
  241. $aValuesDefault = array();
  242. if (isset($aData[$CNF['FIELD_ADDED']]))
  243. $aValuesDefault[] = array(
  244. 'title' => _t('_sys_txt_field_created'),
  245. 'value' => bx_time_js($aData[$CNF['FIELD_ADDED']]),
  246. );
  247. if (isset($aData[$CNF['FIELD_CHANGED']]))
  248. $aValuesDefault[] = array(
  249. 'title' => _t('_sys_txt_field_updated'),
  250. 'value' => bx_time_js($aData[$CNF['FIELD_CHANGED']]),
  251. );
  252. $aValues = array_merge($aValuesDefault, $aValues);
  253. return $this->parseHtmlByName('entry-info.html', array(
  254. 'bx_repeat:info' => $aValues,
  255. ));
  256. }
  257. public function getFavoriteList($oProfile, $iStart, $iPerPage, $aParams)
  258. {
  259. $CNF = $this->_oConfig->CNF;
  260. $bEmptyMessage = false;
  261. if(isset($aParams['empty_message'])) {
  262. $bEmptyMessage = (bool)$aParams['empty_message'];
  263. unset($aParams['empty_message']);
  264. }
  265. $oFavorite = BxDolFavorite::getObjectInstance($CNF['OBJECT_FAVORITES'], 0, true);
  266. $aListsData = $oFavorite->getQueryObject()->getList(array('type' => 'active', 'author_id' => $oProfile->id(), 'need_default' => true, 'start' => $iStart, 'limit' => $iPerPage + 1));
  267. $iNum = count($aListsData);
  268. if ($iNum > $iPerPage)
  269. $aListsData = array_slice($aListsData, 0, $iPerPage);
  270. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_LIST_VIEW']);
  271. $aListsTmpl = array();
  272. foreach($aListsData as $iListId => $sName) {
  273. $aParams['list_id'] = $iListId;
  274. if ($oPrivacy->check($iListId) || $iListId == 0){
  275. $aTmp = $this->getModule()->_serviceBrowse ('favorite', array_merge(array('user' => $oProfile->id()), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, false);
  276. if ($aTmp && $aTmp['content']){
  277. $aListsTmpl[] = array(
  278. 'title' => $sName,
  279. 'content_url' => $this->getModule()->_getFavoriteListUrl($iListId, $oProfile->id()),
  280. 'content' => $aTmp['content']
  281. );
  282. }
  283. }
  284. else{
  285. $aListsTmpl[] = array(
  286. 'title' => 'private list',
  287. 'content_url' => 'javascript:',
  288. 'content' => ''
  289. );
  290. }
  291. }
  292. $oPaginate = new BxTemplPaginate(array(
  293. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}', " . bx_js_string(json_encode($aParams)) . ", 'list_start', 'list_per_page');",
  294. 'num' => $iNum,
  295. 'per_page' => $iPerPage,
  296. 'start' => $iStart,
  297. ));
  298. if (count($aListsTmpl) > 0)
  299. return $this->parseHtmlByName('favorite-lists.html', array('bx_repeat:items' => $aListsTmpl)) . $oPaginate->getSimplePaginate() . $oFavorite->getJsScript();
  300. return false;
  301. }
  302. public function getFavoritesListInfo($aList, $oProfile)
  303. {
  304. $CNF = $this->_oConfig->CNF;
  305. $iListId = !empty($aList['id']) ? (int)$aList['id'] : 0;
  306. $oFavorite = BxDolFavorite::getObjectInstance($CNF['OBJECT_FAVORITES'], 0, true);
  307. $aListInfo = $oFavorite->getQueryObject()->getList(array('type' => 'info', 'list_id' => $iListId, 'author_id' => $oProfile->id()));
  308. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_LIST_VIEW']);
  309. $sTitle = "";
  310. if ($aListInfo['allow_view_favorite_list_to'] < 0){
  311. $oProfileContext = BxDolProfile::getInstance(abs($aListInfo['allow_view_favorite_list_to']));
  312. $sTitle = $oProfileContext->getDisplayName();
  313. }
  314. else{
  315. if (empty($aListInfo['allow_view_favorite_list_to']))
  316. $aListInfo['allow_view_favorite_list_to'] = 3;
  317. $aPrivaciInfo = $oPrivacy->getGroupsBy(array('type'=>'id' , 'id'=> $aListInfo['allow_view_favorite_list_to']));
  318. $sTitle = _t($aPrivaciInfo['title']);
  319. }
  320. $aListsTmpl = array();
  321. if (!empty($aListInfo['created'])){
  322. $aListsTmpl[] = array('title' => _t('_sys_form_favorite_list_title_created'), 'value' => bx_time_js($aListInfo['created']));
  323. }
  324. $aListsTmpl = array_merge($aListsTmpl,
  325. array(
  326. array('title' => _t('_sys_form_favorite_list_title_updated'), 'value' => bx_time_js($aListInfo['updated'])),
  327. array('title' => _t('_sys_form_favorite_list_title_count'), 'value' => $aListInfo['count']),
  328. array('title' => _t('_sys_form_favorite_list_title_visibility'), 'value' => $sTitle)
  329. )
  330. );
  331. return $this->parseHtmlByName('favorite-list-info.html', array('bx_repeat:items' => $aListsTmpl, 'author' => $oProfile->getUnit()));
  332. }
  333. public function getBrowsingFilters($aParams)
  334. {
  335. if(empty($aParams['mode']))
  336. return '';
  337. $mixedContent = $this->_getBrowsingFiltersContent($aParams);
  338. if($this->_bIsApi)
  339. return $mixedContent;
  340. if(empty($mixedContent))
  341. return '';
  342. $sPopupId = $this->_oConfig->getHtmlIds('popup_bfilters_' . $aParams['mode']);
  343. $sPopupContent = $this->parseHtmlByName('browsing_filters.html', [
  344. 'content' => $mixedContent
  345. ]);
  346. return BxTemplFunctions::getInstance()->transBox($sPopupId, $sPopupContent, true);
  347. }
  348. function entryAllActions($sActions)
  349. {
  350. if(empty($sActions))
  351. return '';
  352. return $this->parseHtmlByName('entry-all-actions.html', array (
  353. 'actions' => $sActions
  354. ));
  355. }
  356. function entryAttachments ($aData, $aParams = array())
  357. {
  358. return $this->entryAttachmentsByStorage($this->getModule()->_oConfig->CNF['OBJECT_STORAGE'], $aData, $aParams);
  359. }
  360. function entryAttachmentsByStorage ($mixedStorage, $aData, $aParams = array())
  361. {
  362. if(!is_array($mixedStorage))
  363. $mixedStorage = array($mixedStorage);
  364. $aResult = array();
  365. if (bx_is_api()){
  366. foreach($mixedStorage as $sStorage) {
  367. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  368. if ($aAttachments){
  369. foreach($aAttachments as $aAttachment) {
  370. $aItem = [
  371. 'file_name' => $aAttachment['file_name'],
  372. 'mime_type' => $aAttachment['mime_type'],
  373. 'ext' => $aAttachment['ext'],
  374. 'type' => 'other',
  375. 'size' => $aAttachment['size'],
  376. 'url' => $aAttachment['bx_if:not_image']['content']['url_original']
  377. ];
  378. if ($aAttachment['bx_if:image']['condition'] == 1){
  379. $aItem['type'] = 'image';
  380. $aItem['data'] = bx_api_get_image($sStorage, $aAttachment['id']);
  381. }
  382. if ($aAttachment['bx_if:video']['condition'] == 1){
  383. $aItem['type'] = 'video';
  384. $aItem['data'] = bx_api_get_image($sStorage, $aAttachment['id']);
  385. }
  386. if ($aAttachment['bx_if:sound']['condition'] == 1){
  387. $aItem['type'] = 'sound';
  388. $aItem['data'] = bx_api_get_image($sStorage, $aAttachment['id']);
  389. }
  390. $aResult[] = $aItem;
  391. }
  392. }
  393. }
  394. return [bx_api_get_block('entity_attachments', $aResult)];
  395. }
  396. foreach($mixedStorage as $sStorage) {
  397. $aAttachments = $this->getAttachments($sStorage, $aData, $aParams);
  398. if(empty($aAttachments) || !is_array($aAttachments))
  399. continue;
  400. $aResult = array_merge($aResult, $aAttachments);
  401. }
  402. if(empty($aResult) || !is_array($aResult))
  403. return '';
  404. $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
  405. $this->addJs(array(
  406. 'flickity/flickity.pkgd.min.js',
  407. ));
  408. $s = $this->addJs('pdfjs/pdf.min.mjs.js', true);
  409. return $s . $this->parseHtmlByName('attachments.html', array(
  410. 'bx_repeat:attachments' => $aResult,
  411. 'pdfjs_worker_url' => $this->getJsUrl('pdfjs/pdf.worker.min.mjs.js'),
  412. ));
  413. }
  414. function entryContext ($aData, $iProfileId = false, $sFuncContextDesc = 'getContextDesc', $sTemplateName = 'context.html', $sFuncContextAddon = 'getContextAddon')
  415. {
  416. $CNF = &$this->getModule()->_oConfig->CNF;
  417. $iContextId = $aData[$CNF['FIELD_ALLOW_VIEW_TO']];
  418. if ($iContextId >= 0)
  419. return '';
  420. $iProfileId = - $iContextId;
  421. $oProfile = BxDolProfile::getInstance($iProfileId);
  422. if (!$oProfile)
  423. $oProfile = BxDolProfileUndefined::getInstance();
  424. if (!$oProfile)
  425. return '';
  426. $sName = $oProfile->getDisplayName();
  427. $sAddon = $sFuncContextAddon ? $this->$sFuncContextAddon($aData, $oProfile) : '';
  428. $aVars = array (
  429. 'author_url' => $oProfile->getUrl(),
  430. 'author_thumb_url' => $oProfile->getThumb(),
  431. 'author_unit' => $oProfile->getUnit(0, array('template' => 'unit_wo_info')),
  432. 'author_title' => $sName,
  433. 'author_title_attr' => bx_html_attribute($sName),
  434. 'author_desc' => $sFuncContextDesc ? $this->$sFuncContextDesc($aData) : '',
  435. 'bx_if:addon' => array (
  436. 'condition' => (bool)$sAddon,
  437. 'content' => array (
  438. 'content' => $sAddon,
  439. ),
  440. ),
  441. );
  442. return $this->parseHtmlByName($sTemplateName, $aVars);
  443. }
  444. protected function getAttachmentsImagesTranscoders ($sStorage = '')
  445. {
  446. $CNF = &$this->getModule()->_oConfig->CNF;
  447. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW']);
  448. $oTranscoderView = !empty($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_PICTURE']) : null;
  449. return array($oTranscoder, $oTranscoderView);
  450. }
  451. protected function getAttachmentsVideoTranscoders ($sStorage = '')
  452. {
  453. $CNF = &$this->getModule()->_oConfig->CNF;
  454. if (isset($CNF['OBJECT_VIDEOS_TRANSCODERS']) && $CNF['OBJECT_VIDEOS_TRANSCODERS'])
  455. return array (
  456. 'poster' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']),
  457. 'mp4' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']),
  458. 'mp4_hd' => BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']),
  459. );
  460. return false;
  461. }
  462. public function getAttachmentsData ($sStorage, $aData, $aParams = array())
  463. {
  464. return $this->getAttachments($sStorage, $aData, $aParams );
  465. }
  466. protected function getAttachments ($sStorage, $aData, $aParams = array())
  467. {
  468. $CNF = &$this->getModule()->_oConfig->CNF;
  469. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  470. list($oTranscoder, $oTranscoderPreview) = $this->getAttachmentsImagesTranscoders($sStorage);
  471. $oTranscoderSound = isset($CNF['OBJECT_SOUNDS_TRANSCODER']) && $CNF['OBJECT_SOUNDS_TRANSCODER'] ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_SOUNDS_TRANSCODER']) : null;
  472. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders($sStorage);
  473. $aGhostFiles = $oStorage->getGhosts ($this->getModule()->serviceGetContentOwnerProfileId($aData[$CNF['FIELD_ID']]), $aData[$CNF['FIELD_ID']]);
  474. if (!$aGhostFiles)
  475. return false;
  476. $sFilterField = isset($aParams['filter_field']) ? $aParams['filter_field'] : $CNF['FIELD_THUMB'];
  477. if(!empty($sFilterField) && isset($aData[$sFilterField]))
  478. foreach ($aGhostFiles as $k => $a) {
  479. // don't show thumbnail in attachments
  480. if ($a['id'] == $aData[$sFilterField])
  481. unset($aGhostFiles[$k]);
  482. }
  483. if(!$aGhostFiles)
  484. return false;
  485. $aAttachmnts = array();
  486. foreach ($aGhostFiles as $k => $a) {
  487. $isImage = !empty($CNF['OBJECT_STORAGE_PHOTOS']) && $sStorage == $CNF['OBJECT_STORAGE_PHOTOS'] && $oTranscoder && (0 === strncmp('image/', $a['mime_type'], 6)) && $oTranscoder->isMimeTypeSupported($a['mime_type']); // preview for images, transcoder object for preview must be defined
  488. $isVideo = !empty($CNF['OBJECT_STORAGE_VIDEOS']) && $sStorage == $CNF['OBJECT_STORAGE_VIDEOS'] && $aTranscodersVideo && (0 === strncmp('video/', $a['mime_type'], 6)) && $aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type']); // preview for videos, transcoder object for video must be defined
  489. $isSound = !empty($CNF['OBJECT_STORAGE_SOUNDS']) && $sStorage == $CNF['OBJECT_STORAGE_SOUNDS'] && $oTranscoderSound && (0 === strncmp('audio/', $a['mime_type'], 6)) && $oTranscoderSound->isMimeTypeSupported($a['mime_type']); // preview for sounds, transcoder object for sounds must be defined
  490. $sUrlOriginal = $oStorage->getFileUrlById($a['id']);
  491. $sImgPopupId = 'bx-messages-atachment-popup-' . $a['id'];
  492. // images are displayed with preview and popup upon clicking
  493. $a['bx_if:image'] = array (
  494. 'condition' => $isImage,
  495. 'content' => array (
  496. 'url_original' => $isImage && $oTranscoderPreview ? $oTranscoderPreview->getFileUrl($a['id']) : $sUrlOriginal,
  497. 'attr_file_name' => bx_html_attribute($a['file_name']),
  498. 'popup_id' => $sImgPopupId,
  499. 'url_preview' => $isImage ? $oTranscoder->getFileUrl($a['id']) : '',
  500. 'popup' => BxTemplFunctions::getInstance()->transBox($sImgPopupId, '<img src="' . $sUrlOriginal . '" />', true, true),
  501. ),
  502. );
  503. $sVideoUrlHd = '';
  504. if($isVideo) {
  505. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  506. $aVideoFile = $oStorage->getFile($a['id']);
  507. if (!empty($aVideoFile['dimensions']) && $aTranscodersVideo['mp4_hd']->isProcessHD($aVideoFile['dimensions']))
  508. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  509. }
  510. // videos are displayed inline
  511. $a['bx_if:video'] = array (
  512. 'condition' => $isVideo,
  513. 'content' => array (
  514. 'video' => $isVideo && $aTranscodersVideo ? BxTemplFunctions::getInstance()->videoPlayer(
  515. $aTranscodersVideo['poster']->getFileUrl($a['id']),
  516. $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  517. $sVideoUrlHd,
  518. false, ''
  519. ) : '',
  520. ),
  521. );
  522. // sounds are displayed inline
  523. $a['bx_if:sound'] = array (
  524. 'condition' => $isSound,
  525. 'content' => array (
  526. 'sound' => $isSound && $oTranscoderSound && ($oPlayer = BxDolPlayer::getObjectInstance()) ? $this->parseHtmlByName('attachment_sound.html', array(
  527. 'file_name' => $a['file_name'],
  528. 'file_url' => $oTranscoderSound ? $oTranscoderSound->getFileUrl($a['id']) : '',
  529. 'player' => $oTranscoderSound && $oTranscoderSound->isFileReady($a['id']) ?
  530. $oPlayer->getCodeAudio (BX_PLAYER_STANDARD, array(
  531. 'mp3' => $oTranscoderSound->getFileUrl($a['id']),
  532. )) : _t('_sys_txt_err_sound_not_transcoded_yet'),
  533. )) : '',
  534. ),
  535. );
  536. // non-images are displayed as text links to original file
  537. $a['bx_if:not_image'] = array (
  538. 'condition' => !$isImage && !$isVideo && !$isSound,
  539. 'content' => array (
  540. 'url_original' => $sUrlOriginal,
  541. 'attr_file_name' => bx_html_attribute($a['file_name']),
  542. 'file_name' => bx_process_output($a['file_name']),
  543. ),
  544. );
  545. $aAttachmnts[] = $a;
  546. }
  547. return $aAttachmnts;
  548. }
  549. public function getAttachLinkItem($iUserId, $mixedLink, $aParams = [])
  550. {
  551. $aLink = is_array($mixedLink) ? $mixedLink : $this->_oDb->getLinksBy(['type' => 'id', 'id' => (int)$mixedLink, 'profile_id' => $iUserId]);
  552. if(empty($aLink) || !is_array($aLink))
  553. return '';
  554. $sStylePrefix = $aParams['style_prefix'] ?? '';
  555. $sClass = $sStylePrefix . '-al-item';
  556. $oEmbed = BxDolEmbed::getObjectInstance();
  557. $bEmbed = $oEmbed !== false;
  558. $sThumbnail = '';
  559. $aLinkAttrs = [];
  560. if(!$bEmbed) {
  561. $aLinkAttrs = [
  562. 'title' => bx_html_attribute($aLink['title'])
  563. ];
  564. if(!$this->_oConfig->isEqualUrls(BX_DOL_URL_ROOT, $aLink['url'])) {
  565. $aLinkAttrs['target'] = '_blank';
  566. if($this->_oDb->getParam('sys_add_nofollow') == 'on')
  567. $aLinkAttrs['rel'] = 'nofollow';
  568. }
  569. if((int)$aLink['media_id'] != 0 && ($aParams['transcoder'] ??= ''))
  570. $sThumbnail = BxDolTranscoderImage::getObjectInstance($aParams['transcoder'])->getFileUrl($aLink['media_id']);
  571. }
  572. else
  573. $sClass .= ' embed';
  574. return $this->parseHtmlByName('attach_link_item.html', [
  575. 'html_id' => ($aParams['html_id_link_item'] ?? '') . $aLink['id'],
  576. 'style_prefix' => $sStylePrefix,
  577. 'class' => $sClass,
  578. 'js_object' => $aParams['js_object'] ?? '',
  579. 'id' => $aLink['id'],
  580. 'bx_if:show_embed_outer' => [
  581. 'condition' => $bEmbed,
  582. 'content' => [
  583. 'style_prefix' => $sStylePrefix,
  584. 'embed' => $bEmbed ? $oEmbed->getLinkHTML($aLink['url'], $aLink['title'], 300) : '',
  585. ]
  586. ],
  587. 'bx_if:show_embed_inner' => [
  588. 'condition' => !$bEmbed,
  589. 'content' => [
  590. 'style_prefix' => $sStylePrefix,
  591. 'bx_if:show_thumbnail' => [
  592. 'condition' => !empty($sThumbnail),
  593. 'content' => [
  594. 'style_prefix' => $sStylePrefix,
  595. 'thumbnail' => $sThumbnail
  596. ]
  597. ],
  598. 'url' => $aLink['url'],
  599. 'link' => $this->parseLink($aLink['url'], $aLink['title'], $aLinkAttrs)
  600. ]
  601. ],
  602. ]);
  603. }
  604. public function embedVideo($iFileId)
  605. {
  606. $CNF = $this->getModule()->_oConfig->CNF;
  607. list($oPlayer, $oStorage, $aContentInfo, $a) = $this->_embedChecks('OBJECT_STORAGE_VIDEOS', $iFileId);
  608. // check if file is really video
  609. $aTranscodersVideo = $this->getAttachmentsVideoTranscoders();
  610. if (!$aTranscodersVideo || (0 !== strncmp('video/', $a['mime_type'], 6)) || !$aTranscodersVideo['poster']->isMimeTypeSupported($a['mime_type'])) {
  611. $this->displayErrorOccured('', BX_PAGE_EMBED);
  612. exit;
  613. }
  614. // check HD video version
  615. $sVideoUrlHd = '';
  616. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  617. $aVideoFile = $oStorage->getFile($a['id']);
  618. if (!empty($aVideoFile['dimensions']) && $aTranscodersVideo['mp4_hd']->isProcessHD($aVideoFile['dimensions']))
  619. $sVideoUrlHd = $aTranscodersVideo['mp4_hd']->getFileUrl($a['id']);
  620. // generate player code
  621. $sCode = $oPlayer->getCodeVideo (BX_PLAYER_EMBED, array(
  622. 'poster' => $aTranscodersVideo['poster']->getFileUrl($a['id']),
  623. 'mp4' => array(
  624. 'sd' => $aTranscodersVideo['mp4']->getFileUrl($a['id']),
  625. 'hd' => $sVideoUrlHd
  626. ),
  627. ));
  628. // display page
  629. $oTemplate = BxDolTemplate::getInstance();
  630. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  631. $oTemplate->setPageHeader ($a['file_name']);
  632. $oTemplate->setPageContent ('page_main_code', $sCode);
  633. $oTemplate->getPageCode();
  634. exit;
  635. }
  636. public function embedSound($iFileId)
  637. {
  638. $CNF = $this->getModule()->_oConfig->CNF;
  639. list($oPlayer, $oStorage, $aContentInfo, $a) = $this->_embedChecks('OBJECT_STORAGE_SOUNDS', $iFileId);
  640. // check if file is really audio
  641. $oTranscoderSound = isset($CNF['OBJECT_SOUNDS_TRANSCODER']) ? BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_SOUNDS_TRANSCODER']) : null;
  642. if (!$oTranscoderSound || (0 !== strncmp('audio/', $a['mime_type'], 6)) || !$oTranscoderSound->isMimeTypeSupported($a['mime_type'])) {
  643. $this->displayErrorOccured('', BX_PAGE_EMBED);
  644. exit;
  645. }
  646. if (!$oTranscoderSound->isFileReady($a['id'])) {
  647. $oTranscoderSound->getFileUrl($a['id']); // queue for encoding
  648. $this->displayMsg('_sys_txt_err_sound_not_transcoded_yet', true, BX_PAGE_EMBED);
  649. exit;
  650. }
  651. // generate player code
  652. $sCode = $oPlayer->getCodeAudio (BX_PLAYER_EMBED, array(
  653. 'mp3' => $oTranscoderSound->getFileUrl($a['id']),
  654. ));
  655. // display page
  656. $oTemplate = BxDolTemplate::getInstance();
  657. $oTemplate->setPageNameIndex (BX_PAGE_EMBED);
  658. $oTemplate->setPageHeader ($a['file_name']);
  659. $oTemplate->setPageContent ('page_main_code', $sCode);
  660. $oTemplate->getPageCode();
  661. exit;
  662. }
  663. /**
  664. * Polls
  665. */
  666. public function entryPolls($aData)
  667. {
  668. $CNF = &$this->getModule()->_oConfig->CNF;
  669. $aPolls = $this->_oDb->getPolls(array(
  670. 'type' => 'content_id',
  671. 'content_id' => $aData[$CNF['FIELD_ID']],
  672. ));
  673. if(empty($aPolls) || !is_array($aPolls))
  674. return;
  675. $iProfileId = bx_get_logged_profile_id();
  676. $sPolls = '';
  677. foreach($aPolls as $aPoll)
  678. $sPolls .= $this->getPollItem($aPoll, $iProfileId);
  679. if(empty($sPolls))
  680. return '';
  681. $this->_addCssJsPolls();
  682. return $this->getJsCode('poll') . $this->parseHtmlByName('poll_items.html', array(
  683. 'polls' => $sPolls
  684. ));
  685. }
  686. public function entryPollAnswers($aPoll, $bDynamic = false)
  687. {
  688. $sContent = $this->_getPollAnswers($aPoll, ['dynamic' => $bDynamic]);
  689. if(empty($sContent))
  690. return '';
  691. return [
  692. 'content' => $sContent,
  693. 'menu' => $this->_getPollBlockMenu($aPoll, 'answers')
  694. ];
  695. }
  696. public function entryPollResults($aPoll, $bDynamic = false)
  697. {
  698. $mixedContent = $this->_getPollResults($aPoll, ['dynamic' => $bDynamic]);
  699. if($this->_bIsApi)
  700. return $mixedContent;
  701. if(empty($mixedContent))
  702. return '';
  703. return [
  704. 'content' => $mixedContent,
  705. 'menu' => $this->_getPollBlockMenu($aPoll, 'results')
  706. ];
  707. }
  708. public function getPollForm($iParentCid = 0)
  709. {
  710. $aForm = $this->getModule()->getPollForm($iParentCid);
  711. return $this->parseHtmlByName('poll_form.html', [
  712. 'js_object' => $this->_oConfig->getJsObjectPoll($iParentCid),
  713. 'form_id' => $aForm['form_id'],
  714. 'form' => $aForm['form'],
  715. ]);
  716. }
  717. public function getPollField($iContentId = 0, $iProfileId = 0)
  718. {
  719. if(empty($iProfileId))
  720. $iProfileId = bx_get_logged_profile_id();
  721. $aPolls = [];
  722. if(!empty($iContentId))
  723. $aPolls = array_merge($aPolls, $this->_oDb->getPolls([
  724. 'type' => 'content_id',
  725. 'content_id' => $iContentId,
  726. ]));
  727. $aPolls = array_merge($aPolls, $this->_oDb->getPolls([
  728. 'type' => 'author_id',
  729. 'author_id' => $iProfileId,
  730. 'unused' => true
  731. ]));
  732. $mixedPolls = $this->_bIsApi ? [] : '';
  733. foreach($aPolls as $aPoll) {
  734. $mixedPoll = $this->getPollItem($aPoll, $iProfileId, [
  735. 'parent_cid' => $iContentId,
  736. 'manage' => true
  737. ]);
  738. if($this->_bIsApi)
  739. $mixedPolls[] = $mixedPoll;
  740. else
  741. $mixedPolls .= $mixedPoll;
  742. }
  743. return $this->_bIsApi ? $mixedPolls : $this->parseHtmlByName('poll_form_field.html', [
  744. 'html_id' => $this->_oConfig->getHtmlIds('add_poll_form_field'),
  745. 'polls' => $mixedPolls
  746. ]);
  747. }
  748. public function getPollItem($mixedPoll, $iProfileId = 0, $aParams = [])
  749. {
  750. $oModule = $this->getModule();
  751. $CNF = &$oModule->_oConfig->CNF;
  752. $aPoll = is_array($mixedPoll) ? $mixedPoll : $this->_oDb->getPolls(array('type' => 'id', 'id' => (int)$mixedPoll));
  753. if(empty($aPoll) || !is_array($aPoll))
  754. return $this->_bIsApi ? [] : '';
  755. $iPollId = (int)$aPoll[$CNF['FIELD_POLL_ID']];
  756. $sPollText = bx_process_output($aPoll[$CNF['FIELD_POLL_TEXT']], BX_DATA_TEXT);
  757. if($this->_bIsApi) {
  758. $bPerformed = $oModule->isPollPerformed($iPollId, $iProfileId);
  759. return [
  760. 'id' => $iPollId,
  761. 'title' => $sPollText,
  762. 'object' => $CNF['OBJECT_VOTES_POLL_ANSWERS'],
  763. 'subentries' => $this->_getPollAnswers($aPoll),
  764. 'results' => $this->_getPollResults($aPoll),
  765. 'is_performed' => $bPerformed,
  766. 'value' => $bPerformed ? $oModule->getPollPerformedValue($iPollId) : 0
  767. ];
  768. }
  769. $bDynamic = isset($aParams['dynamic']) && $aParams['dynamic'] === true;
  770. $bManage = isset($aParams['manage']) && $aParams['manage'] === true;
  771. $bSwitchMenu = isset($aParams['switch_menu']) ? (bool)$aParams['switch_menu'] : true;
  772. $bForceDisplayAnswers = isset($aParams['force_display_answers']) && (bool)$aParams['force_display_answers'] === true;
  773. $iParentСid = isset($aParams['parent_cid']) ? (int)$aParams['parent_cid'] : false;
  774. $sPollView = !$bForceDisplayAnswers && $oModule->isPollPerformed($iPollId, $iProfileId) ? 'results' : 'answers';
  775. $sMethod = '_getPoll' . ucfirst($sPollView);
  776. if(!method_exists($this, $sMethod))
  777. return '';
  778. $mixedMenu = '';
  779. if($bSwitchMenu)
  780. $mixedMenu = $this->_getPollBlockMenu($aPoll, $sPollView, [
  781. 'parent_cid' => $iParentСid,
  782. 'template' => 'menu_interactive.html'
  783. ]);
  784. $sJsObject = $iParentСid !== false ? $this->_oConfig->getJsObjectPoll($iParentСid) : $this->_oConfig->getJsObject('poll');
  785. return $this->parseHtmlByName('poll_item.html', array(
  786. 'html_id' => $this->_oConfig->getHtmlIds('poll') . $iPollId,
  787. 'bx_if:show_input_hidden' => array(
  788. 'condition' => $bManage,
  789. 'content' => array(
  790. 'name' => $CNF['FIELD_POLL'],
  791. 'id' => $iPollId
  792. )
  793. ),
  794. 'action_menu' => !empty($mixedMenu) ? $mixedMenu->getCode() : '',
  795. 'bx_if:show_action_embed' => array(
  796. 'condition' => $bManage,
  797. 'content' => array(
  798. 'js_object' => $sJsObject,
  799. 'id' => $iPollId
  800. )
  801. ),
  802. 'bx_if:show_action_delete' => array(
  803. 'condition' => $bManage,
  804. 'content' => array(
  805. 'js_object' => $sJsObject,
  806. 'id' => $iPollId,
  807. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : ''
  808. )
  809. ),
  810. 'text' => $sPollText,
  811. 'content' => $this->$sMethod($aPoll, [
  812. 'dynamic' => $bDynamic,
  813. 'parent_cid' => $iParentСid
  814. ])
  815. ));
  816. }
  817. public function embedPollItem($mixedPoll, $aParams = array())
  818. {
  819. $CNF = &$this->getModule()->_oConfig->CNF;
  820. $sHeader = '';
  821. $sContent = $this->getPollItem($mixedPoll, 0, $aParams);
  822. if(!empty($sContent)) {
  823. $aPoll = is_array($mixedPoll) ? $mixedPoll : $this->_oDb->getPolls(array('type' => 'id', 'id' => (int)$mixedPoll));
  824. $sHeader = strmaxtextlen($aPoll[$CNF['FIELD_POLL_TEXT']], 32, '...');
  825. $sContent = $this->getJsCode('poll') . $sContent;
  826. }
  827. $this->_addCssJsPolls();
  828. $oTemplate = BxDolTemplate::getInstance();
  829. $oTemplate->addCssStyle($CNF['STYLES_POLLS_EMBED_CLASS'], $CNF['STYLES_POLLS_EMBED_CONTENT']);
  830. $oTemplate->setPageNameIndex(BX_PAGE_EMBED);
  831. $oTemplate->setPageHeader($sHeader);
  832. $oTemplate->setPageContent('page_main_code', $sContent);
  833. $oTemplate->getPageCode();
  834. exit;
  835. }
  836. public function embedPollItems($mixedContentInfo, $aParams = array())
  837. {
  838. $CNF = &$this->getModule()->_oConfig->CNF;
  839. $aContentInfo = is_array($mixedContentInfo) ? $mixedContentInfo : $this->_oDb->getContentInfoById((int)$mixedContentInfo);
  840. if(empty($aContentInfo) || !is_array($aContentInfo))
  841. return;
  842. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  843. $aPolls = $this->_oDb->getPolls(array('type' => 'content_id', 'content_id' => $iContentId));
  844. if(empty($aPolls) || !is_array($aPolls))
  845. return;
  846. $iPolls = 0;
  847. $sContent = '';
  848. foreach($aPolls as $aPoll) {
  849. $sPoll = $this->getPollItem($aPoll, 0, $aParams);
  850. if(empty($sPoll))
  851. continue;
  852. $sContent .= $sPoll;
  853. $iPolls += 1;
  854. }
  855. $sJsObjectType = 'poll';
  856. if(!empty($sContent) && isset($aParams['showcase']) && (bool)$aParams['showcase'] === true) {
  857. $this->addJs(array('flickity/flickity.pkgd.min.js'));
  858. $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
  859. $sContent = $this->parseHtmlByName('poll_items_showcase.html', [
  860. 'js_object' => $this->_oConfig->getJsObject($sJsObjectType),
  861. 'html_id' => $this->_oConfig->getHtmlIds('polls_showcase') . $iContentId,
  862. 'type' => $iPolls == 1 ? 'single' : 'multiple',
  863. 'polls' => $sContent
  864. ]);
  865. }
  866. $sHeader = '';
  867. if(!empty($sContent)) {
  868. $sHeader = $iPolls == 1 ? array_shift($aPolls)[$CNF['FIELD_POLL_TEXT']] : _t('_polls_from', $aContentInfo[$CNF['FIELD_TITLE']]);
  869. $sHeader = strmaxtextlen($sHeader, 32, '...');
  870. $sContent = $this->getJsCode($sJsObjectType) . $sContent;
  871. }
  872. $this->_addCssJsPolls();
  873. $oTemplate = BxDolTemplate::getInstance();
  874. $oTemplate->addCssStyle($CNF['STYLES_POLLS_EMBED_CLASS'], $CNF['STYLES_POLLS_EMBED_CONTENT']);
  875. $oTemplate->setPageNameIndex(BX_PAGE_EMBED);
  876. $oTemplate->setPageHeader($sHeader);
  877. $oTemplate->setPageContent('page_main_code', $sContent);
  878. $oTemplate->getPageCode();
  879. exit;
  880. }
  881. protected function _addCssJsPolls($bDynamic = false)
  882. {
  883. $sInclude = '';
  884. $sInclude .= $this->addJs(['modules/base/general/js/|polls.js'], $bDynamic);
  885. $sInclude .= $this->addCss(['polls.css'], $bDynamic);
  886. return $bDynamic ? $sInclude : '';
  887. }
  888. protected function _getPollAnswers($aPoll, $aParams = [])
  889. {
  890. $CNF = &$this->getModule()->_oConfig->CNF;
  891. $bDynamic = isset($aParams['dynamic']) && $aParams['dynamic'] === true;
  892. $iParentСid = isset($aParams['parent_cid']) && $aParams['parent_cid'] !== false ? (int)$aParams['parent_cid'] : false;
  893. $aAnswers = $this->_oDb->getPollAnswers(['type' => 'poll_id', 'poll_id' => $aPoll[$CNF['FIELD_POLL_ID']]]);
  894. if($this->_bIsApi)
  895. return $aAnswers;
  896. if(empty($aAnswers) || !is_array($aAnswers))
  897. return '';
  898. $aTmplVarsAnswers = [];
  899. foreach($aAnswers as $aAnswer) {
  900. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_POLL_ANSWERS'], $aAnswer['id']);
  901. if(!$oVotes)
  902. continue;
  903. $aTmplVarsAnswers[] = [
  904. 'answer' => $oVotes->getElementBlock([
  905. 'dynamic_mode' => $bDynamic,
  906. 'form_mode' => $iParentСid !== false
  907. ])
  908. ];
  909. }
  910. return $this->parseHtmlByName('poll_item_answers.html', [
  911. 'html_id' => $this->_oConfig->getHtmlIds('poll_content') . $aPoll[$CNF['FIELD_POLL_ID']],
  912. 'bx_repeat:answers' => $aTmplVarsAnswers
  913. ]);
  914. }
  915. protected function _getPollResults($aPoll, $aParams = [])
  916. {
  917. $CNF = &$this->getModule()->_oConfig->CNF;
  918. $bDynamic = isset($aParams['dynamic']) && $aParams['dynamic'] === true;
  919. $iParentСid = isset($aParams['parent_cid']) && $aParams['parent_cid'] !== false ? (int)$aParams['parent_cid'] : false;
  920. $aAnswers = $this->_oDb->getPollAnswers(['type' => 'poll_id', 'poll_id' => $aPoll[$CNF['FIELD_POLL_ID']]]);
  921. if(empty($aAnswers) || !is_array($aAnswers))
  922. return $this->_bIsApi ? [] : '';
  923. $iTotal = 0;
  924. foreach($aAnswers as $aAnswer)
  925. $iTotal += $aAnswer['votes'];
  926. $aTmplVarsAnswers = [];
  927. foreach($aAnswers as $aAnswer) {
  928. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES_POLL_ANSWERS'], $aAnswer['id']);
  929. if(!$oVotes)
  930. continue;
  931. $fPercent = $iTotal > 0 ? 100 * (float)$aAnswer['votes']/$iTotal : 0;
  932. $aTmplVarsAnswers[] = array_merge([
  933. 'title' => bx_process_output($aAnswer['title']),
  934. 'width' => (int)round($fPercent) . '%',
  935. 'percent' => _t($CNF['T']['txt_poll_answer_vote_percent'], $iTotal > 0 ? round($fPercent, 2) : 0),
  936. ], ($this->_bIsApi ? [
  937. 'votes' => $oVotes->getCounterAPI([
  938. 'show_counter_empty' => true,
  939. 'show_counter_in_brackets' => false
  940. ]),
  941. ] : [
  942. 'votes' => $oVotes->getCounter([
  943. 'show_counter_empty' => true,
  944. 'show_counter_in_brackets' => false,
  945. 'dynamic_mode' => $bDynamic,
  946. 'form_mode' => $iParentСid !== false
  947. ]),
  948. ]));
  949. }
  950. return $this->_bIsApi ? $aTmplVarsAnswers : $this->parseHtmlByName('poll_item_results.html', array(
  951. 'html_id' => $this->_oConfig->getHtmlIds('poll_content') . $aPoll[$CNF['FIELD_POLL_ID']],
  952. 'bx_repeat:answers' => $aTmplVarsAnswers,
  953. ));
  954. }
  955. protected function _getPollBlockMenu($aPoll, $sSelected = '', $aParams = [])
  956. {
  957. $CNF = &$this->_oConfig->CNF;
  958. $sJsObject = isset($aParams['parent_cid']) && $aParams['parent_cid'] !== false ? $this->_oConfig->getJsObjectPoll((int)$aParams['parent_cid']) : $this->_oConfig->getJsObject('poll');
  959. $iPollId = $aPoll[$CNF['FIELD_POLL_ID']];
  960. $aViews = [
  961. 'answers' => true,
  962. 'results' => $CNF['PARAM_POLL_HIDDEN_RESULTS'] === false || $this->getModule()->isPollPerformed($iPollId)
  963. ];
  964. $aMenu = array();
  965. foreach($aViews as $sView => $bActive) {
  966. if(!$bActive)
  967. continue;
  968. $sId = $this->_oConfig->getHtmlIds('poll_view_link_' . $sView) . $iPollId;
  969. if(!empty($sSelected) && $sSelected == $sView)
  970. $sSelected = $sId;
  971. $aMenu[] = array(
  972. 'id' => $sId,
  973. 'name' => $sId,
  974. 'class' => '',
  975. 'link' => 'javascript:void(0)',
  976. 'onclick' => 'javascript:' . $sJsObject . '.changePollView(this, \'' . $sView . '\', ' . $iPollId . ')',
  977. 'target' => '_self',
  978. 'title_attr' => _t($CNF['T']['txt_poll_menu_view_' . $sView]),
  979. 'title' => $this->parseIcon($CNF['ICON_POLLS_' . strtoupper($sView)])
  980. );
  981. }
  982. if(count($aMenu) <= 1)
  983. return '';
  984. $oMenu = new BxTemplMenuInteractive([
  985. 'template' => !empty($aParams['template']) ? $aParams['template'] : 'menu_interactive_vertical.html',
  986. 'menu_id' => $this->_oConfig->getHtmlIds('poll_view_menu') . '-' . time() . rand(0, PHP_INT_MAX),
  987. 'menu_items' => $aMenu
  988. ]);
  989. if(!empty($sSelected))
  990. $oMenu->setSelected('', $sSelected);
  991. return $oMenu;
  992. }
  993. protected function _embedChecks($sStorageKey, $iFileId)
  994. {
  995. // general checks
  996. $oPlayer = BxDolPlayer::getObjectInstance();
  997. $CNF = $this->getModule()->_oConfig->CNF;
  998. $sStorage = isset($CNF[$sStorageKey]) ? $CNF[$sStorageKey] : false;
  999. if (!$oPlayer || !$sStorage || !($oStorage = BxDolStorage::getObjectInstance($sStorage))) {
  1000. $this->displayErrorOccured('', BX_PAGE_EMBED);
  1001. exit;
  1002. }
  1003. // privacy check
  1004. $aGhost = $oStorage->getGhost($iFileId);
  1005. $aContentInfo = $aGhost && $aGhost['content_id'] ? $this->getModule()->_oDb->getContentInfoById($aGhost['content_id']) : false;
  1006. if (!$aGhost || ($aGhost['content_id'] && !$aContentInfo)) {
  1007. $this->displayPageNotFound('', BX_PAGE_EMBED);
  1008. exit;
  1009. }
  1010. if (!$aGhost['content_id']) {
  1011. // if file is not associated with content yet, then only admin and owner can view it
  1012. if (!isAdmin() && CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedEditAnyEntry() && $aGhost['profile_id'] != bx_get_logged_profile_id()) {
  1013. $this->displayAccessDenied('', BX_PAGE_EMBED);
  1014. exit;
  1015. }
  1016. }
  1017. else {
  1018. // if file is associated with content, then check entry privacy
  1019. if (CHECK_ACTION_RESULT_ALLOWED !== $this->getModule()->checkAllowedView($aContentInfo)) {
  1020. $this->displayAccessDenied('', BX_PAGE_EMBED);
  1021. exit;
  1022. }
  1023. }
  1024. // get file info
  1025. if (!($a = $oStorage->getFile($iFileId))) {
  1026. $this->displayPageNotFound('', BX_PAGE_EMBED);
  1027. exit;
  1028. }
  1029. return array($oPlayer, $oStorage, $aContentInfo, $a);
  1030. }
  1031. function _getImageSettings($sField, $sSettings, $sUsage = '')
  1032. {
  1033. $sCoverSettings = '';
  1034. if(($aCoverData = json_decode($sSettings, true)) && is_array($aCoverData))
  1035. $sCoverSettings = 'background-position: ' . $aCoverData['x'] . '% ' . $aCoverData['y'] . '%';
  1036. return $sCoverSettings;
  1037. }
  1038. function _prepareImage($aData, $sUniqId, $aUploaders, $sStorage, $sField, $bAllowTweak, $bAllowMultiple = false)
  1039. {
  1040. $oUploader = null;
  1041. $sJsName = $sJsCode = '';
  1042. foreach ($aUploaders as $sUploaderObject) {
  1043. $oUploader = BxDolUploader::getObjectInstance($sUploaderObject, $sStorage, $sUniqId, $this);
  1044. if (!$oUploader)
  1045. continue;
  1046. $sGhostTemplate = '{file_id}';
  1047. $sJsCode .= $oUploader->getUploaderJs($sGhostTemplate, $bAllowMultiple, array_merge($oUploader->getUploaderJsParams(), [
  1048. 'latest' => 1,
  1049. 'content_id' => $aData['id'],
  1050. 'storage_private' => '0',
  1051. 'is_init_ghosts' => 0,
  1052. 'is_init_reordering' => 0
  1053. ]));
  1054. $sJsName = $oUploader->getNameJsInstanceUploader();
  1055. }
  1056. $this->addJs(['BxDolUploader.js']);
  1057. $this->addJsTranslation(['_sys_uploader_image_reposition_info']);
  1058. return $this->parseHtmlByName('image_tweak.html', [
  1059. 'id' => $aData['id'],
  1060. 'js_object' => $sJsName,
  1061. 'unique_id' => $sUniqId,
  1062. 'id' => $aData['id'],
  1063. 'allow_tweak' => $bAllowTweak,
  1064. 'image_exists' => !$sField || $aData[$sField] == 0 ? 'bx-image-edit-buttons-no-image' : '',
  1065. 'field' => $sField,
  1066. 'action_url' => BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri(),
  1067. 'uploader' => !$oUploader ? '' : $oUploader->getUploaderButton([
  1068. 'content_id' => $aData['id'],
  1069. 'storage_private' => '0',
  1070. 'btn_class' => '',
  1071. 'button_title' => '',
  1072. 'attrs' => "class='hidden'"
  1073. ]),
  1074. 'uploader_js' => $sJsCode,
  1075. ]);
  1076. }
  1077. public function addCssJs()
  1078. {
  1079. $this->addCss('main.css');
  1080. }
  1081. }
  1082. /** @} */