1
0

BxQuoteOfDayModule.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 QuoteOfTheDay Quote of the Day
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. define('BX_QOD_SOURCE_INTERNAL', 'internal');
  12. define('BX_QOD_SOURCE_RSS', 'rss');
  13. define('BX_QOD_SELECTION_MODE_BY_RANDOM', 'random');
  14. define('BX_QOD_SELECTION_MODE_BY_ORDER', 'order');
  15. define('BX_QOD_LIFETIME_IN_SECONDS', 86400);
  16. class BxQuoteOfDayModule extends BxDolModule
  17. {
  18. function __construct(&$aModule)
  19. {
  20. parent::__construct($aModule);
  21. }
  22. /**
  23. * Service methods
  24. */
  25. public function serviceGetSafeServices()
  26. {
  27. return array (
  28. 'GetQuote' => '',
  29. );
  30. }
  31. /**
  32. * @page service Service Calls
  33. * @section bx_quoteofday Quote of the Day
  34. * @subsection bx_quoteofday-other Other
  35. * @subsubsection bx_quoteofday-get_sources get_sources
  36. *
  37. * @code bx_srv('bx_quoteofday', 'get_sources', [...]); @endcode
  38. *
  39. * Get list of avaliable sources types for studio settings (rss feed or internal list)
  40. *
  41. * @return an array with avaliable sources.
  42. *
  43. * @see BxQuoteOfDayModule::serviceGetSources
  44. */
  45. /**
  46. * @ref bx_quoteofday-get_sources "get_sources"
  47. */
  48. public function serviceGetSources()
  49. {
  50. $aResult = array();
  51. $aChoices = array(BX_QOD_SOURCE_INTERNAL, BX_QOD_SOURCE_RSS);
  52. foreach($aChoices as $sChoice)
  53. $aResult[$sChoice] = _t('_bx_quoteofday_source_' . $sChoice);
  54. return $aResult;
  55. }
  56. /**
  57. * @page service Service Calls
  58. * @section bx_quoteofday Quote of the Day
  59. * @subsection bx_quoteofday-other Other
  60. * @subsubsection bx_quoteofday-get_selection_mode selection_mode
  61. *
  62. * @code bx_srv('bx_quoteofday', 'get_selection_mode', [...]); @endcode
  63. *
  64. * Get list of avaliable selection mode(randome mode or ordered mode) for studio settings
  65. *
  66. * @return an array with selection modes.
  67. *
  68. * @see BxQuoteOfDayModule::serviceGetSelectionMode
  69. */
  70. /**
  71. * @ref bx_quoteofday-get_selection_mode "get_selection_mode"
  72. */
  73. public function serviceGetSelectionMode()
  74. {
  75. $aResult = array();
  76. $aChoices = array(BX_QOD_SELECTION_MODE_BY_RANDOM, BX_QOD_SELECTION_MODE_BY_ORDER);
  77. foreach($aChoices as $sChoice)
  78. $aResult[$sChoice] = _t('_bx_quoteofday_selection_mode_by_' . $sChoice);
  79. return $aResult;
  80. }
  81. /**
  82. * @page service Service Calls
  83. * @section bx_quoteofday Quote of the Day
  84. * @subsection bx_quoteofday-other Other
  85. * @subsubsection bx_quoteofday-get_menu_addon_manage_tools get_menu_addon_manage_tools
  86. *
  87. * @code bx_srv('bx_quoteofday', 'get_menu_addon_manage_tools', [...]); @endcode
  88. *
  89. * Get count of hidden items in internal data for dashbord
  90. *
  91. * @return digit - count of hidden item .
  92. *
  93. * @see BxQuoteOfDayModule::serviceGetMenuAddonManageTools
  94. */
  95. /**
  96. * @ref bx_quoteofday-get_menu_addon_manage_tools "get_menu_addon_manage_tools"
  97. */
  98. public function serviceGetMenuAddonManageTools()
  99. {
  100. return $this->_oDb->getHiddenItemsCount();
  101. }
  102. /**
  103. * @page service Service Calls
  104. * @section bx_quoteofday Quote of the Day
  105. * @subsection bx_quoteofday-other Other
  106. * @subsubsection bx_quoteofday-get_quote
  107. *
  108. * @code bx_srv('bx_quoteofday', 'get_quote', [...]); @endcode
  109. *
  110. * Get current Quote of the Day
  111. *
  112. * @return string - current Quote of the Day from system cache .
  113. *
  114. * @see BxQuoteOfDayModule::serviceGetQuote
  115. */
  116. /**
  117. * @ref bx_quoteofday-get_quote "get_quote"
  118. */
  119. public function serviceGetQuote()
  120. {
  121. $sTextFromCache = $this->getQuoteFromCache();
  122. if ($sTextFromCache == null || $sTextFromCache == "")
  123. {
  124. $sTextFromCache = $this->defineAndSetQuote();
  125. }
  126. return $sTextFromCache;
  127. }
  128. /**
  129. * @page service Service Calls
  130. * @section bx_quoteofday Quote of the Day
  131. * @subsection bx_quoteofday-other Other
  132. * @subsubsection bx_quoteofday-set_quotes_manage
  133. *
  134. * @code bx_srv('bx_quoteofday', 'set_quotes_manage', [...]); @endcode
  135. *
  136. * Return Grid from internal quotes management
  137. *
  138. * @return string - code of grid quotes management.
  139. *
  140. * @see BxQuoteOfDayModule::serviceGetQuotesManage
  141. */
  142. /**
  143. * @ref bx_quoteofday-set_quotes_manage "set_quotes_manage"
  144. */
  145. public function serviceGetQuotesManage()
  146. {
  147. $this->_oTemplate->addJs('jquery.form.min.js');
  148. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->CNF['OBJECT_GRID']);
  149. if(!$oGrid)
  150. return '';
  151. return $oGrid->getCode();
  152. }
  153. public function defineAndSetQuote()
  154. {
  155. $sRssUrl = getParam('bx_quoteofday_rss_url');
  156. $iRssMaxItems = intval(getParam('bx_quoteofday_rss_max_items'));
  157. $aSources = explode(',', getParam('bx_quoteofday_source'));
  158. $sSelectionType = getParam('bx_quoteofday_selection_mode');
  159. $aData = array();
  160. //##### Get data from internal set #####
  161. if (in_array(BX_QOD_SOURCE_INTERNAL, $aSources)) {
  162. $aData = array_merge($aData, $this->getInternalData());
  163. }
  164. //##### Get data from rss #####
  165. if (in_array(BX_QOD_SOURCE_RSS, $aSources) && $sRssUrl != "" && $iRssMaxItems > 0)
  166. $aData = array_merge($aData, $this->getRssData($sRssUrl, $iRssMaxItems));
  167. $sTextToChache = "";
  168. $iIndex = -1;
  169. if (count($aData) > 0){
  170. if ($sSelectionType == BX_QOD_SELECTION_MODE_BY_RANDOM) {
  171. $iIndex = rand(0, count($aData) - 1);
  172. }
  173. else {
  174. $iDayOfYear = date('z');
  175. $iIndex = $iDayOfYear % count($aData);
  176. }
  177. if ($iIndex > -1) {
  178. $sTextToChache = $aData[$iIndex];
  179. $this->putQuoteToCache($sTextToChache);
  180. }
  181. }
  182. return $sTextToChache;
  183. }
  184. public function putQuoteToCache($quoteText)
  185. {
  186. $oCachObject = $this->_oDb->getDbCacheObject();
  187. $oCachObject->setData($this->_oDb->genDbCacheKey($this->_oConfig->CNF['CACHEKEY']), $quoteText, BX_QOD_LIFETIME_IN_SECONDS);
  188. }
  189. public function getQuoteFromCache()
  190. {
  191. $oCachObject = $this->_oDb->getDbCacheObject();
  192. return $oCachObject->getData($this->_oDb->genDbCacheKey($this->_oConfig->CNF['CACHEKEY']), BX_QOD_LIFETIME_IN_SECONDS);
  193. }
  194. public function removeQuoteFromCache()
  195. {
  196. $oCachObject = $this->_oDb->getDbCacheObject();
  197. $oCachObject->delData($this->_oDb->genDbCacheKey($this->_oConfig->CNF['CACHEKEY']));
  198. }
  199. private function getInternalData()
  200. {
  201. return $this->_oDb->getData();
  202. }
  203. private function getRssData($sRssUrl, $iRssMaxItems)
  204. {
  205. $aTmpRv = array();
  206. $oXmlParser = BxDolXmlParser::getInstance();
  207. $sXmlContent = bx_file_get_contents($sRssUrl);
  208. $aTmp = $oXmlParser->getTags($sXmlContent, 'description');
  209. if (is_array($aTmp)) {
  210. $iC = 0;
  211. foreach ($aTmp as $aValue) {
  212. if ($iC == $iRssMaxItems) break;
  213. if (isset($aValue['value']) && $aValue['level'] == 4 && trim(strip_tags($aValue['value'])) != "") {
  214. array_push($aTmpRv, $aValue['value']);
  215. $iC++;
  216. }
  217. }
  218. }
  219. return $aTmpRv;
  220. }
  221. }
  222. /** @} */