BxBaseModTextModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. * My entries actions block
  206. */
  207. public function serviceMyEntriesActions ($iProfileId = 0)
  208. {
  209. if (!$iProfileId)
  210. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  211. if (!$iProfileId || $iProfileId != $this->_iProfileId)
  212. return false;
  213. bx_import('BxTemplMenu');
  214. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  215. return $oMenu ? $oMenu->getCode() : false;
  216. }
  217. /**
  218. * Data for Timeline
  219. */
  220. public function serviceGetTimelineData()
  221. {
  222. return array(
  223. 'handlers' => array(
  224. 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' => ''),
  225. array('type' => 'update', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'edited'),
  226. array('type' => 'delete', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'deleted')
  227. ),
  228. 'alerts' => array(
  229. array('unit' => $this->_aModule['name'], 'action' => 'added'),
  230. array('unit' => $this->_aModule['name'], 'action' => 'edited'),
  231. array('unit' => $this->_aModule['name'], 'action' => 'deleted'),
  232. )
  233. );
  234. }
  235. /**
  236. * Entry post for Timeline
  237. */
  238. public function serviceGetTimelinePost($aEvent)
  239. {
  240. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  241. if(empty($aContentInfo) || !is_array($aContentInfo))
  242. return '';
  243. $CNF = &$this->_oConfig->CNF;
  244. bx_import('BxDolPermalinks');
  245. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  246. //--- Image(s)
  247. $sImage = '';
  248. if($aContentInfo[$CNF['FIELD_THUMB']]) {
  249. bx_import('BxDolStorage');
  250. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  251. if($oStorage)
  252. $sImage = $oStorage->getFileUrlById($aContentInfo[$CNF['FIELD_THUMB']]);
  253. }
  254. //--- Votes
  255. bx_import('BxDolVote');
  256. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $aEvent['object_id']);
  257. $aVotes = array();
  258. if ($oVotes && $oVotes->isEnabled())
  259. $aVotes = array(
  260. 'system' => $CNF['OBJECT_VOTES'],
  261. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  262. 'count' => $aContentInfo['votes']
  263. );
  264. //--- Comments
  265. bx_import('BxDolCmts');
  266. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $aEvent['object_id']);
  267. $aComments = array();
  268. if($oCmts && $oCmts->isEnabled())
  269. $aComments = array(
  270. 'system' => $CNF['OBJECT_COMMENTS'],
  271. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  272. 'count' => $aContentInfo['comments']
  273. );
  274. return array(
  275. 'owner_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  276. 'content' => array(
  277. 'sample' => _t($CNF['T']['txt_sample_single']),
  278. 'url' => $sUrl,
  279. 'title' => $aContentInfo[$CNF['FIELD_TITLE']],
  280. 'text' => $aContentInfo[$CNF['FIELD_TEXT']],
  281. 'images' => array(
  282. array(
  283. 'url' => $sUrl,
  284. 'src' => $sImage
  285. )
  286. )
  287. ), //a string to display or array to parse default template before displaying.
  288. 'votes' => $aVotes,
  289. 'comments' => $aComments,
  290. 'title' => '', //may be empty.
  291. 'description' => '' //may be empty.
  292. );
  293. }
  294. // ====== PERMISSION METHODS
  295. /**
  296. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  297. */
  298. public function checkAllowedView ($aDataEntry, $isPerformAction = false)
  299. {
  300. $CNF = &$this->_oConfig->CNF;
  301. // moderator and owner always have access
  302. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  303. return CHECK_ACTION_RESULT_ALLOWED;
  304. // check ACL
  305. $aCheck = checkActionModule($this->_iProfileId, 'view entry', $this->getName(), $isPerformAction);
  306. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  307. return $aCheck[CHECK_ACTION_MESSAGE];
  308. // check privacy
  309. bx_import('BxDolPrivacy');
  310. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  311. if ($oPrivacy && !$oPrivacy->check($aDataEntry[$CNF['FIELD_ID']]))
  312. return _t('_sys_access_denied_to_private_content');
  313. return CHECK_ACTION_RESULT_ALLOWED;
  314. }
  315. /**
  316. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  317. */
  318. public function checkAllowedBrowse ()
  319. {
  320. return CHECK_ACTION_RESULT_ALLOWED;
  321. }
  322. /**
  323. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  324. */
  325. public function checkAllowedAdd ($isPerformAction = false)
  326. {
  327. // check ACL
  328. $aCheck = checkActionModule($this->_iProfileId, 'create entry', $this->getName(), $isPerformAction);
  329. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  330. return $aCheck[CHECK_ACTION_MESSAGE];
  331. return CHECK_ACTION_RESULT_ALLOWED;
  332. }
  333. /**
  334. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  335. */
  336. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  337. {
  338. // moderator and owner always have access
  339. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_isModerator($isPerformAction))
  340. return CHECK_ACTION_RESULT_ALLOWED;
  341. return _t('_sys_txt_access_denied');
  342. }
  343. /**
  344. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  345. */
  346. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  347. {
  348. // moderator always has access
  349. if ($this->_isModerator($isPerformAction))
  350. return CHECK_ACTION_RESULT_ALLOWED;
  351. // check ACL
  352. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  353. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  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 checkAllowedSetThumb ()
  361. {
  362. // check ACL
  363. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  364. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  365. return $aCheck[CHECK_ACTION_MESSAGE];
  366. return CHECK_ACTION_RESULT_ALLOWED;
  367. }
  368. // ====== PROTECTED METHODS
  369. protected function _isModerator ($isPerformAction = false)
  370. {
  371. // check moderator ACL
  372. $aCheck = checkActionModule($this->_iProfileId, 'edit any entry', $this->getName(), $isPerformAction);
  373. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  374. }
  375. protected function _serviceBrowse ($sMode, $aParams = false)
  376. {
  377. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->checkAllowedBrowse()))
  378. return MsgBox($sMsg);
  379. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  380. bx_import('SearchResult', $this->_aModule);
  381. $o = new $sClass($sMode, $aParams);
  382. $o->setDisplayEmptyMsg(true);
  383. if ($o->isError)
  384. return '';
  385. if ($s = $o->processing())
  386. return $s;
  387. else
  388. return '';
  389. }
  390. protected function _serviceEntityForm ($sFormMethod, $iContentId = 0)
  391. {
  392. if (!$iContentId)
  393. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  394. if (!$iContentId)
  395. return false;
  396. bx_import('FormsEntryHelper', $this->_aModule);
  397. $sClass = $this->_aModule['class_prefix'] . 'FormsEntryHelper';
  398. $oFormsHelper = new $sClass($this);
  399. return $oFormsHelper->$sFormMethod((int)$iContentId);
  400. }
  401. }
  402. /** @} */