BxElsResponse.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 ElasticSearch ElasticSearch
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxElsResponse extends BxDolAlertsResponse
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct()
  16. {
  17. parent::__construct();
  18. $this->_sModule = 'bx_elasticsearch';
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. }
  21. public function response($oAlert)
  22. {
  23. if($oAlert->sUnit == 'grid' && $oAlert->sAction == 'get_data')
  24. return $this->_processGridSearch($oAlert);
  25. if($oAlert->sUnit == 'grid' && $oAlert->sAction == 'get_data_by_filter')
  26. return $this->_processGridSearchFilter($oAlert);
  27. return $this->_processManageActions($oAlert);
  28. }
  29. protected function _processManageActions(&$oAlert)
  30. {
  31. $aAlertTypes = array('add', 'update', 'delete');
  32. foreach($aAlertTypes as $sAlertType) {
  33. $sMethodType = bx_gen_method_name($sAlertType);
  34. $sMethod = 'getObjectInstanceByAlert' . $sMethodType;
  35. $oContentInfo = BxDolContentInfo::$sMethod($oAlert->sUnit, $oAlert->sAction);
  36. if(!$oContentInfo)
  37. continue;
  38. $sMethod = 'service' . $sMethodType;
  39. $iContentId = $oAlert->iObject;
  40. if(in_array($oAlert->sAction, array('commentPost', 'commentUpdated', 'commentRemoved')))
  41. $iContentId = $oAlert->aExtras['comment_id'];
  42. $this->_oModule->$sMethod($iContentId, $oContentInfo);
  43. }
  44. }
  45. /**
  46. * Note. This custom search is currently used in Forum Search only.
  47. */
  48. protected function _processGridSearch(&$oAlert)
  49. {
  50. if(empty($oAlert->aExtras['browse_params']['where']))
  51. return;
  52. $aWhere = $oAlert->aExtras['browse_params']['where'];
  53. $oContentInfo = BxDolContentInfo::getObjectInstanceByGrid($oAlert->aExtras['object']);
  54. if(!$oContentInfo)
  55. return;
  56. $aSelection = array();
  57. $aCondition = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
  58. if(isset($aWhere['grp']) && $aWhere['grp'])
  59. $aCondition['cnds'] = $aWhere['cnds'];
  60. else
  61. $aCondition['cnds'][] = $aWhere;
  62. if(!empty($oAlert->aExtras['filter']))
  63. $aCondition['cnds'][] = array('val' => $oAlert->aExtras['filter']);
  64. $this->_updateSearchParamsByGrid($oContentInfo->getGrid(), $aCondition, $aSelection);
  65. $aResults = $this->_oModule->serviceSearchExtended($aCondition, $aSelection, $oContentInfo->getName());
  66. if(empty($aResults) || !is_array($aResults) || (int)$aResults['total'] == 0)
  67. return;
  68. $aItems = array();
  69. foreach($aResults['hits'] as $aResult)
  70. $aItems[] = $aResult['_source'];
  71. $oAlert->aExtras['results'] = $aItems;
  72. }
  73. protected function _processGridSearchFilter(&$oAlert)
  74. {
  75. $oContentInfo = BxDolContentInfo::getObjectInstanceByGrid($oAlert->aExtras['object']);
  76. if(!$oContentInfo)
  77. return;
  78. $aCondition = array('grp' => true, 'opr' => 'AND', 'cnds' => array());
  79. $aSelection = array();
  80. if(!empty($oAlert->aExtras['filter']))
  81. $aCondition['cnds'][] = array('val' => $oAlert->aExtras['filter']);
  82. $aGrid = $oContentInfo->getGrid();
  83. $this->_updateSearchParamsByGrid($aGrid, $aCondition, $aSelection);
  84. $aResults = $this->_oModule->serviceSearchExtended($aCondition, $aSelection, $oContentInfo->getName());
  85. $aIds = array();
  86. if($aResults && (int)$aResults['total'] > 0 && !empty($aResults['hits']))
  87. foreach($aResults['hits'] as $aResult)
  88. $aIds[] = $aResult['_id'];
  89. $oAlert->aExtras['conditions'] = "`" . $aGrid['grid_field_id'] . "` IN (" . $this->_oModule->_oDb->implode_escape($aIds) . ")";
  90. }
  91. protected function _updateSearchParamsByGrid($aGrid, &$aCondition, &$aSelection)
  92. {
  93. if(empty($aGrid['condition']) && empty($aGrid['selection']))
  94. return;
  95. //--- Update Condition params
  96. $aGridCondition = unserialize($aGrid['condition']);
  97. if(!empty($aGridCondition) && is_array($aGridCondition))
  98. $aCondition['cnds'] = array_merge($aCondition['cnds'], $aGridCondition);
  99. //--- Update Selection params: from, size and sort
  100. $aGridSelection = unserialize($aGrid['selection']);
  101. if(empty($aGridSelection) || !is_array($aGridSelection))
  102. $aSelection = array_merge($aSelection, $aGridSelection);
  103. }
  104. }
  105. /** @} */