advancedsearch.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2023 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. require_once 'lib/trite.class.php';
  21. $Trite = new Trite($DB,$Doomguy);
  22. require_once 'lib/mancubus.class.php';
  23. $Mancubus = new Mancubus($DB,$Doomguy);
  24. $_collection = '';
  25. if(isset($_GET['collection']) && !empty($_GET['collection'])) {
  26. $_collection = trim($_GET['collection']);
  27. $_collection = Summoner::validate($_collection,'digit') ? $_collection : '';
  28. }
  29. $TemplateData['pageTitle'] = 'Advanced search';
  30. $TemplateData['loadedCollection'] = array();
  31. $TemplateData['collections'] = array();
  32. $TemplateData['collectionFields'] = array();
  33. $TemplateData['search'] = false;
  34. $TemplateData['searchResultStyle'] = 'grid';
  35. if(!empty($_collection)) {
  36. $TemplateData['loadedCollection'] = $Trite->load($_collection);
  37. if(!empty($TemplateData['loadedCollection'])) {
  38. $TemplateData['collectionFields'] = $Trite->getCollectionFields();
  39. $Mancubus->setCollection($Trite->param('id'));
  40. $Mancubus->setQueryOptions(array('limit' => 60));
  41. $TemplateData['storagePath'] = PATH_WEB_STORAGE . '/' . $Trite->param('id');
  42. $TemplateData['entryLinkPrefix'] = "index.php?p=entry&collection=".$Trite->param('id');
  43. if(isset($_POST['submitForm'])) {
  44. $fdata = $_POST['fdata'];
  45. if (!empty($fdata)) {
  46. $_search = trim($fdata['search']);
  47. if(isset($fdata['tableView'])) {
  48. $TemplateData['searchResultStyle'] = 'table';
  49. }
  50. if (!empty($_search) && Summoner::validate($_search)) {
  51. if (str_contains($_search, ':')) { // field search
  52. $_matches = array();
  53. if(preg_match_all("/(\p{L}+:)(?(?!\p{L}+:).)*/u",$_search, $_matches) !== false && !empty($_matches[0])) {
  54. // $matches[0] has the identifier: and text
  55. // $matches[1] has only the identifier:
  56. // $matches[0][0] belongs to $matches[1][0] and so on
  57. $_sData = array();
  58. $_ms = count($_matches[0]);
  59. for($i=0;$i<$_ms;$i++) {
  60. $_cn = trim(str_replace(':','',$_matches[1][$i]));
  61. if(isset($TemplateData['collectionFields'][$_cn])) {
  62. $_sData[$i]['colName'] = $_cn;
  63. $_sData[$i]['colValue'] = trim(str_replace($_matches[1][$i],'',$_matches[0][$i]));
  64. $_sData[$i]['fieldData'] = $TemplateData['collectionFields'][$_cn];
  65. if(!isset($TemplateData['loadedCollection']['advancedSearchTableFields'][$_sData[$i]['fieldData']['id']])) {
  66. $TemplateData['loadedCollection']['advancedSearchTableFields'][$_sData[$i]['fieldData']['id']] = $_sData[$i]['fieldData']['id'];
  67. }
  68. }
  69. }
  70. $TemplateData['entries'] = $Mancubus->getEntries($_sData);
  71. $TemplateData['search'] = $_search;
  72. }
  73. else {
  74. $TemplateData['message']['content'] = $I18n->t('advsearch.message.wrongInputFormat');
  75. $TemplateData['message']['status'] = "error";
  76. }
  77. } else { // ordinary search within default field
  78. $TemplateData['entries'] = $Mancubus->getEntries(
  79. array(
  80. 0 => array(
  81. 'colName' => $Trite->param('defaultSearchField'),
  82. 'colValue' => $_search,
  83. 'fieldData' => $TemplateData['collectionFields'][$Trite->param('defaultSearchField')]
  84. )
  85. )
  86. );
  87. $TemplateData['search'] = $_search;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. else {
  94. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotLoadCollection');
  95. $TemplateData['message']['status'] = "error";
  96. }
  97. }
  98. else {
  99. $TemplateData['collections'] = $Trite->getCollections();
  100. }