-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.php
More file actions
174 lines (154 loc) · 5.39 KB
/
index.php
File metadata and controls
174 lines (154 loc) · 5.39 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
<?php
/*
Plugin Name: Группировка характеристик товаров
Description: Сортирует характеристики по группам если были найдены совпадения в карточке товара, Шорткод: [group-property id=""] где id="" id товара, если совпадения по группам не найдены то все характеристики выводятся списком как обычно.
Author: Румянцев Олег
Version: 0.1
*/
new GroupProperty;
class GroupProperty
{
private static $lang = array();
private static $pluginName = 'group_property';
private static $path;
public function __construct()
{
mgActivateThisPlugin(__FILE__, array(__CLASS__, 'activate'));
mgAddAction(__FILE__, array(__CLASS__, 'pageSettingsPlugin'));
mgAddShortcode('group-property', array(__CLASS__, 'handlerShortcode'));
self::$path = PLUGIN_DIR.'group-property';
}
static function activate()
{
self::createTable();
}
static function createTable()
{
DB::query("
CREATE TABLE IF NOT EXISTS `".PREFIX.self::$pluginName."` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Порядковый номер записи',
`name` text NULL COMMENT 'Имя группы',
`property` text NULL COMMENT 'Характеристики',
`sort` int(11) NULL COMMENT 'Порядок сортировки',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;");
if(MG::getSetting(self::$pluginName.'-option') == null){
$arPluginParams = array(
'delete_property' => 'false'
);
MG::setOption(array('option' => self::$pluginName.'-option', 'value' => addslashes(serialize($arPluginParams))));
}
}
static function prepareSettingsPage()
{
echo '
<link rel="stylesheet" href="'.SITE.'/'.self::$path.'/css/settings.css" type="text/css" />
<script type="text/javascript">
includeJS("'.SITE.'/'.self::$path.'/js/settings.js");
</script>
';
}
// Возвращает ids характеристик которые уже распределенны по
// группам
static function getGroupProperty(){
$groups = self::getEntity();
if(!empty($groups)){
$ids = array();
foreach($groups as $group){
if(!empty($group['property'])){
foreach($group['property'] as $property){
$ids[] = $property['id'];
}
}
}
return $ids;
}
}
static function getAllProperty(){
$ids = self::getGroupProperty();
$result = array();
if(!empty($ids)){
$ids = implode($ids, ', ');
$query = DB::query("SELECT id, name FROM `".PREFIX."property` WHERE id NOT IN({$ids}) ORDER BY id DESC");
if(DB::numRows($query) != 0){
while($row = DB::fetchAssoc($query)){
$result[] = $row;
}
}
} else {
$query = DB::query("SELECT id, name FROM `".PREFIX."property` ORDER BY id DESC");
if(DB::numRows($query) != 0){
while($row = DB::fetchAssoc($query)){
$result[] = $row;
}
}
}
return $result;
}
static function getEntity($id = null){
if($id){
$query = DB::query("SELECT * FROM `".PREFIX.self::$pluginName."` WHERE id={$id}");
} else {
$query = DB::query("SELECT * FROM `".PREFIX.self::$pluginName."` ORDER BY `sort`");
}
$result = array();
if(DB::numRows($query) != 0){
while($row = DB::fetchAssoc($query)){
$isDelete = false;
$propertys = unserialize($row['property']);
if(!empty($propertys)){
foreach($propertys as $key => $val){
$q_property = DB::query("SELECT id, name FROM `".PREFIX."property` WHERE id = ".$key."");
if(DB::numRows($q_property) != 0){
$r_property = DB::fetchAssoc($q_property);
$propertys[$key] = $r_property;
} else {
// Если характеристика была удаленна из системы то соотвественно необходимо так же удалить ее из группы
unset($propertys[$key]);
$isDelete = true;
}
$row['property'] = $propertys;
}
}
if($isDelete){
$data = array();
foreach($row['property'] as $item){
$data[$item['id']] = $item['id'];
}
$save = array(
'property' => serialize($data)
);
DB::query('UPDATE `'.PREFIX.self::$pluginName.'` SET '.DB::buildPartQuery($save).' WHERE id = '.$row['id'].'');
}
$result[] = $row;
}
}
return $result;
}
static function getPluginOptions(){
$option = MG::getSetting(self::$pluginName.'-option');
$option = stripslashes($option);
return unserialize($option);
}
static function pageSettingsPlugin()
{
$pluginName = self::$pluginName;
$entity = self::getEntity();
$allProperty = self::getAllProperty();
$options = self::getPluginOptions();
self::prepareSettingsPage();
include('page-settings.php');
}
static function handlerShortcode($args){
$model = new Models_Product;
$product = $model->getProduct($args['id']);
$data = array(
'thisUserFields' => $product['thisUserFields'],
'groupProperty' => self::getEntity()
);
$realDocumentRoot = str_replace(DIRECTORY_SEPARATOR.'mg-plugins'.DIRECTORY_SEPARATOR.'group-property', '', dirname(__FILE__));
ob_start();
include($realDocumentRoot.DIRECTORY_SEPARATOR.self::$path.DIRECTORY_SEPARATOR.'printPropertys.php');
return ob_get_clean();
}
}