BxBaseModGeneralTemplate.php 54 KB

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