1
0

BxNotesModule.php 16 KB

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