BxBaseSearchExtendedServices.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * System services related to Extended Search.
  11. *
  12. * @see BxDolSearchExtended
  13. */
  14. class BxBaseSearchExtendedServices extends BxDol
  15. {
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20. public function serviceGetForm($mParams)
  21. {
  22. $aParams = [];
  23. if (is_string($mParams))
  24. $aParams['object'] = $mParams;
  25. else
  26. $aParams = $mParams;
  27. $this->prepareParams($aParams);
  28. if(empty($aParams['object']))
  29. return '';
  30. $oSearch = BxDolSearchExtended::getObjectInstance($aParams['object']);
  31. if(!$oSearch || !$oSearch->isEnabled())
  32. return '';
  33. $mForm = $oSearch->getForm($aParams);
  34. if (bx_is_api()){
  35. /*$aRes =$this->serviceGetResults($aParams);
  36. if ($aRes)
  37. return [$mForm, $aRes];
  38. else
  39. return [$mForm];*/
  40. return [$mForm];
  41. }
  42. return $mForm;
  43. }
  44. /*
  45. * TODO: Continue from here!
  46. https://linguria.vercel.app/api/api.php?r=system/get_results/TemplSearchExtendedServices&params[]={%22params%22:{%22per_page%22:10,%22start%22:0,%22object%22:%22bx_persons%22,%22search_params%22:{%22fullname%22:{%22type%22:%22text%22,%22value%22:%22umit%22,%22operator%22:%22like%22}}}}&lang=en
  47. https://linguria.vercel.app/api/api.php?r=system/get_results/TemplSearchExtendedServices&params[]={%22params%22:{%22per_page%22:10,%22start%22:0,%22object%22:%22bx_persons%22,%22search_params%22:{%22fullname%22:{%22type%22:%22text%22,%22value%22:%22umit%22,%22operator%22:%22like%22}},%22filters%22:{%22fullname%22:%22umit%22,%22labels%22:%22%22,%22expertise%22:%22%22,%22native_language%22:%22%22,%22second_language%22:%22%22,%22third_language%22:%22%22,%22association_memberships%22:%22%22,%22searchbx_persons%22:%22Apply%22}}}&lang=en
  48. */
  49. public function serviceGetResults($mParams)
  50. {
  51. $aParams = [];
  52. $fProcessDefValues = function($aValues) {
  53. if(empty($aValues) || !is_array($aValues))
  54. return;
  55. foreach($aValues as $sKey => $sValue) {
  56. if(empty($sValue))
  57. continue;
  58. $_POST[$sKey] = $sValue;
  59. }
  60. };
  61. if(($mDefValues = bx_get('filters')) !== false)
  62. $fProcessDefValues(json_decode($mDefValues, true));
  63. if(is_string($mParams)) {
  64. $mParams = json_decode($mParams, true);
  65. if(!empty($mParams['params']) && is_array($mParams['params']))
  66. $aParams = $mParams['params'];
  67. if(isset($aParams['filters']))
  68. $fProcessDefValues($aParams['filters']);
  69. }
  70. else
  71. $aParams = $mParams;
  72. $this->prepareParams($aParams);
  73. if(empty($aParams['object']))
  74. return '';
  75. $oSearch = BxDolSearchExtended::getObjectInstance($aParams['object']);
  76. if(!$oSearch || !$oSearch->isEnabled())
  77. return '';
  78. $sResults = $oSearch->getResults($aParams);
  79. if (bx_is_api()){
  80. return $sResults;
  81. }
  82. return !empty($sResults) ? $sResults : (isset($aParams['show_empty']) && (bool)$aParams['show_empty'] ? MsgBox(_t('_Empty')) : '');
  83. }
  84. public function prepareParams(&$aParams)
  85. {
  86. if(empty($aParams['object']) && bx_get('object') !== false)
  87. $aParams['object'] = bx_process_input(bx_get('object'), BX_DATA_TEXT);
  88. if(!isset($aParams['show_empty']) && bx_get('show_empty') !== false)
  89. $aParams['show_empty'] = (bool)bx_get('show_empty');
  90. if(empty($aParams['template']) && bx_get('template') !== false)
  91. $aParams['template'] = bx_process_input(bx_get('template'), BX_DATA_TEXT);
  92. if(empty($aParams['cond']) && bx_get('cond') !== false)
  93. $aParams['cond'] = BxDolSearchExtended::decodeConditions(bx_process_input(bx_get('cond'), BX_DATA_TEXT));
  94. if(!isset($aParams['start']) && bx_get('start') !== false)
  95. $aParams['start'] = (int)bx_get('start');
  96. if(!isset($aParams['per_page']) && bx_get('per_page') !== false)
  97. $aParams['per_page'] = (int)bx_get('per_page');
  98. if(!isset($aParams['total']) && ($mixedTotal = bx_get('total')) !== false)
  99. $aParams['total'] = is_numeric($mixedTotal) ? (int)$mixedTotal : $mixedTotal == 'true';
  100. }
  101. }
  102. /** @} */