BxBaseModGeneralTemplate.php 49 KB

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