1
0

BxBaseModProfileTemplate.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Profile based modules representation.
  13. */
  14. class BxBaseModProfileTemplate extends BxBaseModGeneralTemplate
  15. {
  16. protected $_sUnitDefault;
  17. protected $_sUnitSizeDefault;
  18. protected $_sUnitClass;
  19. protected $_sUnitClassWithCover;
  20. protected $_sUnitClassWoInfo;
  21. protected $_sUnitClassWoInfoShowCase;
  22. protected $_sUnitClassShowCase;
  23. /*
  24. * Enable/Disable Letter based thumbnail in "Unit with Cover" only.
  25. */
  26. protected $_bLetterAvatar;
  27. function __construct(&$oConfig, &$oDb)
  28. {
  29. parent::__construct($oConfig, $oDb);
  30. $this->_sUnitDefault = 'unit.html';
  31. $this->_sUnitSizeDefault = 'thumb';
  32. $this->_sUnitClass = 'bx-base-pofile-unit';
  33. $this->_sUnitClassWithCover = 'bx-base-pofile-unit-with-cover';
  34. $this->_sUnitClassWoInfo = 'bx-base-pofile-unit-wo-info';
  35. $this->_sUnitClassWoInfoShowCase = 'bx-base-pofile-unit-wo-info bx-base-unit-showcase bx-base-pofile-unit-wo-info-showcase';
  36. $this->_sUnitClassShowCase = 'bx-base-pofile-unit-with-cover bx-base-unit-showcase bx-base-pofile-unit-showcase';
  37. $this->_bLetterAvatar = true;
  38. }
  39. public function addLocationBase()
  40. {
  41. parent::addLocationBase();
  42. $this->addLocation('mod_profile', BX_DIRECTORY_PATH_MODULES . 'base' . DIRECTORY_SEPARATOR . 'profile' . DIRECTORY_SEPARATOR, BX_DOL_URL_MODULES . 'base/profile/');
  43. }
  44. /**
  45. * Get profile unit
  46. */
  47. function unit ($aData, $isCheckPrivateContent = true, $mixedTemplate = false, $aParams = [])
  48. {
  49. $CNF = &$this->_oConfig->CNF;
  50. list($sTemplate, $sTemplateSize) = is_array($mixedTemplate) ? $mixedTemplate : array($mixedTemplate, false);
  51. if(!empty($aParams['template_name']))
  52. $sTemplate = $aParams['template_name'];
  53. if(empty($sTemplate))
  54. $sTemplate = $this->_sUnitDefault;
  55. if(!empty($aParams['template_size']))
  56. $sTemplateSize = $aParams['template_size'];
  57. if(empty($sTemplateSize))
  58. $sTemplateSize = $this->_getUnitSize($aData, $sTemplate);
  59. /**
  60. * Allow use separate template for private profiles.
  61. * These templates will be used if privacy field "Visible to" don't allow to view content.
  62. * To use them you need to create a template with a postfix "_private" and put it in specified module
  63. * Example: "unit_private.html" for "unit.html" or "unit_wo_links_private.html" for "unit_wo_links.html"
  64. */
  65. $oModule = $this->getModule();
  66. if ($oModule->serviceCheckAllowedViewForProfile($aData) !== 0){
  67. $sTemplatePrivate = str_replace('.html', '_private.html' , $sTemplate);
  68. if($this->parseHtmlByName($sTemplatePrivate, array())){
  69. $sTemplate = $sTemplatePrivate;
  70. }
  71. }
  72. // check visibility
  73. $isAllowedViewCoverImage = ($oModule->checkAllowedViewCoverImage($aData) === CHECK_ACTION_RESULT_ALLOWED);
  74. $isAllowedViewProfileImage = ($oModule->checkAllowedViewProfileImage($aData) === CHECK_ACTION_RESULT_ALLOWED);
  75. $isProfilePublic = $this->isProfilePublic($aData);
  76. $bPublic = $isCheckPrivateContent && !empty($CNF['OBJECT_PRIVACY_VIEW']) ? $isProfilePublic : true;
  77. // prepare params
  78. $aParams = array_merge($aParams, [
  79. 'is_allowed_view_cover_img' => $isAllowedViewCoverImage,
  80. 'is_allowed_view_profile_img' => $isAllowedViewProfileImage,
  81. 'is_profile_public' => $isProfilePublic,
  82. ]);
  83. // profile
  84. if (isset($aData['profile_id']))
  85. $oProfile = BxDolProfile::getInstance($aData['profile_id']);
  86. else
  87. $oProfile = BxDolProfile::getInstanceByContentAndType((int)$aData[$CNF['FIELD_ID']], $this->MODULE);
  88. $iProfile = $oProfile->id();
  89. // try to get template variables from cache
  90. $sCacheKey = 'sprofile_unit_vars:' . $iProfile . ':' . $sTemplateSize . ':onl' . ($oProfile->isOnline() ? 1 : 0) . ':' . $sTemplate . ':avci' . ($isAllowedViewCoverImage ? '1' : '0') . ':avpi' . ($isAllowedViewProfileImage ? '1' : '0') . ':pp' . ($isProfilePublic ? '1' : '0');
  91. $aVars = bx_content_cache_get($sCacheKey);
  92. if (null === $aVars) {
  93. // get template variables if not found in cache
  94. $aVars = $this->unitVars($aData, $isCheckPrivateContent, $mixedTemplate, $aParams);
  95. bx_content_cache_set($sCacheKey, $aVars);
  96. }
  97. // get snippet menu
  98. $aTmplVarsMeta = array();
  99. if($this->_isTemplateWithMeta($sTemplate)) {
  100. $aTmplVarsMeta = $this->getSnippetMenuVars ($iProfile, $bPublic, $aParams);
  101. }
  102. // snippet meta menu is never cached
  103. $aVars['bx_if:meta']['condition'] = !empty($aTmplVarsMeta);
  104. $aVars['bx_if:meta']['content'] = $aTmplVarsMeta;
  105. $aExtras = [
  106. 'module' => $oModule->getName(),
  107. 'data' => $aData,
  108. 'check_private_content' => $isCheckPrivateContent,
  109. 'template' => $mixedTemplate,
  110. 'params' => $aParams,
  111. 'tmpl_name' => &$sTemplate,
  112. 'tmpl_vars' => &$aVars
  113. ];
  114. /**
  115. * @hooks
  116. * @hookdef hook-profile-unit 'profile', 'unit' - hook to override profile browsing unit
  117. * - $unit_name - equals `profile`
  118. * - $action - equals `unit`
  119. * - $object_id - not used
  120. * - $sender_id - not used
  121. * - $extra_params - array of additional params with the following array keys:
  122. * - `module` - [string] module name
  123. * - `data` - [array] profile info array as key&value pairs
  124. * - `check_private_content` - [boolean] check if it's a provate profile
  125. * - `template` - [string] or [array] with template for profile unit
  126. * - `params` - [array] additional params array as key&value pairs
  127. * - `tmpl_name` - [string] by ref, template name, can be overridden in hook processing
  128. * - `tmpl_vars` - [array] by ref, template parsable variables as key&value pairs, can be overridden in hook processing
  129. * @hook @ref hook-profile-unit
  130. */
  131. bx_alert('profile', 'unit', 0, 0, $aExtras);
  132. /**
  133. * @hooks
  134. * @hookdef hook-bx_base_profile-unit '{module_name}', 'unit' - hook to override profile browsing unit
  135. * It's equivalent to @ref hook-profile-unit
  136. * @hook @ref hook-bx_base_profile-unit
  137. */
  138. bx_alert($aExtras['module'], 'unit', 0, 0, $aExtras);
  139. return $this->parseHtmlByName($sTemplate, $aVars);
  140. }
  141. public function unitAPI($aData, $aParams = [])
  142. {
  143. return $this->getModule()->getDataAPI($aData, $aParams);
  144. }
  145. public function getBadgeImage($aData, $bSubstituteNoImage = false)
  146. {
  147. $CNF = &$this->_oConfig->CNF;
  148. return $this->_image($CNF['FIELD_BADGE'], $CNF['OBJECT_IMAGES_TRANSCODER_BADGE'], 'no-picture-thumb.png', $aData, $bSubstituteNoImage);
  149. }
  150. public function getBadgeLink($aData)
  151. {
  152. $CNF = &$this->_oConfig->CNF;
  153. $sLink = $aData[$CNF['FIELD_BADGE_LINK']];
  154. if(!$sLink && ($oProfile = BxDolProfile::getInstanceByContentAndType($aData[$CNF['FIELD_ID']], $this->_oConfig->getName())) !== false)
  155. $sLink = $oProfile->getUrl();
  156. return $sLink;
  157. }
  158. public function getBadge($mixedData, $sSize = 'icon')
  159. {
  160. $CNF = &$this->_oConfig->CNF;
  161. if(!is_array($mixedData))
  162. $mixedData = $this->_oDb->getContentInfoById((int)$mixedData);
  163. $oModule = $this->getModule();
  164. if(!$mixedData || !$oModule->isBadge($mixedData) || $oModule->checkAllowedViewBadgeImage($mixedData) !== CHECK_ACTION_RESULT_ALLOWED)
  165. return $this->_bIsApi ? [] : '';
  166. $sBadgeUrl = $this->getBadgeImage($mixedData);
  167. if(empty($sBadgeUrl))
  168. return $this->_bIsApi ? [] : '';
  169. $aBadge = [
  170. 'badge_url' => $sBadgeUrl,
  171. 'badge_link' => $this->getBadgeLink($mixedData),
  172. 'title_attr' => bx_html_attribute($mixedData[$CNF['FIELD_TITLE']])
  173. ];
  174. return $this->_bIsApi ? $aBadge : $this->parseHtmlByName('badge.html', array_merge([
  175. 'size' => $sSize
  176. ], $aBadge));
  177. }
  178. function unitVars ($aData, $isCheckPrivateContent = true, $mixedTemplate = false, $aParams = [])
  179. {
  180. $CNF = &$this->_oConfig->CNF;
  181. list($sTemplate, $sTemplateSize, $aTemplateVars) = is_array($mixedTemplate) ? $mixedTemplate : array($mixedTemplate, false, array());
  182. if(!empty($aParams['template_name']))
  183. $sTemplate = $aParams['template_name'];
  184. if(empty($sTemplate))
  185. $sTemplate = $this->_sUnitDefault;
  186. if(!empty($aParams['template_size']))
  187. $sTemplateSize = $aParams['template_size'];
  188. if(empty($sTemplateSize))
  189. $sTemplateSize = $this->_getUnitSize($aData, $sTemplate);
  190. if(!empty($aParams['template_vars']) && is_array($aParams['template_vars']))
  191. $aTemplateVars = $aParams['template_vars'];
  192. if(empty($aTemplateVars) || !is_array($aTemplateVars))
  193. $aTemplateVars = array();
  194. $oModule = $this->getModule();
  195. $iContentId = (int)$aData[$CNF['FIELD_ID']];
  196. $bPublic = $isCheckPrivateContent && !empty($CNF['OBJECT_PRIVACY_VIEW']) ? ($aParams['is_profile_public'] ?? false) : true;
  197. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->MODULE);
  198. $iProfile = $oProfile->id();
  199. // get profile's url
  200. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $iContentId));
  201. // get profile's title
  202. $sTitle = $oModule->serviceProfileName($iContentId);
  203. $sTitleAttr = bx_html_attribute($sTitle);
  204. $sText = $sSummary = '';
  205. if(!empty($CNF['FIELD_TEXT']) && !empty($aData[$CNF['FIELD_TEXT']])) {
  206. $sText = $this->getText($aData);
  207. $sSummary = $this->getSummary($aData, $sTitle, $sText, $sUrl);
  208. }
  209. $sCoverUrl = '';
  210. if(!$isCheckPrivateContent || ($aParams['is_allowed_view_cover_img'] ?? false))
  211. $sCoverUrl = $this->urlCoverUnit($aData, false);
  212. $bCoverUrl = !empty($sCoverUrl);
  213. if(empty($sCoverUrl) && ($iCoverId = (int)getParam('sys_unit_cover_profile')) != 0)
  214. $sCoverUrl = BxDolTranscoder::getObjectInstance(BX_DOL_TRANSCODER_OBJ_COVER_UNIT_PROFILE)->getFileUrlById($iCoverId);
  215. if(empty($sCoverUrl))
  216. $sCoverUrl = $this->getImageUrl('cover.svg');
  217. $sThumbUrl = '';
  218. if($this->_isUnitThumb($aData, $sTemplate) && (!$isCheckPrivateContent || ($aParams['is_allowed_view_profile_img'] ?? false)))
  219. $sThumbUrl = $this->_getUnitThumbUrl($sTemplateSize, $aData, false);
  220. $bThumbUrl = !empty($sThumbUrl);
  221. $aTmplVarsThumbnail = array(
  222. 'class_size' => '',
  223. 'bx_if:show_thumb_image' => array(
  224. 'condition' => $bThumbUrl,
  225. 'content' => array(
  226. 'class_size' => '',
  227. 'size' => $sTemplateSize,
  228. 'thumb_url' => $sThumbUrl
  229. )
  230. ),
  231. 'bx_if:show_thumb_letter' => array(
  232. 'condition' => !$bThumbUrl,
  233. 'content' => array(
  234. 'class_size' => '',
  235. 'size' => $sTemplateSize,
  236. 'color' => implode(', ', BxDolTemplate::getColorCode($iProfile, 1.0)),
  237. 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1))
  238. )
  239. ),
  240. 'bx_if:show_online' => array(
  241. 'condition' => $oProfile->isOnline(),
  242. 'content' => array()
  243. ),
  244. 'size' => $sTemplateSize,
  245. 'badges' => $oModule->serviceGetBadges($iContentId, false, true),
  246. 'thumb_url' => $bThumbUrl ? $sThumbUrl : $this->getImageUrl('no-picture-thumb.png'),
  247. );
  248. $sCoverData = $aData['cover_data'] ?? '';
  249. $sCoverSettings = $this->_getImageSettings($CNF['FIELD_COVER'], $sCoverData, 'unit_cover');
  250. return array_merge(array (
  251. 'class' => $this->_getUnitClass($aData, $sTemplate) . (!$bCoverUrl ? ' bx-cover-empty' : ''),
  252. 'id' => $iContentId,
  253. 'public' => $bPublic,
  254. 'bx_if:show_thumbnail' => array(
  255. 'condition' => $bThumbUrl || $this->_bLetterAvatar,
  256. 'content' => $aTmplVarsThumbnail
  257. ),
  258. 'cover_url' => $sCoverUrl,
  259. 'cover_settings' => $sCoverSettings,
  260. 'bx_if:show_cover' => array(
  261. 'condition' => $bCoverUrl,
  262. 'content' => array(
  263. 'cover_url' => $sCoverUrl,
  264. 'cover_settins' => $sCoverSettings,
  265. 'title' => $sTitle,
  266. )
  267. ),
  268. 'content_url' => $bPublic ? $sUrl : 'javascript:void(0);',
  269. 'content_click' => !$bPublic ? 'javascript:bx_alert(' . bx_js_string('"' . _t('_sys_access_denied_to_private_content') . '"') . ');' : '',
  270. 'content_target' => !empty($aParams['link_target']) ? $aParams['link_target'] : '_self',
  271. 'title' => $sTitle,
  272. 'title_attr' => $sTitleAttr,
  273. 'addon' => !empty($aData['addon']) ? $aData['addon'] : '',
  274. 'module_name' => _t($CNF['T']['txt_sample_single']),
  275. 'ts' => $aData[$CNF['FIELD_ADDED']],
  276. 'bx_if:meta' => array(
  277. 'condition' => !empty($aParams['snippet_menu_vars']),
  278. 'content' => $aParams['snippet_menu_vars'] ?? [],
  279. ),
  280. 'text' => $sText,
  281. 'summary' => $sSummary,
  282. ), $aTmplVarsThumbnail, $aTemplateVars);
  283. }
  284. function isProfilePublic($aData)
  285. {
  286. $CNF = &$this->_oConfig->CNF;
  287. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  288. if ($oPrivacy && !$oPrivacy->check($aData[$CNF['FIELD_ID']]) && !$oPrivacy->isPartiallyVisible($aData[$CNF['FIELD_ALLOW_VIEW_TO']]))
  289. return false;
  290. return true;
  291. }
  292. function getSnippetMenuVars ($iProfileId, $bPublic = null, $aParams = [])
  293. {
  294. $CNF = &$this->_oConfig->CNF;
  295. $oProfile = false;
  296. if(empty($CNF['OBJECT_MENU_SNIPPET_META']) || !($oProfile = BxDolProfile::getInstance($iProfileId)))
  297. return [];
  298. $oMetaMenu = BxDolMenu::getObjectInstance($CNF['OBJECT_MENU_SNIPPET_META'], $this);
  299. if(!$oMetaMenu)
  300. return [];
  301. if ($bPublic === null) {
  302. $aData = $this->getModule()->serviceGetContentInfoById($oProfile->getContentId());
  303. $bPublic = $this->isProfilePublic($aData);
  304. }
  305. $oMetaMenu->setContentId($oProfile->getContentId());
  306. $oMetaMenu->setContentPublic($bPublic);
  307. if(isset($aParams['context']))
  308. $oMetaMenu->setContext($aParams['context']);
  309. return [
  310. 'meta' => $oMetaMenu->getCode()
  311. ];
  312. }
  313. function getBlockCover($aData, $aParams = [])
  314. {
  315. $CNF = &$this->_oConfig->CNF;
  316. $oProfile = BxDolProfile::getInstanceByContentAndType($aData[$CNF['FIELD_ID']], $this->MODULE);
  317. if(!$oProfile)
  318. return '';
  319. $aTmplVars = $this->prepareCover($aData, array_merge($aParams, [
  320. 'use_as_block' => true
  321. ]));
  322. $this->addCss(array('cover.css'));
  323. return $this->parseHtmlByName('cover_block.html', $aTmplVars);
  324. }
  325. function prepareCover($aData, $aParams = [])
  326. {
  327. $CNF = &$this->_oConfig->CNF;
  328. $oModule = $this->getModule();
  329. $sClass = isset($aParams['class']) ? $aParams['class'] : '';
  330. $sShowData = isset($aParams['show_data']) ? $aParams['show_data'] : '';
  331. $bShowCover = !isset($aParams['show_cover']) || $aParams['show_cover'] === true;
  332. $bShowAvatar = !isset($aParams['show_avatar']) || $aParams['show_avatar'] === true;
  333. $bShowBadge = $oModule->isBadge($aData) && (!isset($aParams['show_badge']) || $aParams['show_badge'] === true);
  334. $sAddCode = "";
  335. $bUseAsAuthor = isset($aParams['use_as_author']) && $aParams['use_as_author'] === true;
  336. $bUseAsBlock = $bUseAsAuthor || (isset($aParams['use_as_block']) && $aParams['use_as_block'] === true);
  337. if($bUseAsAuthor)
  338. $sClass .= ' bx-base-author';
  339. else
  340. BxDolTemplate::getInstance()->addInjection('injection_main_class', 'text', 'bx-base-profile-view');
  341. $bProfileViewAllowed = $oModule->checkAllowedView($aData) === CHECK_ACTION_RESULT_ALLOWED;
  342. if($oModule->checkAllowedViewProfileImage($aData) !== CHECK_ACTION_RESULT_ALLOWED)
  343. $aData[$CNF['FIELD_PICTURE']] = 0;
  344. if($oModule->checkAllowedViewCoverImage($aData) !== CHECK_ACTION_RESULT_ALLOWED)
  345. $aData[$CNF['FIELD_COVER']] = 0;
  346. $oProfile = BxDolProfile::getInstanceByContentAndType($aData[$CNF['FIELD_ID']], $this->MODULE);
  347. $iProfile = $oProfile->id();
  348. $sUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]));
  349. $sTitle = bx_process_output($oProfile->getDisplayName());
  350. //--- Process Cover
  351. $bTmplVarsShowCover = $bShowCover;
  352. $aTmplVarsShowCover = [];
  353. if($bTmplVarsShowCover) {
  354. $sCoverPopupId = $this->MODULE . '-popup-cover';
  355. $bIsAllowEditCover = $oModule->checkAllowedChangeCover($aData) === CHECK_ACTION_RESULT_ALLOWED;
  356. $oPage = false;
  357. if(isset($aParams['page']) && is_a($aParams['page'], 'BxDolPage'))
  358. $oPage = $aParams['page'];
  359. $bUrlCover = false;
  360. $sUrlCover = $this->urlCover($aData, false);
  361. if(!$sUrlCover && $oPage !== false && $oPage->isPageCover()) {
  362. $aCover = $oPage->getPageCoverImage();
  363. if(!empty($aCover))
  364. $sUrlCover = BxDolCover::getCoverImageUrl($aCover);
  365. }
  366. if(!empty($sUrlCover)) {
  367. if(!$bUseAsAuthor)
  368. BxDolTemplate::getInstance()->addPageMetaImage($sUrlCover);
  369. $bUrlCover = true;
  370. }
  371. else
  372. $sUrlCover = $this->getImageUrl('cover.svg');
  373. $sAddClassCover = "";
  374. if(isset($CNF['FIELD_COVER']) && isset($CNF['OBJECT_UPLOADERS_COVER']) && isset($CNF['OBJECT_STORAGE_COVER']) && isset($CNF['OBJECT_IMAGES_TRANSCODER_COVER'])){
  375. /**
  376. * @hooks
  377. * @hookdef hook-system-image_editor 'system', 'image_editor' - hook to override profile cover editor
  378. * - $unit_name - equals `system`
  379. * - $action - equals `image_editor`
  380. * - $object_id - not used
  381. * - $sender_id - not used
  382. * - $extra_params - array of additional params with the following array keys:
  383. * - `module` - [string] module name
  384. * - `content_id` - [int] profile content id
  385. * - `is_allow_edit` - [boolean] if edit action is allowed to current user
  386. * - `image_type` - [string] image type equals to `cover`
  387. * - `image_url` - [string] image URL
  388. * - `uploader` - [string] uploader name
  389. * - `storage` - [string] storage name
  390. * - `transcoder` - [string] transcoder name
  391. * - `field` - [string] field name
  392. * - `is_background` - [boolean] if image is used as background, always `true`
  393. * - `add_class` - [string] by ref, class to add, can be overridden in hook processing
  394. * - `add_code` - [array] by ref, code to add, can be overridden in hook processing
  395. * @hook @ref hook-system-image_editor
  396. */
  397. bx_alert('system', 'image_editor', 0, 0, [
  398. 'module' => $oModule->getName(),
  399. 'content_id' => $aData[$CNF['FIELD_ID']],
  400. 'is_allow_edit' => $bIsAllowEditCover,
  401. 'image_type' => 'cover',
  402. 'image_url' => $CNF['FIELD_COVER'] && $aData[$CNF['FIELD_COVER']] ? $sUrlCover : '',
  403. 'uploader' => $CNF['OBJECT_UPLOADERS_COVER'][0],
  404. 'storage' => $CNF['OBJECT_STORAGE_COVER'],
  405. 'transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_COVER'],
  406. 'field' => $CNF['FIELD_COVER'],
  407. 'is_background' => true,
  408. 'add_class' => &$sAddClassCover,
  409. 'add_code' => &$sAddCode
  410. ]);
  411. }
  412. $sCoverTweak = '';
  413. $sUniqIdCover = genRndPwd (8, false);
  414. if ($bIsAllowEditCover && empty($sAddCode)){
  415. $sCoverTweak = $this->_prepareImage($aData, $sUniqIdCover, $CNF['OBJECT_UPLOADERS_COVER'], $CNF['OBJECT_STORAGE_COVER'], $CNF['FIELD_COVER'], true);
  416. }
  417. $aTmplVarsShowCover = [
  418. 'cover_popup_id' => $sCoverPopupId,
  419. 'cover_url' => $sUrlCover,
  420. 'unique_id' => $sUniqIdCover,
  421. 'cover_settins' => isset($CNF['FIELD_COVER_POSITION']) ? $this->_getImageSettings($CNF['FIELD_COVER'], $aData[$CNF['FIELD_COVER_POSITION']], 'page_cover') : '',
  422. 'add_class' => $sAddClassCover,
  423. 'img_class' => $sAddClassCover != '' ? 'bx-media-editable-src' : '',
  424. ];
  425. }
  426. else
  427. $sClass .= ' bx-no-cover';
  428. //--- Process Avatar
  429. $sUrlAvatar = $this->urlAvatarBig($aData, false);
  430. $bUrlAvatar = !empty($sUrlAvatar);
  431. $bTmplVarsShowAvatar = $bShowAvatar && ($bUrlAvatar || $this->_bLetterAvatar);
  432. $aTmplVarsShowAvatar = [];
  433. $sPicturePopup = '';
  434. if($bTmplVarsShowAvatar) {
  435. $sPicturePopupId = $this->MODULE . '-popup-picture';
  436. $bIsAllowEditPicture = $oModule->checkAllowedEdit($aData) === CHECK_ACTION_RESULT_ALLOWED;
  437. $sUrlPicture = $this->urlPicture ($aData);
  438. $sUrlPictureChange = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_EDIT_ENTRY'] . '&id=' . $aData[$CNF['FIELD_ID']]));
  439. $sAddClassPicture = "";
  440. if(isset($CNF['FIELD_PICTURE']) && isset($CNF['OBJECT_UPLOADERS_PICTURE']) && isset($CNF['OBJECT_STORAGE']) && isset($CNF['OBJECT_IMAGES_TRANSCODER_THUMB'])){
  441. /**
  442. * @hooks
  443. * @hookdef hook-system-image_editor 'system', 'image_editor' - hook to override profile avatar editor
  444. * - $unit_name - equals `system`
  445. * - $action - equals `image_editor`
  446. * - $object_id - not used
  447. * - $sender_id - not used
  448. * - $extra_params - array of additional params with the following array keys:
  449. * - `module` - [string] module name
  450. * - `content_id` - [int] profile content id
  451. * - `is_allow_edit` - [boolean] if edit action is allowed to current user
  452. * - `image_type` - [string] image type equals to `avatar`
  453. * - `image_url` - [string] image URL
  454. * - `uploader` - [string] uploader name
  455. * - `storage` - [string] storage name
  456. * - `transcoder` - [string] transcoder name
  457. * - `field` - [string] field name
  458. * - `is_background` - [boolean] if image used is as background, always `false`
  459. * - `add_class` - [string] by ref, class to add, can be overridden in hook processing
  460. * - `add_code` - [array] by ref, code to add, can be overridden in hook processing
  461. * @hook @ref hook-system-image_editor
  462. */
  463. bx_alert('system', 'image_editor', 0, 0, [
  464. 'module' => $oModule->getName(),
  465. 'content_id' => $aData[$CNF['FIELD_ID']],
  466. 'is_allow_edit' => $bIsAllowEditPicture,
  467. 'image_type' => 'avatar',
  468. 'image_url' => $CNF['FIELD_PICTURE'] && $aData[$CNF['FIELD_PICTURE']] ? $sUrlPicture : '',
  469. 'uploader' => $CNF['OBJECT_UPLOADERS_PICTURE'][0],
  470. 'storage' => $CNF['OBJECT_STORAGE'],
  471. 'transcoder' => $CNF['OBJECT_IMAGES_TRANSCODER_AVATAR_BIG'],
  472. 'field' => $CNF['FIELD_PICTURE'],
  473. 'is_background' => false,
  474. 'add_class' => &$sAddClassPicture,
  475. 'add_code' => &$sAddCode
  476. ]);
  477. }
  478. $sPictureTweak = '';
  479. $sUniqIdPicture = genRndPwd (8, false);
  480. if ($bIsAllowEditPicture && empty($sAddCode)){
  481. $sPictureTweak = $this->_prepareImage($aData, $sUniqIdPicture, $CNF['OBJECT_UPLOADERS_PICTURE'], $CNF['OBJECT_STORAGE'], $CNF['FIELD_PICTURE'], false);
  482. }
  483. $aTmplVarsShowAvatar = array(
  484. 'add_class' => $sAddClassPicture,
  485. 'letter' => mb_strtoupper(mb_substr($sTitle, 0, 1)),
  486. 'img_class' => $sAddClassPicture != '' ? 'bx-media-editable-src' : '',
  487. 'ava_url' => $sUrlAvatar,
  488. 'color' => implode(', ', BxDolTemplate::getColorCode($iProfile, 1.0)),
  489. 'bx_if:show_ava_image' => array(
  490. 'condition' => $bUrlAvatar,
  491. 'content' => [true]
  492. ),
  493. 'bx_if:show_ava_letter' => array(
  494. 'condition' => !$bUrlAvatar,
  495. 'content' => [true]
  496. ),
  497. 'bx_if:show_online' => array(
  498. 'condition' => $oProfile->isOnline(),
  499. 'content' => [true]
  500. ),
  501. 'bx_if:is_avatar' => array(
  502. 'condition' => $bUrlAvatar,
  503. 'content' => [
  504. 'picture_popup_id' => $sPicturePopupId,
  505. 'picture_url' => $sUrlPicture,
  506. ]
  507. ),
  508. 'picture_avatar_url' => $bUrlAvatar ? $sUrlAvatar : $this->getImageUrl('no-picture-preview.png'),
  509. 'unique_id' => $sUniqIdPicture,
  510. 'picture_tweak' => $sPictureTweak,
  511. 'cover_settins' => isset($CNF['FIELD_PICTURE_POSITION']) ? $this->_getImageSettings($CNF['FIELD_PICTURE'], $aData[$CNF['FIELD_PICTURE_POSITION']], 'page_cover') : '',
  512. 'picture_href' => (!$CNF['FIELD_PICTURE'] || !$aData[$CNF['FIELD_PICTURE']]) && CHECK_ACTION_RESULT_ALLOWED === $oModule->checkAllowedEdit($aData) ? $sUrlPictureChange : 'javascript:void(0);',
  513. );
  514. if($bProfileViewAllowed && $CNF['FIELD_PICTURE'] && $aData[$CNF['FIELD_PICTURE']]) {
  515. $sPicturePopup = BxTemplFunctions::getInstance()->transBox($sPicturePopupId, $this->parseHtmlByName('image_popup.html', [
  516. 'image_url' => $sUrlPicture,
  517. ]), true, true);
  518. }
  519. }
  520. //--- Process Badge
  521. $bTmplVarsShowBadge = false;
  522. $aTmplVarsShowBadge = [];
  523. if($bShowBadge) {
  524. $sUrlBadge = $this->getBadgeImage($aData);
  525. if(($bTmplVarsShowBadge = !empty($sUrlBadge)))
  526. $aTmplVarsShowBadge = [
  527. 'size' => 'ava-big',
  528. 'badge_url' => $sUrlBadge,
  529. 'badge_link' => $this->getBadgeLink($aData),
  530. 'title_attr' => bx_html_attribute($sTitle),
  531. ];
  532. }
  533. //--- Process Actions menu
  534. $sActionsMenu = '';
  535. if(!isset($aParams['show_menu_actions']) || $aParams['show_menu_actions'] === true) {
  536. $sActionsMenu = $oModule->serviceEntityAllActions([$aData[$CNF['FIELD_ID']], $aData]);
  537. if(!$sActionsMenu && ($oMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY'], $this)) !== false)
  538. $sActionsMenu = $oMenu->getCode();
  539. }
  540. //--- Process Meta menu
  541. $sMetaMenu = '';
  542. if((!isset($aParams['show_menu_meta']) || $aParams['show_menu_meta'] === true) && !empty($CNF['OBJECT_MENU_VIEW_ENTRY_META'])) {
  543. $oMetaMenu = BxTemplMenu::getObjectInstance($CNF['OBJECT_MENU_VIEW_ENTRY_META']);
  544. if($oMetaMenu !== false) {
  545. $oMetaMenu->setContentId($aData[$CNF['FIELD_ID']]);
  546. $oMetaMenu->setContentPublic($this->isProfilePublic($aData));
  547. $sMetaMenu = $oMetaMenu->getCode();
  548. }
  549. }
  550. $aTmplVars = [
  551. 'module' => $this->_oConfig->getName(),
  552. 'class' => trim($sClass),
  553. 'id' => $aData[$CNF['FIELD_ID']],
  554. 'content_url' => $sUrl,
  555. 'title' => $sTitle,
  556. 'bx_if:show_title_as_tag' => [
  557. 'condition' => !$bUseAsAuthor,
  558. 'content' => [
  559. 'title' => $sTitle,
  560. 'bx_if:show_badge' => [
  561. 'condition' => $bTmplVarsShowBadge,
  562. 'content' => $aTmplVarsShowBadge
  563. ]
  564. ]
  565. ],
  566. 'bx_if:show_title_as_text' => [
  567. 'condition' => $bUseAsAuthor,
  568. 'content' => [
  569. 'title' => $sTitle,
  570. 'bx_if:show_badge' => [
  571. 'condition' => $bTmplVarsShowBadge,
  572. 'content' => $aTmplVarsShowBadge
  573. ]
  574. ]
  575. ],
  576. 'bx_if:show_avatar' => [
  577. 'condition' => $bTmplVarsShowAvatar,
  578. 'content' => $aTmplVarsShowAvatar
  579. ],
  580. 'bx_if:show_avatar_placeholder' => [
  581. 'condition' => !$bTmplVarsShowAvatar,
  582. 'content' => [true]
  583. ],
  584. 'bx_if:show_badge' => [
  585. 'condition' => $bTmplVarsShowBadge,
  586. 'content' => $aTmplVarsShowBadge
  587. ],
  588. 'badges' => $oModule->serviceGetBadges($aData[$CNF['FIELD_ID']]),
  589. 'action_menu' => $sActionsMenu,
  590. 'meta' => $sMetaMenu,
  591. 'show_data' => $sShowData,
  592. 'picture_popup' => $sPicturePopup,
  593. 'additional_code' => $sAddCode,
  594. 'cover_tweak' => $sCoverTweak,
  595. ];
  596. $bShowClickable = !isset($aParams['show_clickable']) || $aParams['show_clickable'] === true; //--- Is available for UseAsBlock appearance only.
  597. if($bUseAsBlock && $bShowClickable)
  598. $sClass .= ' bx-clickable';
  599. if($bUseAsBlock)
  600. $aTmplVars = array_merge($aTmplVars, [
  601. 'bx_if:show_clickable' => [
  602. 'condition' => $bShowClickable,
  603. 'content' => [
  604. 'content_url' => $sUrl
  605. ]
  606. ],
  607. 'bx_if:show_cover' => [
  608. 'condition' => $bTmplVarsShowCover,
  609. 'content' => $aTmplVarsShowCover
  610. ],
  611. 'bx_if:show_text' => [
  612. 'condition' => !isset($aParams['show_text']) || $aParams['show_text'] === true,
  613. 'content' => [
  614. 'text' => isset($CNF['FIELD_TEXT']) ? strmaxtextlen(strip_tags($aData[$CNF['FIELD_TEXT']]), 150) : ''
  615. ]
  616. ]
  617. ]);
  618. else
  619. $aTmplVars = array_merge($aTmplVars, $aTmplVarsShowCover, [
  620. 'bx_if:show_cover' => [
  621. 'condition' => $bUrlCover,
  622. 'content' => [true]
  623. ],
  624. 'bx_if:show_cover_placeholder' => [
  625. 'condition' => !$bUrlCover,
  626. 'content' => [true]
  627. ],
  628. ]);
  629. return $aTmplVars;
  630. }
  631. /**
  632. * Get profile cover
  633. */
  634. function setCover ($oPage, $aData, $sTemplateName = 'cover.html')
  635. {
  636. BxDolCover::getInstance($this)->set($this->prepareCover($aData, ['page' => $oPage]), $sTemplateName);
  637. }
  638. /**
  639. * Get profile picture thumb url
  640. * @deprecated since 11.0.0 use BxBaseModProfileTemplate::urlAvatar instead.
  641. */
  642. function avatar ($aData, $bSubstituteNoImage = true)
  643. {
  644. return $this->urlAvatar($aData, $bSubstituteNoImage);
  645. }
  646. /**
  647. * Get profile picture thumb url
  648. * @deprecated since 11.0.0 use BxBaseModProfileTemplate::urlThumb instead.
  649. */
  650. function thumb ($aData, $bSubstituteNoImage = true)
  651. {
  652. return $this->urlThumb($aData, $bSubstituteNoImage);
  653. }
  654. /**
  655. * Get profile picture thumb url
  656. * @deprecated since 11.0.0 use BxBaseModProfileTemplate::urlIcon instead.
  657. */
  658. function icon ($aData, $bSubstituteNoImage = true)
  659. {
  660. return $this->urlIcon($aData, $bSubstituteNoImage);
  661. }
  662. /**
  663. * Get profile picture icon url
  664. */
  665. function urlIcon ($aData, $bSubstituteNoImage = true)
  666. {
  667. $CNF = &$this->_oConfig->CNF;
  668. return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_ICON'], 'no-picture-icon.png', $aData, $bSubstituteNoImage);
  669. }
  670. /**
  671. * Get profile thumb url
  672. */
  673. function urlThumb ($aData, $bSubstituteNoImage = true)
  674. {
  675. $CNF = &$this->_oConfig->CNF;
  676. return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_THUMB'], 'no-picture-thumb.png', $aData, $bSubstituteNoImage);
  677. }
  678. /**
  679. * Get profile avatar url
  680. */
  681. function urlAvatar ($aData, $bSubstituteNoImage = true)
  682. {
  683. $CNF = &$this->_oConfig->CNF;
  684. $sDefault = bx_is_api() ? '' : 'no-picture-preview.png';
  685. return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_AVATAR'], $sDefault, $aData, $bSubstituteNoImage);
  686. }
  687. /**
  688. * Get profile avatar big url
  689. */
  690. function urlAvatarBig ($aData, $bSubstituteNoImage = true)
  691. {
  692. $CNF = &$this->_oConfig->CNF;
  693. return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_AVATAR_BIG'], 'no-picture-preview.png', $aData, $bSubstituteNoImage);
  694. }
  695. /**
  696. * Get profile picture url
  697. */
  698. function urlPicture ($aData, $bSubstituteNoImage = true)
  699. {
  700. $CNF = &$this->_oConfig->CNF;
  701. return $this->_image ($CNF['FIELD_PICTURE'], $CNF['OBJECT_IMAGES_TRANSCODER_PICTURE'], 'no-picture-preview.png', $aData, $bSubstituteNoImage);
  702. }
  703. /**
  704. * Get profile cover image url
  705. */
  706. function urlCover ($aData, $bSubstituteNoImage = false)
  707. {
  708. $CNF = &$this->_oConfig->CNF;
  709. $sImageUrl = $this->_image($CNF['FIELD_COVER'], $CNF['OBJECT_IMAGES_TRANSCODER_COVER'], '', $aData, $bSubstituteNoImage);
  710. return $sImageUrl;
  711. }
  712. /**
  713. * Get profile cover image url for browse unit
  714. */
  715. function urlCoverUnit ($aData, $bSubstituteNoImage = true)
  716. {
  717. $CNF = &$this->_oConfig->CNF;
  718. $sImageUrl = $this->_image ($CNF['FIELD_COVER'], $CNF['OBJECT_IMAGES_TRANSCODER_GALLERY'], '', $aData, false);
  719. if(!$bSubstituteNoImage || $sImageUrl)
  720. return $sImageUrl;
  721. $iImageId = (int)getParam('sys_unit_cover_profile');
  722. $oImageTranscoder = BxDolTranscoderImage::getObjectInstance(BX_DOL_TRANSCODER_OBJ_COVER_UNIT_PROFILE);
  723. if($oImageTranscoder && $iImageId != 0)
  724. $sImageUrl = $oImageTranscoder->getFileUrl($iImageId);
  725. if(!$sImageUrl)
  726. $sImageUrl = $this->getImageUrl('cover.svg');
  727. return $sImageUrl;
  728. }
  729. /**
  730. * Get profile picture icon url
  731. */
  732. function _image ($sField, $sTranscodeObject, $sNoImage, $aData, $bSubstituteNoImage = true)
  733. {
  734. $sImageUrl = false;
  735. if (isset($aData[$sField]) && $aData[$sField]) {
  736. $oImagesTranscoder = BxDolTranscoderImage::getObjectInstance($sTranscodeObject);
  737. if ($oImagesTranscoder)
  738. $sImageUrl = $oImagesTranscoder->getFileUrl($aData[$sField]);
  739. }
  740. if(!$bSubstituteNoImage || $sImageUrl)
  741. return $sImageUrl;
  742. $sImageUrl = $this->getImageUrl($sNoImage);
  743. if(!$sImageUrl)
  744. $sImageUrl = bx_is_api() ? '' : $this->getImageUrl(substr($sNoImage, 0, strrpos($sNoImage, '-')) . '.svg');
  745. return $sImageUrl;
  746. }
  747. function _getImageSettings($sField, $sSettings, $sUsage = '')
  748. {
  749. $CNF = &$this->_oConfig->CNF;
  750. if(isset($CNF['FIELD_COVER']) && $sField == $CNF['FIELD_COVER'] && $sUsage == 'unit_cover')
  751. return '';
  752. return parent::_getImageSettings($sField, $sSettings, $sUsage);
  753. }
  754. protected function _isTemplateWithMeta(string $sTemplateName = 'unit.html'): bool
  755. {
  756. $sResult = '';
  757. switch($sTemplateName) {
  758. case 'unit_ext.html':
  759. case 'unit_with_cover.html':
  760. $sResult = true;
  761. break;
  762. default:
  763. $sResult = false;
  764. break;
  765. }
  766. return $sResult;
  767. }
  768. protected function _getUnitClass($aData, $sTemplateName = 'unit.html')
  769. {
  770. $sResult = '';
  771. switch($sTemplateName) {
  772. default:
  773. case 'unit.html':
  774. $sResult = $this->_sUnitClass;
  775. break;
  776. case 'unit_with_cover.html':
  777. $sResult = $this->_sUnitClassWithCover;
  778. break;
  779. case 'unit_wo_info.html':
  780. case 'unit_wo_info_links.html':
  781. $sResult = $this->_sUnitClassWoInfo;
  782. break;
  783. case 'unit_with_cover_showcase.html':
  784. $sResult = $this->_sUnitClassShowCase;
  785. break;
  786. case 'unit_wo_info_showcase.html':
  787. $sResult = $this->_sUnitClassWoInfoShowCase;
  788. break;
  789. }
  790. return $sResult;
  791. }
  792. protected function _getUnitSize($aData, $sTemplateName = 'unit.html')
  793. {
  794. $sResult = '';
  795. switch($sTemplateName) {
  796. case 'unit_with_cover.html':
  797. case 'unit_with_cover_showcase.html':
  798. $sResult = 'ava';
  799. break;
  800. default:
  801. $sResult = $this->_sUnitSizeDefault;
  802. break;
  803. }
  804. return $sResult;
  805. }
  806. protected function _isUnitThumb($aData, $sTemplateName = 'unit.html')
  807. {
  808. return true;
  809. }
  810. protected function _getUnitThumbUrl($sSize, $aData, $bSubstituteNoImage = true)
  811. {
  812. $sMethod = 'url' . bx_gen_method_name(str_replace('ava', 'avatar', $sSize), array('_', '-'));
  813. if(method_exists($this, $sMethod))
  814. return $this->$sMethod($aData, $bSubstituteNoImage);
  815. else
  816. return $this->_getUnitThumbUrl($this->_sUnitSizeDefault, $aData, $bSubstituteNoImage);
  817. }
  818. }
  819. /** @} */