1<?php
2/**
3 * @package     Joomla.Administrator
4 * @subpackage  com_privacy
5 *
6 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
7 * @license     GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10defined('_JEXEC') or die;
11
12use Joomla\CMS\Factory;
13use Joomla\CMS\Language\Text;
14use Joomla\CMS\Response\JsonResponse;
15use Joomla\CMS\Session\Session;
16
17/**
18 * Privacy Controller
19 *
20 * @since  3.9.0
21 */
22class PrivacyController extends JControllerLegacy
23{
24	/**
25	 * The default view.
26	 *
27	 * @var    string
28	 * @since  3.9.0
29	 */
30	protected $default_view = 'dashboard';
31
32	/**
33	 * Method to display a view.
34	 *
35	 * @param   boolean  $cachable   If true, the view output will be cached
36	 * @param   array    $urlparams  An array of safe URL parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
37	 *
38	 * @return  $this
39	 *
40	 * @since   3.9.0
41	 */
42	public function display($cachable = false, $urlparams = array())
43	{
44		JLoader::register('PrivacyHelper', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/privacy.php');
45
46		// Get the document object.
47		$document = JFactory::getDocument();
48
49		// Set the default view name and format from the Request.
50		$vName   = $this->input->get('view', $this->default_view);
51		$vFormat = $document->getType();
52		$lName   = $this->input->get('layout', 'default', 'string');
53
54		// Get and render the view.
55		if ($view = $this->getView($vName, $vFormat))
56		{
57			$model = $this->getModel($vName);
58			$view->setModel($model, true);
59
60			// For the dashboard view, we need to also push the requests model into the view
61			if ($vName === 'dashboard')
62			{
63				$requestsModel = $this->getModel('Requests');
64
65				$view->setModel($requestsModel, false);
66			}
67
68			if ($vName === 'request')
69			{
70				// For the default layout, we need to also push the action logs model into the view
71				if ($lName === 'default')
72				{
73					JLoader::register('ActionlogsHelper', JPATH_ADMINISTRATOR . '/components/com_actionlogs/helpers/actionlogs.php');
74					JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_actionlogs/models', 'ActionlogsModel');
75
76					$logsModel = $this->getModel('Actionlogs', 'ActionlogsModel');
77
78					// Set default ordering for the context
79					$logsModel->setState('list.fullordering', 'a.log_date DESC');
80
81					// And push the model into the view
82					$view->setModel($logsModel, false);
83				}
84
85				// For the edit layout, if mail sending is disabled then redirect back to the list view as the form is unusable in this state
86				if ($lName === 'edit' && !JFactory::getConfig()->get('mailonline', 1))
87				{
88					$this->setRedirect(
89						JRoute::_('index.php?option=com_privacy&view=requests', false),
90						JText::_('COM_PRIVACY_WARNING_CANNOT_CREATE_REQUEST_WHEN_SENDMAIL_DISABLED'),
91						'warning'
92					);
93
94					return $this;
95				}
96			}
97
98			$view->setLayout($lName);
99
100			// Push document object into the view.
101			$view->document = $document;
102
103			// Load the submenu.
104			PrivacyHelper::addSubmenu($this->input->get('view', $this->default_view));
105
106			$view->display();
107		}
108
109		return $this;
110	}
111
112	/**
113	 * Fetch and report number urgent privacy requests in JSON format, for AJAX requests
114	 *
115	 * @return void
116	 *
117	 * @since 3.9.0
118	 */
119	public function getNumberUrgentRequests()
120	{
121		$app = Factory::getApplication();
122
123		// Check for a valid token. If invalid, send a 403 with the error message.
124		if (!Session::checkToken('get'))
125		{
126			$app->setHeader('status', 403, true);
127			$app->sendHeaders();
128			echo new JsonResponse(new \Exception(Text::_('JINVALID_TOKEN'), 403));
129			$app->close();
130		}
131
132		/** @var PrivacyModelRequests $model */
133		$model                = $this->getModel('requests');
134		$numberUrgentRequests = $model->getNumberUrgentRequests();
135
136		echo new JResponseJson(array('number_urgent_requests' => $numberUrgentRequests));
137
138		$app->close();
139	}
140}
141