BxBaseModTextModule.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 = $this->_alertParams($aContentInfo);
  231. bx_alert($this->getName(), 'added', $iContentId, $aContentInfo[$CNF['FIELD_AUTHOR']], $aParams);
  232. }
  233. public function onFailed($iContentId)
  234. {
  235. parent::onFailed($iContentId);
  236. $CNF = &$this->_oConfig->CNF;
  237. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  238. if(!$aContentInfo)
  239. return;
  240. $aParams = array('object_author_id' => $aContentInfo[$CNF['FIELD_AUTHOR']]);
  241. bx_alert($this->getName(), 'failed', $iContentId, $aContentInfo[$CNF['FIELD_AUTHOR']], $aParams);
  242. }
  243. public function alertAfterAdd($aContentInfo)
  244. {
  245. $CNF = &$this->_oConfig->CNF;
  246. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  247. $iAuthorId = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  248. $sAction = 'added';
  249. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'awaiting')
  250. $sAction = 'deferred';
  251. $aParams = $this->_alertParams($aContentInfo);
  252. bx_alert($this->getName(), $sAction, $iId, $iAuthorId, $aParams);
  253. }
  254. public function alertAfterEdit($aContentInfo)
  255. {
  256. $CNF = &$this->_oConfig->CNF;
  257. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  258. $aParams = $this->_alertParams($aContentInfo);
  259. bx_alert($this->getName(), 'edited', $iId, false, $aParams);
  260. }
  261. /**
  262. * Get array of params to be passed in Add/Edit Alert.
  263. */
  264. protected function _alertParams($aContentInfo)
  265. {
  266. $CNF = &$this->_oConfig->CNF;
  267. $iId = (int)$aContentInfo[$CNF['FIELD_ID']];
  268. $iAuthorId = (int)$aContentInfo[$CNF['FIELD_AUTHOR']];
  269. $aParams = array(
  270. 'object_author_id' => $iAuthorId
  271. );
  272. if(isset($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]))
  273. $aParams['privacy_view'] = $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']];
  274. if(!empty($CNF['OBJECT_METATAGS']))
  275. $aParams['timeline_group'] = array(
  276. 'by' => $this->getName() . '_' . $iAuthorId . '_' . $iId,
  277. 'field' => 'owner_id'
  278. );
  279. return $aParams;
  280. }
  281. public function isEntryActive($aContentInfo)
  282. {
  283. $CNF = &$this->_oConfig->CNF;
  284. if($aContentInfo[$CNF['FIELD_AUTHOR']] == bx_get_logged_profile_id() || $this->_isModerator())
  285. return true;
  286. if(isset($CNF['FIELD_STATUS']) && $aContentInfo[$CNF['FIELD_STATUS']] != 'active')
  287. return false;
  288. if(isset($CNF['FIELD_STATUS_ADMIN']) && $aContentInfo[$CNF['FIELD_STATUS_ADMIN']] != 'active')
  289. return false;
  290. return true;
  291. }
  292. public function getPollForm()
  293. {
  294. $CNF = &$this->_oConfig->CNF;
  295. if(empty($CNF['OBJECT_FORM_POLL']))
  296. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  297. $iProfileId = bx_get_logged_profile_id();
  298. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_POLL'], $CNF['OBJECT_FORM_POLL_DISPLAY_ADD'], $this->_oTemplate);
  299. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'submit_poll_form/';
  300. $oForm->initChecker();
  301. if($oForm->isSubmittedAndValid()) {
  302. $iId = $oForm->insert();
  303. if($iId)
  304. return array('code' => 0, 'id' => $iId, 'item' => $this->_oTemplate->getPollItem($iId, $iProfileId, array(
  305. 'manage' => true
  306. )));
  307. else
  308. return array('code' => 2, 'message' => '_sys_txt_error_entry_creation');
  309. }
  310. return array('form' => $oForm->getCode(), 'form_id' => $oForm->id);
  311. }
  312. // ====== PROTECTED METHODS
  313. protected function _getImagesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  314. {
  315. $CNF = &$this->_oConfig->CNF;
  316. $aResults = parent::_getImagesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  317. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_PHOTOS']) || empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_PHOTOS']))
  318. return $aResults;
  319. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  320. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_PHOTOS']);
  321. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  322. if(!$aGhostFiles)
  323. return array();
  324. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_PHOTOS']);
  325. foreach ($aGhostFiles as $k => $a) {
  326. $sPhotoSrc = $oTranscoder->getFileUrl($a['id']);
  327. if(empty($sPhotoSrc))
  328. continue;
  329. $aResults[] = array(
  330. 'src' => $sPhotoSrc,
  331. 'src_medium' => $sPhotoSrc,
  332. 'src_orig' => $oStorage->getFileUrlById($a['id']),
  333. );
  334. }
  335. return $aResults;
  336. }
  337. protected function _getVideosForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  338. {
  339. $CNF = &$this->_oConfig->CNF;
  340. $aResults = parent::_getVideosForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  341. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_VIDEOS']) || empty($CNF['OBJECT_VIDEOS_TRANSCODERS']))
  342. return $aResults;
  343. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  344. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_VIDEOS']);
  345. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  346. if(!$aGhostFiles)
  347. return array();
  348. $oTcPoster = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['poster']);
  349. $oTcMp4 = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  350. $oTcMp4Hd = BxDolTranscoderVideo::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4_hd']);
  351. if(!$oTcPoster || !$oTcMp4 || !$oTcMp4Hd)
  352. return array();
  353. $aResults = array();
  354. foreach ($aGhostFiles as $k => $a) {
  355. $sVideoUrl = $oStorage->getFileUrlById($a['id']);
  356. $aVideoSize = $oTcMp4Hd->getVideoSize($sVideoUrl);
  357. $sVideoUrlHd = '';
  358. if(!empty($aVideoSize) && is_array($aVideoSize) && (int)$aVideoSize['h'] >= 720)
  359. $sVideoUrlHd = $oTcMp4Hd->getFileUrl($a['id']);
  360. $aResults[$a['id']] = array(
  361. 'id' => $a['id'],
  362. 'src_poster' => $oTcPoster->getFileUrl($a['id']),
  363. 'src_mp4' => $oTcMp4->getFileUrl($a['id']),
  364. 'src_mp4_hd' => $sVideoUrlHd,
  365. );
  366. }
  367. return $aResults;
  368. }
  369. protected function _getFilesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams = array())
  370. {
  371. $CNF = &$this->_oConfig->CNF;
  372. $aResults = parent::_getFilesForTimelinePostAttach($aEvent, $aContentInfo, $sUrl, $aBrowseParams);
  373. if(!$this->_oConfig->isAttachmentsInTimeline() || empty($CNF['OBJECT_STORAGE_FILES']))
  374. return $aResults;
  375. $iContentId = (int)$aContentInfo[$CNF['FIELD_ID']];
  376. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  377. $aGhostFiles = $oStorage->getGhosts($this->serviceGetContentOwnerProfileId($iContentId), $iContentId);
  378. if(!$aGhostFiles)
  379. return array();
  380. $oTranscoder = null;
  381. if(!empty($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_FILES']))
  382. $oTranscoder = BxDolTranscoderImage::getObjectInstance($CNF['OBJECT_IMAGES_TRANSCODER_GALLERY_FILES']);
  383. foreach ($aGhostFiles as $k => $a) {
  384. $bImage = in_array($a['ext'], array('jpg', 'jpeg', 'png', 'gif'));
  385. $sPhotoSrc = '';
  386. if($bImage && $oTranscoder)
  387. $sPhotoSrc = $oTranscoder->getFileUrl($a['id']);
  388. if(empty($sPhotoSrc) && $oStorage)
  389. $sPhotoSrc = $this->_oTemplate->getIconUrl($oStorage->getIconNameByFileName($a['file_name']));
  390. $sUrl = $oStorage->getFileUrlById($a['id']);
  391. $aResults[] = array(
  392. 'src' => $sPhotoSrc,
  393. 'src_medium' => $sPhotoSrc,
  394. 'src_orig' => $bImage ? $sUrl : '',
  395. 'url' => !$bImage ? $sUrl : ''
  396. );
  397. }
  398. return $aResults;
  399. }
  400. protected function _getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams = array())
  401. {
  402. $aResult = parent::_getContentForTimelinePost($aEvent, $aContentInfo, $aBrowseParams);
  403. if(!$this->_oConfig->isAttachmentsInTimeline())
  404. return $aResult;
  405. $CNF = &$this->_oConfig->CNF;
  406. $iProfile = bx_get_logged_profile_id();
  407. $bDynamic = isset($aBrowseParams['dynamic_mode']) && (bool)$aBrowseParams['dynamic_mode'] === true;
  408. $sPolls = '';
  409. $aPolls = $this->_oDb->getPolls(array('type' => 'content_id', 'content_id' => (int)$aContentInfo[$CNF['FIELD_ID']]));
  410. if(!empty($aPolls) && is_array($aPolls)) {
  411. foreach($aPolls as $aPoll) {
  412. $sPoll = $this->_oTemplate->getPollItem($aPoll, $iProfile, array(
  413. 'dynamic' => $bDynamic,
  414. 'switch_menu' => false
  415. ));
  416. if(empty($sPoll))
  417. continue;
  418. $sPolls .= $sPoll;
  419. }
  420. if(!empty($sPolls)) {
  421. $sInclude = '';
  422. $sInclude .= $this->_oTemplate->addJs(array('polls.js'), $bDynamic);
  423. $sInclude .= $this->_oTemplate->addCss(array('polls.css'), $bDynamic);
  424. $aResult['raw'] = ($bDynamic ? $sInclude : '') . $this->_oTemplate->getJsCode('poll') . $this->_oTemplate->parseHtmlByName('poll_items_showcase.html', array(
  425. 'js_object' => $this->_oConfig->getJsObject('poll'),
  426. 'html_id' => $this->_oConfig->getHtmlIds('polls_showcase') . $aEvent['id'],
  427. 'polls' => $sPolls
  428. ));
  429. }
  430. }
  431. return $aResult;
  432. }
  433. protected function _buildRssParams($sMode, $aArgs)
  434. {
  435. $aParams = array ();
  436. $sMode = bx_process_input($sMode);
  437. switch ($sMode) {
  438. case 'author':
  439. $aParams = array('author' => isset($aArgs[0]) ? (int)$aArgs[0] : '');
  440. break;
  441. }
  442. return $aParams;
  443. }
  444. }
  445. /** @} */