Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AjaxController
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 jsonAction
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 recommendAction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 systemStatusAction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * Ajax Controller Module
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Controller
25 * @author   Chris Hallberg <challber@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:plugins:controllers Wiki
28 */
29
30namespace VuFind\Controller;
31
32use Laminas\Mvc\Controller\AbstractActionController;
33use VuFind\AjaxHandler\PluginManager;
34use VuFind\I18n\Translator\TranslatorAwareInterface;
35
36/**
37 * This controller handles global AJAX functionality
38 *
39 * @category VuFind
40 * @package  Controller
41 * @author   Chris Hallberg <challber@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:plugins:controllers Wiki
44 */
45class AjaxController extends AbstractActionController implements TranslatorAwareInterface
46{
47    use AjaxResponseTrait;
48    use \VuFind\I18n\Translator\TranslatorAwareTrait;
49
50    /**
51     * Constructor
52     *
53     * @param PluginManager $am AJAX Handler Plugin Manager
54     */
55    public function __construct(PluginManager $am)
56    {
57        // Prevent errors, notices etc. from being displayed so that they don't mess
58        // with the output (only in production mode):
59        if ('production' === APPLICATION_ENV) {
60            ini_set('display_errors', '0');
61        }
62        $this->ajaxManager = $am;
63    }
64
65    /**
66     * Make an AJAX call with a JSON-formatted response.
67     *
68     * @return \Laminas\Http\Response
69     */
70    public function jsonAction()
71    {
72        $method = $this->params()->fromQuery('method');
73        if (!$method) {
74            return $this->getAjaxResponse('application/json', ['error' => 'Parameter "method" missing'], 400);
75        }
76        return $this->callAjaxMethod($method);
77    }
78
79    /**
80     * Load a recommendation module via AJAX.
81     *
82     * @return \Laminas\Http\Response
83     */
84    public function recommendAction()
85    {
86        return $this->callAjaxMethod('recommend', 'text/html');
87    }
88
89    /**
90     * Check status and return a status message for e.g. a load balancer.
91     *
92     * A simple OK as text/plain is returned if everything works properly.
93     *
94     * @return \Laminas\Http\Response
95     */
96    public function systemStatusAction()
97    {
98        return $this->callAjaxMethod('systemStatus', 'text/plain');
99    }
100}