forked from techjoomla/TJ-Notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.php
More file actions
294 lines (239 loc) · 7.89 KB
/
subscription.php
File metadata and controls
294 lines (239 loc) · 7.89 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* @package Tjnotifications
* @subpackage com_tjnotifications
*
* @copyright Copyright (C) 2009 - 2020 Techjoomla. All rights reserved.
* @license http:/www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/
// No direct access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
/**
* Controller for form for subscription
*
* @package Tjnotifications
*
* @since 2.0.0
*/
class TjnotificationsControllerSubscription extends FormController
{
/**
* The extension for which the subscription apply.
*
* @var string
* @since 1.6
*/
protected $extension;
protected $view_list;
/**
* Constructor
*
* @throws Exception
*/
public function __construct()
{
$this->view_list = 'subscriptions';
// Guess the extension
if (empty($this->extension))
{
$this->extension = Factory::getApplication()->input->getCmd('extension', '');
}
parent::__construct();
}
/**
* Save subscription data
*
* @param integer $key key.
*
* @param integer $urlVar url
*
* @return boolean|string The arguments to append to the redirect URL.
*
* @since 2.3.0
*/
public function save($key = null, $urlVar = '')
{
// Check for request forgeries.
Session::checkToken() or Factory::getApplication()->close();
// Initialise variables.
$app = Factory::getApplication();
$input = $app->input;
$model = $this->getModel('Subscription', 'TjnotificationsModel');
$table = $model->getTable();
$data = $input->post->get('jform', array(), 'array');
$task = $this->getTask();
$checkin = property_exists($table, $table->getColumnAlias('checked_out'));
// Determine the name of the primary key for the data.
if (empty($key))
{
$key = $table->getKeyName();
}
// To avoid data collisions the urlVar may be different from the primary key.
if (empty($urlVar))
{
$urlVar = $key;
}
$recordId = $this->input->getInt($urlVar);
// Populate the row id from the session.
$data[$key] = $recordId;
// The save2copy task needs to be handled slightly differently.
if ($task === 'save2copy')
{
// Check-in the original row.
if ($checkin && $model->checkin($data[$key]) === false)
{
// Check-in failed. Go back to the item and display a notice.
$this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error');
$this->setRedirect(
Route::_(
'index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
return false;
}
// Reset the ID, the multilingual associations and then treat the request as for Apply.
$data[$key] = 0;
$data['title'] = 'Copy of ' . $data['title'];
$task = 'apply';
}
// Access check.
if (!$this->allowSave($data, $key))
{
$this->setMessage(Text::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'), 'error');
$this->setRedirect(
Route::_(
'index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToListAppend(),
false
)
);
return false;
}
// Get form
// Sometimes the form needs some posted data, such as for plugins and modules.
$form = $model->getForm($data, false);
if (!$form)
{
$app->enqueueMessage($model->getError(), 'error');
return false;
}
// Validate the posted data.
$validData = $model->validate($form, $data);
// Check for errors.
if ($validData === false)
{
// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
{
if ($errors[$i] instanceof Exception)
{
$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
}
else
{
$app->enqueueMessage($errors[$i], 'warning');
}
}
// Save the data in the session.
$app->setUserState('com_tjnotifications.edit.subscription.data', $data);
// Redirect back to the edit screen
$this->setRedirect(Route::_('index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToItemAppend($recordId, $urlVar), false));
$this->redirect();
}
// Attempt to save the data.
if (!$model->save($validData))
{
// Save the data in the session.
$app->setUserState('com_tjnotifications.edit.subscription.data', $data);
// Redirect back to the edit screen.
$this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error');
$this->setRedirect(Route::_('index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToItemAppend($recordId, $urlVar), false));
return false;
}
// Save succeeded, so check-in the record.
if ($checkin && $model->checkin($validData['id']) === false)
{
// Save the data in the session.
$app->setUserState('com_tjnotifications.edit.subscription.data', $validData);
// Check-in failed, so go back to the record and display a notice.
$this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error');
$this->setRedirect(Route::_('index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToItemAppend($recordId, $urlVar), false));
return false;
}
$this->setMessage(Text::_('COM_TJNOTIFICATIONS_MSG_SUCCESS_SAVE_SUBSCRIPTION'));
// Redirect the user and adjust session state based on the chosen task.
switch ($task)
{
case 'apply':
// Set the record data in the session.
$recordId = $model->getState('com_tjnotifications.edit.subscription.id');
$this->holdEditId('com_tjnotifications.edit.subscription', $recordId);
$app->setUserState('com_tjnotifications.edit.subscription.data', null);
$model->checkout($recordId);
// Redirect back to the edit screen.
$this->setRedirect(
Route::_(
'index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
break;
case 'save2new':
// Clear the record id and data from the session.
$this->releaseEditId('com_tjnotifications.edit.subscription', $recordId);
$app->setUserState('com_tjnotifications.edit.subscription.data', null);
// Redirect back to the edit screen.
$this->setRedirect(Route::_('index.php?option=com_tjnotifications&view=subscription' . $this->getRedirectToItemAppend(null, $urlVar), false));
break;
default:
// Clear the record id and data from the session.
$this->releaseEditId('com_tjnotifications.edit.subscription', $recordId);
$app->setUserState('com_tjnotifications.edit.subscription.data', null);
$url = 'index.php?option=com_tjnotifications&view=subscriptions' . $this->getRedirectToListAppend();
// Check if there is a return value
$return = $this->input->get('return', null, 'base64');
if (!is_null($return) && Uri::isInternal(base64_decode($return)))
{
$url = base64_decode($return);
}
// Redirect to the list screen.
$this->setRedirect(Route::_($url, false));
break;
}
}
/**
* Gets the URL arguments to append to an item redirect.
*
* @param integer $recordId The primary key id for the item.
* @param string $urlVar The name of the URL variable for the id.
*
* @return string The arguments to append to the redirect URL.
*
* @since 1.6
*/
protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
{
$append = parent::getRedirectToItemAppend($recordId);
$append .= '&extension=' . $this->extension;
return $append;
}
/**
* Gets the URL arguments to append to a list redirect.
*
* @return string The arguments to append to the redirect URL.
*
* @since 1.6
*/
protected function getRedirectToListAppend()
{
$append = parent::getRedirectToListAppend();
$append .= '&extension=' . $this->extension;
return $append;
}
}