language.php
5.88 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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_languages
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\Utilities\ArrayHelper;
/**
* Languages Component Language Model
*
* @since 1.5
*/
class LanguagesModelLanguage extends JModelAdmin
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*/
public function __construct($config = array())
{
$config = array_merge(
array(
'event_after_save' => 'onExtensionAfterSave',
'event_before_save' => 'onExtensionBeforeSave',
'events_map' => array(
'save' => 'extension'
)
), $config
);
parent::__construct($config);
}
/**
* Override to get the table.
*
* @param string $name Name of the table.
* @param string $prefix Table name prefix.
* @param array $options Array of options.
*
* @return JTable
*
* @since 1.6
*/
public function getTable($name = '', $prefix = '', $options = array())
{
return JTable::getInstance('Language');
}
/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
*
* @since 1.6
*/
protected function populateState()
{
$app = JFactory::getApplication('administrator');
$params = JComponentHelper::getParams('com_languages');
// Load the User state.
$langId = $app->input->getInt('lang_id');
$this->setState('language.id', $langId);
// Load the parameters.
$this->setState('params', $params);
}
/**
* Method to get a member item.
*
* @param integer $langId The id of the member to get.
*
* @return mixed User data object on success, false on failure.
*
* @since 1.0
*/
public function getItem($langId = null)
{
$langId = (!empty($langId)) ? $langId : (int) $this->getState('language.id');
// Get a member row instance.
$table = $this->getTable();
// Attempt to load the row.
$return = $table->load($langId);
// Check for a table object error.
if ($return === false && $table->getError())
{
$this->setError($table->getError());
return false;
}
// Set a valid accesslevel in case '0' is stored due to a bug in the installation SQL (was fixed with PR 2714).
if ($table->access == '0')
{
$table->access = (int) JFactory::getConfig()->get('access');
}
$properties = $table->getProperties(1);
$value = ArrayHelper::toObject($properties, 'JObject');
return $value;
}
/**
* Method to get the group form.
*
* @param array $data Data for the form.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return mixed A JForm object on success, false on failure.
*
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Get the form.
$form = $this->loadForm('com_languages.language', 'language', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to get the data that should be injected in the form.
*
* @return mixed The data for the form.
*
* @since 1.6
*/
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_languages.edit.language.data', array());
if (empty($data))
{
$data = $this->getItem();
}
$this->preprocessData('com_languages.language', $data);
return $data;
}
/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success.
*
* @since 1.6
*/
public function save($data)
{
$langId = (!empty($data['lang_id'])) ? $data['lang_id'] : (int) $this->getState('language.id');
$isNew = true;
$dispatcher = JEventDispatcher::getInstance();
JPluginHelper::importPlugin($this->events_map['save']);
$table = $this->getTable();
$context = $this->option . '.' . $this->name;
// Load the row if saving an existing item.
if ($langId > 0)
{
$table->load($langId);
$isNew = false;
}
// Prevent white spaces, including East Asian double bytes.
$spaces = array('/\xE3\x80\x80/', ' ');
$data['lang_code'] = str_replace($spaces, '', $data['lang_code']);
// Prevent saving an incorrect language tag
if (!preg_match('#\b([a-z]{2,3})[-]([A-Z]{2})\b#', $data['lang_code']))
{
$this->setError(JText::_('COM_LANGUAGES_ERROR_LANG_TAG'));
return false;
}
$data['sef'] = str_replace($spaces, '', $data['sef']);
$data['sef'] = JApplicationHelper::stringURLSafe($data['sef']);
// Bind the data.
if (!$table->bind($data))
{
$this->setError($table->getError());
return false;
}
// Check the data.
if (!$table->check())
{
$this->setError($table->getError());
return false;
}
// Trigger the before save event.
$result = $dispatcher->trigger($this->event_before_save, array($context, &$table, $isNew));
// Check the event responses.
if (in_array(false, $result, true))
{
$this->setError($table->getError());
return false;
}
// Store the data.
if (!$table->store())
{
$this->setError($table->getError());
return false;
}
// Trigger the after save event.
$dispatcher->trigger($this->event_after_save, array($context, &$table, $isNew));
$this->setState('language.id', $table->lang_id);
// Clean the cache.
$this->cleanCache();
return true;
}
/**
* Custom clean cache method.
*
* @param string $group Optional cache group name.
* @param integer $client_id Application client id.
*
* @return void
*
* @since 1.6
*/
protected function cleanCache($group = null, $client_id = 0)
{
parent::cleanCache('_system');
parent::cleanCache('com_languages');
}
}