1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 15 16 17 18
19 class JTableCategory extends JTableNested
20 {
21 22 23 24 25 26 27
28 public function __construct(JDatabaseDriver $db)
29 {
30 parent::__construct('#__categories', 'id', $db);
31
32 JTableObserverTags::createObserver($this, array('typeAlias' => '{extension}.category'));
33 JTableObserverContenthistory::createObserver($this, array('typeAlias' => '{extension}.category'));
34
35 $this->access = (int) JFactory::getConfig()->get('access');
36 }
37
38 39 40 41 42 43 44 45 46
47 protected function _getAssetName()
48 {
49 $k = $this->_tbl_key;
50
51 return $this->extension . '.category.' . (int) $this->$k;
52 }
53
54 55 56 57 58 59 60
61 protected function _getAssetTitle()
62 {
63 return $this->title;
64 }
65
66 67 68 69 70 71 72 73 74 75
76 protected function _getAssetParentId(JTable $table = null, $id = null)
77 {
78 $assetId = null;
79
80
81 if ($this->parent_id > 1)
82 {
83
84 $query = $this->_db->getQuery(true)
85 ->select($this->_db->quoteName('asset_id'))
86 ->from($this->_db->quoteName('#__categories'))
87 ->where($this->_db->quoteName('id') . ' = ' . $this->parent_id);
88
89
90 $this->_db->setQuery($query);
91
92 if ($result = $this->_db->loadResult())
93 {
94 $assetId = (int) $result;
95 }
96 }
97
98 elseif ($assetId === null)
99 {
100
101 $query = $this->_db->getQuery(true)
102 ->select($this->_db->quoteName('id'))
103 ->from($this->_db->quoteName('#__assets'))
104 ->where($this->_db->quoteName('name') . ' = ' . $this->_db->quote($this->extension));
105
106
107 $this->_db->setQuery($query);
108
109 if ($result = $this->_db->loadResult())
110 {
111 $assetId = (int) $result;
112 }
113 }
114
115
116 if ($assetId)
117 {
118 return $assetId;
119 }
120 else
121 {
122 return parent::_getAssetParentId($table, $id);
123 }
124 }
125
126 127 128 129 130 131 132 133
134 public function check()
135 {
136
137 if (trim($this->title) == '')
138 {
139 $this->setError(JText::_('JLIB_DATABASE_ERROR_MUSTCONTAIN_A_TITLE_CATEGORY'));
140
141 return false;
142 }
143
144 $this->alias = trim($this->alias);
145
146 if (empty($this->alias))
147 {
148 $this->alias = $this->title;
149 }
150
151 $this->alias = JApplicationHelper::stringURLSafe($this->alias, $this->language);
152
153 if (trim(str_replace('-', '', $this->alias)) == '')
154 {
155 $this->alias = JFactory::getDate()->format('Y-m-d-H-i-s');
156 }
157
158 return true;
159 }
160
161 162 163 164 165 166 167 168 169 170 171 172
173 public function bind($array, $ignore = '')
174 {
175 if (isset($array['params']) && is_array($array['params']))
176 {
177 $registry = new Registry($array['params']);
178 $array['params'] = (string) $registry;
179 }
180
181 if (isset($array['metadata']) && is_array($array['metadata']))
182 {
183 $registry = new Registry($array['metadata']);
184 $array['metadata'] = (string) $registry;
185 }
186
187
188 if (isset($array['rules']) && is_array($array['rules']))
189 {
190 $rules = new JAccessRules($array['rules']);
191 $this->setRules($rules);
192 }
193
194 return parent::bind($array, $ignore);
195 }
196
197 198 199 200 201 202 203 204 205
206 public function store($updateNulls = false)
207 {
208 $date = JFactory::getDate();
209 $user = JFactory::getUser();
210
211 $this->modified_time = $date->toSql();
212
213 if ($this->id)
214 {
215
216 $this->modified_user_id = $user->get('id');
217 }
218 else
219 {
220
221
222 if (!(int) $this->created_time)
223 {
224 $this->created_time = $date->toSql();
225 }
226
227 if (empty($this->created_user_id))
228 {
229 $this->created_user_id = $user->get('id');
230 }
231 }
232
233
234 $table = JTable::getInstance('Category', 'JTable', array('dbo' => $this->getDbo()));
235
236 if ($table->load(array('alias' => $this->alias, 'parent_id' => (int) $this->parent_id, 'extension' => $this->extension))
237 && ($table->id != $this->id || $this->id == 0))
238 {
239 $this->setError(JText::_('JLIB_DATABASE_ERROR_CATEGORY_UNIQUE_ALIAS'));
240
241 return false;
242 }
243
244 return parent::store($updateNulls);
245 }
246 }
247