1
0

BxBaseModTextModule.php 14 KB

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