BxBaseModProfileTemplate.php 38 KB

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