-
Notifications
You must be signed in to change notification settings - Fork 1
/
sl7_catalog.install
executable file
·361 lines (339 loc) · 11.4 KB
/
sl7_catalog.install
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
<?php
/**
* @author Semyon Dragunov <[email protected]>
* https://github.com/SemyonDragunov
*/
/**
* Implements hook_install().
*/
function sl7_catalog_install() {
$t = get_t();
_sl7_catalog_create_taxonomy();
_sl7_catalog_create_node_type();
// Set path breadcrumbs settings.
variable_set('path_breadcrumbs_home_link_title', 'Главная');
// Set pathauto pattern
variable_set('pathauto_taxonomy_term_sl7_catalog_category_pattern', 'catalog/[term:parents:join-path]/[term:name]');
variable_set('pathauto_node_sl7_catalog_product_pattern', 'catalog/[node:sl7-catalog-product-category:parents:join-path]/[node:sl7-catalog-product-category:name]/[node:title]');
}
function _sl7_catalog_create_node_type() {
node_types_rebuild();
$types = node_type_get_types();
node_add_body_field($types['sl7_catalog_product'], 'Описание');
variable_set("comment_{$types['sl7_catalog_product']->type}", 1);
variable_set("node_preview_{$types['sl7_catalog_product']->type}", 0);
variable_set("node_options_{$types['sl7_catalog_product']->type}", array('status'));
variable_set("node_submitted_{$types['sl7_catalog_product']->type}", 0);
$fields = array(
'sl7_catalog_product_category' => array(
'field_name' => 'sl7_catalog_product_category',
'type' => 'taxonomy_term_reference',
'cardinality' => 1,
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'sl7_catalog_category',
'parent' => 0
),
),
),
),
'sl7_catalog_product_image' => array(
'field_name' => 'sl7_catalog_product_image',
'type' => 'image',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'module' => 'image',
'locked' => TRUE,
'settings' => array(
'uri_scheme' => 'public',
'default_image' => 0,
),
'foreign keys' => array(
'fid' => array(
'columns' => array(
'fid' => 'fid',
),
'table' => 'file_managed',
),
),
'indexes' => array(
'fid' => array(
0 => 'fid',
),
),
),
'sl7_catalog_product_price' => array(
'field_name' => 'sl7_catalog_product_price',
'label' => 'Стоимость товара',
'locked' => TRUE,
'type' => 'text',
'settings' => array(
'max_length' => 20,
)
),
'sl7_catalog_extra_output' => array(
'field_name' => 'sl7_catalog_extra_output',
'type' => 'taxonomy_term_reference',
'label' => 'Специальный вывод',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'locked' => TRUE,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => 'sl7_catalog_extra_output',
'parent' => 0
),
),
),
),
);
foreach ($fields as $field) {
$exists = db_query("SELECT id FROM {field_config} WHERE field_name=:field_name", array(':field_name' => $field['field_name']))->fetchField();
if (!$exists) {
field_create_field($field);
}
}
$instances = array(
'sl7_catalog_product_category' => array(
'field_name' => 'sl7_catalog_product_category',
'description' => '<a href="/admin/structure/taxonomy/sl7_catalog_category" target="_blank">Управление категориями.</a>',
'entity_type' => 'node',
'label' => 'Категория',
'bundle' => 'sl7_catalog_product',
'required' => true,
'widget' => array(
'type' => 'options_select'
),
'display' => array(
'default' => array('type' => 'hidden'),
'teaser' => array('type' => 'hidden')
),
),
'sl7_catalog_product_image' => array(
'field_name' => 'sl7_catalog_product_image',
'entity_type' => 'node',
'bundle' => 'sl7_catalog_product',
'required' => TRUE,
'label' => 'Изображение',
'settings' => array(
'max_filesize' => 1024*1024*2,
'file_extensions' => 'png gif jpg jpeg',
'file_directory' => 'sl7_catalog',
),
'widget' => array(
'type' => 'image_image',
'module' => 'image',
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'galleryformatter_default',
'settings' => array(
'slide_style' => 'sl7_catalog_product_slide',
'thumb_style' => 'galleryformatter_thumb',
'modal' => 'colorbox',
'linking_method' => 'onclick_full',
),
'weight' => -2,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array(
'image_style' => 'sl7_catalog_product'
),
),
),
),
'sl7_catalog_product_price' => array(
'field_name' => 'sl7_catalog_product_price',
'entity_type' => 'node',
'bundle' => 'sl7_catalog_product',
'type' => 'text',
'label' => 'Стоимость товара',
'widget' => array(
'type' => 'text_textfield'
),
'display' => array(
'default' => array(
'label' => 'inline',
'type' => 'text',
'weight' => -1,
)
)
),
'sl7_catalog_extra_output' => array(
'field_name' => 'sl7_catalog_extra_output',
'description' => 'Если необходимо выводить этот товар в особых местах. Можно указать несколько категорий сразу, через удержание кнопки Ctrl.<br />Дополнительные места вывода Вы можете заказать через <a href="http://elsa.sl-7.ru/billmgr" target="_blank">создание запроса</a>.',
'entity_type' => 'node',
'label' => 'Специальный вывод',
'bundle' => 'sl7_catalog_product',
'widget' => array(
'type' => 'options_select',
'weight' => 50,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'hidden',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'hidden',
)
),
),
);
foreach ($instances as $instance) {
$exists = db_query("SELECT id FROM {field_config_instance} WHERE field_name=:field_name AND entity_type=:entity_type AND bundle=:bundle", array(':field_name' => $instance['field_name'], ':entity_type' => $instance['entity_type'], ':bundle' => $instance['bundle']))->fetchField();
if (!$exists) {
field_create_instance($instance);
}
}
}
function _sl7_catalog_create_taxonomy() {
// Create taxonomy vocabulary.
$vocab1 = (object) array(
'name' => 'Специальный вывод товаров',
'description' => 'Места дополнительного вывода товара.',
'machine_name' => 'sl7_catalog_extra_output',
);
if (!taxonomy_vocabulary_machine_name_load($vocab1->machine_name)) {
taxonomy_vocabulary_save($vocab1);
$term_front_page = (object) array(
'name' => 'Главная страница',
'vid' => $vocab1->vid,
);
taxonomy_term_save($term_front_page);
variable_set('sl7_catalog_extra_output_front_page_tid', $term_front_page->tid);
}
$vocab2 = (object) array(
'name' => 'Каталог',
'description' => 'Каталог продукции или услуг.',
'machine_name' => 'sl7_catalog_category',
);
if (!taxonomy_vocabulary_machine_name_load($vocab2->machine_name)) {
taxonomy_vocabulary_save($vocab2);
}
$fields = array(
'sl7_catalog_category_image' => array(
'field_name' => 'sl7_catalog_category_image',
'type' => 'image',
'cardinality' => 9,
'module' => 'image',
'locked' => TRUE,
'settings' => array(
'uri_scheme' => 'public',
'default_image' => 0,
),
'foreign keys' => array(
'fid' => array(
'columns' => array(
'fid' => 'fid',
),
'table' => 'file_managed',
),
),
'indexes' => array(
'fid' => array(
0 => 'fid',
),
),
),
);
foreach ($fields as $field) {
$exists = db_query("SELECT id FROM {field_config} WHERE field_name=:field_name", array(':field_name' => $field['field_name']))->fetchField();
if (!$exists) {
field_create_field($field);
}
}
$instances = array(
'sl7_catalog_category_image' => array(
'field_name' => 'sl7_catalog_category_image',
'entity_type' => 'taxonomy_term',
'bundle' => $vocab2->machine_name,
'required' => TRUE,
'label' => 'Изображение',
'settings' => array(
'max_filesize' => 1024*1024*2,
'file_extensions' => 'png gif jpg jpeg',
'file_directory' => 'sl7_catalog',
),
'widget' => array(
'type' => 'image_image',
'module' => 'image',
'weight' => 1,
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array(
'image_style' => 'sl7_catalog_category'
),
),
),
),
);
foreach ($instances as $instance) {
$exists = db_query("SELECT id FROM {field_config_instance} WHERE field_name=:field_name AND entity_type=:entity_type AND bundle=:bundle", array(':field_name' => $instance['field_name'], ':entity_type' => $instance['entity_type'], ':bundle' => $instance['bundle']))->fetchField();
if (!$exists) {
field_create_instance($instance);
}
}
}
/**
* Implements hook_uninstall().
*/
function sl7_catalog_uninstall() {
// Удаление нод sl7_catalog_product
$node_type = 'sl7_catalog_product';
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type';
$result = db_query($sql, array(':type' => $node_type));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
node_delete_multiple($nids);
node_type_delete($node_type);
// Удаление полей
field_attach_delete_bundle('node', 'sl7_catalog_product');
db_delete('field_config_instance')->condition('entity_type', 'node')->condition('bundle', 'sl7_catalog_product')->execute();
db_delete('field_config_instance')
->condition('field_name', 'comment_body')
->condition('bundle', 'comment_node_sl7_catalog_product')
->execute();
field_attach_delete_bundle('taxonomy_term', 'sl7_catalog_category');
db_delete('field_config_instance')->condition('entity_type', 'taxonomy_term')->condition('bundle', 'sl7_catalog_category')->execute();
db_delete('field_config')->condition('field_name', 'sl7_catalog_category_%', 'LIKE')->execute();
// Удаление таксономии
$vocabs = array(
'sl7_catalog_category',
'sl7_catalog_extra_output',
);
foreach ($vocabs as $name) {
if ($vocab = taxonomy_vocabulary_machine_name_load($name)) {
taxonomy_vocabulary_delete($vocab->vid);
}
}
// Удаление views
$views = array(
'sl7_catalog_category',
'sl7_catalog_product',
);
foreach ($views as $name) {
if ($view = views_get_view($name)) {
$view->delete();
}
}
// Delete variables.
$result = db_select('variable', 'v')
->fields('v', array('name'))
->condition('name', 'sl7_catalog_%', 'LIKE')
->execute();
foreach ($result as $row) {
variable_del($row->name);
}
field_purge_batch(1000);
}