Open Journal Systems  3.3.0
SearchHandler.inc.php
1 <?php
2 
16 import('classes.search.ArticleSearch');
17 import('classes.handler.Handler');
18 
19 class SearchHandler extends Handler {
20 
24  function authorize($request, &$args, $roleAssignments) {
25  import('classes.security.authorization.OjsJournalMustPublishPolicy');
26  if ($request->getContext()) $this->addPolicy(new OjsJournalMustPublishPolicy($request));
27 
28  return parent::authorize($request, $args, $roleAssignments);
29  }
30 
36  function index($args, $request) {
37  $this->validate(null, $request);
38  $this->search($args, $request);
39  }
40 
48  function _assignSearchFilters($request, &$templateMgr, $searchFilters) {
49  // Get the journal id (if any).
50  $journal =& $searchFilters['searchJournal'];
51  $journalId = ($journal ? $journal->getId() : null);
52  $searchFilters['searchJournal'] = $journalId;
53 
54  // Assign all filters except for dates which need special treatment.
55  $templateSearchFilters = array();
56  foreach($searchFilters as $filterName => $filterValue) {
57  if (in_array($filterName, array('fromDate', 'toDate'))) continue;
58  $templateSearchFilters[$filterName] = $filterValue;
59  }
60 
61  // Assign the filters to the template.
62  $templateMgr->assign($templateSearchFilters);
63 
64  // Special case: publication date filters.
65  foreach(array('From', 'To') as $fromTo) {
66  $month = $request->getUserVar("date${fromTo}Month");
67  $day = $request->getUserVar("date${fromTo}Day");
68  $year = $request->getUserVar("date${fromTo}Year");
69  if (empty($year)) {
70  $date = NULL;
71  $hasEmptyFilters = true;
72  } else {
73  $defaultMonth = ($fromTo == 'From' ? 1 : 12);
74  $defaultDay = ($fromTo == 'From' ? 1 : 31);
75  $date = date(
76  'Y-m-d H:i:s',
77  mktime(
78  0, 0, 0, empty($month) ? $defaultMonth : $month,
79  empty($day) ? $defaultDay : $day, $year
80  )
81  );
82  $hasActiveFilters = true;
83  }
84  $templateMgr->assign(array(
85  "date${fromTo}Month" => $month,
86  "date${fromTo}Day" => $day,
87  "date${fromTo}Year" => $year,
88  "date${fromTo}" => $date
89  ));
90  }
91 
92  // Assign the year range.
93  $yearRange = Services::get('publication')->getDateBoundaries(['contextIds' => $journalId]);
94  $yearStart = substr($yearRange[1], 0, 4);
95  $yearEnd = substr($yearRange[0], 0, 4);
96  $templateMgr->assign(array(
97  'yearStart' => $yearStart,
98  'yearEnd' => $yearEnd,
99  ));
100  }
101 
107  function search($args, $request) {
108  $this->validate(null, $request);
109 
110  // Get and transform active filters.
111  $articleSearch = new ArticleSearch();
112  $searchFilters = $articleSearch->getSearchFilters($request);
113  $keywords = $articleSearch->getKeywordsFromSearchFilters($searchFilters);
114 
115  // Get the range info.
116  $rangeInfo = $this->getRangeInfo($request, 'search');
117 
118  // Retrieve results.
119  $error = '';
120  $results = $articleSearch->retrieveResults(
121  $request, $searchFilters['searchJournal'], $keywords, $error,
122  $searchFilters['fromDate'], $searchFilters['toDate'],
123  $rangeInfo
124  );
125 
126  // Prepare and display the search template.
127  $this->setupTemplate($request);
128  $templateMgr = TemplateManager::getManager($request);
129  $templateMgr->setCacheability(CACHEABILITY_NO_STORE);
130 
131  // Result set ordering options.
132  $orderByOptions = $articleSearch->getResultSetOrderingOptions($request);
133  $templateMgr->assign('searchResultOrderOptions', $orderByOptions);
134  $orderDirOptions = $articleSearch->getResultSetOrderingDirectionOptions();
135  $templateMgr->assign('searchResultOrderDirOptions', $orderDirOptions);
136 
137  // Result set ordering selection.
138  list($orderBy, $orderDir) = $articleSearch->getResultSetOrdering($request);
139  $templateMgr->assign('orderBy', $orderBy);
140  $templateMgr->assign('orderDir', $orderDir);
141 
142  // Similar documents.
143  $templateMgr->assign('simDocsEnabled', true);
144 
145  // Result set display.
146  $this->_assignSearchFilters($request, $templateMgr, $searchFilters);
147  $templateMgr->assign('results', $results);
148  $templateMgr->assign('error', $error);
149  $templateMgr->display('frontend/pages/search.tpl');
150  }
151 
159  function similarDocuments($args, &$request) {
160  $this->validate(null, $request);
161 
162  // Retrieve the (mandatory) ID of the article that
163  // we want similar documents for.
164  $articleId = $request->getUserVar('articleId');
165  if (!is_numeric($articleId)) {
166  $request->redirect(null, 'search');
167  }
168 
169  // Check whether a search plugin provides terms for a similarity search.
170  $articleSearch = new ArticleSearch();
171  $searchTerms = $articleSearch->getSimilarityTerms($articleId);
172 
173  // Redirect to a search query with the identified search terms (if any).
174  if (empty($searchTerms)) {
175  $searchParams = null;
176  } else {
177  $searchParams = array('query' => implode(' ', $searchTerms));
178  }
179  $request->redirect(null, 'search', 'search', null, $searchParams);
180  }
181 
187  function authors($args, $request) {
188  $this->validate(null, $request);
189  $this->setupTemplate($request);
190 
191  $journal = $request->getJournal();
192  $user = $request->getUser();
193 
194  $authorDao = DAORegistry::getDAO('AuthorDAO'); /* @var $authorDao AuthorDAO */
195 
196  if (isset($args[0]) && $args[0] == 'view') {
197  // View a specific author
198  $authorName = $request->getUserVar('authorName');
199  $givenName = $request->getUserVar('givenName');
200  $familyName = $request->getUserVar('familyName');
201  $affiliation = $request->getUserVar('affiliation');
202  $country = $request->getUserVar('country');
203 
204  $authorRecords = iterator_to_array(Services::get('author')->getMany([
205  'contextIds' => $journal?[$journal->getId()]:[],
206  'givenName' => $givenName,
207  'familyName' => $familyName,
208  'affiliation' => $affiliation,
209  'country' => $country,
210  ]));
211  $publicationIds = array_map(function($author) {
212  return $author->getData('publicationId');
213  }, $authorRecords);
214  $submissionIds = array_filter(array_map(function($publicationId) {
215  $publication = Services::get('publication')->get($publicationId);
216  return $publication->getData('status') == STATUS_PUBLISHED ? $publication->getData('submissionId') : null;
217  }, array_unique($publicationIds)));
218  $submissions = array_map(function($submissionId) {
219  return Services::get('submission')->get($submissionId);
220  }, array_unique($submissionIds));
221 
222  // Load information associated with each article.
223  $journals = array();
224  $issues = array();
225  $sections = array();
226  $issuesUnavailable = array();
227 
228  $issueDao = DAORegistry::getDAO('IssueDAO'); /* @var $issueDao IssueDAO */
229  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
230  $journalDao = DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */
231 
232  foreach ($submissions as $article) {
233  $articleId = $article->getId();
234  $issueId = $article->getCurrentPublication()->getData('issueId');
235  $sectionId = $article->getSectionId();
236  $journalId = $article->getData('contextId');
237 
238  if (!isset($journals[$journalId])) {
239  $journals[$journalId] = $journalDao->getById($journalId);
240  }
241  if (!isset($issues[$issueId])) {
242  import('classes.issue.IssueAction');
243  $issue = $issueDao->getById($issueId);
244  $issues[$issueId] = $issue;
245  $issueAction = new IssueAction();
246  $issuesUnavailable[$issueId] = $issueAction->subscriptionRequired($issue, $journals[$journalId]) && (!$issueAction->subscribedUser($user, $journals[$journalId], $issueId, $articleId) && !$issueAction->subscribedDomain($request, $journals[$journalId], $issueId, $articleId));
247  }
248  if (!isset($sections[$sectionId])) {
249  $sections[$sectionId] = $sectionDao->getById($sectionId, $journalId, true);
250  }
251  }
252 
253  if (empty($submissions)) {
254  $request->redirect(null, $request->getRequestedPage());
255  }
256 
257  $templateMgr = TemplateManager::getManager($request);
258  $templateMgr->assign(array(
259  'submissions' => $submissions,
260  'issues' => $issues,
261  'issuesUnavailable' => $issuesUnavailable,
262  'sections' => $sections,
263  'journals' => $journals,
264  'givenName' => $givenName,
265  'familyName' => $familyName,
266  'affiliation' => $affiliation,
267  'authorName' => $authorName
268  ));
269 
270  $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
271  $countries = $countries = $isoCodes->getCountries();
272  $country = $countries->getByAlpha2($country);
273  $templateMgr->assign('country', $country?$country->getLocalName():'');
274 
275  $templateMgr->display('frontend/pages/searchAuthorDetails.tpl');
276  } else {
277  // Show the author index
278  $searchInitial = $request->getUserVar('searchInitial');
279  $rangeInfo = $this->getRangeInfo($request, 'authors');
280 
281  $authors = $authorDao->getAuthorsAlphabetizedByJournal(
282  isset($journal)?$journal->getId():null,
283  $searchInitial,
284  $rangeInfo
285  );
286 
287  $templateMgr = TemplateManager::getManager($request);
288  $templateMgr->assign(array(
289  'searchInitial' => $request->getUserVar('searchInitial'),
290  'alphaList' => array_merge(array('-'), explode(' ', __('common.alphaList'))),
291  'authors' => $authors,
292  ));
293  $templateMgr->display('frontend/pages/searchAuthorIndex.tpl');
294  }
295  }
296 
301  function setupTemplate($request) {
302  parent::setupTemplate($request);
303  $templateMgr = TemplateManager::getManager($request);
304  $journal = $request->getJournal();
305  if (!$journal || !$journal->getData('restrictSiteAccess')) {
306  $templateMgr->setCacheability(CACHEABILITY_PUBLIC);
307  }
308  }
309 }
310 
311 
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
SearchHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: SearchHandler.inc.php:24
PKPHandler\getRangeInfo
static getRangeInfo($request, $rangeName, $contextData=null)
Definition: PKPHandler.inc.php:417
SearchHandler\_assignSearchFilters
_assignSearchFilters($request, &$templateMgr, $searchFilters)
Definition: SearchHandler.inc.php:48
IssueAction
IssueAction class.
Definition: IssueAction.inc.php:17
SearchHandler
Handle site index requests.
Definition: SearchHandler.inc.php:19
SearchHandler\authors
authors($args, $request)
Definition: SearchHandler.inc.php:187
SearchHandler\search
search($args, $request)
Definition: SearchHandler.inc.php:107
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1239
PKPHandler\validate
validate($requiredContexts=null, $request=null)
Definition: PKPHandler.inc.php:351
OjsJournalMustPublishPolicy
Access policy to limit access to journals that do not publish online.
Definition: OjsJournalMustPublishPolicy.inc.php:18
SearchHandler\setupTemplate
setupTemplate($request)
Definition: SearchHandler.inc.php:301
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
SearchHandler\similarDocuments
similarDocuments($args, &$request)
Definition: SearchHandler.inc.php:159
SearchHandler\index
index($args, $request)
Definition: SearchHandler.inc.php:36
ArticleSearch
Class for retrieving article search results.
Definition: ArticleSearch.inc.php:20
Handler
Base request handler application class.
Definition: Handler.inc.php:18
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49