-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathincptvk2multiplecategories.php
296 lines (243 loc) · 11.3 KB
/
incptvk2multiplecategories.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
<?php
/**
* @package Inceptive Multiple Categories for K2
* @author Inceptive Design Labs - http://www.inceptive.gr
* @copyright Copyright (c) 2016 Inceptive Design Labs. All rights reserved.
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
require_once("helpers/incptvk2multiplecategories.php");
// Load the base K2 plugin class. All K2 plugins extend this class.
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/'.'components'.'/'.'com_k2'.'/'.'lib'.'/'.'k2plugin.php');
class plgK2Incptvk2multiplecategories extends K2Plugin
{
// K2 plugin name. Used to namespace parameters.
var $pluginName = 'incptvk2multiplecategories';
// K2 human readable plugin name. This the title of the plugin users see in K2 form.
var $pluginNameHumanReadable = 'Inceptive Multiple Categories for K2';
var $plg_copyrights_start = "\n\n<!-- Inceptive Multiple Categories for K2 Plugin starts here -->\n";
var $plg_copyrights_end = "\n<!-- Inceptive Multiple Categories for K2 Plugin ends here -->\n\n";
// Constructor
public function __construct(&$subject, $config)
{
// Construct
parent::__construct($subject, $config);
}
function onK2BeforeSetQuery(&$query)
{
$mainframe = JFactory::getApplication();
if($mainframe->isAdmin())
{
$view= JRequest::getCmd('view');
if($view == "items")
{
$isCount = strpos($query, 'COUNT');
if($isCount == 0)
{
$queryAddition = " ADDITIONAL_CATEGORIES_TAG_TO_BE_REPLACED ";
// $queryAddition = " ,(SELECT GROUP_CONCAT(ca.name)
// FROM #__k2_multiple_categories as mci
// LEFT JOIN #__k2_categories as ca on ca.id = mci.catID
// WHERE mci.itemID = i.id AND mci.catID != i.catid) as mcnames ";
$query = substr_replace($query, $queryAddition, strpos($query, "FROM")).substr($query, strpos($query, "FROM"));
}
}
$query = str_replace('i.*', 'DISTINCT i.*', $query);
//Filtering K2 items by K2 category
if(strpos($query, 'AND i.catid'))
{
$pos = strpos($query, 'WHERE');
$first = substr($query, 0 , $pos);
$second = substr($query, $pos);
$queryAddition = " LEFT JOIN #__k2_multiple_categories as mc ON mc.itemID = i.id ";
$query = $first.$queryAddition.str_replace("AND i.catid", "AND mc.catID", $second);
}
if(strpos($query, 'AND catid'))
{
$pos = strpos($query, 'WHERE');
$first = substr($query, 0 , $pos);
$second = substr($query, $pos);
$queryAddition = " LEFT JOIN #__k2_multiple_categories as mc ON mc.itemID = i.id ";
$query = $first.$queryAddition.str_replace("AND catid", "AND mc.catID", $second);
}
$query = str_replace("ORDER BY i.catid","ORDER BY mc.catID",$query);
$queryAddition = ",(SELECT GROUP_CONCAT(ca.name)
FROM #__k2_multiple_categories as mci
LEFT JOIN #__k2_categories as ca on ca.id = mci.catID
WHERE mci.itemID = i.id AND mci.catID != i.catid) as mcnames";
$query = str_replace("ADDITIONAL_CATEGORIES_TAG_TO_BE_REPLACED",$queryAddition,$query);
}
if ($mainframe->isSite())
{
$task = JRequest::getCmd('task');
$view= JRequest::getCmd('view');
$layout= JRequest::getCmd('layout');
//Presenting one category
if(($task=='category' && $view=='itemlist') || ($task=='user' && $view=='itemlist') || ($layout=='category' && $view=='itemlist' && $task == "") || ($task=='tag' && $view=='itemlist') || ($task=='date' && $view=='itemlist'))
{
if(strpos($query, 'AND c.id'))
{
$isCount = strpos($query, 'COUNT');
if($isCount != 0)
{
$query = str_replace('COUNT(*)', 'COUNT(DISTINCT i.id)', $query);
}
else
{
$query = str_replace('i.*', 'DISTINCT i.*', $query);
}
$pos = strpos($query, 'WHERE');
$first = substr($query, 0 , $pos);
$second = substr($query, $pos);
$queryAddition = " RIGHT JOIN #__k2_multiple_categories as mc ON mc.itemID = i.id ";
$query = $first.$queryAddition.str_replace("AND c.id", "AND mc.catID", $second);
}
}
//Presenting multiple categories
if($layout=='category' && $view=='itemlist')
{
if(strpos($query, 'AND i.catid'))
{
$isCount = strpos($query, 'COUNT');
if($isCount != 0)
{
$query = str_replace('COUNT(*)', 'COUNT(DISTINCT i.id)', $query);
}
else
{
$query = str_replace('i.*', 'DISTINCT i.*', $query);
}
$pos = strpos($query, 'WHERE');
$first = substr($query, 0 , $pos);
$second = substr($query, $pos);
$queryAddition = " RIGHT JOIN #__k2_multiple_categories as mc ON mc.itemID = i.id ";
$query = $first.$queryAddition.str_replace("AND i.catid", "AND mc.catID", $second);
}
}
$query = str_replace("c.ordering,", '', $query);
}
return;
}
function onAfterK2Save (& $item, $isNew) {
global $mainframe;
$db = &JFactory::getDBO();
/* Checking if some Extended fields to process */
$request_params = JRequest::getVar('plugins', array());
$fields = (!empty($request_params['incptvk2multiplecategories'])) ? $request_params['incptvk2multiplecategories']: array();
array_push($fields, $item->catid) ;
$fields = array_diff(array_unique($fields), array('0'));
/* Checking if the no categories were changed */
$selectedCategories = array();
$rows = &JTable::getInstance('IncptvK2MultipleCategories', 'Table');
$retrievedCategories = $rows->getSomeObjectsList('SELECT * FROM #__k2_multiple_categories mc WHERE mc.itemID = '.$item->id);
foreach ($retrievedCategories as $retrievedCategory) { array_push($selectedCategories, $retrievedCategory->catID); }
/* Evaluating the difference of the previous and current categories arrays */
$dif1 = array_diff($fields,$selectedCategories);
$dif2 = array_diff($selectedCategories,$fields);
if(!empty($dif1) OR !empty($dif2))
{
//new JTable instance to save the new added values
$row_action = &JTable::getInstance('IncptvK2MultipleCategories', 'Table');
$row_action->delete($item->id);
$row_action->change_key("id",$db);
foreach($fields as $catID){
$from = array( 'id' => 0,
'itemID' => $item->id,
'catID' => $catID
);
$row_action->bind($from);
$row_action->store();
}
if (!$row_action->check()) {
$mainframe->redirect('index.php?option=com_k2&view=item&cid='.$item->id, $row_action->getError(), 'error');
}
if (!$row_action->store()) {
$mainframe->redirect('index.php?option=com_k2&view=items', $row_action->getError(), 'error');
}
}
return '';
}
function onRenderAdminForm (&$item, $type, $tab='') {
if ($tab == 'other' && $type == 'item') {
$mainframe = JFactory::getApplication();
$selectedCategories = array();
if($item->id != 0)
{
$db = JFactory::getDBO();
$rows = JTable::getInstance('IncptvK2MultipleCategories', 'Table');
$retrievedCategories = $rows->getSomeObjectsList('SELECT * FROM #__k2_multiple_categories mc WHERE mc.itemID = '.$item->id);
foreach ($retrievedCategories as $retrievedCategory) {
if($retrievedCategory->catID != $item->catid)
array_push($selectedCategories, $retrievedCategory->catID);
}
}
if(empty($selectedCategories))
array_push($selectedCategories, 0);
$document = JFactory::getDocument();
$path = str_replace("administrator/", "",JURI::base());
$plugin_folder = basename(dirname(__FILE__));
$document->addScript($path.'plugins/k2/'.$plugin_folder.'/js/incptvk2multiplecategories.js');
$document->addStyleSheet($path.'plugins/k2/'.$plugin_folder.'/css/style.css');
//Loading the appropriate language files
$lang = JFactory::getLanguage();
$languagePath = JPATH_PLUGINS.DS.'k2'.DS.'incptvk2multiplecategories';
$lang->load("plg_k2_incptvk2multiplecategories", $languagePath, null, false);
$categoriesModel = K2Model::getInstance('Categories', 'K2Model');
$categories_option[] = JHTML::_('select.option', 0, JText::_('K2_SELECT_CATEGORY'));
$categories = $categoriesModel->categoriesTree(NUll, true, false);
if ($mainframe->isSite())
{
$task = JRequest::getCmd('task');
JLoader::register('K2HelperPermissions', JPATH_SITE.DS.'components'.DS.'com_k2'.DS.'helpers'.DS.'permissions.php');
if (isset($task) && ($task == 'add' || $task == 'edit') && !K2HelperPermissions::canAddToAll())
{
for ($i = 0; $i < sizeof($categories); $i++)
{
if (!K2HelperPermissions::canAddItem($categories[$i]->value) && $task == 'add')
{
$categories[$i]->disable = true;
}
if (!K2HelperPermissions::canEditItem($item->created_by, $categories[$i]->value) && $task == 'edit')
{
$categories[$i]->disable = true;
}
}
}
}
for ($i = 0; $i < sizeof($categories); $i++)
{
if($categories[$i]->value == $item->catid)
$categories[$i]->disable = true;
}
$categories_options = @array_merge($categories_option, $categories);
$tabIncptvMC_innerHtml = '<table class="admintable"><tbody>';
$tabIncptvMC_innerHtml .='<tr><td class="key">'.JText::_('PLG_K2_MC_ADDITIONAL_CATEGORIES_LABEL').'</td>';
$tabIncptvMC_innerHtml .='<td class="adminK2RightCol">';
$tabIncptvMC_innerHtml .= JHTML::_('select.genericlist', $categories_options, 'plugins[incptvk2multiplecategories][]', 'style="width:100%;" multiple="multiple" size="10"', 'value', 'text', $selectedCategories);
$tabIncptvMC_innerHtml .= '</td></tr>';
$tabIncptvMC_innerHtml .='</tbody></table>';
$tabIncptvMC_innerHtmlK2ge27 = '<div class="itemAdditionalField">';
$tabIncptvMC_innerHtmlK2ge27 .= '<div class="k2FLeft k2Right itemAdditionalValue">';
$tabIncptvMC_innerHtmlK2ge27 .= '<label>'.JText::_('PLG_K2_MC_ADDITIONAL_CATEGORIES_LABEL').'</label>';
$tabIncptvMC_innerHtmlK2ge27 .= '</div>';
$tabIncptvMC_innerHtmlK2ge27 .= '<div class="itemAdditionalData">';
$tabIncptvMC_innerHtmlK2ge27 .= JHTML::_('select.genericlist', $categories_options, 'plugins[incptvk2multiplecategories][]', 'style="width:100%;" multiple="multiple" size="10"', 'value', 'text', $selectedCategories);
$tabIncptvMC_innerHtmlK2ge27 .= '</div>';
$tabIncptvMC_innerHtmlK2ge27 .= '</div>';
$tabIncptvMC = '<li id="tabIncptvMC">
<a href="#k2TabIncptvMC" id="tabIncptvMCold">'.JText::_('PLG_K2_MC_MULTIPLE_CATEGORIES_LABEL').'</a>
<a href="#k2TabIncptvMC" id="tabIncptvMCK2ge27"><i class="fa fa-clone" aria-hidden="true"></i> '.JText::_('PLG_K2_MC_MULTIPLE_CATEGORIES_LABEL').'</a>
</li>';
$tabIncptvMC_content = '<div id="k2TabIncptvMC" class="simpleTabsContent k2TabsContent k2TabsContentLower" >' . $tabIncptvMC_innerHtml . $tabIncptvMC_innerHtmlK2ge27 . '</div>';
echo $tabIncptvMC.$tabIncptvMC_content;
}
}
public function onK2BeforeAssignColumns(&$columns)
{
$lang = JFactory::getLanguage();
$languagePath = JPATH_PLUGINS.DS.'k2'.DS.'incptvk2multiplecategories';
$lang->load("plg_k2_incptvk2multiplecategories", $languagePath, null, false);
$columns = array( (object) array('label'=>JText::_('PLG_K2_MC_ADDITIONAL_CATEGORIES_LABEL'),'property'=>'mcnames', 'class'=>''));
}
}