-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathnotification.php
More file actions
329 lines (287 loc) · 7.57 KB
/
Copy pathnotification.php
File metadata and controls
329 lines (287 loc) · 7.57 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* @package Com_Tjnotification
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access to this file
defined('_JEXEC') or die;
jimport('joomla.application.component.model');
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjnotifications/models');
use Joomla\CMS\Table\Table;
use Joomla\CMS\Factory;
/**
* notification model.
*
* @since 1.6
*/
class TjnotificationsModelNotification extends JModelAdmin
{
/**
* Constructor.
*
* @param array $config An optional associative array of configuration settings.
*
* @since 3.2
*/
public function __construct($config = array())
{
$config['event_after_save'] = 'tjnOnAfterSaveNotificationTemplate';
parent::__construct($config);
}
/**
* Returns a reference to the a Table object, always creating it.
*
* @param string $type The table type to instantiate
* @param string $prefix A prefix for the table class name. Optional.
* @param array $config Configuration array for model. Optional.
*
* @return JTable A database object
*
* @since 1.6
*/
public function getTable($type='Notification',$prefix='tjnotificationTable',$config=array())
{
// Get the table.
return JTable::getInstance($type, $prefix, $config);
}
/**
* Method to get the record form.
*
* @param array $data An optional array of data for the form to interogate.
* @param boolean $loadData True if the form is to load its own data (default case), false if not.
*
* @return JForm 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_tjnotifications.notification',
'notification',
array
(
'control' => 'jform',
'load_data' => $loadData
)
);
if (empty($form))
{
return false;
}
return $form;
}
/**
* Batch copy items to a new category or current.
*
* @return mixed An array of new IDs on success, boolean false on failure.
*
* @since 1.0
*/
protected function loadFormData()
{
$extension = JFactory::getApplication()->input->get('extension', '', 'word');
$parts = explode('.', $extension);
// Extract the component name
$this->setState('filter.component', $parts[0]);
// Extract the optional section name
$this->setState('filter.section', (count($parts) > 1) ? $parts[1] : null);
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState(
'com_tjnotifications.edit.tjnotifications.data',
array()
);
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
/**
* Method to create notifications templates.
*
* @param array $templates An array of data for the notifications templates.
*
* @return Boolean true or false
*
* @since 1.6
*/
public function createTemplates($templates)
{
$data = $templates;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$model = JModelList::getInstance('Notifications', 'TJNotificationsModel');
if (!empty($data['replacement_tags']))
{
$data['replacement_tags'] = json_encode($data['replacement_tags']);
}
if ($data['client'] and $data['key'])
{
$model->setState('filter.client', $data['client']);
$model->setState('filter.key', $data['key']);
$result = $model->getItems();
foreach ($result as $res)
{
if ($res->id)
{
$data['id'] = $res->id;
}
}
}
if ($data)
{
parent::save($data);
return true;
}
else
{
return false;
}
}
/**
* Method to delete notification template
*
* @param int &$cid Id of template.
*
* @return void
*
* @since 1.0
*/
public function delete(&$cid)
{
$db = JFactory::getDbo();
$deleteQuery = $db->getQuery(true);
$value = array();
$model = JModelAdmin::getInstance('Notification', 'TJNotificationsModel');
foreach ($cid as $id)
{
$data = $model->getItem($id);
if ($data->core == 0)
{
$deleteQuery = $db->getQuery(true);
$conditions = array(
$db->quoteName('client') . ' = ' . $db->quote($data->client),
$db->quoteName('key') . ' = ' . $db->quote($data->key)
);
$deleteQuery->delete($db->quoteName('#__tj_notification_user_exclusions'));
$deleteQuery->where($conditions);
$db->setQuery($deleteQuery);
$result = $db->execute();
if ($result)
{
$value[] = 1;
parent::delete($data->id);
$dispatcher = JDispatcher::getInstance();
JPluginHelper::importPlugin('tjnotification');
$dispatcher->trigger('tjnOnAfterDeleteNotificationTemplate', array($data));
}
}
else
{
$value[] = 0;
}
}
return $value;
}
/**
* Method to get existing keys
*
* @param string $client client.
*
* @return existingkeys
*
* @since 1.0
*/
public function getKeys($client)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('key'));
$query->from($db->quoteName('#__tj_notification_templates'));
$query->where($db->quoteName('client') . ' = ' . $db->quote($client));
$db->setQuery($query);
$existingKeys = $db->loadColumn();
return $existingKeys;
}
/**
* Method to get tag replacement count
*
* @param string $key Template key.
* @param string $client client.
*
* @return integer replacement tags count
*
* @since 1.0.4
*/
public function getReplacementTagsCount($key, $client)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('replacement_tags'));
$query->from($db->quoteName('#__tj_notification_templates'));
$query->where($db->quoteName('client') . ' = ' . $db->quote($client));
$query->where($db->quoteName('key') . ' = ' . $db->quote($key));
$db->setQuery($query);
$replacementTags = $db->loadResult();
return count(json_decode($replacementTags));
}
/**
* Method to replace tags if they are changed
*
* @param array $data template data
*
* @return void
*
* @since 1.0.4
*/
public function updateReplacementTags($data)
{
if (!empty($data['replacement_tags']))
{
$replacementTags = json_encode($data['replacement_tags']);
}
else
{
return;
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
// Fields to update.
$fields = array(
$db->quoteName('replacement_tags') . ' = ' . $db->quote($replacementTags)
);
// Conditions for which records should be updated.
$conditions = array(
$db->quoteName('client') . ' = ' . $db->quote($data['client']),
$db->quoteName('key') . ' = ' . $db->quote($data['key'])
);
$query->update($db->quoteName('#__tj_notification_templates'))->set($fields)->where($conditions);
$db->setQuery($query);
$result = $db->execute();
}
/**
* Method to get Sample data for email template.
*
* @return string $body
*
* @since __DEPLOY_VERSION__
*/
public static function getSampleBodyData($id)
{
Table::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjnotifications/tables');
$storeTable = Table::getInstance('Notification', 'TjnotificationTable');
$storeTable->load($id);
$bodyTemplate = $storeTable->email_body;
$replacementsdata = $storeTable->replacement_tags;
$replacements = json_decode($replacementsdata);
foreach ($replacements as $value)
{
$replaceWith = !empty($value->sampledata) ? $value->sampledata : $value->name;
$bodyTemplate = str_replace($value->name, $replaceWith, $bodyTemplate);
}
return $bodyTemplate;
}
}