BxDolSearchExtendedQuery.php 8.5 KB

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