collections.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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/mancubus.class.php';
  21. $Mancubus = new Mancubus($DB,$Doomguy);
  22. require_once 'lib/trite.class.php';
  23. $Trite = new Trite($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. // field identifier to search within
  30. $_fid = '';
  31. if(isset($_GET['fid']) && !empty($_GET['fid'])) {
  32. $_fid = trim($_GET['fid']);
  33. $_fid = Summoner::validate($_fid,'nospace') ? $_fid : '';
  34. }
  35. // field value to look up
  36. $_fv = false;
  37. if(isset($_GET['fv']) && !empty($_GET['fv'])) {
  38. $_fv = trim($_GET['fv']);
  39. $_fv = Summoner::validate($_fv) ? $_fv : false;
  40. }
  41. // nav search
  42. $_search = '';
  43. if(isset($_GET['navSearch'])) {
  44. $_search = trim($_GET['navSearch']);
  45. $_search = urldecode($_search);
  46. $_search = Summoner::validate($_search) ? $_search : '';
  47. }
  48. ## pagination
  49. $TemplateData['pagination'] = array('pages' => 0);
  50. $_curPage = 1;
  51. if(isset($_GET['page']) && !empty($_GET['page'])) {
  52. $_curPage = trim($_GET['page']);
  53. $_curPage = Summoner::validate($_curPage,'digit') ? $_curPage : 1;
  54. }
  55. $_sort = '';
  56. if(isset($_GET['s']) && !empty($_GET['s'])) {
  57. $_sort = trim($_GET['s']);
  58. $_sort = Summoner::validate($_sort,'nospace') ? $_sort : '';
  59. }
  60. $_sortDirection = '';
  61. if(isset($_GET['sd']) && !empty($_GET['sd'])) {
  62. $_sortDirection = trim($_GET['sd']);
  63. $_sortDirection = Summoner::validate($_sortDirection,'nospace') ? $_sortDirection : '';
  64. }
  65. $_queryOptions = array(
  66. 'limit' => RESULTS_PER_PAGE,
  67. 'offset' => (RESULTS_PER_PAGE * ($_curPage-1)),
  68. 'sort' => $_sort,
  69. 'sortDirection' => $_sortDirection
  70. );
  71. ## pagination end
  72. $TemplateData['pageTitle'] = "Collection overview";
  73. $TemplateData['loadedCollection'] = array();
  74. $TemplateData['storagePath'] = '';
  75. $TemplateData['entries'] = array();
  76. $TemplateData['collections'] = array();
  77. $TemplateData['search'] = '';
  78. // needed for pagination link building
  79. $TemplateData['pagination']['currentGetParameters']['p'] = 'collections';
  80. $TemplateData['pagination']['currentGetParameters']['collection'] = $_collection;
  81. if(!empty($_collection)) {
  82. $TemplateData['loadedCollection'] = $Trite->load($_collection);
  83. if(!empty($TemplateData['loadedCollection'])) {
  84. $Mancubus->setCollection($Trite->param('id'));
  85. $TemplateData['defaultSortField'] = $Trite->param('defaultSortField');
  86. $TemplateData['defaultSortOrder'] = $Trite->param('defaultSortOrder');
  87. $TemplateData['simpleSearchFields'] = $Trite->getSimpleSearchFields();
  88. if(!empty($_queryOptions['sort'])) {
  89. $TemplateData['simpleSearchFields'][$_queryOptions['sort']]['selected'] = true;
  90. }
  91. if(!empty($TemplateData['defaultSortField'])) {
  92. unset($TemplateData['simpleSearchFields'][$TemplateData['defaultSortField']]);
  93. if(empty($_queryOptions['sort'])) {
  94. $_queryOptions['sort'] = $TemplateData['defaultSortField'];
  95. }
  96. }
  97. if(!empty($TemplateData['defaultSortOrder'])) {
  98. if(empty($_queryOptions['sortDirection'])) {
  99. $_queryOptions['sortDirection'] = $TemplateData['defaultSortOrder'];
  100. }
  101. }
  102. $Mancubus->setQueryOptions($_queryOptions);
  103. $TemplateData['storagePath'] = PATH_WEB_STORAGE . '/' . $Trite->param('id');
  104. $TemplateData['entryLinkPrefix'] = "index.php?p=entry&collection=".$Trite->param('id');
  105. $TemplateData['navSearchAction'] = array('p' => 'collections', 'collection' => $Trite->param('id'));
  106. $_fd = $Trite->getCollectionFields();
  107. $_sdata = array();
  108. if (!empty($_fv) && !empty($_fid)) {
  109. $_sdata[0] = array(
  110. 'colName' => $_fd[$_fid]['identifier'],
  111. 'colValue' => $_fv,
  112. 'fieldData' => $_fd[$_fid],
  113. 'exactTagMatch' => true
  114. );
  115. $_search = $_fv;
  116. $TemplateData['pagination']['currentGetParameters']['fid'] = $_fid;
  117. $TemplateData['pagination']['currentGetParameters']['fv'] = $_fv;
  118. }
  119. elseif(isset($_fd[$Trite->param('defaultSearchField')])) {
  120. $_sdata[0] = array(
  121. 'colName' => $Trite->param('defaultSearchField'),
  122. 'colValue' => $_search,
  123. 'fieldData' =>$_fd[$Trite->param('defaultSearchField')]
  124. );
  125. if (!empty($_search)) {
  126. $TemplateData['pagination']['hideSort'] = true;
  127. }
  128. }
  129. $TemplateData['entries'] = $Mancubus->getEntries($_sdata);
  130. if (!empty($_search)) {
  131. $TemplateData['search'] = $_search;
  132. $TemplateData['pagination']['currentGetParameters']['navSearch'] = urlencode($_search);
  133. }
  134. $TemplateData['pageTitle'] = $Trite->param('name');
  135. }
  136. else {
  137. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotLoadCollection');
  138. $TemplateData['message']['status'] = "error";
  139. }
  140. }
  141. else {
  142. $TemplateData['collections'] = $Trite->getCollections();
  143. }
  144. # pagination
  145. if(!empty($TemplateData['entries']['amount'])) {
  146. $TemplateData['pagination']['pages'] = (int)ceil($TemplateData['entries']['amount'] / RESULTS_PER_PAGE);
  147. $TemplateData['pagination']['curPage'] = $_curPage;
  148. $TemplateData['pagination']['currentGetParameters']['page'] = $_curPage;
  149. $TemplateData['pagination']['currentGetParameters']['s'] = $_sort;
  150. $TemplateData['pagination']['currentGetParameters']['sd'] = $_sortDirection;
  151. }
  152. if($TemplateData['pagination']['pages'] > 11) {
  153. # first pages
  154. $TemplateData['pagination']['visibleRange'] = range(1,3);
  155. # last pages
  156. foreach(range($TemplateData['pagination']['pages']-2, $TemplateData['pagination']['pages']) as $e) {
  157. $TemplateData['pagination']['visibleRange'][] = $e;
  158. }
  159. # pages before and after current page
  160. $cRange = range($TemplateData['pagination']['curPage']-1, $TemplateData['pagination']['curPage']+1);
  161. foreach($cRange as $e) {
  162. $TemplateData['pagination']['visibleRange'][] = $e;
  163. }
  164. $TemplateData['pagination']['currentRangeStart'] = array_shift($cRange);
  165. $TemplateData['pagination']['currentRangeEnd'] = array_pop($cRange);
  166. }
  167. else {
  168. $TemplateData['pagination']['visibleRange'] = range(1,$TemplateData['pagination']['pages']);
  169. }
  170. # pagination end