/
bot.php
425 lines (352 loc) · 12.5 KB
/
bot.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?php
/**
*------------------------------------------------------------------------------
* @package T3 Framework for Joomla!
*------------------------------------------------------------------------------
* @copyright Copyright (C) 2004-2013 JoomlArt.com. All Rights Reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
* @authors JoomlArt, JoomlaBamboo, (contribute to this project at github
* & Google group to become co-author)
* @Google group: https://groups.google.com/forum/#!forum/t3fw
* @Link: http://t3-framework.org
*------------------------------------------------------------------------------
*/
// No direct access
defined('_JEXEC') or die();
/**
* T3Bot class
* Auto trigger
*
* @package T3
*/
class T3Bot extends JObject
{
// call before checking & loading T3
public static function preload () {
// check if menu is alter, then turn a flag to reupdate megamenu configuration
$input = JFactory::getApplication()->input;
if ($input->get('option') == 'com_menus' &&
preg_match('#save|apply|trash|remove|delete|publish|order#i', $input->get('task'))) {
// get all template styles
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query
->select('*')
->from('#__template_styles')
->where('client_id=0');
$db->setQuery($query);
$themes = $db->loadObjectList();
//update all global parameters
foreach($themes as $theme){
$registry = new JRegistry;
$registry->loadString($theme->params);
$mm_config = $registry->get('mm_config');
if (!$mm_config) continue;
// turn on flag
$registry->set('mm_config_needupdate', 1); //overwrite with new value
$query = $db->getQuery(true);
$query
->update('#__template_styles')
->set('params =' . $db->quote($registry->toString()))
->where('id =' . (int)$theme->id);
$db->setQuery($query);
$db->execute();
}
// force reload cache template
$cache = JFactory::getCache('com_templates', '');
$cache->clean();
}
}
// call before call T3::init
public static function beforeInit () {
}
// call after call T3::init
public static function afterInit () {
$app = JFactory::getApplication();
$input = $app->input;
$tplparams = $app->getTemplate(true)->params;
if (!$app->isAdmin()) {
// check if need update megamenu configuration
if ($tplparams->get ('mm_config_needupdate')) {
T3::import('menu/megamenu');
T3::import('admin/megamenu');
$currentconfig = @json_decode($tplparams->get ('mm_config', ''), true);
if (!is_array($currentconfig)){
$currentconfig = array();
} else {
$menuassoc = T3AdminMegamenu::menus();
$menulangs = array();
$menutypes = array();
foreach ($menuassoc as $key => $massoc) {
$menutypes[] = $massoc->value;
$menulangs[$massoc->value] = $massoc->language;
}
}
foreach ($currentconfig as $menukey => $mmconfig) {
if (!is_array($mmconfig)){
continue;
}
$menutype = $menukey;
if(!in_array($menutype, $menutypes) && preg_match('@(-(\d))+$@', $menukey, $match)){
$menutype = preg_replace('@(-(\d))+$@', '', $menutype);
$access = explode('-', $match[0]);
$access[] = 1;
$access = array_filter($access);
$access = array_unique($access);
$mmconfig['access'] = $access;
}
if(!in_array($menutype, $menutypes)){
continue;
}
$mmconfig['language'] = $menulangs[$menutype];
$menu = new T3MenuMegamenu ($menutype, $mmconfig);
$children = $menu->get ('children');
//remove additional settings
unset($mmconfig['language']);
unset($mmconfig['access']);
foreach ($mmconfig as $item => $setting) {
if (is_array($setting) && isset($setting['sub'])) {
$sub = &$setting['sub'];
$id = (int) substr($item, 5); // remove item-
$modify = false;
if (!isset($children[$id]) || !count ($children[$id])){
//check and remove any empty row
for ($j=0; $j < count($sub['rows']); $j++) {
$remove = true;
for ($k=0; $k < count($sub['rows'][$j]); $k++) {
if (isset($sub['rows'][$j][$k]['position'])) {
$remove = false;
break;
}
}
if($remove){
$modify = true;
unset($sub['rows'][$j]);
}
}
if($modify){
$sub['rows'] = array_values($sub['rows']); //re-index
$mmconfig[$item]['sub'] = $sub;
}
continue;
}
$items = array();
foreach ($sub['rows'] as $row) {
foreach ($row as $col) {
if (!isset($col['position'])) {
$items[] = $col['item'];
}
}
}
// update the order of items
$_items = array();
$_itemsids = array();
$firstitem = 0;
foreach ($children[$id] as $child) {
$_itemsids[] = (int)$child->id;
if (!$firstitem) $firstitem = (int)$child->id;
if (in_array($child->id, $items)) {
$_items [] = (int)$child->id;
}
}
// $_items[0] = $firstitem;
if (empty($_items) || $_items[0] != $firstitem) {
if (count ($_items) == count($items)) {
$_items[0] = $firstitem;
} else {
array_splice($_items, 0, 0, $firstitem);
}
}
// no need update config for this item
if ($items == $_items) continue;
// update back to setting
$i = 0;
$c = count ($_items);
for ($j=0; $j < count($sub['rows']); $j++) {
for ($k=0; $k < count($sub['rows'][$j]); $k++) {
if (!isset($sub['rows'][$j][$k]['position'])) {
$sub['rows'][$j][$k]['item'] = $i < $c ? $_items[$i++] : "";
}
}
}
//update - add new rows for new items - at the first rows
if(!empty($_items) && count($items) == 0){
$modify = true;
array_unshift($sub['rows'], array(array('item' => $_items[0], 'width' => 12)));
}
//check and remove any empty row
for ($j=0; $j < count($sub['rows']); $j++) {
$remove = true;
for ($k=0; $k < count($sub['rows'][$j]); $k++) {
if (isset($sub['rows'][$j][$k]['position']) || in_array($sub['rows'][$j][$k]['item'], $_itemsids)) {
$remove = false;
break;
}
}
if($remove){
$modify = true;
unset($sub['rows'][$j]);
}
}
if($modify){
$sub['rows'] = array_values($sub['rows']); //re-index
}
$mmconfig[$item]['sub'] = $sub;
}
}
$currentconfig[$menukey] = $mmconfig;
}
// update megamenu back to other template styles parameter
$mm_config = json_encode($currentconfig, JSON_UNESCAPED_UNICODE);
// update megamenu back to current template style parameter
$template = $app->getTemplate(true);
$params = $template->params;
$params->set ('mm_config', $mm_config);
$template->params = $params;
//update the cache
T3::setTemplate(T3_TEMPLATE, $params);
//get all other styles that have the same template
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query
->select('*')
->from('#__template_styles')
->where('template=' . $db->quote(T3_TEMPLATE))
->where('client_id=0');
$db->setQuery($query);
$themes = $db->loadObjectList();
//update all global parameters
foreach($themes as $theme){
$registry = new JRegistry;
$registry->loadString($theme->params);
$registry->set('mm_config', $mm_config); //overwrite with new value
$registry->set('mm_config_needupdate', ""); //overwrite with new value
$query = $db->getQuery(true);
$query
->update('#__template_styles')
->set('params =' . $db->quote($registry->toString()))
->where('id =' . (int)$theme->id);
$db->setQuery($query);
$db->execute();
}
// force reload cache template
$cache = JFactory::getCache('com_templates', '');
$cache->clean();
}
}
}
// call when prepare form for template parameter
// looking in less/extras folder to render parameters for extended template style
public static function prepareForm (&$form) {
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
// load add-ons setting
$path = T3_TEMPLATE_PATH . '/less/extras';
if (!is_dir ($path)) return ;
$files = JFolder::files($path, '.less');
if (!$files || !count($files)){
return ;
}
$extras = array();
foreach ($files as $file) {
$extras[] = JFile::stripExt($file);
}
if (count($extras)) {
//load languages
if(!defined('T3_TEMPLATE')){
JFactory::getLanguage()->load(T3_PLUGIN, JPATH_ADMINISTRATOR);
}
$_xml =
'<?xml version="1.0"?>
<form>
<fields name="params">
<fieldset name="addon_params" label="T3_ADDON_LABEL" description="T3_ADDON_DESC">
<field type="t3depend" name="t3_addon_theme_extra" function="@legend" label="T3_ADDON_THEME_EXTRAS_LABEL" description="T3_ADDON_THEME_EXTRAS_DESC" />
';
foreach ($extras as $extra) {
$_xml .= '
<field name="theme_extras_'.$extra.'" global="1" type="menuitem" multiple="true" default="" label="'.$extra.'" description="'.$extra.'" published="1" class="t3-extra-setting">
<option value="-1">T3_ADDON_THEME_EXTRAS_ALL</option>
<option value="0">T3_ADDON_THEME_EXTRAS_NONE</option>
</field>';
}
$_xml .= '
</fieldset>
</fields>
</form>
';
$xml = simplexml_load_string($_xml);
$form->load ($xml, false);
}
}
public static function extraFields(&$form, $data, $tplpath){
if ($form->getName() == 'com_categories.categorycom_content' || $form->getName() == 'com_content.article') {
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
// check for extrafields overwrite
$path = $tplpath . '/etc/extrafields';
if (!is_dir ($path)) return ;
$files = JFolder::files($path, '.xml');
if (!$files || !count($files)){
return ;
}
$extras = array();
foreach ($files as $file) {
$extras[] = JFile::stripExt($file);
}
if (count($extras)) {
if ($form->getName() == 'com_categories.categorycom_content'){
//load languages
if(!defined('T3_TEMPLATE')){
JFactory::getLanguage()->load(T3_PLUGIN, JPATH_ADMINISTRATOR);
}
$_xml =
'<?xml version="1.0"?>
<form>
<fields name="params">
<fieldset name="t3_extrafields_params" label="T3_EXTRA_FIELDS_GROUP_LABEL" description="T3_EXTRA_FIELDS_GROUP_DESC">
<field name="t3_extrafields" type="list" default="" show_none="true" label="T3_EXTRA_FIELDS_LABEL" description="T3_EXTRA_FIELDS_DESC">
<option value="">JNONE</option>';
foreach ($extras as $extra) {
$_xml .= '<option value="' . $extra . '">' . ucfirst($extra) . '</option>';
}
$_xml .= '
</field>
</fieldset>
</fields>
</form>
';
$xml = simplexml_load_string($_xml);
$form->load ($xml, false);
} else {
$app = JFactory::getApplication();
$input = $app->input;
$fdata = empty($data) ? $input->post->get('jform', array(), 'array') : (is_object($data) ? $data->getProperties() : $data);
$catid = $input->getInt('catid', $app->getUserState('com_content.articles.filter.category_id'));
if(!$catid && is_array($fdata) && !empty($fdata)){
$catid = $fdata['catid'];
}
if($catid){
if(version_compare(JVERSION, '3.0', 'lt')){
jimport('joomla.application.categories');
}
$categories = JCategories::getInstance('Content', array('countItems' => 0 ));
$category = $categories->get($catid);
$params = $category->params;
if(!$params instanceof JRegistry) {
$params = new JRegistry;
$params->loadString($category->params);
}
if($params instanceof JRegistry){
$extrafile = $path . '/' . $params->get('t3_extrafields') . '.xml';
if(is_file($extrafile)){
JForm::addFormPath($path);
$form->loadFile($params->get('t3_extrafields'), false);
}
}
}
}
}
}
}
}