BxBaseLiveUpdates.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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();
  31. /* tmp fix for #3002 */
  32. foreach ($aCached as $sName => $a)
  33. if (!in_array($sName, $this->_aSystemsTransient))
  34. $aCached[$sName] = 0;
  35. $aParams = array_merge(array(
  36. 'sActionsUrl' => BX_DOL_URL_ROOT . 'live_updates.php',
  37. 'sObjName' => $this->_sJsObject,
  38. 'iInterval' => $this->_iInterval,
  39. 'aSystemsActive' => array_intersect_key($aCached, array_flip($this->_aSystemsActive)),
  40. 'aSystemsTransient' => array_flip($this->_aSystemsTransient),
  41. 'bServerRequesting' => !empty($this->_aSystems),
  42. 'sHash' => $this->_encodeHash(),
  43. ), $aParams);
  44. $sContent = "var " . $this->_sJsObject . " = new " . $this->_sJsClass . "(" . json_encode($aParams) . ");";
  45. $oTemplate->addJs(array('BxDolLiveUpdates.js'));
  46. return $oTemplate->_wrapInTagJsCode($sContent);
  47. }
  48. /**
  49. *
  50. * Add transient live update for current user on this current page.
  51. * @param string $sName - unique name.
  52. * @param integer $iFrequency - call frequency.
  53. * @param string $sServiceCall - serialized service call.
  54. * @param boolean $bActive - add active/not active live update.
  55. * @param mixed $mixedValue - a value (mainly integer) which will be used as initial value.
  56. * When it's false then initial data will be gotten automatically using provided serialized service call.
  57. */
  58. public function add($sName, $iFrequency, $sServiceCall, $bActive = true, $mixedValue = false)
  59. {
  60. $sCacheKeySystems = $this->_sCacheKeySystems;
  61. if(!$this->_addSystem($sName, $iFrequency, $sServiceCall, $bActive))
  62. return '';
  63. $iValue = 0;
  64. if($mixedValue === false) {
  65. $mixedResponce = $this->_getRequestedDataBySystem($this->_aSystems[$sName]);
  66. if($mixedResponce !== false && isset($mixedResponce['count']))
  67. $iValue = (int)$mixedResponce['count'];
  68. }
  69. else
  70. $iValue = (int)$mixedValue;
  71. $this->_addData($sName, $iValue);
  72. $aParams = array('name' => $sName, 'value' => $iValue);
  73. if($sCacheKeySystems != $this->_sCacheKeySystems)
  74. $aParams['hash'] = $this->_encodeHash();
  75. $sContent = "if(" . $this->_sJsObject . " != undefined) " . $this->_sJsObject . ".add(" . json_encode($aParams) . ");";
  76. return BxDolTemplate::getInstance()->_wrapInTagJsCode($sContent);
  77. }
  78. }
  79. /** @} */