-
Notifications
You must be signed in to change notification settings - Fork 0
/
sl7_slider.module
executable file
·459 lines (401 loc) · 13.8 KB
/
sl7_slider.module
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* @file
* The SL7 Slider module.
*
* @author Semyon Dragunov <[email protected]>
* https://github.com/SemyonDragunov
*/
define('SL7_SLIDER_ADMIN_PATH', SL7_CONTROL_PANEL_ADMIN_PATH . '/apps/slider');
/**
* Implements hook_menu().
*/
function sl7_slider_menu() {
$items[SL7_SLIDER_ADMIN_PATH] = array(
'title' => 'Слайдер',
'description' => 'Управление слайдами.',
'page callback' => 'drupal_get_form',
'page arguments' => array('sl7_slider_settings_form'),
'access arguments' => array('sl7_slider administer'),
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
return $items;
}
/**
* Implement hook_permission().
*/
function sl7_slider_permission() {
return array(
'sl7_slider administer' => array('title' => 'Управление слайдером.')
);
}
/**
* Implements hook_theme().
*/
function sl7_slider_theme() {
$items = array(
'sl7_slider' => array(
'variables' => array(
'slider_settings' => NULL,
'slides' => NULL,
),
'template' => 'templates/sl7-slider',
),
'sl7_slider_configuration_form' => array(
'render element' => 'form',
),
);
foreach ($items as &$item) {
if (!isset($item['file'])) {
$item['file'] = 'templates/theme.inc';
}
}
return $items;
}
/**
* Implements hook_sl7_slider_theme().
*/
function sl7_slider_sl7_slider_theme() {
$path = drupal_get_path('module', 'sl7_slider');
$themes['dark'] = array(
'title' => 'Тёмный',
'css' => $path . '/css/sl7_slider.css',
);
$themes['light'] = array(
'title' => 'Светлый',
'css' => $path . '/css/sl7_slider.css',
);
return $themes;
}
/**
* Получение массива тем для слайдера.
*
* @param null $theme
* Или машинное название темы или пустое значение (выдаст все темы).
* @return array
*/
function sl7_slider_get_theme($theme = NULL) {
$themes = &drupal_static(__FUNCTION__);
if (!isset($themes)) {
$themes = module_invoke_all('sl7_slider_theme');
foreach ($themes as $name => $info) {
$themes[$name] += array(
'css' => FALSE,
'js' => FALSE,
);
}
}
drupal_alter(__FUNCTION__, $themes);
if (array_key_exists($theme, $themes)) {
return $themes[$theme];
}
return $themes;
}
/**
* Получение списка машинных названий тем.
*
* @param null $theme
* Или машинное название темы или пустое значение (выдаст все темы).
* @return array
*/
function sl7_slider_get_theme_name($theme = NULL) {
$themes = sl7_slider_get_theme();
$theme_all = array();
foreach ($themes as $name => $options) {
$theme_all[$name] = $options['title'];
}
if (array_key_exists($theme, $theme_all)) {
return $theme_all[$theme];
}
return $theme_all;
}
/**
* Implements hook_block_info().
*/
function sl7_slider_block_info() {
$blocks['slider'] = array(
'info' => 'Слайдер',
'status' => 1,
'weight' => -10,
'cache' => DRUPAL_CACHE_GLOBAL,
'visibility' => BLOCK_VISIBILITY_NOTLISTED,
);
if (array_key_exists('slider', system_region_list(variable_get('theme_default')))) {
$blocks['slider']['region'] = 'slider';
}
else {
$blocks['slider']['region'] = 'front';
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function sl7_slider_block_view($delta = '') {
$block = array();
if ($delta == 'slider') {
$settings = variable_get('sl7_slider_settings');
$slides = variable_get('sl7_slider_slides');
$theme = sl7_slider_get_theme($settings['designStyle']);
if (isset($slides) && !empty($slides)) {
$prepare_slides = sl7_slider_prepare_slide($slides);
}
if (count($prepare_slides) >= 2) {
$block['content'] = array(
'#markup' => theme('sl7_slider', array(
'slider_settings' => $settings,
'slides' => $prepare_slides,
)),
'#attached' => array(
'js' => array(
drupal_get_path('module', 'sl7_slider') . '/js/sl7_slider.js',
drupal_get_path('module', 'sl7_slider') . '/vendors/owl.carousel/owl.carousel.min.js',
array('data' => $settings, 'type' => 'setting'),
),
'css' => array(
drupal_get_path('module', 'sl7_slider') . '/vendors/owl.carousel/owl.carousel.css',
drupal_get_path('module', 'sl7_slider') . '/vendors/owl.carousel/owl.transitions.css',
),
),
);
if ($theme['css']) {
$block['content']['#attached']['css'][] = $theme['css'];
}
if ($theme['js']) {
$block['content']['#attached']['js'][] = $theme['js'];
}
}
}
return $block;
}
/**
* Подготовка слайдов к показу. Отключение не опубликованных. Сортировка.
* @param $slides
* @return array
*/
function sl7_slider_prepare_slide($slides) {
$prepare_slides = array();
foreach ($slides as $i => $value) {
if ($value['published'] == TRUE) {
$weight = isset($value['weight']) ? $value['weight'] : 0;
while (isset($prepare_slides[$weight])) {
$weight++;
}
$prepare_slides[$weight] = $value;
}
}
return $prepare_slides;
}
/**
* Implements hook_contextual_links_view_alter().
*/
function sl7_slider_contextual_links_view_alter(&$element, &$items) {
if (isset($element['#element']['#block']) && $element['#element']['#block']->delta == "slider") {
$element['#links']['settings'] = array(
'title' => 'Настроить слайдер',
'href' => url(SL7_SLIDER_ADMIN_PATH),
);
}
}
function sl7_slider_settings_form($form, &$form_state) {
$settings = variable_get('sl7_slider_settings', array());
$form_state['slides'] = variable_get('sl7_slider_slides');
$form['#tree'] = TRUE;
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => 'Настройки',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['settings']['pagination'] = array(
'#type' => 'checkbox',
'#title' => 'Точки для переключения слайдов',
'#default_value' => isset($settings['pagination']) ? $settings['pagination'] : 0,
);
$form['settings']['navigation'] = array(
'#type' => 'checkbox',
'#title' => 'Кнопки управления: Назад, Вперед',
'#default_value' => isset($settings['navigation']) ? $settings['navigation'] : 0,
);
$form['settings']['designStyle'] = array(
'#type' => 'select',
'#title' => 'Стиль оформления переключателей',
'#options' => sl7_slider_get_theme_name(),
'#default_value' => isset($settings['designStyle']) ? $settings['designStyle'] : 'dark',
);
$form['settings']['autoPlay'] = array(
'#type' => 'textfield',
'#title' => 'Автоматическое переключение слайда',
'#description' => 'В миллисекундах. Если хотите выключить автоматическую смену слайдов, установите на 0.',
'#element_validate' => array('element_validate_number'),
'#required' => TRUE,
'#default_value' => isset($settings['autoPlay']) ? $settings['autoPlay'] : 5000,
);
$form['settings']['transitionStyle'] = array(
'#type' => 'select',
'#title' => 'Стиль переключения слайдов',
'#options' => array(
'fade' => 'Замещение',
'backSlide' => 'Конвейер',
'goDown' => 'Выпадение сверху',
'fadeUp' => 'Замещение с увеличением',
),
'#default_value' => isset($settings['transitionStyle']) ? $settings['transitionStyle'] : 'fade',
);
// Draggable table.
$form['order'] = array();
$form['tabs'] = array(
'#type' => 'vertical_tabs',
'#prefix' => '<div id="sliders">',
'#suffix' => '</div>',
);
$form['new'] = array(
'#type' => 'fieldset',
'#title' => 'Добавить слайд',
'#group' => 'tabs',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -10,
);
$form['new']['upload'] = array(
'#name' => 'files[upload]',
'#type' => 'file',
'#title' => 'Добавить слайд',
'#description' => 'Разрешенные форматы: gif png jpg jpeg. До 2 Мб.<br />Если слайдер будет расположен на главной странице сайта, то загружайте изображения с шириной не менее 1920px.<br />Так же загружайте одинаковые по размерам слайды.<br />Для начала показа загрузите 2 и более слайда.',
);
if (isset($form_state['slides']) && !empty($form_state['slides'])) {
foreach ($form_state['slides'] as $i => $value) {
$name = 'Слайд ' . $value['fid'];
$form['slides'][$i] = array(
'#type' => 'fieldset',
'#title' => $name,
'#group' => 'tabs',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['slides'][$i]['weight'] = array(
'#type' => 'weight',
'#delta' => 10,
'#default_value' => isset($value['weight']) ? $value['weight'] : 1,
'#attributes' => array(
'class' => array('slide-weight'),
),
);
$form['slides'][$i]['published'] = array(
'#type' => 'checkbox',
'#default_value' => isset($value['published']) ? $value['published'] : '',
);
$form['slides'][$i]['delete'] = array(
'#type' => 'checkbox',
'#default_value' => 0,
);
$file = file_load($value['fid']);
$variables = array(
'path' => $file->uri,
'style_name' => 'thumbnail',
);
$image = theme('image_style', $variables);
$form['slides'][$i]['preview'] = array(
'#markup' => $image,
);
$form['slides'][$i]['fid'] = array(
'#type' => 'hidden',
'#value' => isset($value['fid']) ? $value['fid'] : 0,
);
$form['slides'][$i]['link'] = array(
'#type' => 'textfield',
'#title' => 'Ссылка',
'#description' => 'Если укажите ссылку, то весь слайд станет ссылкой.',
'#default_value' => isset($value['link']) ? $value['link'] : '',
);
$form['slides'][$i]['link_target'] = array(
'#type' => 'select',
'#title' => 'Назначение ссылки',
'#options' => array(
'_self' => 'В этой же вкладке',
'_blank' => 'В новой вкладке',
),
'#default_value' => '_self',
'#states' => array(
'visible' => array(
":input[name='slides[$i][link]']" => array('!value' => '')
)
)
);
$form['slides'][$i]['title'] = array(
'#type' => 'textfield',
'#title' => 'Заголовок',
'#default_value' => isset($value['title']) ? $value['title'] : '',
);
$form['slides'][$i]['description'] = array(
'#type' => 'text_format',
'#title' => 'Текст слайда',
'#default_value' => isset($value['description']['value']) ? $value['description']['value'] : '',
'#format' => isset($value['description']['format']) ? $value['description']['format'] : 'plain_text',
);
}
}
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => 'Сохранить',
'#weight' => 999,
);
// Add a theme function to theme the form
$form['#theme'][] = 'sl7_slider_configuration_form';
return $form;
}
function sl7_slider_settings_form_add_slide($file) {
$file = is_object($file) ? $file : file_load($file);
$slide = array(
'fid' => $file->fid,
'title' => '',
'delete' => 0,
'description' => array(
'value' => '',
'format' => filter_fallback_format(),
),
);
return $slide;
}
function sl7_slider_settings_form_submit($form, &$form_state) {
if (isset($form_state['values']['slides']) && !empty($form_state['values']['slides'])) {
foreach ($form_state['values']['slides'] as $i => &$value) {
// Удаление слайдов.
if ($value['delete'] == 1) {
unset($form_state['values']['slides'][$i]);
if (isset($value['fid']) && $value['fid'] != 0) {
$file = file_load($value['fid']);
file_delete($file);
}
}
else {
unset($value['delete']);
// Смена статуса сохраненных файлов.
SL7ApiFile::setPermanent($value['fid'], 'sl7_slider', 'slide', $i);
}
}
}
// Создание нового слайда.
$file = new stdClass;
$validate = array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('gif png jpg jpeg'),
'file_validate_size' => array(1024*1024*2),
);
if ($file = file_save_upload('upload', $validate)) {
$folder = 'public://sl7_slider';
file_prepare_directory($folder, FILE_CREATE_DIRECTORY);
$parts = pathinfo($file->filename);
$destination = $folder . '/' . $parts['basename'];
$file->status = FILE_STATUS_PERMANENT;
$file = file_copy($file, $destination, FILE_EXISTS_REPLACE);
$form_state['values']['slides'][] = sl7_slider_settings_form_add_slide($file);
}
// Пересортируем.
sort($form_state['values']['slides']);
// Сохраняем.
variable_set('sl7_slider_settings', $form_state['values']['settings']);
variable_set('sl7_slider_slides', $form_state['values']['slides']);
drupal_set_message('Настройки слайдера сохранены.');
}