1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Redcore |
4
|
|
|
* @subpackage Controller |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) 2008 - 2020 redWEB.dk. All rights reserved. |
7
|
|
|
* @license GNU General Public License version 2 or later, see LICENSE. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
defined('JPATH_REDCORE') or die; |
11
|
|
|
|
12
|
|
|
JLoader::import('joomla.application.component.controllerform'); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Controller Form class. |
16
|
|
|
* Works with a RModelAdmin or a Model using RForm. |
17
|
|
|
* |
18
|
|
|
* @package Redcore |
19
|
|
|
* @subpackage Controller |
20
|
|
|
* @since 1.0 |
21
|
|
|
*/ |
22
|
|
|
class RControllerForm extends JControllerForm |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param array $config An optional associative array of configuration settings. |
28
|
|
|
* Recognized key values include 'name', 'default_task', 'model_path', and |
29
|
|
|
* 'view_path' (this list is not meant to be comprehensive). |
30
|
|
|
* |
31
|
|
|
* @throws Exception |
32
|
|
|
*/ |
33
|
|
|
public function __construct($config = array()) |
34
|
|
|
{ |
35
|
|
|
/** JControllerLegacy */ |
36
|
|
|
$this->methods = array(); |
37
|
|
|
$this->message = null; |
38
|
|
|
$this->messageType = 'message'; |
39
|
|
|
$this->paths = array(); |
40
|
|
|
$this->redirect = null; |
41
|
|
|
$this->taskMap = array(); |
42
|
|
|
|
43
|
|
|
if (defined('JDEBUG') && JDEBUG) |
44
|
|
|
{ |
45
|
|
|
JLog::addLogger(array('text_file' => 'jcontroller.log.php'), JLog::ALL, array('controller')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$this->input = JFactory::getApplication()->input; |
49
|
|
|
|
50
|
|
|
// Determine the methods to exclude from the base class. |
51
|
|
|
$xMethods = get_class_methods('JControllerLegacy'); |
52
|
|
|
|
53
|
|
|
// Get the public methods in this class using reflection. |
54
|
|
|
$r = new ReflectionClass($this); |
55
|
|
|
$rMethods = $r->getMethods(ReflectionMethod::IS_PUBLIC); |
56
|
|
|
|
57
|
|
|
foreach ($rMethods as $rMethod) |
58
|
|
|
{ |
59
|
|
|
$mName = $rMethod->getName(); |
60
|
|
|
|
61
|
|
|
// Add default display method if not explicitly declared. |
62
|
|
|
if (!in_array($mName, $xMethods) || $mName == 'display') |
63
|
|
|
{ |
64
|
|
|
$this->methods[] = strtolower($mName); |
65
|
|
|
|
66
|
|
|
// Auto register the methods as tasks. |
67
|
|
|
$this->taskMap[strtolower($mName)] = $mName; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Set the view name |
72
|
|
|
if (empty($this->name)) |
73
|
|
|
{ |
74
|
|
|
if (array_key_exists('name', $config)) |
75
|
|
|
{ |
76
|
|
|
$this->name = $config['name']; |
77
|
|
|
} |
78
|
|
|
else |
79
|
|
|
{ |
80
|
|
|
$this->name = $this->getName(); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Set a base path for use by the controller |
85
|
|
|
if (array_key_exists('base_path', $config)) |
86
|
|
|
{ |
87
|
|
|
$this->basePath = $config['base_path']; |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
$this->basePath = JPATH_COMPONENT; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// If the default task is set, register it as such |
95
|
|
|
if (array_key_exists('default_task', $config)) |
96
|
|
|
{ |
97
|
|
|
$this->registerDefaultTask($config['default_task']); |
98
|
|
|
} |
99
|
|
|
else |
100
|
|
|
{ |
101
|
|
|
$this->registerDefaultTask('display'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// Set the models prefix |
105
|
|
|
if (empty($this->model_prefix)) |
106
|
|
|
{ |
107
|
|
|
if (array_key_exists('model_prefix', $config)) |
108
|
|
|
{ |
109
|
|
|
// User-defined prefix |
110
|
|
|
$this->model_prefix = $config['model_prefix']; |
111
|
|
|
} |
112
|
|
|
else |
113
|
|
|
{ |
114
|
|
|
$this->model_prefix = ucfirst($this->name) . 'Model'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Set the default model search path |
119
|
|
|
if (array_key_exists('model_path', $config)) |
120
|
|
|
{ |
121
|
|
|
// User-defined dirs |
122
|
|
|
$this->addModelPath($config['model_path'], $this->model_prefix); |
123
|
|
|
} |
124
|
|
|
else |
125
|
|
|
{ |
126
|
|
|
$this->addModelPath($this->basePath . '/models', $this->model_prefix); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Set the default view search path |
130
|
|
|
if (array_key_exists('view_path', $config)) |
131
|
|
|
{ |
132
|
|
|
// User-defined dirs |
133
|
|
|
$this->setPath('view', $config['view_path']); |
134
|
|
|
} |
135
|
|
|
else |
136
|
|
|
{ |
137
|
|
|
$this->setPath('view', $this->basePath . '/views'); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// Set the default view. |
141
|
|
|
if (array_key_exists('default_view', $config)) |
142
|
|
|
{ |
143
|
|
|
$this->default_view = $config['default_view']; |
144
|
|
|
} |
145
|
|
|
elseif (empty($this->default_view)) |
146
|
|
|
{ |
147
|
|
|
$this->default_view = $this->getName(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** JControllerForm */ |
151
|
|
|
// Guess the option as com_NameOfController |
152
|
|
|
if (empty($this->option)) |
153
|
|
|
{ |
154
|
|
|
$this->option = 'com_' . strtolower($this->getName()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// Guess the JText message prefix. Defaults to the option. |
158
|
|
|
if (empty($this->text_prefix)) |
159
|
|
|
{ |
160
|
|
|
$this->text_prefix = strtoupper($this->option); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Guess the context as the suffix, eg: OptionControllerContent. |
164
|
|
|
if (empty($this->context)) |
165
|
|
|
{ |
166
|
|
|
$r = null; |
167
|
|
|
|
168
|
|
|
if (!preg_match('/(.*)Controller(.*)/i', get_class($this), $r)) |
169
|
|
|
{ |
170
|
|
|
throw new Exception(JText::_('JLIB_APPLICATION_ERROR_CONTROLLER_GET_NAME'), 500); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->context = strtolower($r[2]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Apply, Save & New, and Save As copy should be standard on forms. |
177
|
|
|
$this->registerTask('apply', 'save'); |
178
|
|
|
$this->registerTask('save2new', 'save'); |
179
|
|
|
$this->registerTask('save2copy', 'save'); |
180
|
|
|
|
181
|
|
|
/** Custom */ |
182
|
|
|
// Guess the item view as the context. |
183
|
|
|
if (empty($this->view_item)) |
184
|
|
|
{ |
185
|
|
|
$this->view_item = $this->context; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Guess the list view as the plural of the item view. |
189
|
|
|
if (empty($this->view_list)) |
190
|
|
|
{ |
191
|
|
|
$this->view_list = RInflector::pluralize($this->view_item); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if (!property_exists($this, 'input') || empty($this->input)) |
195
|
|
|
{ |
196
|
|
|
$this->input = JFactory::getApplication()->input; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Method to get a model object, loading it if required. |
202
|
|
|
* |
203
|
|
|
* @param string $name The model name. Optional. |
204
|
|
|
* @param string $prefix The class prefix. Optional. |
205
|
|
|
* @param array $config Configuration array for model. Optional. |
206
|
|
|
* |
207
|
|
|
* @return object The model. |
208
|
|
|
*/ |
209
|
|
|
public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true)) |
210
|
|
|
{ |
211
|
|
|
$class = get_class($this); |
212
|
|
|
|
213
|
|
|
if (empty($name)) |
214
|
|
|
{ |
215
|
|
|
$name = strstr($class, 'Controller'); |
216
|
|
|
$name = str_replace('Controller', '', $name); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (empty($prefix)) |
220
|
|
|
{ |
221
|
|
|
$prefix = strstr($class, 'Controller', true) . 'Model'; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return parent::getModel($name, $prefix, $config); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Validates the form and displays the error per field. |
229
|
|
|
* |
230
|
|
|
* { |
231
|
|
|
* "error": "error", => global error |
232
|
|
|
* "field_name": "error" |
233
|
|
|
* } |
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
|
|
public function validateFormAjax() |
238
|
|
|
{ |
239
|
|
|
/** @var RModelAdmin $model */ |
240
|
|
|
$model = $this->getModel(); |
241
|
|
|
$data = $this->input->post->get('jform', array(), 'array'); |
242
|
|
|
|
243
|
|
|
$form = $model->getForm($data, false); |
244
|
|
|
|
245
|
|
|
// Filter and validate the form data. |
246
|
|
|
$data = $form->filter($data); |
247
|
|
|
$return = $form->validate($data); |
|
|
|
|
248
|
|
|
|
249
|
|
|
// Prepare the json array. |
250
|
|
|
$jsonArray = array(); |
251
|
|
|
|
252
|
|
|
// Check for an error. |
253
|
|
|
if ($return instanceof Exception) |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
$jsonArray['error'] = $return->getMessage(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// Check the validation results. |
259
|
|
|
elseif ($return === false) |
260
|
|
|
{ |
261
|
|
|
// Get the validation messages from the form. |
262
|
|
|
foreach ($form->getErrors() as $key => $message) |
263
|
|
|
{ |
264
|
|
|
if ($message instanceof Exception) |
265
|
|
|
{ |
266
|
|
|
$jsonArray[$key] = $message->getMessage(); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
else |
270
|
|
|
{ |
271
|
|
|
$jsonArray[$key] = $message; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
echo json_encode($jsonArray); |
277
|
|
|
|
278
|
|
|
JFactory::getApplication()->close(); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Method to cancel an edit. |
283
|
|
|
* |
284
|
|
|
* @param string $key The name of the primary key of the URL variable. |
285
|
|
|
* |
286
|
|
|
* @return boolean True if access level checks pass, false otherwise. |
287
|
|
|
*/ |
288
|
|
|
public function cancel($key = null) |
289
|
|
|
{ |
290
|
|
|
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); |
291
|
|
|
|
292
|
|
|
$app = JFactory::getApplication(); |
293
|
|
|
$model = $this->getModel(); |
294
|
|
|
$table = $model->getTable(); |
295
|
|
|
$checkin = property_exists($table, 'checked_out'); |
296
|
|
|
$context = "$this->option.edit.$this->context"; |
297
|
|
|
|
298
|
|
|
if (empty($key)) |
299
|
|
|
{ |
300
|
|
|
$key = $table->getKeyName(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$recordId = $app->input->getInt($key); |
304
|
|
|
|
305
|
|
|
// Attempt to check-in the current record. |
306
|
|
|
if ($recordId) |
307
|
|
|
{ |
308
|
|
|
// Check we are holding the id in the edit list. |
309
|
|
|
if (!$this->checkEditId($context, $recordId)) |
310
|
|
|
{ |
311
|
|
|
// Somehow the person just went to the form - we don't allow that. |
312
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $recordId)); |
|
|
|
|
313
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
314
|
|
|
|
315
|
|
|
// Redirect to the list screen |
316
|
|
|
$this->setRedirect( |
317
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
|
|
|
|
318
|
|
|
); |
319
|
|
|
|
320
|
|
|
return false; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
if ($checkin) |
324
|
|
|
{ |
325
|
|
|
if ($model->checkin($recordId) === false) |
326
|
|
|
{ |
327
|
|
|
// Check-in failed, go back to the record and display a notice. |
328
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); |
|
|
|
|
329
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
330
|
|
|
|
331
|
|
|
// Redirect back to the edit screen. |
332
|
|
|
$this->setRedirect( |
333
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $key)) |
334
|
|
|
); |
335
|
|
|
|
336
|
|
|
return false; |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
// Clean the session data and redirect. |
342
|
|
|
$this->releaseEditId($context, $recordId); |
343
|
|
|
$app->setUserState($context . '.data', null); |
344
|
|
|
|
345
|
|
|
// Redirect to the list screen |
346
|
|
|
$this->setRedirect( |
347
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
return true; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Method to edit an existing record. |
355
|
|
|
* |
356
|
|
|
* @param string $key The name of the primary key of the URL variable. |
357
|
|
|
* @param string $urlVar The name of the URL variable if different from the primary key |
358
|
|
|
* (sometimes required to avoid router collisions). |
359
|
|
|
* |
360
|
|
|
* @return boolean True if access level check and checkout passes, false otherwise. |
361
|
|
|
*/ |
362
|
|
|
public function edit($key = null, $urlVar = null) |
363
|
|
|
{ |
364
|
|
|
// Do not cache the response to this, its a redirect, and mod_expires and google chrome browser bugs cache it forever! |
365
|
|
|
JFactory::getApplication()->allowCache(false); |
366
|
|
|
$app = JFactory::getApplication(); |
367
|
|
|
$model = $this->getModel(); |
368
|
|
|
$table = $model->getTable(); |
369
|
|
|
$cid = $this->input->post->get('cid', array(), 'array'); |
370
|
|
|
$context = "$this->option.edit.$this->context"; |
371
|
|
|
|
372
|
|
|
// Determine the name of the primary key for the data. |
373
|
|
|
if (empty($key)) |
374
|
|
|
{ |
375
|
|
|
$key = $table->getKeyName(); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
// To avoid data collisions the urlVar may be different from the primary key. |
379
|
|
|
if (empty($urlVar)) |
380
|
|
|
{ |
381
|
|
|
$urlVar = $key; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
// Get the previous record id (if any) and the current record id. |
385
|
|
|
$recordId = (int) (count($cid) ? $cid[0] : $this->input->getInt($urlVar)); |
386
|
|
|
$checkin = property_exists($table, 'checked_out'); |
387
|
|
|
|
388
|
|
|
// Access check. |
389
|
|
|
if (!$this->allowEdit(array($key => $recordId), $key)) |
390
|
|
|
{ |
391
|
|
|
$this->setError(JText::_('JLIB_APPLICATION_ERROR_EDIT_NOT_PERMITTED')); |
|
|
|
|
392
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
393
|
|
|
|
394
|
|
|
// Redirect to the list screen |
395
|
|
|
$this->setRedirect( |
396
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
|
|
|
|
397
|
|
|
); |
398
|
|
|
|
399
|
|
|
return false; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
// Attempt to check-out the new record for editing and redirect. |
403
|
|
|
if ($checkin && !$model->checkout($recordId)) |
404
|
|
|
{ |
405
|
|
|
// Check-out failed, display a notice but allow the user to see the record. |
406
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKOUT_FAILED', $model->getError())); |
|
|
|
|
407
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
408
|
|
|
|
409
|
|
|
// Redirect back to the edit screen. |
410
|
|
|
$this->setRedirect( |
411
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
return false; |
415
|
|
|
} |
416
|
|
|
else |
417
|
|
|
{ |
418
|
|
|
// Check-out succeeded, push the new record id into the session. |
419
|
|
|
$this->holdEditId($context, $recordId); |
420
|
|
|
$app->setUserState($context . '.data', null); |
421
|
|
|
|
422
|
|
|
// Redirect back to the edit screen. |
423
|
|
|
$this->setRedirect( |
424
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
425
|
|
|
); |
426
|
|
|
|
427
|
|
|
return true; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Method to add a new record. |
433
|
|
|
* |
434
|
|
|
* @return mixed True if the record can be added, a error object if not. |
435
|
|
|
*/ |
436
|
|
|
public function add() |
437
|
|
|
{ |
438
|
|
|
$app = JFactory::getApplication(); |
439
|
|
|
$context = "$this->option.edit.$this->context"; |
440
|
|
|
|
441
|
|
|
// Access check. |
442
|
|
|
if (!$this->allowAdd()) |
443
|
|
|
{ |
444
|
|
|
// Set the internal error and also the redirect error. |
445
|
|
|
$this->setError(JText::_('JLIB_APPLICATION_ERROR_CREATE_RECORD_NOT_PERMITTED')); |
|
|
|
|
446
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
447
|
|
|
|
448
|
|
|
// Redirect to the list screen |
449
|
|
|
$this->setRedirect( |
450
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
|
|
|
|
451
|
|
|
); |
452
|
|
|
|
453
|
|
|
return false; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
// Clear the record edit information from the session. |
457
|
|
|
$app->setUserState($context . '.data', null); |
458
|
|
|
|
459
|
|
|
// Redirect back to the edit screen. |
460
|
|
|
$this->setRedirect( |
461
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend()) |
462
|
|
|
); |
463
|
|
|
|
464
|
|
|
return true; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Method to save a record. |
469
|
|
|
* |
470
|
|
|
* @param string $key The name of the primary key of the URL variable. |
471
|
|
|
* @param string $urlVar The name of the URL variable if different from the primary key (sometimes required to avoid router collisions). |
472
|
|
|
* |
473
|
|
|
* @return boolean True if successful, false otherwise. |
474
|
|
|
*/ |
475
|
|
|
public function save($key = null, $urlVar = null) |
476
|
|
|
{ |
477
|
|
|
// Check for request forgeries. |
478
|
|
|
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); |
479
|
|
|
|
480
|
|
|
$app = JFactory::getApplication(); |
481
|
|
|
$lang = JFactory::getLanguage(); |
482
|
|
|
$model = $this->getModel(); |
483
|
|
|
$table = $model->getTable(); |
484
|
|
|
$data = $this->getSaveData(); |
485
|
|
|
$checkin = property_exists($table, 'checked_out'); |
486
|
|
|
$context = "$this->option.edit.$this->context"; |
487
|
|
|
$task = $this->getTask(); |
488
|
|
|
|
489
|
|
|
// Determine the name of the primary key for the data. |
490
|
|
|
if (empty($key)) |
491
|
|
|
{ |
492
|
|
|
$key = $table->getKeyName(); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
// To avoid data collisions the urlVar may be different from the primary key. |
496
|
|
|
if (empty($urlVar)) |
497
|
|
|
{ |
498
|
|
|
$urlVar = $key; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
$recordId = $this->input->getInt($urlVar); |
502
|
|
|
|
503
|
|
|
if (!$this->checkEditId($context, $recordId)) |
504
|
|
|
{ |
505
|
|
|
// Somehow the person just went to the form and tried to save it. We don't allow that. |
506
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_UNHELD_ID', $recordId)); |
|
|
|
|
507
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
508
|
|
|
|
509
|
|
|
// Redirect to the list screen |
510
|
|
|
$this->setRedirect( |
511
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
|
|
|
|
512
|
|
|
); |
513
|
|
|
|
514
|
|
|
return false; |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
// Populate the row id from the session. |
518
|
|
|
$data[$key] = $recordId; |
519
|
|
|
|
520
|
|
|
// The save2copy task needs to be handled slightly differently. |
521
|
|
|
if ($task == 'save2copy') |
522
|
|
|
{ |
523
|
|
|
// Check-in the original row. |
524
|
|
|
if ($checkin && $model->checkin($data[$key]) === false) |
525
|
|
|
{ |
526
|
|
|
// Check-in failed. Go back to the item and display a notice. |
527
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); |
|
|
|
|
528
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
529
|
|
|
|
530
|
|
|
// Redirect back to the edit screen. |
531
|
|
|
$this->setRedirect( |
532
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
533
|
|
|
); |
534
|
|
|
|
535
|
|
|
return false; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
// Reset the ID, the multilingual associations and then treat the request as for Apply. |
539
|
|
|
$data[$key] = 0; |
540
|
|
|
$data['associations'] = array(); |
541
|
|
|
$task = 'apply'; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
// Access check. |
545
|
|
|
if (!$this->allowSave($data, $key)) |
546
|
|
|
{ |
547
|
|
|
$this->setError(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED')); |
|
|
|
|
548
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
549
|
|
|
|
550
|
|
|
// Redirect to the list screen |
551
|
|
|
$this->setRedirect( |
552
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
553
|
|
|
); |
554
|
|
|
|
555
|
|
|
return false; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
// Validate the posted data. |
559
|
|
|
// Sometimes the form needs some posted data, such as for plugins and modules. |
560
|
|
|
$form = $model->getForm($data, false); |
561
|
|
|
|
562
|
|
|
if (!$form) |
563
|
|
|
{ |
564
|
|
|
$app->enqueueMessage($model->getError(), 'error'); |
565
|
|
|
|
566
|
|
|
return false; |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
// Test whether the data is valid. |
570
|
|
|
$validData = $model->validate($form, $data); |
571
|
|
|
|
572
|
|
|
// Check for validation errors. |
573
|
|
|
if ($validData === false) |
574
|
|
|
{ |
575
|
|
|
// Get the validation messages. |
576
|
|
|
$errors = $model->getErrors(); |
577
|
|
|
|
578
|
|
|
// Push up to three validation messages out to the user. |
579
|
|
|
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) |
580
|
|
|
{ |
581
|
|
|
if ($errors[$i] instanceof Exception) |
582
|
|
|
{ |
583
|
|
|
$app->enqueueMessage($errors[$i]->getMessage(), 'warning'); |
584
|
|
|
} |
585
|
|
|
else |
586
|
|
|
{ |
587
|
|
|
$app->enqueueMessage($errors[$i], 'warning'); |
588
|
|
|
} |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
// Save the data in the session. |
592
|
|
|
$app->setUserState($context . '.data', $data); |
593
|
|
|
|
594
|
|
|
// Redirect back to the edit screen. |
595
|
|
|
$this->setRedirect( |
596
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
597
|
|
|
); |
598
|
|
|
|
599
|
|
|
return false; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
if (!isset($validData['tags'])) |
603
|
|
|
{ |
604
|
|
|
$validData['tags'] = null; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
// Attempt to save the data. |
608
|
|
|
if (!$model->save($validData)) |
609
|
|
|
{ |
610
|
|
|
// Save the data in the session. |
611
|
|
|
$app->setUserState($context . '.data', $validData); |
612
|
|
|
|
613
|
|
|
// Redirect back to the edit screen. |
614
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError())); |
|
|
|
|
615
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
616
|
|
|
|
617
|
|
|
// Redirect back to the edit screen. |
618
|
|
|
$this->setRedirect( |
619
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
620
|
|
|
); |
621
|
|
|
|
622
|
|
|
return false; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
// Save succeeded, so check-in the record. |
626
|
|
|
if ($checkin && $model->checkin($validData[$key]) === false) |
627
|
|
|
{ |
628
|
|
|
// Save the data in the session. |
629
|
|
|
$app->setUserState($context . '.data', $validData); |
630
|
|
|
|
631
|
|
|
// Check-in failed, so go back to the record and display a notice. |
632
|
|
|
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError())); |
|
|
|
|
633
|
|
|
$this->setMessage($this->getError(), 'error'); |
|
|
|
|
634
|
|
|
|
635
|
|
|
// Redirect back to the edit screen. |
636
|
|
|
$this->setRedirect( |
637
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
638
|
|
|
); |
639
|
|
|
|
640
|
|
|
return false; |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
$this->setMessage( |
644
|
|
|
JText::_( |
645
|
|
|
($lang->hasKey( |
646
|
|
|
$this->text_prefix |
647
|
|
|
. ($recordId == 0 && (version_compare(JVERSION, '3.7', '<') ? $app->isSite() : $app->isClient('site')) ? '_SUBMIT' : '') |
|
|
|
|
648
|
|
|
. '_SAVE_SUCCESS' |
649
|
|
|
) |
650
|
|
|
? $this->text_prefix |
651
|
|
|
: 'JLIB_APPLICATION') |
652
|
|
|
. ($recordId == 0 && (version_compare(JVERSION, '3.7', '<') ? $app->isSite() : $app->isClient('site')) ? '_SUBMIT' : '') |
|
|
|
|
653
|
|
|
. '_SAVE_SUCCESS' |
654
|
|
|
), |
655
|
|
|
'success' |
656
|
|
|
); |
657
|
|
|
|
658
|
|
|
// Redirect the user and adjust session state based on the chosen task. |
659
|
|
|
switch ($task) |
660
|
|
|
{ |
661
|
|
|
case 'apply': |
662
|
|
|
// Set the record data in the session. |
663
|
|
|
$recordId = $model->getState($this->context . '.id'); |
664
|
|
|
$this->holdEditId($context, $recordId); |
665
|
|
|
$app->setUserState($context . '.data', null); |
666
|
|
|
$model->checkout($recordId); |
667
|
|
|
|
668
|
|
|
// Redirect back to the edit screen. |
669
|
|
|
$this->setRedirect( |
670
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend($recordId, $urlVar)) |
671
|
|
|
); |
672
|
|
|
break; |
673
|
|
|
|
674
|
|
|
case 'save2new': |
675
|
|
|
// Clear the record id and data from the session. |
676
|
|
|
$this->releaseEditId($context, $recordId); |
677
|
|
|
$app->setUserState($context . '.data', null); |
678
|
|
|
|
679
|
|
|
// Redirect back to the edit screen. |
680
|
|
|
$this->setRedirect( |
681
|
|
|
$this->getRedirectToItemRoute($this->getRedirectToItemAppend(null, $urlVar)) |
682
|
|
|
); |
683
|
|
|
break; |
684
|
|
|
|
685
|
|
|
default: |
686
|
|
|
// Clear the record id and data from the session. |
687
|
|
|
$this->releaseEditId($context, $recordId); |
688
|
|
|
$app->setUserState($context . '.data', null); |
689
|
|
|
|
690
|
|
|
// Set redirect |
691
|
|
|
$this->setRedirect( |
692
|
|
|
$this->getRedirectToListRoute($this->getRedirectToListAppend()) |
693
|
|
|
); |
694
|
|
|
break; |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
// Invoke the postSave method to allow for the child class to access the model. |
698
|
|
|
$this->postSaveHook($model, $validData); |
699
|
|
|
|
700
|
|
|
return true; |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* Gets the URL arguments to append to an item redirect. |
705
|
|
|
* |
706
|
|
|
* @param integer $recordId The primary key id for the item. |
707
|
|
|
* @param string $urlVar The name of the URL variable for the id. |
708
|
|
|
* |
709
|
|
|
* @return string The arguments to append to the redirect URL. |
710
|
|
|
*/ |
711
|
|
|
protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id') |
712
|
|
|
{ |
713
|
|
|
$append = parent::getRedirectToItemAppend($recordId, $urlVar); |
714
|
|
|
|
715
|
|
|
$return = $this->input->get('return', '', 'Base64'); |
716
|
|
|
|
717
|
|
|
if ($return) |
718
|
|
|
{ |
719
|
|
|
$append .= '&return=' . $return; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
return $append; |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* Gets the URL arguments to append to a list redirect. |
727
|
|
|
* |
728
|
|
|
* @return string The arguments to append to the redirect URL. |
729
|
|
|
*/ |
730
|
|
|
protected function getRedirectToListAppend() |
731
|
|
|
{ |
732
|
|
|
$append = parent::getRedirectToListAppend(); |
733
|
|
|
|
734
|
|
|
$return = $this->input->get('return', '', 'Base64'); |
735
|
|
|
|
736
|
|
|
if ($return) |
737
|
|
|
{ |
738
|
|
|
$append .= '&return=' . $return; |
739
|
|
|
} |
740
|
|
|
|
741
|
|
|
return $append; |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
/** |
745
|
|
|
* Get the JRoute object for a redirect to list. |
746
|
|
|
* |
747
|
|
|
* @param string $append An optionnal string to append to the route |
748
|
|
|
* |
749
|
|
|
* @return JRoute The JRoute object |
750
|
|
|
*/ |
751
|
|
|
protected function getRedirectToListRoute($append = null) |
752
|
|
|
{ |
753
|
|
|
$returnUrl = $this->input->get('return', '', 'Base64'); |
754
|
|
|
|
755
|
|
|
if ($returnUrl) |
756
|
|
|
{ |
757
|
|
|
$returnUrl = base64_decode($returnUrl); |
758
|
|
|
|
759
|
|
|
if (!strstr($returnUrl, '?') && $append && $append[0] == '&') |
760
|
|
|
{ |
761
|
|
|
$append[0] = '?'; |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
return JRoute::_($returnUrl . $append, false); |
|
|
|
|
765
|
|
|
} |
766
|
|
|
else |
767
|
|
|
{ |
768
|
|
|
return JRoute::_('index.php?option=' . $this->option . '&view=' . $this->view_list . $append, false); |
|
|
|
|
769
|
|
|
} |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* Get the JRoute object for a redirect to item. |
774
|
|
|
* |
775
|
|
|
* @param string $append An optionnal string to append to the route |
776
|
|
|
* |
777
|
|
|
* @return JRoute The JRoute object |
778
|
|
|
*/ |
779
|
|
|
protected function getRedirectToItemRoute($append = null) |
780
|
|
|
{ |
781
|
|
|
return JRoute::_( |
|
|
|
|
782
|
|
|
'index.php?option=' . $this->option . '&view=' . $this->view_item |
783
|
|
|
. $append, false |
784
|
|
|
); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Get the data for form saving |
789
|
|
|
* Allows for subclasses to get data from multiple sources (e.g. $this->input->files) |
790
|
|
|
* |
791
|
|
|
* @return array |
792
|
|
|
*/ |
793
|
|
|
protected function getSaveData() |
794
|
|
|
{ |
795
|
|
|
return $this->input->post->get('jform', array(), 'array'); |
796
|
|
|
} |
797
|
|
|
} |
798
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..