BxDolSearchExtendedQuery.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolSearchExtendedQuery extends BxDolDb
  10. {
  11. public function __construct($aObject = array())
  12. {
  13. parent::__construct();
  14. }
  15. static public function getSearchObject($sObject)
  16. {
  17. $oDb = BxDolDb::getInstance();
  18. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_search_extended` WHERE `object` = ?", $sObject);
  19. $aObject = $oDb->getRow($sQuery);
  20. if(!$aObject || !is_array($aObject))
  21. return false;
  22. $aObject['fields'] = self::getSearchFields($aObject);
  23. $aObject['sortable_fields'] = self::getSearchSortableFields($aObject);
  24. return $aObject;
  25. }
  26. static public function getSearchSortableFields($aObject)
  27. {
  28. $oDb = BxDolDb::getInstance();
  29. $sQueryFields = "SELECT * FROM `sys_search_extended_sorting_fields` WHERE `object` = :object ORDER BY `order`";
  30. $aQueryFieldsBindings = array('object' => $aObject['object']);
  31. $aFields = $oDb->getAll($sQueryFields, $aQueryFieldsBindings);
  32. //--- Get fields
  33. if(empty($aFields) || !is_array($aFields)) {
  34. $mFields = BxDolContentInfo::getObjectInstance($aObject['object_content_info'])->getSortableFieldsExtended();
  35. if (!empty($mFields)){
  36. $iOrder = 0;
  37. foreach($mFields as $sField => $aField) {
  38. $bResult = (int)$oDb->query("INSERT INTO `sys_search_extended_sorting_fields`(`object`, `name`, `direction`, `caption`, `active`, `order`) VALUES(:object, :name, :direction, :caption, :active, :order)", array(
  39. 'object' => $aObject['object'],
  40. 'name' => $aField['name'],
  41. 'direction' => $aField['direction'],
  42. 'caption' => isset($aField['caption']) ? $aField['caption'] : 'none',
  43. 'active' => 1,
  44. 'order' => $iOrder++
  45. )) > 0;
  46. }
  47. }
  48. }
  49. return $aFields;
  50. }
  51. static public function getSearchFields($aObject)
  52. {
  53. $oDb = BxDolDb::getInstance();
  54. $sQueryFields = "SELECT * FROM `sys_search_extended_fields` WHERE `object` = :object ORDER BY `order`";
  55. $aQueryFieldsBindings = array('object' => $aObject['object']);
  56. $aFields = $oDb->getAll($sQueryFields, $aQueryFieldsBindings);
  57. //--- Get fields
  58. if(empty($aFields) || !is_array($aFields)) {
  59. $aFields = BxDolContentInfo::getObjectInstance($aObject['object_content_info'])->getSearchableFieldsExtended();
  60. if(empty($aFields) || !is_array($aFields))
  61. return array();
  62. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  63. $aLangIds = array_keys($oLanguage->getLanguages(true));
  64. $iOrder = 0;
  65. foreach($aFields as $sField => $aField) {
  66. $iNow = time();
  67. $sCaptionKey = (!empty($aField['caption']) ? $aField['caption'] : '_sys_form_input_' . $sField) . '_' . $iNow;
  68. $sInfoKey = (!empty($aField['info']) ? $aField['info'] : '_sys_form_input_' . $sField . '_info') . '_' . $iNow;
  69. $sSearchType = isset($aField['search_type']) ? $aField['search_type'] : reset(BxDolSearchExtended::$TYPE_TO_TYPE_SEARCH[$aField['type']]);
  70. $sSearchValue = isset($aField['search_value']) ? $aField['search_value'] : (in_array($aField['type'], array('checkbox', 'switcher')) ? $aField['value'] : '');
  71. if ($sSearchType == 'datepicker_range_age' || $sSearchType == 'datepicker_range'){
  72. $sSearchValue = BxDolService::getSerializedService($aObject['module'], 'get_search_options', array($sField, $aField['type'], $sSearchType));
  73. }
  74. $iActive = isset($aField['active']) ? (int)$aField['active'] : 1;
  75. $bResult = (int)$oDb->query("INSERT INTO `sys_search_extended_fields`(`object`, `name`, `type`, `caption`, `info`, `values`, `pass`, `search_type`, `search_value`, `search_operator`, `active`, `order`) VALUES(:object, :name, :type, :caption, :info, :values, :pass, :search_type, :search_value, :search_operator, :active, :order)", array(
  76. 'object' => $aObject['object'],
  77. 'name' => $sField,
  78. 'type' => $aField['type'],
  79. 'caption' => $sCaptionKey,
  80. 'info' => $sInfoKey,
  81. 'values' => $aField['values'],
  82. 'pass' => $aField['pass'],
  83. 'search_type' => $sSearchType,
  84. 'search_value' => $sSearchValue,
  85. 'search_operator' => isset($aField['search_operator']) ? $aField['search_operator'] : reset(BxDolSearchExtended::$TYPE_TO_OPERATOR[$aField['type']]),
  86. 'active' => $iActive,
  87. 'order' => $iOrder++
  88. )) > 0;
  89. if($bResult) {
  90. $aCaptionValues = !empty($aField['caption']) ? $oLanguage->getLanguageString($aField['caption']) : array();
  91. $aCaptionSystemValues = !empty($aField['caption_system']) ? $oLanguage->getLanguageString($aField['caption_system']) : array();
  92. $aInfoValues = !empty($aField['info']) ? $oLanguage->getLanguageString($aField['info']) : array();
  93. foreach($aLangIds as $iLangId) {
  94. $sCaptionValue = '';
  95. if(isset($aCaptionValues[$iLangId]) && !empty($aCaptionValues[$iLangId]['string']))
  96. $sCaptionValue = $aCaptionValues[$iLangId]['string'];
  97. if(empty($sCaptionValue) && isset($aCaptionSystemValues[$iLangId]) && !empty($aCaptionSystemValues[$iLangId]['string']))
  98. $sCaptionValue = $aCaptionSystemValues[$iLangId]['string'];
  99. $oLanguage->addLanguageString($sCaptionKey, $sCaptionValue, $iLangId, 0, false);
  100. $sInfoValue = '';
  101. if(isset($aInfoValues[$iLangId]) && !empty($aInfoValues[$iLangId]['string']))
  102. $sInfoValue = $aInfoValues[$iLangId]['string'];
  103. $oLanguage->addLanguageString($sInfoKey, $sInfoValue, $iLangId, 0, false);
  104. }
  105. }
  106. }
  107. $oLanguage->compileLanguage(0, true);
  108. $aFields = $oDb->getAll($sQueryFields, $aQueryFieldsBindings);
  109. }
  110. //--- Process fields
  111. foreach ($aFields as $iIndex => $aField) {
  112. //--- Process field Values
  113. if(!empty($aField['values'])) {
  114. if(strncmp(BX_DATA_LISTS_KEY_PREFIX, $aField['values'], 2) === 0) {
  115. $sList = trim($aField['values'], BX_DATA_LISTS_KEY_PREFIX . ' ');
  116. $aFields[$iIndex] = array_merge($aField, array(
  117. 'values' => BxDolFormQuery::getDataItems($sList),
  118. 'values_list_name' => $sList,
  119. ));
  120. }
  121. else if(BxDolService::isSerializedService($aField['values']))
  122. $aFields[$iIndex]['values'] = BxDolService::callSerialized($aField['values']);
  123. else
  124. $aFields[$iIndex]['values'] = unserialize($aField['values']);
  125. }
  126. }
  127. return $aFields;
  128. }
  129. public function deleteFields($aWhere)
  130. {
  131. if(empty($aWhere))
  132. return false;
  133. $sWhereClause = $this->arrayToSQL($aWhere, ' AND ');
  134. $aFields = $this->getAll("SELECT * FROM `sys_search_extended_fields` WHERE " . $sWhereClause);
  135. if(!empty($aFields) && is_array($aFields)) {
  136. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  137. foreach($aFields as $aField) {
  138. if(!empty($aField['caption']))
  139. $oLanguage->deleteLanguageString($aField['caption'], 0, false);
  140. if(!empty($aField['info']))
  141. $oLanguage->deleteLanguageString($aField['info'], 0, false);
  142. }
  143. $oLanguage->compileLanguage(0, true);
  144. }
  145. return $this->query("DELETE FROM `sys_search_extended_fields` WHERE " . $sWhereClause);
  146. }
  147. public function deleteSortableFields($aWhere)
  148. {
  149. if(empty($aWhere))
  150. return false;
  151. $sWhereClause = $this->arrayToSQL($aWhere, ' AND ');
  152. return $this->query("DELETE FROM `sys_search_extended_sorting_fields` WHERE " . $sWhereClause);
  153. }
  154. }
  155. /** @} */