-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ActionlogsController.php
174 lines (149 loc) · 4.47 KB
/
ActionlogsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* @package Joomla.Administrator
* @subpackage com_actionlogs
*
* @copyright Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Actionlogs\Administrator\Controller;
\defined('_JEXEC') or die;
use DateTimeZone;
use Exception;
use InvalidArgumentException;
use Joomla\CMS\Application\CMSApplication;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Date\Date;
use Joomla\CMS\Input\Input;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\AdminController;
use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
use Joomla\CMS\Router\Route;
use Joomla\Component\Actionlogs\Administrator\Helper\ActionlogsHelper;
use Joomla\Component\Actionlogs\Administrator\Model\ActionlogsModel;
use Joomla\Utilities\ArrayHelper;
/**
* Actionlogs list controller class.
*
* @since 3.9.0
*/
class ActionlogsController extends AdminController
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
* Recognized key values include 'name', 'default_task', 'model_path', and
* 'view_path' (this list is not meant to be comprehensive).
* @param MVCFactoryInterface $factory The factory.
* @param CMSApplication $app The JApplication for the dispatcher
* @param Input $input Input
*
* @since 3.9.0
*
* @throws Exception
*/
public function __construct($config = [], MVCFactoryInterface $factory = null, $app = null, $input = null)
{
parent::__construct($config, $factory, $app, $input);
$this->registerTask('exportSelectedLogs', 'exportLogs');
}
/**
* Method to export logs
*
* @return void
*
* @since 3.9.0
*
* @throws Exception
*/
public function exportLogs()
{
// Check for request forgeries.
$this->checkToken();
$task = $this->getTask();
$pks = array();
if ($task == 'exportSelectedLogs')
{
// Get selected logs
$pks = ArrayHelper::toInteger(explode(',', $this->input->post->getString('cids')));
}
/** @var ActionlogsModel $model */
$model = $this->getModel();
// Get the logs data
$data = $model->getLogDataAsIterator($pks);
if (count($data))
{
try
{
$rows = ActionlogsHelper::getCsvData($data);
}
catch (InvalidArgumentException $exception)
{
$this->setMessage(Text::_('COM_ACTIONLOGS_ERROR_COULD_NOT_EXPORT_DATA'), 'error');
$this->setRedirect(Route::_('index.php?option=com_actionlogs&view=actionlogs', false));
return;
}
// Destroy the iterator now
unset($data);
$date = new Date('now', new DateTimeZone('UTC'));
$filename = 'logs_' . $date->format('Y-m-d_His_T');
$csvDelimiter = ComponentHelper::getComponent('com_actionlogs')->getParams()->get('csv_delimiter', ',');
$this->app->setHeader('Content-Type', 'application/csv', true)
->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '.csv"', true)
->setHeader('Cache-Control', 'must-revalidate', true)
->sendHeaders();
$output = fopen("php://output", "w");
foreach ($rows as $row)
{
fputcsv($output, $row, $csvDelimiter);
}
fclose($output);
$this->app->triggerEvent('onAfterLogExport', array());
$this->app->close();
}
else
{
$this->setMessage(Text::_('COM_ACTIONLOGS_NO_LOGS_TO_EXPORT'));
$this->setRedirect(Route::_('index.php?option=com_actionlogs&view=actionlogs', false));
}
}
/**
* Method to get a model object, loading it if required.
*
* @param string $name The model name. Optional.
* @param string $prefix The class prefix. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return object The model.
*
* @since 3.9.0
*/
public function getModel($name = 'Actionlogs', $prefix = 'Administrator', $config = ['ignore_request' => true])
{
// Return the model
return parent::getModel($name, $prefix, $config);
}
/**
* Clean out the logs
*
* @return void
*
* @since 3.9.0
*/
public function purge()
{
// Check for request forgeries.
$this->checkToken();
$model = $this->getModel();
if ($model->purge())
{
$message = Text::_('COM_ACTIONLOGS_PURGE_SUCCESS');
}
else
{
$message = Text::_('COM_ACTIONLOGS_PURGE_FAIL');
}
$this->setRedirect(Route::_('index.php?option=com_actionlogs&view=actionlogs', false), $message);
}
}