BxBaseLiveUpdates.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolLiveUpdates
  11. */
  12. class BxBaseLiveUpdates extends BxDolLiveUpdates
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function init($aParams = array())
  19. {
  20. if(!$this->_iProfileId)
  21. return '';
  22. $oTemplate = BxDolTemplate::getInstance();
  23. $iNameIndex = $oTemplate->getPageNameIndex();
  24. if(in_array($iNameIndex, array(BX_PAGE_EMBED)))
  25. return '';
  26. /*
  27. * Load and cache(if it's needed) default(system)
  28. * live updates for current user.
  29. */
  30. $aCached = $this->_getCachedData(true);
  31. $aParams = array_merge(array(
  32. 'sActionsUrl' => BX_DOL_URL_ROOT . 'live_updates.php',
  33. 'sObjName' => $this->_sJsObject,
  34. 'iInterval' => $this->_iInterval,
  35. 'aSystemsActive' => array_intersect_key($aCached, array_flip($this->_aSystemsActive)),
  36. 'aSystemsTransient' => array_flip($this->_aSystemsTransient),
  37. 'bServerRequesting' => !empty($this->_aSystems),
  38. 'sHash' => $this->_encodeHash(),
  39. ), $aParams);
  40. $sContent = "var " . $this->_sJsObject . " = new " . $this->_sJsClass . "(" . json_encode($aParams) . ");";
  41. $oTemplate->addJs(array('BxDolLiveUpdates.js'));
  42. return $oTemplate->_wrapInTagJsCode($sContent);
  43. }
  44. /**
  45. *
  46. * Add transient live update for current user on this current page.
  47. * @param string $sName - unique name.
  48. * @param integer $iFrequency - call frequency.
  49. * @param string $sServiceCall - serialized service call.
  50. * @param boolean $bActive - add active/not active live update.
  51. * @param mixed $mixedValue - a value (mainly integer) which will be used as initial value.
  52. * When it's false then initial data will be gotten automatically using provided serialized service call.
  53. */
  54. public function add($sName, $iFrequency, $sServiceCall, $bActive = true, $mixedValue = false)
  55. {
  56. $sCacheKeySystems = $this->_sCacheKeySystems;
  57. if(!$this->_addSystem($sName, $iFrequency, $sServiceCall, $bActive))
  58. return '';
  59. $iValue = 0;
  60. if($mixedValue === false) {
  61. $mixedResponce = $this->_getRequestedDataBySystem($this->_aSystems[$sName]);
  62. if($mixedResponce !== false && isset($mixedResponce['count']))
  63. $iValue = (int)$mixedResponce['count'];
  64. }
  65. else
  66. $iValue = (int)$mixedValue;
  67. $this->_addData($sName, $iValue);
  68. $aParams = array('name' => $sName, 'value' => $iValue);
  69. if($sCacheKeySystems != $this->_sCacheKeySystems)
  70. $aParams['hash'] = $this->_encodeHash();
  71. $sContent = "if(" . $this->_sJsObject . " != undefined) " . $this->_sJsObject . ".add(" . json_encode($aParams) . ");";
  72. return BxDolTemplate::getInstance()->_wrapInTagJsCode($sContent);
  73. }
  74. }
  75. /** @} */