BxBaseModGeneralTemplate.php 55 KB

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