1
0

BxBaseModTextModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup BaseText Base classes for text modules
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Base module class for text based modules
  13. */
  14. class BxBaseModTextModule extends BxBaseModGeneralModule
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. // ====== SERVICE METHODS
  21. public function serviceGetMenuAddonManageTools()
  22. {
  23. bx_import('SearchResult', $this->_aModule);
  24. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  25. $o = new $sClass();
  26. $o->fillFilters(array(
  27. 'status' => 'hidden'
  28. ));
  29. $o->unsetPaginate();
  30. return $o->getNum();
  31. }
  32. public function serviceGetMenuAddonManageToolsProfileStats()
  33. {
  34. bx_import('SearchResult', $this->_aModule);
  35. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  36. $o = new $sClass();
  37. $o->fillFilters(array(
  38. 'author' => bx_get_logged_profile_id()
  39. ));
  40. $o->unsetPaginate();
  41. return $o->getNum();
  42. }
  43. /**
  44. * Display public entries
  45. * @return HTML string
  46. */
  47. public function serviceBrowsePublic ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  48. {
  49. return $this->_serviceBrowse ('public', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  50. }
  51. /**
  52. * Display popular entries
  53. * @return HTML string
  54. */
  55. public function serviceBrowsePopular ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  56. {
  57. return $this->_serviceBrowse ('popular', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  58. }
  59. /**
  60. * Display recently updated entries
  61. * @return HTML string
  62. */
  63. public function serviceBrowseUpdated ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  64. {
  65. return $this->_serviceBrowse ('updated', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  66. }
  67. /**
  68. * Display entries of the author
  69. * @return HTML string
  70. */
  71. public function serviceBrowseAuthor ($iProfileId = 0, $sUnitView = false)
  72. {
  73. if (!$iProfileId)
  74. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  75. if (!$iProfileId)
  76. return '';
  77. return $this->_serviceBrowse ('author', array('author' => $iProfileId, 'unit_view' => $sUnitView ? $sUnitView : ''), BX_DB_PADDING_DEF, true);
  78. }
  79. /**
  80. * Entry social sharing block
  81. */
  82. public function serviceEntitySocialSharing ($iContentId = 0)
  83. {
  84. if (!$iContentId)
  85. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  86. if (!$iContentId)
  87. return false;
  88. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  89. if (!$aContentInfo)
  90. return false;
  91. $CNF = &$this->_oConfig->CNF;
  92. return $this->_entitySocialSharing ($iContentId, $iContentId, $aContentInfo[$CNF['FIELD_THUMB']], $aContentInfo[$CNF['FIELD_TITLE']], $CNF['OBJECT_STORAGE'], false, $CNF['OBJECT_VOTES'], $CNF['URI_VIEW_ENTRY']);
  93. }
  94. public function serviceEntityAllActions ($iContentId = 0)
  95. {
  96. return $this->_oTemplate->entryAllActions($this->serviceEntityActions($iContentId), $this->serviceEntitySocialSharing($iContentId));
  97. }
  98. /**
  99. * Entry text with some additional controls
  100. */
  101. public function serviceEntityTextBlock ($iContentId = 0)
  102. {
  103. return $this->_serviceEntityForm ('viewDataEntry', $iContentId);
  104. }
  105. /**
  106. * Entry location info
  107. */
  108. public function serviceEntityLocation ($iContentId = 0)
  109. {
  110. if (!$iContentId)
  111. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  112. if (!$iContentId)
  113. return false;
  114. return $this->_oTemplate->entryLocation ($iContentId);
  115. }
  116. /**
  117. * Entry comments
  118. */
  119. public function serviceEntityComments ($iContentId = 0)
  120. {
  121. return $this->_entityComments($this->_oConfig->CNF['OBJECT_COMMENTS'], $iContentId);
  122. }
  123. /**
  124. * Entry author block
  125. */
  126. public function serviceEntityAuthor ($iContentId = 0)
  127. {
  128. return $this->_serviceTemplateFunc ('entryAuthor', $iContentId);
  129. }
  130. /**
  131. * Entry attachments block
  132. */
  133. public function serviceEntityAttachments ($iContentId = 0)
  134. {
  135. return $this->_serviceTemplateFunc ('entryAttachments', $iContentId);
  136. }
  137. /**
  138. * My entries actions block
  139. */
  140. public function serviceMyEntriesActions ($iProfileId = 0)
  141. {
  142. if (!$iProfileId)
  143. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  144. if (!$iProfileId || $iProfileId != $this->_iProfileId)
  145. return false;
  146. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  147. return $oMenu ? $oMenu->getCode() : false;
  148. }
  149. /**
  150. * Delete all content by profile
  151. * @param $iProfileId profile id
  152. * @return number of deleted items
  153. */
  154. public function serviceDeleteEntitiesByAuthor ($iProfileId)
  155. {
  156. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  157. if (!$a)
  158. return 0;
  159. $iCount = 0;
  160. foreach ($a as $aContentInfo)
  161. $iCount += ('' == $this->serviceDeleteEntity($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  162. return $iCount;
  163. }
  164. /**
  165. * Data for Notification
  166. */
  167. public function serviceGetNotificationsData()
  168. {
  169. return array(
  170. 'handlers' => array(
  171. array('type' => 'insert', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'added', 'module_name' => $this->_aModule['name'], 'module_method' => 'get_notifications_post', 'module_class' => 'Module'),
  172. array('type' => 'update', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'edited'),
  173. array('type' => 'delete', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'deleted')
  174. ),
  175. 'alerts' => array(
  176. array('unit' => $this->_aModule['name'], 'action' => 'added'),
  177. array('unit' => $this->_aModule['name'], 'action' => 'edited'),
  178. array('unit' => $this->_aModule['name'], 'action' => 'deleted'),
  179. )
  180. );
  181. }
  182. /**
  183. * Entry post for Timeline
  184. */
  185. public function serviceGetNotificationsPost($aEvent)
  186. {
  187. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  188. if(empty($aContentInfo) || !is_array($aContentInfo))
  189. return array();
  190. $CNF = &$this->_oConfig->CNF;
  191. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  192. $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
  193. return array(
  194. 'entry_sample' => _t($CNF['T']['txt_sample_single']),
  195. 'entry_url' => $sEntryUrl,
  196. 'entry_caption' => $sEntryCaption,
  197. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  198. );
  199. }
  200. /**
  201. * Data for Timeline
  202. */
  203. public function serviceGetTimelineData()
  204. {
  205. return array(
  206. 'handlers' => array(
  207. array('type' => 'insert', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'added', 'module_name' => $this->_aModule['name'], 'module_method' => 'get_timeline_post', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  208. array('type' => 'update', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'edited'),
  209. array('type' => 'delete', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'deleted')
  210. ),
  211. 'alerts' => array(
  212. array('unit' => $this->_aModule['name'], 'action' => 'added'),
  213. array('unit' => $this->_aModule['name'], 'action' => 'edited'),
  214. array('unit' => $this->_aModule['name'], 'action' => 'deleted'),
  215. )
  216. );
  217. }
  218. /**
  219. * Entry post for Timeline
  220. */
  221. public function serviceGetTimelinePost($aEvent)
  222. {
  223. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  224. if(empty($aContentInfo) || !is_array($aContentInfo))
  225. return '';
  226. $CNF = &$this->_oConfig->CNF;
  227. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  228. //--- Image(s)
  229. $aImages = $this->_getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl);
  230. //--- Votes
  231. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $aEvent['object_id']);
  232. $aVotes = array();
  233. if ($oVotes && $oVotes->isEnabled())
  234. $aVotes = array(
  235. 'system' => $CNF['OBJECT_VOTES'],
  236. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  237. 'count' => $aContentInfo['votes']
  238. );
  239. //--- Comments
  240. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $aEvent['object_id']);
  241. $aComments = array();
  242. if($oCmts && $oCmts->isEnabled())
  243. $aComments = array(
  244. 'system' => $CNF['OBJECT_COMMENTS'],
  245. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  246. 'count' => $aContentInfo['comments']
  247. );
  248. return array(
  249. 'owner_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  250. 'content' => array(
  251. 'sample' => _t($CNF['T']['txt_sample_single']),
  252. 'url' => $sUrl,
  253. 'title' => isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...'),
  254. 'text' => $aContentInfo[$CNF['FIELD_TEXT']],
  255. 'images' => $aImages,
  256. ), //a string to display or array to parse default template before displaying.
  257. 'date' => $aContentInfo[$CNF['FIELD_ADDED']],
  258. 'votes' => $aVotes,
  259. 'comments' => $aComments,
  260. 'title' => '', //may be empty.
  261. 'description' => '' //may be empty.
  262. );
  263. }
  264. // ====== PERMISSION METHODS
  265. /**
  266. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  267. */
  268. public function checkAllowedSetThumb ()
  269. {
  270. // check ACL
  271. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  272. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  273. return $aCheck[CHECK_ACTION_MESSAGE];
  274. return CHECK_ACTION_RESULT_ALLOWED;
  275. }
  276. // ====== PROTECTED METHODS
  277. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl)
  278. {
  279. $CNF = &$this->_oConfig->CNF;
  280. $sImage = '';
  281. if (isset($aContentInfo[$CNF['FIELD_THUMB']]) && $aContentInfo[$CNF['FIELD_THUMB']]) {
  282. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  283. if($oTranscoder)
  284. $sImage = $oTranscoder->getFileUrl($aContentInfo[$CNF['FIELD_THUMB']]);
  285. }
  286. if (empty($sImage))
  287. return array();
  288. return array(
  289. array('url' => $sUrl, 'src' => $sImage),
  290. );
  291. }
  292. protected function _buildRssParams($sMode, $aArgs)
  293. {
  294. $aParams = array ();
  295. $sMode = bx_process_input($sMode);
  296. switch ($sMode) {
  297. case 'author':
  298. $aParams = array('author' => isset($aArgs[0]) ? (int)$aArgs[0] : '');
  299. break;
  300. }
  301. return $aParams;
  302. }
  303. protected function _entityComments ($sObject, $iId = 0)
  304. {
  305. if (!$iId)
  306. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  307. if (!$iId)
  308. return false;
  309. $oCmts = BxDolCmts::getObjectInstance($sObject, $iId);
  310. if (!$oCmts || !$oCmts->isEnabled())
  311. return false;
  312. return $oCmts->getCommentsBlock(0, 0, false);
  313. }
  314. protected function _entitySocialSharing ($iId, $iIdForTimeline, $iIdThumb, $sTitle, $sObjectStorage, $sObjectTranscoder, $sObjectVote, $sUriViewEntry, $sCommentsObject = '', $bEnableSocialSharing = true)
  315. {
  316. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sUriViewEntry . '&id=' . $iId);
  317. $sComments = '';
  318. if ($sCommentsObject && ($oComments = BxTemplCmts::getObjectInstance($sCommentsObject, $iId))) {
  319. $iNum = $oComments->getCommentsCountAll();
  320. $sComments = $this->_oTemplate->parseHtmlByName('comments-item.html', array (
  321. 'url' => $sUrl . '#' . $oComments->getListAnchor(),
  322. 'bx_if:comments' => array (
  323. 'condition' => $iNum,
  324. 'content' => array (
  325. 'num' => $iNum,
  326. ),
  327. ),
  328. ));
  329. }
  330. $aCustomParams = false;
  331. if ($iIdThumb && $bEnableSocialSharing) {
  332. if ($sObjectTranscoder)
  333. $o = BxDolTranscoder::getObjectInstance($sObjectTranscoder);
  334. else
  335. $o = BxDolStorage::getObjectInstance($sObjectStorage);
  336. if ($sImgUrl = $o->getFileUrlById($iIdThumb)) {
  337. $aCustomParams = array (
  338. 'img_url' => $sImgUrl,
  339. 'img_url_encoded' => rawurlencode($sImgUrl),
  340. );
  341. }
  342. }
  343. //TODO: Rebuild using menus engine when it will be ready for such elements like Vote, Share, etc.
  344. $sVotes = '';
  345. $oVotes = BxDolVote::getObjectInstance($sObjectVote, $iId);
  346. if ($oVotes)
  347. $sVotes = $oVotes->getElementBlock(array('show_do_vote_as_button' => true));
  348. $sShare = '';
  349. if ($iIdForTimeline && BxDolRequest::serviceExists('bx_timeline', 'get_share_element_block'))
  350. $sShare = BxDolService::call('bx_timeline', 'get_share_element_block', array(bx_get_logged_profile_id(), $this->_aModule['name'], 'added', $iIdForTimeline, array('show_do_share_as_button' => true)));
  351. $sSocial = $bEnableSocialSharing ? BxTemplSocialSharing::getInstance()->getCode($iId, $this->_aModule['name'], BX_DOL_URL_ROOT . $sUrl, $sTitle, $aCustomParams) : '';
  352. return $this->_oTemplate->parseHtmlByName('entry-share.html', array(
  353. 'comments' => $sComments,
  354. 'vote' => $sVotes,
  355. 'share' => $sShare,
  356. 'social' => $sSocial,
  357. ));
  358. //TODO: Rebuild using menus engine when it will be ready for such elements like Vote, Share, etc.
  359. }
  360. }
  361. /** @} */