BxNotesModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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('FormsEntryHelper', $this->_aModule);
  50. $oProfileForms = new BxNotesFormsEntryHelper($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('FormsEntryHelper', $this->_aModule);
  62. $oProfileForms = new BxNotesFormsEntryHelper($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('FormsEntryHelper', $this->_aModule);
  74. $oProfileForms = new BxNotesFormsEntryHelper($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('FormsEntryHelper', $this->_aModule);
  86. $oProfileForms = new BxNotesFormsEntryHelper($this);
  87. return $oProfileForms->viewDataEntry((int)$iContentId);
  88. }
  89. public function serviceEntityInfo ($iContentId = 0) {
  90. if (!$iContentId)
  91. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  92. if (!$iContentId)
  93. return false;
  94. bx_import('FormsEntryHelper', $this->_aModule);
  95. $oProfileForms = new BxNotesFormsEntryHelper($this);
  96. return $oProfileForms->viewDataForm((int)$iContentId);
  97. }
  98. public function serviceEntitySocialSharing ($iContentId = 0) {
  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=view-note&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(), 'bx_notes', 'added', $aContentInfo[$CNF['FIELD_ID']], array('show_do_share_as_button' => true)));
  129. bx_import('BxTemplSocialSharing');
  130. $sSocial = BxTemplSocialSharing::getInstance()->getCode($iContentId, 'bx_notes', 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. public function serviceEntityComments ($iContentId = 0) {
  139. if (!$iContentId)
  140. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  141. if (!$iContentId)
  142. return false;
  143. bx_import('BxDolCmts');
  144. $oCmts = BxDolCmts::getObjectInstance($this->_oConfig->CNF['OBJECT_COMMENTS'], $iContentId);
  145. if(!$oCmts->isEnabled())
  146. return false;
  147. return $oCmts->getCommentsBlock(0, 0, false);
  148. }
  149. public function serviceEntityAuthor ($iContentId = 0) {
  150. if (!$iContentId)
  151. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  152. if (!$iContentId)
  153. return false;
  154. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  155. if (!$aContentInfo)
  156. return false;
  157. $oProfile = BxDolProfile::getInstance($aContentInfo[$this->_oConfig->CNF['FIELD_AUTHOR']]);
  158. if (!$oProfile) {
  159. bx_import('BxDolProfileUndefined');
  160. $oProfile = BxDolProfileUndefined::getInstance();
  161. }
  162. return $this->_oTemplate->entryAuthor ($aContentInfo, $oProfile);
  163. }
  164. public function serviceEntityActions ($iContentId = 0) {
  165. if (!$iContentId)
  166. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  167. if (!$iContentId)
  168. return false;
  169. bx_import('BxTemplMenu');
  170. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY']);
  171. return $oMenu ? $oMenu->getCode() : false;
  172. }
  173. public function serviceMyEntriesActions ($iProfileId = 0) {
  174. if (!$iProfileId)
  175. $iProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  176. if (!$iProfileId || $iProfileId != $this->_iProfileId)
  177. return false;
  178. bx_import('BxTemplMenu');
  179. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_MY_ENTRIES']);
  180. return $oMenu ? $oMenu->getCode() : false;
  181. }
  182. public function serviceGetTimelineData()
  183. {
  184. return array(
  185. 'handlers' => array(
  186. 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' => ''),
  187. array('type' => 'update', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'edited'),
  188. array('type' => 'delete', 'alert_unit' => $this->_aModule['name'], 'alert_action' => 'deleted')
  189. ),
  190. 'alerts' => array(
  191. array('unit' => $this->_aModule['name'], 'action' => 'added'),
  192. array('unit' => $this->_aModule['name'], 'action' => 'edited'),
  193. array('unit' => $this->_aModule['name'], 'action' => 'deleted'),
  194. )
  195. );
  196. }
  197. public function serviceGetTimelinePost($aEvent)
  198. {
  199. $aContentInfo = $this->_oDb->getContentInfoById($aEvent['object_id']);
  200. if(empty($aContentInfo) || !is_array($aContentInfo))
  201. return '';
  202. $CNF = &$this->_oConfig->CNF;
  203. bx_import('BxDolPermalinks');
  204. $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-note&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  205. //--- Image(s)
  206. $sImage = '';
  207. if($aContentInfo[$CNF['FIELD_THUMB']]) {
  208. bx_import('BxDolStorage');
  209. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  210. if($oStorage)
  211. $sImage = $oStorage->getFileUrlById($aContentInfo[$CNF['FIELD_THUMB']]);
  212. }
  213. //--- Votes
  214. bx_import('BxDolVote');
  215. $oVotes = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $aEvent['object_id']);
  216. $aVotes = array();
  217. if ($oVotes && $oVotes->isEnabled())
  218. $aVotes = array(
  219. 'system' => $CNF['OBJECT_VOTES'],
  220. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  221. 'count' => $aContentInfo['votes']
  222. );
  223. //--- Comments
  224. bx_import('BxDolCmts');
  225. $oCmts = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $aEvent['object_id']);
  226. $aComments = array();
  227. if($oCmts->isEnabled())
  228. $aComments = array(
  229. 'system' => $CNF['OBJECT_COMMENTS'],
  230. 'object_id' => $aContentInfo[$CNF['FIELD_ID']],
  231. 'count' => $aContentInfo['comments']
  232. );
  233. return array(
  234. 'owner_id' => $aContentInfo[$CNF['FIELD_AUTHOR']],
  235. 'content' => array(
  236. 'sample' => _t('_bx_notes_txt_sample_single'),
  237. 'url' => $sUrl,
  238. 'title' => $aContentInfo[$CNF['FIELD_TITLE']],
  239. 'text' => $aContentInfo[$CNF['FIELD_TEXT']],
  240. 'images' => array(
  241. array(
  242. 'url' => $sUrl,
  243. 'src' => $sImage
  244. )
  245. )
  246. ), //a string to display or array to parse default template before displaying.
  247. 'votes' => $aVotes,
  248. 'comments' => $aComments,
  249. 'title' => '', //may be empty.
  250. 'description' => '' //may be empty.
  251. );
  252. }
  253. // ====== ACTION METHODS
  254. function actionBrowse ($sMode = '') {
  255. $sMode = bx_process_input($sMode);
  256. /*
  257. if ('user' == $sMode || 'my' == $sMode) {
  258. $aProfile = getProfileInfo ($this->_iProfileId);
  259. if (0 == strcasecmp($sValue, $aProfile['NickName']) || 'my' == $sMode) {
  260. $this->_browseMy ($aProfile);
  261. return;
  262. }
  263. }
  264. if ('tag' == $sMode || 'category' == $sMode)
  265. $sValue = uri2title($sValue);
  266. */
  267. if (CHECK_ACTION_RESULT_ALLOWED !== $this->isAllowedBrowse()) {
  268. $this->_oTemplate->displayAccessDenied ();
  269. return;
  270. }
  271. bx_import ('SearchResult', $this->_aModule);
  272. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  273. $o = new $sClass($sMode);
  274. if ($o->isError) {
  275. $this->_oTemplate->displayPageNotFound ();
  276. return;
  277. }
  278. if (bx_get('rss')) {
  279. echo $o->rss();
  280. exit;
  281. }
  282. if (!($s = $o->processing())) {
  283. $this->_oTemplate->displayNoData ();
  284. return;
  285. }
  286. // TODO: remake to use "pages"
  287. $this->_oTemplate->addCss ('main.css');
  288. $oTemplate = BxDolTemplate::getInstance();
  289. $oTemplate->setPageHeader ($o->aCurrent['title']);
  290. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  291. $oTemplate->setPageContent ('page_main_code', $s);
  292. $oTemplate->getPageCode();
  293. }
  294. // ====== PERMISSION METHODS
  295. function _checkModeratorAccess ($isPerformAction = false) {
  296. // check moderator ACnoteL
  297. $aCheck = checkActionModule($this->_iProfileId, 'edit any note', $this->getName(), $isPerformAction);
  298. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  299. }
  300. /**
  301. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  302. */
  303. function isAllowedView ($aDataEntry, $isPerformAction = false) {
  304. $CNF = &$this->_oConfig->CNF;
  305. // moderator and owner always have access
  306. if ($aDataEntry[$CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_checkModeratorAccess($isPerformAction))
  307. return CHECK_ACTION_RESULT_ALLOWED;
  308. // check ACL
  309. $aCheck = checkActionModule($this->_iProfileId, 'view note', $this->getName(), $isPerformAction);
  310. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  311. return $aCheck[CHECK_ACTION_MESSAGE];
  312. // check privacy
  313. bx_import('BxDolPrivacy');
  314. $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW']);
  315. if ($oPrivacy && !$oPrivacy->check($aDataEntry[$CNF['FIELD_ID']]))
  316. return _t('_sys_access_denied_to_private_content');
  317. return CHECK_ACTION_RESULT_ALLOWED;
  318. }
  319. /**
  320. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  321. */
  322. function isAllowedBrowse () {
  323. return CHECK_ACTION_RESULT_ALLOWED;
  324. }
  325. /**
  326. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  327. */
  328. function isAllowedAdd ($isPerformAction = false) {
  329. // check ACL
  330. $aCheck = checkActionModule($this->_iProfileId, 'create note', $this->getName(), $isPerformAction);
  331. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  332. return $aCheck[CHECK_ACTION_MESSAGE];
  333. return CHECK_ACTION_RESULT_ALLOWED;
  334. }
  335. /**
  336. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  337. */
  338. function isAllowedEdit ($aDataEntry, $isPerformAction = false) {
  339. // moderator and owner always have access
  340. if ($aDataEntry[$this->_oConfig->CNF['FIELD_AUTHOR']] == $this->_iProfileId || $this->_checkModeratorAccess($isPerformAction))
  341. return CHECK_ACTION_RESULT_ALLOWED;
  342. return _t('_sys_txt_access_denied');
  343. }
  344. /**
  345. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  346. */
  347. function isAllowedDelete (&$aDataEntry, $isPerformAction = false) {
  348. // moderator always has access
  349. if ($this->_checkModeratorAccess($isPerformAction))
  350. return CHECK_ACTION_RESULT_ALLOWED;
  351. // check ACL
  352. $aCheck = checkActionModule($this->_iProfileId, 'delete note', $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.
  359. */
  360. function isAllowedSetThumb () {
  361. // check ACL
  362. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  363. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  364. return $aCheck[CHECK_ACTION_MESSAGE];
  365. return CHECK_ACTION_RESULT_ALLOWED;
  366. }
  367. }
  368. /** @} */