BxBaseModTextModule.php 17 KB

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