BxBaseModTextModule.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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['OBJECT_REPORTS'], $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 Notifications module
  166. */
  167. public function serviceGetNotificationsData()
  168. {
  169. $sModule = $this->_aModule['name'];
  170. return array(
  171. 'handlers' => array(
  172. array('group' => $sModule . '_object', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added', 'module_name' => $sModule, 'module_method' => 'get_notifications_post', 'module_class' => 'Module'),
  173. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  174. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted'),
  175. array('group' => $sModule . '_comment', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'commentPost', 'module_name' => $sModule, 'module_method' => 'get_notifications_comment', 'module_class' => 'Module'),
  176. array('group' => $sModule . '_comment', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'commentRemoved'),
  177. array('group' => $sModule . '_vote', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'doVote', 'module_name' => $sModule, 'module_method' => 'get_notifications_vote', 'module_class' => 'Module'),
  178. array('group' => $sModule . '_vote', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'undoVote'),
  179. ),
  180. 'alerts' => array(
  181. array('unit' => $sModule, 'action' => 'added'),
  182. array('unit' => $sModule, 'action' => 'edited'),
  183. array('unit' => $sModule, 'action' => 'deleted'),
  184. array('unit' => $sModule, 'action' => 'commentPost'),
  185. array('unit' => $sModule, 'action' => 'commentRemoved'),
  186. array('unit' => $sModule, 'action' => 'doVote'),
  187. array('unit' => $sModule, 'action' => 'undoVote'),
  188. )
  189. );
  190. }
  191. /**
  192. * Entry post for Notifications module
  193. */
  194. public function serviceGetNotificationsPost($aEvent)
  195. {
  196. $CNF = &$this->_oConfig->CNF;
  197. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  198. if(empty($aContentInfo) || !is_array($aContentInfo))
  199. return array();
  200. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  201. $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
  202. return array(
  203. 'entry_sample' => _t($CNF['T']['txt_sample_single']),
  204. 'entry_url' => $sEntryUrl,
  205. 'entry_caption' => $sEntryCaption,
  206. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  207. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  208. );
  209. }
  210. /**
  211. * Entry post comment for Notifications module
  212. */
  213. public function serviceGetNotificationsComment($aEvent)
  214. {
  215. $CNF = &$this->_oConfig->CNF;
  216. $iContentId = (int)$aEvent['object_id'];
  217. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  218. if(empty($aContentInfo) || !is_array($aContentInfo))
  219. return array();
  220. $oComment = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  221. if(!$oComment || !$oComment->isEnabled())
  222. return array();
  223. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  224. $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
  225. return array(
  226. 'entry_sample' => _t($CNF['T']['txt_sample_single']),
  227. 'entry_url' => $sEntryUrl,
  228. 'entry_caption' => $sEntryCaption,
  229. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  230. 'subentry_sample' => _t($CNF['T']['txt_sample_comment_single']),
  231. 'subentry_url' => $oComment->getViewUrl((int)$aEvent['subobject_id']),
  232. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  233. );
  234. }
  235. /**
  236. * Entry post vote for Notifications module
  237. */
  238. public function serviceGetNotificationsVote($aEvent)
  239. {
  240. $CNF = &$this->_oConfig->CNF;
  241. $iContentId = (int)$aEvent['object_id'];
  242. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  243. if(empty($aContentInfo) || !is_array($aContentInfo))
  244. return array();
  245. $oVote = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  246. if(!$oVote || !$oVote->isEnabled())
  247. return array();
  248. $sEntryUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  249. $sEntryCaption = isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...');
  250. return array(
  251. 'entry_sample' => _t($CNF['T']['txt_sample_single']),
  252. 'entry_url' => $sEntryUrl,
  253. 'entry_caption' => $sEntryCaption,
  254. 'entry_author' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  255. 'subentry_sample' => _t($CNF['T']['txt_sample_vote_single']),
  256. 'lang_key' => '', //may be empty or not specified. In this case the default one from Notification module will be used.
  257. );
  258. }
  259. /**
  260. * Data for Timeline module
  261. */
  262. public function serviceGetTimelineData()
  263. {
  264. $sModule = $this->_aModule['name'];
  265. return array(
  266. 'handlers' => array(
  267. array('group' => $sModule . '_object', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'added', 'module_name' => $sModule, 'module_method' => 'get_timeline_post', 'module_class' => 'Module', 'groupable' => 0, 'group_by' => ''),
  268. array('group' => $sModule . '_object', 'type' => 'update', 'alert_unit' => $sModule, 'alert_action' => 'edited'),
  269. array('group' => $sModule . '_object', 'type' => 'delete', 'alert_unit' => $sModule, 'alert_action' => 'deleted')
  270. ),
  271. 'alerts' => array(
  272. array('unit' => $sModule, 'action' => 'added'),
  273. array('unit' => $sModule, 'action' => 'edited'),
  274. array('unit' => $sModule, 'action' => 'deleted'),
  275. )
  276. );
  277. }
  278. /**
  279. * Entry post for Timeline module
  280. */
  281. public function serviceGetTimelinePost($aEvent)
  282. {
  283. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  284. if(empty($aContentInfo) || !is_array($aContentInfo))
  285. return '';
  286. $CNF = &$this->_oConfig->CNF;
  287. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  288. //--- Image(s)
  289. $aImages = $this->_getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl);
  290. //--- Votes
  291. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $aEvent['object_id']);
  292. $aVotes = array();
  293. if ($oVotes && $oVotes->isEnabled())
  294. $aVotes = array(
  295. 'system' => $CNF['OBJECT_VOTES'],
  296. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  297. 'count' => $aContentInfo['votes']
  298. );
  299. //--- Reports
  300. $oReports = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $aEvent['object_id']);
  301. $aReports = array();
  302. if ($oReports && $oReports->isEnabled())
  303. $aReports = array(
  304. 'system' => $CNF['OBJECT_REPORTS'],
  305. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  306. 'count' => $aContentInfo['reports']
  307. );
  308. //--- Comments
  309. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $aEvent['object_id']);
  310. $aComments = array();
  311. if($oCmts && $oCmts->isEnabled())
  312. $aComments = array(
  313. 'system' => $CNF['OBJECT_COMMENTS'],
  314. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  315. 'count' => $aContentInfo['comments']
  316. );
  317. return array(
  318. 'owner_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  319. 'content' => array(
  320. 'sample' => _t($CNF['T']['txt_sample_single']),
  321. 'url' => $sUrl,
  322. 'title' => isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...'),
  323. 'text' => $aContentInfo[$CNF['FIELD_TEXT']],
  324. 'images' => $aImages,
  325. ), //a string to display or array to parse default template before displaying.
  326. 'date' => $aContentInfo[$CNF['FIELD_ADDED']],
  327. 'votes' => $aVotes,
  328. 'reports' => $aReports,
  329. 'comments' => $aComments,
  330. 'title' => '', //may be empty.
  331. 'description' => '' //may be empty.
  332. );
  333. }
  334. // ====== PERMISSION METHODS
  335. /**
  336. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  337. */
  338. public function checkAllowedSetThumb ()
  339. {
  340. // check ACL
  341. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  342. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  343. return $aCheck[CHECK_ACTION_MESSAGE];
  344. return CHECK_ACTION_RESULT_ALLOWED;
  345. }
  346. // ====== PROTECTED METHODS
  347. protected function _getImagesForTimelinePost($aEvent, $aContentInfo, $sUrl)
  348. {
  349. $CNF = &$this->_oConfig->CNF;
  350. $sImage = '';
  351. if (isset($aContentInfo[$CNF['FIELD_THUMB']]) && $aContentInfo[$CNF['FIELD_THUMB']]) {
  352. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']);
  353. if($oTranscoder)
  354. $sImage = $oTranscoder->getFileUrl($aContentInfo[$CNF['FIELD_THUMB']]);
  355. }
  356. if (empty($sImage))
  357. return array();
  358. return array(
  359. array('url' => $sUrl, 'src' => $sImage),
  360. );
  361. }
  362. protected function _buildRssParams($sMode, $aArgs)
  363. {
  364. $aParams = array ();
  365. $sMode = bx_process_input($sMode);
  366. switch ($sMode) {
  367. case 'author':
  368. $aParams = array('author' => isset($aArgs[0]) ? (int)$aArgs[0] : '');
  369. break;
  370. }
  371. return $aParams;
  372. }
  373. protected function _entityComments ($sObject, $iId = 0)
  374. {
  375. if (!$iId)
  376. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  377. if (!$iId)
  378. return false;
  379. $oCmts = BxDolCmts::getObjectInstance($sObject, $iId);
  380. if (!$oCmts || !$oCmts->isEnabled())
  381. return false;
  382. return $oCmts->getCommentsBlock(0, 0, false);
  383. }
  384. protected function _entitySocialSharing ($iId, $iIdForTimeline, $iIdThumb, $sTitle, $sObjectStorage, $sObjectTranscoder, $sObjectVote, $sObjectReport, $sUriViewEntry, $sCommentsObject = '', $bEnableSocialSharing = true)
  385. {
  386. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $sUriViewEntry . '&id=' . $iId);
  387. $sComments = '';
  388. if ($sCommentsObject && ($oComments = BxTemplCmts::getObjectInstance($sCommentsObject, $iId))) {
  389. $iNum = $oComments->getCommentsCountAll();
  390. $sComments = $this->_oTemplate->parseHtmlByName('comments-item.html', array (
  391. 'url' => $sUrl . '#' . $oComments->getListAnchor(),
  392. 'bx_if:comments' => array (
  393. 'condition' => $iNum,
  394. 'content' => array (
  395. 'num' => $iNum,
  396. ),
  397. ),
  398. ));
  399. }
  400. $aCustomParams = false;
  401. if ($iIdThumb && $bEnableSocialSharing) {
  402. if ($sObjectTranscoder)
  403. $o = BxDolTranscoder::getObjectInstance($sObjectTranscoder);
  404. else
  405. $o = BxDolStorage::getObjectInstance($sObjectStorage);
  406. if ($sImgUrl = $o->getFileUrlById($iIdThumb)) {
  407. $aCustomParams = array (
  408. 'img_url' => $sImgUrl,
  409. 'img_url_encoded' => rawurlencode($sImgUrl),
  410. );
  411. }
  412. }
  413. //TODO: Rebuild using menus engine when it will be ready for such elements like Vote, Share, etc.
  414. $sVotes = '';
  415. $oVotes = BxDolVote::getObjectInstance($sObjectVote, $iId);
  416. if ($oVotes)
  417. $sVotes = $oVotes->getElementBlock(array('show_do_vote_as_button' => true));
  418. $sShare = '';
  419. if ($iIdForTimeline && BxDolRequest::serviceExists('bx_timeline', 'get_share_element_block'))
  420. $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)));
  421. $sReport = '';
  422. $oReport = BxDolReport::getObjectInstance($sObjectReport, $iId);
  423. if ($oReport)
  424. $sReport = $oReport->getElementBlock(array('show_do_report_as_button' => true));
  425. $sSocial = $bEnableSocialSharing ? BxTemplSocialSharing::getInstance()->getCode($iId, $this->_aModule['name'], BX_DOL_URL_ROOT . $sUrl, $sTitle, $aCustomParams) : '';
  426. return $this->_oTemplate->parseHtmlByName('entry-share.html', array(
  427. 'comments' => $sComments,
  428. 'vote' => $sVotes,
  429. 'share' => $sShare,
  430. 'report' => $sReport,
  431. 'social' => $sSocial,
  432. ));
  433. //TODO: Rebuild using menus engine when it will be ready for such elements like Vote, Share, etc.
  434. }
  435. }
  436. /** @} */