BxBaseModProfileTemplate.php 39 KB

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