BxNotesModule.php 16 KB

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