PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / trunk
VikAppointments Services Booking Calendar vtrunk
trunk 1.2.17 1.2.18 1.2.19
vikappointments / admin / views / backups / view.html.php
vikappointments / admin / views / backups Last commit date
tmpl 4 years ago index.html 4 years ago view.html.php 2 years ago
view.html.php
188 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikAppointments backups view.
16 *
17 * @since 1.7.1
18 */
19 class VikAppointmentsViewbackups extends JViewVAP
20 {
21 /**
22 * VikAppointments view display method.
23 *
24 * @return void
25 */
26 function display($tpl = null)
27 {
28 $app = JFactory::getApplication();
29 $input = $app->input;
30 $dbo = JFactory::getDbo();
31
32 $model = JModelVAP::getInstance('backup');
33
34 // set the toolbar
35 $this->addToolBar();
36
37 $filters = array();
38 $filters['date'] = $app->getUserStateFromRequest($this->getPoolName() . '.date', 'date', '', 'string');
39 $filters['type'] = $app->getUserStateFromRequest($this->getPoolName() . '.type', 'type', '', 'string');
40
41 $this->filters = $filters;
42
43 $this->ordering = $app->getUserStateFromRequest($this->getPoolName() . '.ordering', 'filter_order', 'createdon', 'string');
44 $this->orderDir = $app->getUserStateFromRequest($this->getPoolName() . '.orderdir', 'filter_order_Dir', 'DESC', 'string');
45
46 // db object
47 $lim = $app->getUserStateFromRequest('com_vikappointments.limit', 'limit', $app->get('list_limit'), 'uint');
48 $lim0 = $this->getListLimitStart($filters);
49 $navbut = "";
50
51 // load all the export types
52 $this->exportTypes = $model->getExportTypes();
53
54 $rows = array();
55
56 // fetch folder in which the backup are stored
57 $folder = VAPFactory::getConfig()->get('backupfolder');
58
59 if (!$folder)
60 {
61 // use temporary folder if not specified
62 $folder = JFactory::getApplication()->get('tmp_path');
63 }
64
65 if ($folder && JFolder::exists($folder))
66 {
67 // load all backup archives
68 $rows = JFolder::files($folder, 'backup_', $recurse = false, $fullpath = true);
69 }
70
71 // fetch backup details
72 $rows = array_map(function($file) use ($model)
73 {
74 return $model->getItem($file);
75 }, $rows);
76
77 // filter the backups
78 $rows = array_values(array_filter($rows, function($r) use ($filters)
79 {
80 if ($filters['type'] && $r->type->id !== $filters['type'])
81 {
82 return false;
83 }
84
85 if (VAPDateHelper::isNull($filters['date']) === false)
86 {
87 // get locale SQL strings to have dates adjusted to the current
88 // timezone. This way the dates will be refactored for being
89 // used in UTC, even if the locale is different.
90 $start = VAPDateHelper::getDate($filters['date']);
91
92 if ($start->format('Y-m-d') !== JFactory::getDate($r->date)->format('Y-m-d'))
93 {
94 return false;
95 }
96 }
97
98 return true;
99 }));
100
101 $ordering = $this->ordering;
102 $direction = $this->orderDir;
103
104 // fetch the type of ordering
105 usort($rows, function($a, $b) use ($ordering, $direction)
106 {
107 switch ($ordering)
108 {
109 case 'filesize':
110 // sort by file size
111 $factor = $a->size - $b->size;
112 break;
113
114 default:
115 // sort by creation date
116 $factor = $a->timestamp - $b->timestamp;
117 }
118
119 // in case of descending direction, reverse the ordering factor
120 if (preg_match("/desc/i", $direction))
121 {
122 $factor *= -1;
123 }
124
125 return $factor;
126 });
127
128 $tot_count = count($rows);
129
130 if ($tot_count > $lim)
131 {
132 if ($lim0 >= $tot_count)
133 {
134 // We exceeded the pagination, probably because we deleted all the records of the last page.
135 // For this reason, we need to go back to the previous one.
136 $lim0 = max(array(0, $lim0 - $lim));
137 }
138
139 $rows = array_slice($rows, $lim0, $lim);
140
141 jimport('joomla.html.pagination');
142 $pageNav = new JPagination($tot_count, $lim0, $lim);
143 $navbut = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]);
144 }
145
146 $this->rows = $rows;
147 $this->navbut = $navbut;
148
149 // display the template (default.php)
150 parent::display($tpl);
151 }
152
153 /**
154 * Setting the toolbar.
155 *
156 * @return void
157 */
158 protected function addToolBar()
159 {
160 // add menu title and some buttons to the page
161 JToolBarHelper::title(JText::translate('VAPMAINTITLEVIEWBACKUPS'), 'vikappointments');
162
163 $user = JFactory::getUser();
164
165 JToolBarHelper::back('JTOOLBAR_BACK', 'index.php?option=com_vikappointments&view=editconfigapp');
166
167 if ($user->authorise('core.create', 'com_vikappointments'))
168 {
169 JToolBarHelper::addNew('backup.add');
170 }
171
172 if ($user->authorise('core.delete', 'com_vikappointments'))
173 {
174 JToolBarHelper::deleteList(VikAppointments::getConfirmSystemMessage(), 'backup.delete');
175 }
176 }
177
178 /**
179 * Checks for advanced filters set in the request.
180 *
181 * @return boolean True if active, otherwise false.
182 */
183 protected function hasFilters()
184 {
185 return !empty($this->filters['type']);
186 }
187 }
188