BxBaseModTextModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseText Base classes for text modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Base module class for text based modules
  13. */
  14. class BxBaseModTextModule extends BxBaseModGeneralModule implements iBxDolContentInfoService
  15. {
  16. function __construct(&$aModule)
  17. {
  18. parent::__construct($aModule);
  19. }
  20. // ====== ACTIONS METHODS
  21. public function actionGetPoll()
  22. {
  23. $iPollId = (int)bx_get('poll_id');
  24. $sView = bx_process_input(bx_get('view'));
  25. $sMethod = 'serviceGetBlockPoll' . bx_gen_method_name($sView);
  26. if(!method_exists($this, $sMethod))
  27. return echoJson(array());
  28. $aBlock = $this->$sMethod($iPollId, true);
  29. if(empty($aBlock) || !is_array($aBlock))
  30. return echoJson(array());
  31. return echoJson(array(
  32. 'content' => $aBlock['content']
  33. ));
  34. }
  35. public function actionDeletePoll()
  36. {
  37. $CNF = &$this->_oConfig->CNF;
  38. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  39. if(empty($iId))
  40. return echoJson(array());
  41. $aResult = array();
  42. if($this->_oDb->deletePolls(array($CNF['FIELD_POLL_ID'] => $iId)))
  43. $aResult = array('code' => 0);
  44. else
  45. $aResult = array('code' => 1, 'message' => _t($CNF['txt_err_cannot_perform_action']));
  46. echoJson($aResult);
  47. }
  48. public function actionGetPollForm()
  49. {
  50. echo $this->_oTemplate->getPollForm();
  51. }
  52. public function actionSubmitPollForm()
  53. {
  54. echoJson($this->getPollForm());
  55. }
  56. // ====== SERVICE METHODS
  57. public function serviceGetBlockPollAnswers($iPollId, $bForceDisplay = false)
  58. {
  59. if(!$iPollId)
  60. return false;
  61. if(!$bForceDisplay && $this->_oDb->isPollPerformed($iPollId, bx_get_logged_profile_id()))
  62. return $this->serviceGetBlockPollResults($iPollId);
  63. return $this->_serviceTemplateFunc('entryPollAnswers', $iPollId, 'getPollInfoById');
  64. }
  65. public function serviceGetBlockPollResults($iPollId)
  66. {
  67. return $this->_serviceTemplateFunc('entryPollResults', $iPollId, 'getPollInfoById');
  68. }
  69. public function serviceGetThumb ($iContentId, $sTranscoder = '')
  70. {
  71. $CNF = &$this->_oConfig->CNF;
  72. if(empty($sTranscoder) && !empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY']))
  73. $sTranscoder = $CNF['OBJECT_IMAGES_TRANSCODER_GALLERY'];
  74. $mixedResult = $this->_getFieldValueThumb('FIELD_THUMB', $iContentId, $sTranscoder);
  75. return $mixedResult !== false ? $mixedResult : '';
  76. }
  77. public function serviceGetMenuAddonManageTools()
  78. {
  79. bx_import('SearchResult', $this->_aModule);
  80. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  81. $o = new $sClass();
  82. $o->fillFilters(array(
  83. 'status' => 'hidden'
  84. ));
  85. $o->unsetPaginate();
  86. $iNum1 = $o->getNum();
  87. $iNum2 = 0;
  88. $CNF = &$this->_oConfig->CNF;
  89. if (isset($CNF['OBJECT_REPORTS'])){
  90. $o->fillFilters(array('status' => ''));
  91. $o->fillFiltersByObjects(array('reported' => array('value' => '0', 'field' => 'reports', 'operator' => '>')));
  92. $iNum2 = $o->getNum();
  93. }
  94. return array('counter1_value' => $iNum1, 'counter2_value' => $iNum2);
  95. }
  96. public function serviceGetMenuAddonManageToolsProfileStats()
  97. {
  98. bx_import('SearchResult', $this->_aModule);
  99. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  100. $o = new $sClass();
  101. $o->fillFilters(array(
  102. 'author' => bx_get_logged_profile_id()
  103. ));
  104. $o->unsetPaginate();
  105. return $o->getNum();
  106. }
  107. /**
  108. * Display public entries
  109. * @return HTML string
  110. */
  111. public function serviceBrowsePublic ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  112. {
  113. return $this->_serviceBrowse ('public', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  114. }
  115. /**
  116. * Display popular entries
  117. * @return HTML string
  118. */
  119. public function serviceBrowsePopular ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  120. {
  121. return $this->_serviceBrowse ('popular', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  122. }
  123. /**
  124. * Display recently updated entries
  125. * @return HTML string
  126. */
  127. public function serviceBrowseUpdated ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  128. {
  129. return $this->_serviceBrowse ('updated', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  130. }
  131. /**
  132. * Display entries of the author
  133. * @return HTML string
  134. */
  135. public function serviceBrowseAuthor ($iProfileId = 0, $aParams = array())
  136. {
  137. return $this->_serviceBrowseWithParam ('author', 'profile_id', $iProfileId, $aParams);
  138. }
  139. /**
  140. * Display entries posted into particular context
  141. * @return HTML string
  142. */
  143. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  144. {
  145. return $this->_serviceBrowseWithParam ('context', 'profile_id', $iProfileId, $aParams);
  146. }
  147. public function _serviceBrowseWithParam ($sParamName, $sParamGet, $sParamVal, $aParams = array())
  148. {
  149. if (!$sParamVal)
  150. $sParamVal = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  151. if (!$sParamVal)
  152. return '';
  153. $bEmptyMessage = true;
  154. if(isset($aParams['empty_message'])) {
  155. $bEmptyMessage = (bool)$aParams['empty_message'];
  156. unset($aParams['empty_message']);
  157. }
  158. return $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $sParamVal), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage);
  159. }
  160. /**
  161. * Entry author block
  162. */
  163. public function serviceEntityAuthor ($iContentId = 0)
  164. {
  165. return $this->_serviceTemplateFunc ('entryAuthor', $iContentId);
  166. }
  167. /**
  168. * Entry context block
  169. */
  170. public function serviceEntityContext ($iContentId = 0)
  171. {
  172. return $this->_serviceTemplateFunc ('entryContext', $iContentId);
  173. }
  174. /**
  175. * Entry polls block
  176. */
  177. public function serviceEntityPolls ($iContentId = 0)
  178. {
  179. return $this->_serviceTemplateFunc ('entryPolls', $iContentId);
  180. }
  181. public function serviceEntityBreadcrumb ($iContentId = 0)
  182. {
  183. if (!$iContentId)
  184. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  185. if (!$iContentId)
  186. return false;
  187. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  188. if (!$aContentInfo)
  189. return false;
  190. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  191. }
  192. /**
  193. * Delete all content by profile
  194. * @param $iProfileId profile id
  195. * @return number of deleted items
  196. */
  197. public function serviceDeleteEntitiesByAuthor ($iProfileId)
  198. {
  199. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  200. if (!$a)
  201. return 0;
  202. $iCount = 0;
  203. foreach ($a as $aContentInfo)
  204. $iCount += ('' == $this->serviceDeleteEntity($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  205. return $iCount;
  206. }
  207. // ====== PERMISSION METHODS
  208. /**
  209. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  210. */
  211. public function checkAllowedSetThumb ($iContentId = 0)
  212. {
  213. // check ACL
  214. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  215. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  216. return $aCheck[CHECK_ACTION_MESSAGE];
  217. return CHECK_ACTION_RESULT_ALLOWED;
  218. }
  219. // ====== COMMON METHODS
  220. public function isEntryActive($aContentInfo)
  221. {
  222. $CNF = &$this->_oConfig->CNF;
  223. if($aContentInfo[$CNF['FIELD_AUTHOR']] == bx_get_logged_profile_id() || $this->_isModerator())
  224. return true;
  225. if(isset($CNF['FIELD_STATUS']) && $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  226. return false;
  227. if(isset($CNF['FIELD_STATUS_ADMIN']) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  228. return false;
  229. return true;
  230. }
  231. public function getPollForm()
  232. {
  233. $CNF = &$this->_oConfig->CNF;
  234. if(empty($CNF['OBJECT_FORM_POLL']))
  235. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  236. $iProfileId = bx_get_logged_profile_id();
  237. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_POLL'], $CNF['OBJECT_FORM_POLL_DISPLAY_ADD'], $this->_oTemplate);
  238. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'submit_poll_form/';
  239. $oForm->initChecker();
  240. if($oForm->isSubmittedAndValid()) {
  241. $iId = $oForm->insert();
  242. if($iId)
  243. return array('code' => 0, 'id' => $iId, 'item' => $this->_oTemplate->getPollItem($iId, $iProfileId, array(
  244. 'manage' => true
  245. )));
  246. else
  247. return array('code' => 2, 'message' => '_sys_txt_error_entry_creation');
  248. }
  249. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  250. }
  251. // ====== PROTECTED METHODS
  252. protected function _getImagesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  253. {
  254. $CNF = &$this->_oConfig->CNF;
  255. $aResults = parent::_getImagesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  256. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_PHOTOS']) || empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_PHOTOS']))
  257. return $aResults;
  258. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  259. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_PHOTOS']);
  260. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  261. if(!$aGhostFiles)
  262. return array();
  263. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_PHOTOS']);
  264. foreach ($aGhostFiles as $k => $a) {
  265. $sPhotoSrc = $oTranscoder->getFileUrl($a['id']);
  266. if(empty($sPhotoSrc))
  267. continue;
  268. $aResults[] = array(
  269. 'src' => $sPhotoSrc,
  270. 'src_medium' => $sPhotoSrc,
  271. 'src_orig' => $oStorage->getFileUrlById($a['id']),
  272. );
  273. }
  274. return $aResults;
  275. }
  276. protected function _getVideosForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  277. {
  278. $CNF = &$this->_oConfig->CNF;
  279. $aResults = parent::_getVideosForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  280. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_VIDEOS']) || empty($CNF['OBJECT_VIDEOS_TRANSCODERS']))
  281. return $aResults;
  282. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  283. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_VIDEOS']);
  284. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  285. if(!$aGhostFiles)
  286. return array();
  287. $oTcPoster = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']);
  288. $oTcMp4 = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  289. $oTcWebm = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['webm']);
  290. if(!$oTcPoster || !$oTcMp4 || !$oTcWebm)
  291. return array();
  292. $aResults = array();
  293. foreach ($aGhostFiles as $k => $a)
  294. $aResults[$a['id']] = array(
  295. 'id' => $a['id'],
  296. 'src_poster' => $oTcPoster->getFileUrl($a['id']),
  297. 'src_mp4' => $oTcMp4->getFileUrl($a['id']),
  298. 'src_webm' => $oTcWebm->getFileUrl($a['id']),
  299. );
  300. return $aResults;
  301. }
  302. protected function _getFilesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  303. {
  304. $CNF = &$this->_oConfig->CNF;
  305. $aResults = parent::_getFilesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  306. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_FILES']))
  307. return $aResults;
  308. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  309. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  310. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  311. if(!$aGhostFiles)
  312. return array();
  313. $oTranscoder = null;
  314. if(!empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_FILES']))
  315. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_FILES']);
  316. foreach ($aGhostFiles as $k => $a) {
  317. $bImage = in_array($a['ext'], array('jpg', 'jpeg', 'png', 'gif'));
  318. $sPhotoSrc = '';
  319. if($bImage && $oTranscoder)
  320. $sPhotoSrc = $oTranscoder->getFileUrl($a['id']);
  321. if(empty($sPhotoSrc) && $oStorage)
  322. $sPhotoSrc = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($a['file_name']));
  323. $sUrl = $oStorage->getFileUrlById($a['id']);
  324. $aResults[] = array(
  325. 'src' => $sPhotoSrc,
  326. 'src_medium' => $sPhotoSrc,
  327. 'src_orig' => $bImage ? $sUrl : '',
  328. 'url' => !$bImage ? $sUrl : ''
  329. );
  330. }
  331. return $aResults;
  332. }
  333. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  334. {
  335. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  336. if(!$this->_oConfig->isAttachmentsInTimeline())
  337. return $aResult;
  338. $CNF = &$this->_oConfig->CNF;
  339. $iProfile = bx_get_logged_profile_id();
  340. $bDynamic = isset($aBrowseParams['dynamic_mode']) && $aBrowseParams['dynamic_mode'] === true;
  341. $sPolls = '';
  342. $aPolls = $this->_oDb->getPolls(array('type' => 'content_id', 'content_id' => (int)$aContentInfo[$CNF['FIELD_ID']]));
  343. if(!empty($aPolls) && is_array($aPolls)) {
  344. foreach($aPolls as $aPoll) {
  345. $sPoll = $this->_oTemplate->getPollItem($aPoll, $iProfile, array(
  346. 'dynamic' => $bDynamic,
  347. 'switch_menu' => false
  348. ));
  349. if(empty($sPoll))
  350. continue;
  351. $sPolls .= $sPoll;
  352. }
  353. if(!empty($sPolls)) {
  354. $sInclude = '';
  355. $sInclude .= $this->_oTemplate->addJs(array('polls.js'), $bDynamic);
  356. $sInclude .= $this->_oTemplate->addCss(array('polls.css'), $bDynamic);
  357. $aResult['raw'] = ($bDynamic ? $sInclude : '') . $this->_oTemplate->getJsCode('poll') . $this->_oTemplate->parseHtmlByName('poll_items_showcase.html', array(
  358. 'js_object' => $this->_oConfig->getJsObject('poll'),
  359. 'html_id' => $this->_oConfig->getHtmlIds('polls_showcase') . $aEvent['id'],
  360. 'polls' => $sPolls
  361. ));
  362. }
  363. }
  364. return $aResult;
  365. }
  366. protected function _buildRssParams($sMode, $aArgs)
  367. {
  368. $aParams = array ();
  369. $sMode = bx_process_input($sMode);
  370. switch ($sMode) {
  371. case 'author':
  372. $aParams = array('author' => isset($aArgs[0]) ? (int)$aArgs[0] : '');
  373. break;
  374. }
  375. return $aParams;
  376. }
  377. }
  378. /** @} */