-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ConfigModel.php
177 lines (149 loc) · 3.52 KB
/
ConfigModel.php
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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_messages
*
* @copyright Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Messages\Administrator\Model;
defined('_JEXEC') or die;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\MVC\Model\FormModel;
use Joomla\CMS\Factory;
/**
* Message configuration model.
*
* @since 1.6
*/
class ConfigModel extends FormModel
{
/**
* Method to auto-populate the model state.
*
* This method should only be called once per instantiation and is designed
* to be called on the first call to the getState() method unless the model
* configuration flag to ignore the request is set.
*
* Note. Calling getState in this method will result in recursion.
*
* @return void
*
* @since 1.6
*/
protected function populateState()
{
$user = Factory::getUser();
$this->setState('user.id', $user->get('id'));
// Load the parameters.
$params = ComponentHelper::getParams('com_messages');
$this->setState('params', $params);
}
/**
* Method to get a single record.
*
* @return mixed Object on success, false on failure.
*
* @since 1.6
*/
public function &getItem()
{
$item = new \JObject;
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('cfg_name, cfg_value')
->from('#__messages_cfg')
->where($db->quoteName('user_id') . ' = ' . (int) $this->getState('user.id'));
$db->setQuery($query);
try
{
$rows = $db->loadObjectList();
}
catch (\RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
foreach ($rows as $row)
{
$item->set($row->cfg_name, $row->cfg_value);
}
$this->preprocessData('com_messages.config', $item);
return $item;
}
/**
* Method to get the record 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 \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_messages.config', 'config', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
/**
* Method to save the form data.
*
* @param array $data The form data.
*
* @return boolean True on success.
*
* @since 1.6
*/
public function save($data)
{
$db = $this->getDbo();
if ($userId = (int) $this->getState('user.id'))
{
$query = $db->getQuery(true)
->delete($db->quoteName('#__messages_cfg'))
->where($db->quoteName('user_id') . '=' . (int) $userId);
$db->setQuery($query);
try
{
$db->execute();
}
catch (\RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
if (count($data))
{
$query = $db->getQuery(true)
->insert($db->quoteName('#__messages_cfg'))
->columns($db->quoteName(array('user_id', 'cfg_name', 'cfg_value')));
foreach ($data as $k => $v)
{
$query->values($userId . ', ' . $db->quote($k) . ', ' . $db->quote($v));
}
$db->setQuery($query);
try
{
$db->execute();
}
catch (\RuntimeException $e)
{
$this->setError($e->getMessage());
return false;
}
}
return true;
}
else
{
$this->setError('COM_MESSAGES_ERR_INVALID_USER');
return false;
}
}
}