BxBaseModTextModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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->unsetPaginate();
  83. $iNumTotal = $o->getNum();
  84. $o->fillFilters(array(
  85. 'status' => 'hidden'
  86. ));
  87. $iNum1 = $o->getNum();
  88. $iNum2 = 0;
  89. $CNF = &$this->_oConfig->CNF;
  90. if (isset($CNF['OBJECT_REPORTS'])){
  91. $o->fillFilters(array('status' => ''));
  92. $o->fillFiltersByObjects(array('reported' => array('value' => '0', 'field' => 'reports', 'operator' => '>')));
  93. $iNum2 = $o->getNum();
  94. }
  95. return array('counter1_value' => $iNum1, 'counter2_value' => $iNum2, 'counter3_value' => $iNumTotal );
  96. }
  97. public function serviceGetMenuAddonManageToolsProfileStats()
  98. {
  99. bx_import('SearchResult', $this->_aModule);
  100. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  101. $o = new $sClass();
  102. $o->fillFilters(array(
  103. 'author' => bx_get_logged_profile_id()
  104. ));
  105. $o->unsetPaginate();
  106. return $o->getNum();
  107. }
  108. /**
  109. * Display public entries
  110. * @return HTML string
  111. */
  112. public function serviceBrowsePublic ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  113. {
  114. return $this->_serviceBrowse ('public', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  115. }
  116. /**
  117. * Display popular entries
  118. * @return HTML string
  119. */
  120. public function serviceBrowsePopular ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  121. {
  122. return $this->_serviceBrowse ('popular', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  123. }
  124. /**
  125. * Display recently updated entries
  126. * @return HTML string
  127. */
  128. public function serviceBrowseUpdated ($sUnitView = false, $bEmptyMessage = true, $bAjaxPaginate = true)
  129. {
  130. return $this->_serviceBrowse ('updated', $sUnitView ? array('unit_view' => $sUnitView) : false, BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  131. }
  132. /**
  133. * Display entries of the author
  134. * @return HTML string
  135. */
  136. public function serviceBrowseAuthor ($iProfileId = 0, $aParams = array())
  137. {
  138. return $this->_serviceBrowseWithParam ('author', 'profile_id', $iProfileId, $aParams);
  139. }
  140. /**
  141. * Display entries posted into particular context
  142. * @return HTML string
  143. */
  144. public function serviceBrowseContext ($iProfileId = 0, $aParams = array())
  145. {
  146. return $this->_serviceBrowseWithParam ('context', 'profile_id', $iProfileId, $aParams);
  147. }
  148. public function _serviceBrowseWithParam ($sParamName, $sParamGet, $sParamVal, $aParams = array())
  149. {
  150. if(!$sParamVal)
  151. $sParamVal = bx_process_input(bx_get($sParamGet), BX_DATA_INT);
  152. if(!$sParamVal)
  153. return '';
  154. $bEmptyMessage = true;
  155. if(isset($aParams['empty_message'])) {
  156. $bEmptyMessage = (bool)$aParams['empty_message'];
  157. unset($aParams['empty_message']);
  158. }
  159. $bAjaxPaginate = true;
  160. if(isset($aParams['ajax_paginate'])) {
  161. $bAjaxPaginate = (bool)$aParams['ajax_paginate'];
  162. unset($aParams['ajax_paginate']);
  163. }
  164. return $this->_serviceBrowse ($sParamName, array_merge(array($sParamName => $sParamVal), $aParams), BX_DB_PADDING_DEF, $bEmptyMessage, $bAjaxPaginate);
  165. }
  166. /**
  167. * Entry author block
  168. */
  169. public function serviceEntityAuthor ($iContentId = 0)
  170. {
  171. return $this->_serviceTemplateFunc ('entryAuthor', $iContentId);
  172. }
  173. /**
  174. * Entry context block
  175. */
  176. public function serviceEntityContext ($iContentId = 0)
  177. {
  178. return $this->_serviceTemplateFunc ('entryContext', $iContentId);
  179. }
  180. /**
  181. * Entry polls block
  182. */
  183. public function serviceEntityPolls ($iContentId = 0)
  184. {
  185. return $this->_serviceTemplateFunc ('entryPolls', $iContentId);
  186. }
  187. public function serviceEntityBreadcrumb ($iContentId = 0)
  188. {
  189. if (!$iContentId)
  190. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  191. if (!$iContentId)
  192. return false;
  193. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  194. if (!$aContentInfo)
  195. return false;
  196. return $this->_oTemplate->entryBreadcrumb($aContentInfo);
  197. }
  198. /**
  199. * Delete all content by profile
  200. * @param $iProfileId profile id
  201. * @return number of deleted items
  202. */
  203. public function serviceDeleteEntitiesByAuthor ($iProfileId)
  204. {
  205. $a = $this->_oDb->getEntriesByAuthor((int)$iProfileId);
  206. if (!$a)
  207. return 0;
  208. $iCount = 0;
  209. foreach ($a as $aContentInfo)
  210. $iCount += ('' == $this->serviceDeleteEntity($aContentInfo[$this->_oConfig->CNF['FIELD_ID']]) ? 1 : 0);
  211. return $iCount;
  212. }
  213. // ====== PERMISSION METHODS
  214. /**
  215. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make strict(===) checking.
  216. */
  217. public function checkAllowedSetThumb ($iContentId = 0)
  218. {
  219. // check ACL
  220. $aCheck = checkActionModule($this->_iProfileId, 'set thumb', $this->getName(), false);
  221. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  222. return $aCheck[CHECK_ACTION_MESSAGE];
  223. return CHECK_ACTION_RESULT_ALLOWED;
  224. }
  225. // ====== COMMON METHODS
  226. public function isEntryActive($aContentInfo)
  227. {
  228. $CNF = &$this->_oConfig->CNF;
  229. if($aContentInfo[$CNF['FIELD_AUTHOR']] == bx_get_logged_profile_id() || $this->_isModerator())
  230. return true;
  231. if(isset($CNF['FIELD_STATUS']) && $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  232. return false;
  233. if(isset($CNF['FIELD_STATUS_ADMIN']) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  234. return false;
  235. return true;
  236. }
  237. public function getPollForm()
  238. {
  239. $CNF = &$this->_oConfig->CNF;
  240. if(empty($CNF['OBJECT_FORM_POLL']))
  241. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  242. $iProfileId = bx_get_logged_profile_id();
  243. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_POLL'], $CNF['OBJECT_FORM_POLL_DISPLAY_ADD'], $this->_oTemplate);
  244. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'submit_poll_form/';
  245. $oForm->initChecker();
  246. if($oForm->isSubmittedAndValid()) {
  247. $iId = $oForm->insert();
  248. if($iId)
  249. return array('code' => 0, 'id' => $iId, 'item' => $this->_oTemplate->getPollItem($iId, $iProfileId, array(
  250. 'manage' => true
  251. )));
  252. else
  253. return array('code' => 2, 'message' => '_sys_txt_error_entry_creation');
  254. }
  255. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  256. }
  257. // ====== PROTECTED METHODS
  258. protected function _getImagesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  259. {
  260. $CNF = &$this->_oConfig->CNF;
  261. $aResults = parent::_getImagesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  262. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_PHOTOS']) || empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_PHOTOS']))
  263. return $aResults;
  264. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  265. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_PHOTOS']);
  266. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  267. if(!$aGhostFiles)
  268. return array();
  269. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_PHOTOS']);
  270. foreach ($aGhostFiles as $k => $a) {
  271. $sPhotoSrc = $oTranscoder->getFileUrl($a['id']);
  272. if(empty($sPhotoSrc))
  273. continue;
  274. $aResults[] = array(
  275. 'src' => $sPhotoSrc,
  276. 'src_medium' => $sPhotoSrc,
  277. 'src_orig' => $oStorage->getFileUrlById($a['id']),
  278. );
  279. }
  280. return $aResults;
  281. }
  282. protected function _getVideosForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  283. {
  284. $CNF = &$this->_oConfig->CNF;
  285. $aResults = parent::_getVideosForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  286. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_VIDEOS']) || empty($CNF['OBJECT_VIDEOS_TRANSCODERS']))
  287. return $aResults;
  288. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  289. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_VIDEOS']);
  290. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  291. if(!$aGhostFiles)
  292. return array();
  293. $oTcPoster = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']);
  294. $oTcMp4 = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  295. $oTcMp4Hd = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']);
  296. if(!$oTcPoster || !$oTcMp4 || !$oTcMp4Hd)
  297. return array();
  298. $aResults = array();
  299. foreach ($aGhostFiles as $k => $a)
  300. $aResults[$a['id']] = array(
  301. 'id' => $a['id'],
  302. 'src_poster' => $oTcPoster->getFileUrl($a['id']),
  303. 'src_mp4' => $oTcMp4->getFileUrl($a['id']),
  304. 'src_mp4_hd' => $oTcMp4Hd->getFileUrl($a['id']),
  305. );
  306. return $aResults;
  307. }
  308. protected function _getFilesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  309. {
  310. $CNF = &$this->_oConfig->CNF;
  311. $aResults = parent::_getFilesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  312. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_FILES']))
  313. return $aResults;
  314. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  315. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  316. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  317. if(!$aGhostFiles)
  318. return array();
  319. $oTranscoder = null;
  320. if(!empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_FILES']))
  321. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_FILES']);
  322. foreach ($aGhostFiles as $k => $a) {
  323. $bImage = in_array($a['ext'], array('jpg', 'jpeg', 'png', 'gif'));
  324. $sPhotoSrc = '';
  325. if($bImage && $oTranscoder)
  326. $sPhotoSrc = $oTranscoder->getFileUrl($a['id']);
  327. if(empty($sPhotoSrc) && $oStorage)
  328. $sPhotoSrc = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($a['file_name']));
  329. $sUrl = $oStorage->getFileUrlById($a['id']);
  330. $aResults[] = array(
  331. 'src' => $sPhotoSrc,
  332. 'src_medium' => $sPhotoSrc,
  333. 'src_orig' => $bImage ? $sUrl : '',
  334. 'url' => !$bImage ? $sUrl : ''
  335. );
  336. }
  337. return $aResults;
  338. }
  339. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  340. {
  341. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  342. if(!$this->_oConfig->isAttachmentsInTimeline())
  343. return $aResult;
  344. $CNF = &$this->_oConfig->CNF;
  345. $iProfile = bx_get_logged_profile_id();
  346. $bDynamic = isset($aBrowseParams['dynamic_mode']) && $aBrowseParams['dynamic_mode'] === true;
  347. $sPolls = '';
  348. $aPolls = $this->_oDb->getPolls(array('type' => 'content_id', 'content_id' => (int)$aContentInfo[$CNF['FIELD_ID']]));
  349. if(!empty($aPolls) && is_array($aPolls)) {
  350. foreach($aPolls as $aPoll) {
  351. $sPoll = $this->_oTemplate->getPollItem($aPoll, $iProfile, array(
  352. 'dynamic' => $bDynamic,
  353. 'switch_menu' => false
  354. ));
  355. if(empty($sPoll))
  356. continue;
  357. $sPolls .= $sPoll;
  358. }
  359. if(!empty($sPolls)) {
  360. $sInclude = '';
  361. $sInclude .= $this->_oTemplate->addJs(array('polls.js'), $bDynamic);
  362. $sInclude .= $this->_oTemplate->addCss(array('polls.css'), $bDynamic);
  363. $aResult['raw'] = ($bDynamic ? $sInclude : '') . $this->_oTemplate->getJsCode('poll') . $this->_oTemplate->parseHtmlByName('poll_items_showcase.html', array(
  364. 'js_object' => $this->_oConfig->getJsObject('poll'),
  365. 'html_id' => $this->_oConfig->getHtmlIds('polls_showcase') . $aEvent['id'],
  366. 'polls' => $sPolls
  367. ));
  368. }
  369. }
  370. return $aResult;
  371. }
  372. protected function _buildRssParams($sMode, $aArgs)
  373. {
  374. $aParams = array ();
  375. $sMode = bx_process_input($sMode);
  376. switch ($sMode) {
  377. case 'author':
  378. $aParams = array('author' => isset($aArgs[0]) ? (int)$aArgs[0] : '');
  379. break;
  380. }
  381. return $aParams;
  382. }
  383. }
  384. /** @} */