1
0

BxBaseModTextModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxDolModule');
  12. bx_import ('BxDolAcl');
  13. /**
  14. * Base module class for text based modules
  15. */
  16. class BxBaseModTextModule extends BxDolModule
  17. {
  18. protected $_iProfileId;
  19. function __construct(&$aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_iProfileId = bx_get_logged_profile_id();
  23. }
  24. // ====== ACTIONS METHODS
  25. public function actionRss ($sMode = '', $sExtra = '')
  26. {
  27. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedBrowse())) {
  28. $this->_oTemplate->displayAccessDenied ();
  29. exit;
  30. }
  31. $aParams = array ();
  32. $sMode = bx_process_input($sMode);
  33. switch ($sMode) {
  34. case 'author':
  35. $aParams = array('author' => (int)$sExtra);
  36. break;
  37. }
  38. bx_import ('SearchResult', $this->_aModule);
  39. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  40. $o = new $sClass($sMode, $aParams);
  41. if ($o->isError) {
  42. $this->_oTemplate->displayPageNotFound ();
  43. exit;
  44. }
  45. $o->outputRSS();
  46. exit;
  47. }
  48. // ====== SERVICE METHODS
  49. /**
  50. * Display pablic entries
  51. * @return HTML string
  52. */
  53. public function serviceBrowsePublic ()
  54. {
  55. return $this->_serviceBrowse ('public');
  56. }
  57. /**
  58. * Display featured entries
  59. * @return HTML string
  60. */
  61. public function serviceBrowseFeatured ()
  62. {
  63. return $this->_serviceBrowse ('public');
  64. }
  65. /**
  66. * Display entries of the author
  67. * @return HTML string
  68. */
  69. public function serviceBrowseAuthor ($iProfileId = 0)
  70. {
  71. if (!$iProfileId)
  72. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  73. if (!$iProfileId)
  74. return '';
  75. return $this->_serviceBrowse ('author', array('author' => $iProfileId));
  76. }
  77. /**
  78. * Add entry form
  79. * @return HTML string
  80. */
  81. public function serviceEntityCreate ()
  82. {
  83. bx_import('FormsEntryHelper', $this->_aModule);
  84. $sClass = $this->_aModule['class_prefix'] . 'FormsEntryHelper';
  85. $oFormsHelper = new $sClass($this);
  86. return $oFormsHelper->addDataForm();
  87. }
  88. /**
  89. * Edit entry form
  90. * @return HTML string
  91. */
  92. public function serviceEntityEdit ($iContentId = 0)
  93. {
  94. return $this->_serviceEntityForm ('editDataForm', $iContentId);
  95. }
  96. /**
  97. * Delete entry form
  98. * @return HTML string
  99. */
  100. public function serviceEntityDelete ($iContentId = 0)
  101. {
  102. return $this->_serviceEntityForm ('deleteDataForm', $iContentId);
  103. }
  104. /**
  105. * Entry text with some additional controls
  106. */
  107. public function serviceEntityTextBlock ($iContentId = 0)
  108. {
  109. return $this->_serviceEntityForm ('viewDataEntry', $iContentId);
  110. }
  111. /**
  112. * Additional entry info
  113. */
  114. public function serviceEntityInfo ($iContentId = 0)
  115. {
  116. return $this->_serviceEntityForm ('viewDataForm', $iContentId);
  117. }
  118. /**
  119. * Entry social sharing block
  120. */
  121. public function serviceEntitySocialSharing ($iContentId = 0)
  122. {
  123. if (!$iContentId)
  124. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  125. if (!$iContentId)
  126. return false;
  127. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  128. if (!$aContentInfo)
  129. return false;
  130. $CNF = &$this->_oConfig->CNF;
  131. bx_import('BxDolPermalinks');
  132. $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  133. $aCustomParams = false;
  134. if ($aContentInfo[$CNF['FIELD_THUMB']]) {
  135. bx_import('BxDolStorage');
  136. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  137. if ($oStorage && ($sImgUrl = $oStorage->getFileUrlById($aContentInfo[$CNF['FIELD_THUMB']]))) {
  138. $aCustomParams = array (
  139. 'img_url' => $sImgUrl,
  140. 'img_url_encoded' => rawurlencode($sImgUrl),
  141. );
  142. }
  143. }
  144. //TODO: Rebuild using menus engine when it will be ready for such elements like Vote, Share, etc.
  145. $sVotes = '';
  146. bx_import('BxDolVote');
  147. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $aContentInfo[$CNF['FIELD_ID']]);
  148. if ($oVotes)
  149. $sVotes = $oVotes->getElementBlock(array('show_do_vote_as_button' => true));
  150. $sShare = '';
  151. if (BxDolRequest::serviceExists('bx_timeline', 'get_share_element_block'))
  152. $sShare = BxDolService::call('bx_timeline', 'get_share_element_block', array(bx_get_logged_profile_id(), $this->_aModule['name'], 'added', $aContentInfo[$CNF['FIELD_ID']], array('show_do_share_as_button' => true)));
  153. bx_import('BxTemplSocialSharing');
  154. $sSocial = BxTemplSocialSharing::getInstance()->getCode($iContentId, $this->_aModule['name'], BX_DOL_URL_ROOT . $sUrl, $aContentInfo[$CNF['FIELD_TITLE']], $aCustomParams);
  155. return $this->_oTemplate->parseHtmlByName('entry-share.html', array(
  156. 'vote' => $sVotes,
  157. 'share' => $sShare,
  158. 'social' => $sSocial,
  159. ));
  160. //TODO: Rebuild using menus engine when it will be ready for such elements like Vote, Share, etc.
  161. }
  162. /**
  163. * Entry comments
  164. */
  165. public function serviceEntityComments ($iContentId = 0)
  166. {
  167. if (!$iContentId)
  168. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  169. if (!$iContentId)
  170. return false;
  171. bx_import('BxDolCmts');
  172. $oCmts = BxDolCmts::getObjectInstance($this->_oConfig->CNF['OBJECT_COMMENTS'], $iContentId);
  173. if (!$oCmts || !$oCmts->isEnabled())
  174. return false;
  175. return $oCmts->getCommentsBlock(0, 0, false);
  176. }
  177. /**
  178. * Entry author block
  179. */
  180. public function serviceEntityAuthor ($iContentId = 0)
  181. {
  182. if (!$iContentId)
  183. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  184. if (!$iContentId)
  185. return false;
  186. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  187. if (!$aContentInfo)
  188. return false;
  189. return $this->_oTemplate->entryAuthor ($aContentInfo);
  190. }
  191. /**
  192. * Entry actions block
  193. */
  194. public function serviceEntityActions ($iContentId = 0)
  195. {
  196. if (!$iContentId)
  197. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  198. if (!$iContentId)
  199. return false;
  200. bx_import('BxTemplMenu');
  201. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY']);
  202. return $oMenu ? $oMenu->getCode() : false;
  203. }
  204. /**
  205. * Entry attachments block
  206. */
  207. public function serviceEntityAttachments ($iContentId = 0)
  208. {
  209. if (!$iContentId)
  210. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  211. if (!$iContentId)
  212. return false;
  213. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  214. if (!$aContentInfo)
  215. return false;
  216. return $this->_oTemplate->entryAttachments ($aContentInfo);
  217. }
  218. /**
  219. * My entries actions block
  220. */
  221. public function serviceMyEntriesActions ($iProfileId = 0)
  222. {
  223. if (!$iProfileId)
  224. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  225. if (!$iProfileId || $iProfileId != $this->_iProfileId)
  226. return false;
  227. bx_import('BxTemplMenu');
  228. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  229. return $oMenu ? $oMenu->getCode() : false;
  230. }
  231. /**
  232. * Data for Timeline
  233. */
  234. public function serviceGetTimelineData()
  235. {
  236. return array(
  237. 'handlers' => array(
  238. 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' => ''),
  239. array('type' => 'update', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'edited'),
  240. array('type' => 'delete', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'deleted')
  241. ),
  242. 'alerts' => array(
  243. array('unit' => $this->_aModule['name'], 'action' => 'added'),
  244. array('unit' => $this->_aModule['name'], 'action' => 'edited'),
  245. array('unit' => $this->_aModule['name'], 'action' => 'deleted'),
  246. )
  247. );
  248. }
  249. /**
  250. * Entry post for Timeline
  251. */
  252. public function serviceGetTimelinePost($aEvent)
  253. {
  254. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  255. if(empty($aContentInfo) || !is_array($aContentInfo))
  256. return '';
  257. $CNF = &$this->_oConfig->CNF;
  258. bx_import('BxDolPermalinks');
  259. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  260. //--- Image(s)
  261. $sImage = '';
  262. if (isset($aContentInfo[$CNF['FIELD_THUMB']]) && $aContentInfo[$CNF['FIELD_THUMB']]) {
  263. bx_import('BxDolStorage');
  264. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  265. if($oStorage)
  266. $sImage = $oStorage->getFileUrlById($aContentInfo[$CNF['FIELD_THUMB']]);
  267. }
  268. //--- Votes
  269. bx_import('BxDolVote');
  270. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $aEvent['object_id']);
  271. $aVotes = array();
  272. if ($oVotes && $oVotes->isEnabled())
  273. $aVotes = array(
  274. 'system' => $CNF['OBJECT_VOTES'],
  275. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  276. 'count' => $aContentInfo['votes']
  277. );
  278. //--- Comments
  279. bx_import('BxDolCmts');
  280. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $aEvent['object_id']);
  281. $aComments = array();
  282. if($oCmts && $oCmts->isEnabled())
  283. $aComments = array(
  284. 'system' => $CNF['OBJECT_COMMENTS'],
  285. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  286. 'count' => $aContentInfo['comments']
  287. );
  288. return array(
  289. 'owner_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  290. 'content' => array(
  291. 'sample' => _t($CNF['T']['txt_sample_single']),
  292. 'url' => $sUrl,
  293. 'title' => isset($aContentInfo[$CNF['FIELD_TITLE']]) ? $aContentInfo[$CNF['FIELD_TITLE']] : strmaxtextlen($aContentInfo[$CNF['FIELD_TEXT']], 20, '...'),
  294. 'text' => $aContentInfo[$CNF['FIELD_TEXT']],
  295. 'images' => array(
  296. array(
  297. 'url' => $sUrl,
  298. 'src' => $sImage
  299. )
  300. )
  301. ), //a string to display or array to parse default template before displaying.
  302. 'votes' => $aVotes,
  303. 'comments' => $aComments,
  304. 'title' => '', //may be empty.
  305. 'description' => '' //may be empty.
  306. );
  307. }
  308. // ====== PERMISSION METHODS
  309. /**
  310. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  311. */
  312. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  313. {
  314. $CNF = &$this->_oConfig->CNF;
  315. // moderator and owner always have access
  316. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  317. return CHECK_ACTION_RESULT_ALLOWED;
  318. // check ACL
  319. $aCheck = checkActionModule($this->_iProfileId, 'view entry', $this->getName(), $isPerformAction);
  320. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  321. return $aCheck[CHECK_ACTION_MESSAGE];
  322. // check privacy
  323. bx_import('BxDolPrivacy');
  324. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  325. if ($oPrivacy && !$oPrivacy->check($aDataEntry[$CNF['FIELD_ID']]))
  326. return _t('_sys_access_denied_to_private_content');
  327. return CHECK_ACTION_RESULT_ALLOWED;
  328. }
  329. /**
  330. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  331. */
  332. public function checkAllowedBrowse ()
  333. {
  334. return CHECK_ACTION_RESULT_ALLOWED;
  335. }
  336. /**
  337. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  338. */
  339. public function checkAllowedAdd ($isPerformAction = false)
  340. {
  341. // check ACL
  342. $aCheck = checkActionModule($this->_iProfileId, 'create entry', $this->getName(), $isPerformAction);
  343. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  344. return $aCheck[CHECK_ACTION_MESSAGE];
  345. return CHECK_ACTION_RESULT_ALLOWED;
  346. }
  347. /**
  348. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  349. */
  350. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  351. {
  352. // moderator and owner always have access
  353. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  354. return CHECK_ACTION_RESULT_ALLOWED;
  355. return _t('_sys_txt_access_denied');
  356. }
  357. /**
  358. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  359. */
  360. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  361. {
  362. // moderator always has access
  363. if ($this->_isModerator($isPerformAction))
  364. return CHECK_ACTION_RESULT_ALLOWED;
  365. // check ACL
  366. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  367. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  368. return CHECK_ACTION_RESULT_ALLOWED;
  369. return _t('_sys_txt_access_denied');
  370. }
  371. /**
  372. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  373. */
  374. public function checkAllowedSetThumb ()
  375. {
  376. // check ACL
  377. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  378. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  379. return $aCheck[CHECK_ACTION_MESSAGE];
  380. return CHECK_ACTION_RESULT_ALLOWED;
  381. }
  382. // ====== PROTECTED METHODS
  383. protected function _isModerator ($isPerformAction = false)
  384. {
  385. // check moderator ACL
  386. $aCheck = checkActionModule($this->_iProfileId, 'edit any entry', $this->getName(), $isPerformAction);
  387. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  388. }
  389. protected function _serviceBrowse ($sMode, $aParams = false)
  390. {
  391. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedBrowse()))
  392. return MsgBox($sMsg);
  393. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  394. bx_import('SearchResult', $this->_aModule);
  395. $o = new $sClass($sMode, $aParams);
  396. $o->setDisplayEmptyMsg(true);
  397. if ($o->isError)
  398. return '';
  399. if ($s = $o->processing())
  400. return $s;
  401. else
  402. return '';
  403. }
  404. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0)
  405. {
  406. if (!$iContentId)
  407. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  408. if (!$iContentId)
  409. return false;
  410. bx_import('FormsEntryHelper', $this->_aModule);
  411. $sClass = $this->_aModule['class_prefix'] . 'FormsEntryHelper';
  412. $oFormsHelper = new $sClass($this);
  413. return $oFormsHelper->$sFormMethod((int)$iContentId);
  414. }
  415. }
  416. /** @} */