-
Notifications
You must be signed in to change notification settings - Fork 0
/
breakpoints.module
1125 lines (1036 loc) · 32.5 KB
/
breakpoints.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Breakpoints
* @todo: provide button to reload breakpoints from theme
*/
define('BREAKPOINTS_SOURCE_TYPE_THEME', 'theme');
define('BREAKPOINTS_SOURCE_TYPE_MODULE', 'module');
define('BREAKPOINTS_SOURCE_TYPE_CUSTOM', 'custom');
define('BREAKPOINTS_GROUP', 'group');
/**
* Implements hook_permission().
*/
function breakpoints_permission() {
return array(
'administer breakpoints' => array(
'title' => t('Administer Breakpoints'),
'description' => t('Administer all breakpoints'),
),
);
}
/**
* Implements hook_ctools_plugin_directory().
*/
function breakpoints_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && $plugin == 'export_ui') {
return 'plugins/' . $plugin;
}
}
/**
* Implements hook_ctools_plugin_api().
*/
function breakpoints_ctools_plugin_api($owner, $api) {
return array('version' => 1);
}
/**
* Implements hook_enable().
*
* Import breakpoints from all enabled themes.
*/
function breakpoints_enable() {
$themes = list_themes();
foreach ($themes as $theme_key => $theme) {
if (!$theme->status) {
unset($themes[$theme_key]);
}
}
breakpoints_themes_enabled(array_keys($themes));
}
/**
* Implements hook_themes_enabled();
*
* Import breakpoints from all new enabled themes.
* Do not use breakpoints_breakpoints_group_reload_from_theme as is clears the cache.
*/
function breakpoints_themes_enabled($theme_list, $rebuild_menu = TRUE) {
$themes = list_themes();
$updated = FALSE;
$new_breakpoint_group = FALSE;
foreach ($theme_list as $theme_key) {
if (isset($themes[$theme_key]->info['breakpoints'])) {
$updated = TRUE;
$weight = 0;
$theme_settings = $themes[$theme_key]->info['breakpoints'];
$multipliers = isset($themes[$theme_key]->info['multipliers']) ? $themes[$theme_key]->info['multipliers'] : array();
$settings = breakpoints_settings();
$current_multipliers = backdrop_map_assoc($settings->multipliers);
$breakpoint_group = breakpoints_breakpoint_group_load($theme_key);
if(!$breakpoint_group) {
// Build a group for each theme
$new_breakpoint_group = TRUE;
$breakpoint_group = breakpoints_breakpoint_group_empty_object();
$breakpoint_group->machine_name = $theme_key;
$breakpoint_group->name = $themes[$theme_key]->info['name'];
$breakpoint_group->type = BREAKPOINTS_SOURCE_TYPE_THEME;
}
foreach ($theme_settings as $name => $media_query) {
$breakpoint = breakpoints_breakpoint_load($name, $theme_key, 'theme');
if (!$breakpoint) {
$breakpoint = breakpoints_breakpoint_empty_object();
$breakpoint->name = $name;
$breakpoint->source = $theme_key;
$breakpoint->source_type = 'theme';
$breakpoint->theme = '';
$breakpoint->status = TRUE;
$breakpoint->weight = $weight++;
$breakpoint->machine_name = breakpoints_breakpoint_config_name($breakpoint);
}
$breakpoint->breakpoint = $media_query;
$breakpoint->multipliers = isset($multipliers[$name]) ? backdrop_map_assoc($multipliers[$name]) : array();
$current_multipliers += backdrop_map_assoc($breakpoint->multipliers);
breakpoints_breakpoint_save($breakpoint);
$breakpoint_group->breakpoints[] = $breakpoint->machine_name;
}
breakpoints_settings_save($current_multipliers);
breakpoints_breakpoint_group_save($breakpoint_group);
if($new_breakpoint_group) {
$message_text = 'The breakpoints from theme %theme are imported and <a href="@url">a new group is created</a>.';
} else {
$message_text = 'The breakpoints from theme %theme are imported and <a href="@url">an existing group was updated</a>.';
}
$message = t($message_text, array(
'%theme' => $themes[$theme_key]->info['name'],
'@url' => url('admin/config/media/breakpoints/groups/' . $theme_key),
));
backdrop_set_message($message, 'status');
}
}
if ($rebuild_menu && $updated) {
config_set('breakpoints.settings','menu_rebuild_needed', TRUE);
}
}
/**
* Implements hook_themes_disabled();
* Remove breakpoints from all disabled themes.
*/
function breakpoints_themes_disabled($theme_list) {
$themes = list_themes();
foreach ($theme_list as $theme_key) {
$breakpoints = breakpoints_breakpoint_load_all_theme($theme_key);
foreach ($breakpoints as $breakpoint) {
breakpoints_breakpoint_delete($breakpoint);
}
breakpoints_breakpoint_group_delete_by_name($theme_key);
}
config_set('breakpoints.settings','menu_rebuild_needed', TRUE);
}
/**
* Implements hook_menu().
*/
function breakpoints_menu() {
$items = array();
// @todo: link to all breakpoints and a list of all groups
// cf theme settings page
$items['admin/config/media/breakpoints'] = array(
'title' => 'Breakpoints',
'description' => 'Manage breakpoints',
'page callback' => 'drupal_get_form',
'page arguments' => array('breakpoints_admin_breakpoints'),
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
);
$items['admin/config/media/breakpoints/create_style'] = array(
'title' => 'Add responsive style',
'description' => 'Add a responsive image style',
'page callback' => 'drupal_get_form',
'page arguments' => array('breakpoints_add_style_form'),
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 30,
);
$items['admin/config/media/breakpoints/multipliers'] = array(
'title' => 'Multipliers',
'description' => 'Manage multipliers',
'page callback' => 'drupal_get_form',
'page arguments' => array('breakpoints_multipliers_form'),
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
$items['admin/config/media/breakpoints/settings'] = array(
'title' => 'Settings',
'description' => 'Manage breakpoint settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('breakpoints_admin_settings_form'),
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 30,
);
$items['admin/config/media/breakpoints/multipliers/%/delete'] = array(
'title' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array('breakpoints_admin_multiplier_delete_form', 5),
'type' => MENU_CALLBACK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$items['admin/config/media/breakpoints/groups'] = array(
'title' => 'Groups',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 10,
);
$items['admin/config/media/breakpoints/groups/global'] = array(
'title' => 'All breakpoints',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -1,
);
$items['admin/config/media/breakpoints/groups/add'] = array(
'title' => 'Add a new group',
'page arguments' => array('breakpoints_admin_breakpoint_group_edit_form'),
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 99,
);
$items['admin/config/media/breakpoints/groups/import'] = array(
'title' => 'Import a new group',
'page arguments' => array('breakpoints_admin_breakpoint_group_import_form'),
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 99,
);
$items['admin/config/media/breakpoints/groups/import-breakpoint'] = array(
'title' => 'Import a new breakpoint',
'page arguments' => array('breakpoints_admin_breakpoint_import_form'),
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 99,
);
$items['admin/config/media/breakpoints/%/%'] = array(
'title' => '',
'page callback' => 'breakpoints_admin_breakpoint_actions_page',
'page arguments' => array('', 4, 5),
'type' => MENU_CALLBACK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$breakpoint_groups = breakpoints_breakpoint_group_load_all();
foreach ($breakpoint_groups as $breakpoint_group_name => $breakpoint_group) {
if (!empty($breakpoint_group->machine_name)) {
$items['admin/config/media/breakpoints/groups/' . $breakpoint_group->machine_name] = array(
'title' => $breakpoint_group->name,
'page arguments' => array('breakpoints_admin_breakpoints', $breakpoint_group->machine_name),
'type' => MENU_LOCAL_TASK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$items['admin/config/media/breakpoints/groups/' . $breakpoint_group->machine_name . '/edit'] = array(
'title' => 'Edit ' . $breakpoint_group->name,
'page arguments' => array('breakpoints_admin_breakpoint_group_edit_form', $breakpoint_group->machine_name),
'type' => MENU_CALLBACK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$items['admin/config/media/breakpoints/groups/' . $breakpoint_group->machine_name . '/delete'] = array(
'title' => 'Delete ' . $breakpoint_group->name,
'page arguments' => array('breakpoints_admin_breakpoint_group_delete_form', $breakpoint_group->machine_name),
'type' => MENU_CALLBACK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$items['admin/config/media/breakpoints/groups/' . $breakpoint_group->machine_name . '/export'] = array(
'title' => 'Export ' . $breakpoint_group->name,
'page arguments' => array('breakpoints_admin_breakpoint_group_export_form', $breakpoint_group->machine_name),
'type' => MENU_LOCAL_ACTION,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$items['admin/config/media/breakpoints/groups/' . $breakpoint_group->machine_name . '/duplicate'] = array(
'title' => 'Duplicate ' . $breakpoint_group->name,
'page arguments' => array('breakpoints_admin_breakpoint_group_duplicate_form', $breakpoint_group->machine_name),
'type' => MENU_CALLBACK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
$items['admin/config/media/breakpoints/groups/' . $breakpoint_group->machine_name . '/%/%'] = array(
'title' => '',
'page arguments' => array('breakpoints_admin_breakpoint_actions_form', $breakpoint_group->machine_name, 6, 7),
'type' => MENU_CALLBACK,
'access arguments' => array('administer breakpoints'),
'file' => 'breakpoints.admin.inc',
'weight' => 15,
);
}
}
return $items;
}
/**
* Load general settings.
*/
function breakpoints_settings() {
$config = new StdClass;
$config->multipliers = config_get('breakpoints.settings','breakpoints_multipliers');
return $config;
}
/**
* Save general settings.
*
* @param $multipliers
*/
function breakpoints_settings_save($multipliers) {
config_set('breakpoints.settings','breakpoints_multipliers', $multipliers);
}
/**
* Sort breakpoints by weight.
*
* @param $a
* @param $b
*
* @return int
*/
function _breakpoints_sort_by_weight($a, $b) {
if (isset($a->weight) && isset($b->weight)) {
if ($a->weight == $b->weight) {
if (isset($a->source_type) && $a->source_type == BREAKPOINTS_SOURCE_TYPE_CUSTOM) {
return -1;
}
if (isset($b->source_type) && $b->source_type == BREAKPOINTS_SOURCE_TYPE_CUSTOM) {
return 1;
}
return 0;
}
return ($a->weight < $b->weight) ? -1 : 1;
}
return 0;
}
/**
* Sort breakpoints by weight.
*
* @param $a
* @param $b
*
* @return int
*/
function _breakpoints_sort_by_weight_array($a, $b) {
return _breakpoints_sort_by_weight((object) $a, (object) $b);
}
/**
* Construct config name.
*
* @param $breakpoints_breakpoint
*
* @return string
*/
function breakpoints_breakpoint_config_name($breakpoints_breakpoint) {
if (is_string($breakpoints_breakpoint)) {
return $breakpoints_breakpoint;
}
else {
return backdrop_strtolower('breakpoints'
. '.' . $breakpoints_breakpoint->source_type
. '.' . $breakpoints_breakpoint->source
. '.' . $breakpoints_breakpoint->name);
}
}
/**
* Load a single breakpoint.
*
* @param $name
* @param $source
* @param $source_type
*
* @return array|bool|mixed
*/
function breakpoints_breakpoint_load($name, $source, $source_type) {
$key = backdrop_strtolower('breakpoints.' . $source_type . '.' . $source . '.' . $name);
return breakpoints_breakpoint_load_by_fullkey($key);
}
/**
* Load a single breakpoint using the full config key.
*
* @param null $machine_name
*
* @return array|bool|mixed
*/
function breakpoints_breakpoint_load_by_fullkey($machine_name = NULL) {
$breakpoints = &backdrop_static(__FUNCTION__);
if (!isset($breakpoints)) {
$breakpoints = _breakpoints_breakpoint_load_all_callback();
}
if ($machine_name) {
$breakpoint = isset($breakpoints[$machine_name]) ? $breakpoints[$machine_name] : FALSE;
return $breakpoint;
}
else {
return $breakpoints;
}
}
/**
* CTools export 'load all callback' CRUD callback for breakpoints.
*/
function _breakpoints_breakpoint_load_all_callback($reset = FALSE) {
if (!$reset && $cache = cache_get('breakpoints:breakpoints')) {
return $cache->data;
}
else {
ctools_include('export');
$breakpoints = ctools_export_load_object('breakpoints', 'all');
cache_set('breakpoints:breakpoints', $breakpoints);
return $breakpoints;
}
}
/**
* Load all breakpoints.
*/
function breakpoints_breakpoint_load_all($theme_key = '') {
$breakpoints_user = breakpoints_breakpoint_load_all_custom();
$breakpoints_module = breakpoints_breakpoint_load_all_module();
$breakpoints_theme = breakpoints_breakpoint_load_all_theme($theme_key);
$breakpoints = array_merge($breakpoints_theme, $breakpoints_module, $breakpoints_user);
uasort($breakpoints, '_breakpoints_sort_by_weight');
return $breakpoints;
}
/**
* Load all enabled breakpoints.
*/
function breakpoints_breakpoint_load_all_active($theme_key = '') {
$breakpoints = breakpoints_breakpoint_load_all($theme_key);
$enabled = array();
if (!empty($breakpoints)) {
foreach ($breakpoints as $breakpoint_name => $breakpoint) {
if ($breakpoint->status) {
$enabled[$breakpoint_name] = $breakpoint;
}
}
}
return $enabled;
}
/**
* Load all breakpoints by source type.
*
* @param $source_type
* @param string $source
*
* @return array|bool|mixed
*/
function _breakpoints_breakpoint_load_all_by_type($source_type, $source = '') {
$breakpoints = breakpoints_breakpoint_load_by_fullkey();
foreach ($breakpoints as $machine_name => $breakpoint) {
if ($breakpoint->source_type != $source_type) {
unset($breakpoints[$machine_name]);
continue;
}
if ($source != '' && $breakpoint->source != $source) {
unset($breakpoints[$machine_name]);
}
}
return $breakpoints;
}
/**
* Load all custom breakpoints.
*
* @return array|bool|mixed
*/
function breakpoints_breakpoint_load_all_custom() {
$breakpoints = _breakpoints_breakpoint_load_all_by_type(BREAKPOINTS_SOURCE_TYPE_CUSTOM);
return $breakpoints;
}
/**
* Load all user defined breakpoints.
*
* @return array|bool|mixed
*/
function breakpoints_breakpoint_load_all_module() {
return _breakpoints_breakpoint_load_all_by_type(BREAKPOINTS_SOURCE_TYPE_MODULE);
}
/**
* Load all breakpoints from the theme.
*
* @param string $theme_key
*
* @return array|bool|mixed
*/
function breakpoints_breakpoint_load_all_theme($theme_key = '') {
return _breakpoints_breakpoint_load_all_by_type(BREAKPOINTS_SOURCE_TYPE_THEME, $theme_key);
}
/**
* Empty breakpoint object.
*
* @return object
*/
function breakpoints_breakpoint_empty_object() {
return (object) breakpoints_breakpoint_empty_array();
}
/**
* Empty breakpoint array.
*
* @return array
*/
function breakpoints_breakpoint_empty_array() {
return array(
'name' => '',
'machine_name' => '',
'breakpoint' => '',
'source' => '',
'source_type' => '',
'status' => TRUE,
'weight' => 0,
'multipliers' => array(),
);
}
/**
* Save a single breakpoint.
*
* @param $breakpoint
*
* @return bool|int
*/
function breakpoints_breakpoint_save(&$breakpoint) {
ctools_include('export');
if (!isset($breakpoint->machine_name) || empty($breakpoint->machine_name)) {
$breakpoint->machine_name = breakpoints_breakpoint_config_name($breakpoint);
}
$update = (isset($breakpoint->id) && is_numeric($breakpoint->id)) ? array('id') : array();
// Remove unused multipliers.
$breakpoint->multipliers = array_filter($breakpoint->multipliers);
if (is_null($breakpoint->multipliers)){
$breakpoint->multipliers = array();
}
// Add the '1x' multiplier.
$breakpoint->multipliers = array_merge($breakpoint->multipliers, array('1x' => '1x'));
$result = drupal_write_record('breakpoints', $breakpoint, $update);
breakpoints_breakpoint_reset();
return $result;
}
/**
* Delete a single breakpoint.
*
* @param $breakpoint
*/
function breakpoints_breakpoint_delete($breakpoint) {
$name = breakpoints_breakpoint_config_name($breakpoint);
return breakpoints_breakpoint_delete_by_fullkey($name);
}
/**
* Delete a single breakpoint.
*
* @param $key
*/
function breakpoints_breakpoint_delete_by_fullkey($key) {
if (!empty($key)) {
$sql = "DELETE FROM {breakpoints} where machine_name = :key";
db_query($sql, array(':key' => $key));
}
breakpoints_breakpoint_reset();
}
/**
* Clears the breakpoint caches.
*/
function breakpoints_breakpoint_reset() {
ctools_include('export');
ctools_export_load_object_reset('breakpoints');
drupal_static_reset('breakpoints_breakpoint_load_by_fullkey');
cache_clear_all('breakpoints:breakpoints', 'cache');
}
/**
* Toggle status of a single breakpoint.
*
* @param $machine_name
*/
function breakpoints_breakpoint_toggle_status($machine_name) {
$breakpoint = breakpoints_breakpoint_load_by_fullkey($machine_name);
if ($breakpoint) {
$breakpoint->status = !$breakpoint->status;
breakpoints_breakpoint_save($breakpoint);
}
}
/**
* Check if a breakpoint name already exists.
*
* @param $machine_name
*
* @return bool
*/
function breakpoints_breakpoint_name_exists($machine_name) {
$breakpoints = breakpoints_breakpoint_load_all_custom();
$fullkey = 'custom.user.' . $machine_name;
return array_key_exists($fullkey, $breakpoints);
}
/**
* Check if a breakpoint machine name already exists.
*
* @param $machine_name
*
* @return bool
*/
function breakpoints_breakpoint_machine_name_exists($machine_name) {
// Just try to load the breakpoint object, we profit from ctool's cache mechanism,
// better that doing a query to the db every time this function is called.
return (bool) breakpoints_breakpoint_load_by_fullkey($machine_name);
}
/**
* Empty breakpoint group object.
*
* @return object
*/
function breakpoints_breakpoint_group_empty_object() {
return (object) breakpoints_breakpoint_group_empty_array();
}
/**
* Empty breakpoint group array.
*
* @return array
*/
function breakpoints_breakpoint_group_empty_array() {
return array(
'machine_name' => '',
'name' => '',
'breakpoints' => array(),
'type' => 'custom',
);
}
/**
* Check if a group name already exists.
*
* @param $machine_name
*
* @return bool
*/
function breakpoints_breakpoint_group_name_exists($machine_name) {
// Check for reserved words.
if ($machine_name == 'global' || $machine_name == 'add') {
return TRUE;
}
// Check if group name is used before.
$group_check = breakpoints_breakpoint_group_load($machine_name);
if ($group_check && isset($group_check->machine_name) && !empty($group_check->machine_name)) {
return TRUE;
}
return FALSE;
}
/**
* Load all breakpoint groups.
*
* @param bool $reset
*
* @return array|mixed
*/
function breakpoints_breakpoint_group_load_all($reset = FALSE) {
$groups = &drupal_static(__FUNCTION__);
if (!isset($groups)) {
if (!$reset && $cache = cache_get('breakpoints:groups')) {
return $cache->data;
}
else {
ctools_include('export');
$groups = ctools_export_load_object('breakpoint_group', 'all');
cache_set('breakpoints:groups', $groups);
}
}
return $groups;
}
/**
* Load a single breakpoint group.
*
* @param null $name
*
* @return array|bool|mixed
*/
function breakpoints_breakpoint_group_load($name = NULL) {
$groups = breakpoints_breakpoint_group_load_all();
if ($name) {
$group = isset($groups[$name]) ? $groups[$name] : FALSE;
return $group;
}
else {
return $groups;
}
}
/**
* Validate a single breakpoint group.
*
* @param $group
*
* @return bool
*/
function breakpoints_breakpoint_group_validate($group) {
if (!is_object($group)) {
return FALSE;
}
foreach (array('machine_name', 'name', 'breakpoints', 'type') as $property) {
if (!property_exists($group, $property)) {
return FALSE;
}
}
return TRUE;
}
/**
* Validate a single breakpoint.
*
* @param $breakpoint
*
* @return bool
*/
function breakpoints_breakpoint_validate($breakpoint) {
if (!is_object($breakpoint)) {
return FALSE;
}
foreach (array_keys(breakpoints_breakpoint_empty_array()) as $property) {
if (!property_exists($breakpoint, $property)) {
return FALSE;
}
}
return TRUE;
}
/**
* Save a single breakpoint group.
*
* @param $breakpoint_group
*
* @return bool|int
*/
function breakpoints_breakpoint_group_save(&$breakpoint_group) {
// TODO: Ctools?
ctools_include('export');
$update = (isset($breakpoint_group->id) && is_numeric($breakpoint_group->id)) ? array('id') : array();
$result = drupal_write_record('breakpoint_group', $breakpoint_group, $update);
// rebuild menu if we add a new group
if (empty($update)) {
config_set('breakpoints.settings','menu_rebuild_needed', TRUE);
}
breakpoints_breakpoint_group_reset();
return $result;
}
/**
* Delete a single breakpoint group.
*
* @param $breakpoint_group
*/
function breakpoints_breakpoint_group_delete($breakpoint_group) {
$name = $breakpoint_group->machine_name;
breakpoints_breakpoint_group_delete_by_fullkey($name);
}
/**
* Delete a single breakpoint group by fullkey.
*
* @param $machine_name
*/
function breakpoints_breakpoint_group_delete_by_name($machine_name) {
$name = $machine_name;
breakpoints_breakpoint_group_delete_by_fullkey($name);
}
/**
* Delete a single breakpoint group by fullkey.
*
* @param $key
*/
function breakpoints_breakpoint_group_delete_by_fullkey($key) {
if (!empty($key)) {
$sql = "DELETE FROM {breakpoint_group} where machine_name = :key";
db_query($sql, array(':key' => $key));
}
config_set('breakpoints.settings','menu_rebuild_needed', TRUE);
breakpoints_breakpoint_group_reset();
}
/**
* Clears the breakpoint group caches.
*/
function breakpoints_breakpoint_group_reset() {
// TODO: Ctools?
ctools_include('export');
ctools_export_load_object_reset('breakpoint_group');
backdrop_static_reset('breakpoints_breakpoint_group_load_all');
cache_clear_all('breakpoints:groups', 'cache');
}
/**
* Implements hook_theme().
*/
function breakpoints_theme() {
return array(
'breakpoints_admin_breakpoints_table' => array(
'render element' => 'form',
'theme_key' => NULL,
),
'breakpoints_multipliers_table_form' => array(
'render element' => 'form',
'theme_key' => NULL,
)
);
}
/**
* Export callback.
* @see breakpoints_schema()
*
* @param $object
* @param $field
* @param $value
* @param $indent
*
* @return mixed
*/
function breakpoint_group_export_breakpoints($object, $field, $value, $indent) {
// TODO: Ctools? How do I convert this to CMI?
ctools_include('export');
$export = array();
if (isset($object->breakpoints)) {
foreach ($object->breakpoints as $breakpoint) {
if (!is_array($breakpoint) && !is_object($breakpoint)) {
$breakpoint = breakpoints_breakpoint_load_by_fullkey($breakpoint);
}
$export[] = $breakpoint->machine_name;
}
}
return ctools_var_export($export, $indent);
}
/**
* Reload the breakpoints as defined by the group.
*
* @param $group
* @param bool $force
*/
function breakpoints_breakpoints_group_reload(&$group, $force = TRUE) {
switch ($group->type) {
case BREAKPOINTS_SOURCE_TYPE_THEME:
// delete all breakpoints defined by this theme.
$breakpoints = breakpoints_breakpoint_load_all_theme($group->machine_name);
foreach ($breakpoints as $breakpoint) {
breakpoints_breakpoint_delete($breakpoint);
}
// Reload all breakpoints from theme.info.
$reloaded_group = breakpoints_breakpoints_group_reload_from_theme($group->machine_name);
// Reset the breakpoints for this group.
if ($force) {
$group->breakpoints = $reloaded_group->breakpoints;
breakpoints_breakpoint_group_save($group);
}
break;
}
}
/**
* @param $theme_key
*
* @return object
*/
function breakpoints_breakpoints_group_reload_from_theme($theme_key) {
// Clear caches so theme.info is fresh.
system_rebuild_theme_data();
backdrop_theme_rebuild();
$themes = list_themes();
if (isset($themes[$theme_key]->info['breakpoints'])) {
$weight = 0;
$theme_settings = $themes[$theme_key]->info['breakpoints'];
$multipliers = isset($themes[$theme_key]->info['multipliers']) ? $themes[$theme_key]->info['multipliers'] : array();
$settings = breakpoints_settings();
$current_multipliers = drupal_map_assoc($settings->multipliers);
// Build a group for each theme
$breakpoint_group = breakpoints_breakpoint_group_empty_object();
$breakpoint_group->machine_name = $theme_key;
$breakpoint_group->name = $themes[$theme_key]->info['name'];
$breakpoint_group->type = BREAKPOINTS_SOURCE_TYPE_THEME;
foreach ($theme_settings as $name => $media_query) {
$breakpoint = breakpoints_breakpoint_load($name, $theme_key, 'theme');
if (!$breakpoint) {
$breakpoint = breakpoints_breakpoint_empty_object();
$breakpoint->name = $name;
$breakpoint->source = $theme_key;
$breakpoint->source_type = 'theme';
$breakpoint->theme = '';
$breakpoint->status = TRUE;
$breakpoint->weight = $weight++;
$breakpoint->machine_name = breakpoints_breakpoint_config_name($breakpoint);
}
$breakpoint->breakpoint = $media_query;
$breakpoint->multipliers = isset($multipliers[$name]) ? drupal_map_assoc($multipliers[$name]) : array();
$current_multipliers += drupal_map_assoc($breakpoint->multipliers);
breakpoints_breakpoint_save($breakpoint);
$breakpoint_group->breakpoints[] = $breakpoint->machine_name;
}
breakpoints_settings_save($current_multipliers);
return $breakpoint_group;
}
}
/**
* Revert the breakpoints of a group.
*
* @param $group
*/
function breakpoints_breakpoints_group_revert(&$group) {
breakpoints_breakpoints_group_reload($group);
$group->overridden = 0;
breakpoints_breakpoint_group_save($group);
}
/**
* Duplicate a group.
*
* @param $group
* @param $new_name
* @param $new_machine_name
*
* @return object
*/
function breakpoints_breakpoints_group_duplicate($group, $new_name, $new_machine_name) {
$new_group = breakpoints_breakpoint_group_empty_object();
$new_group->machine_name = $new_machine_name;
$new_group->name = $new_name;
$new_group->type = BREAKPOINTS_SOURCE_TYPE_CUSTOM;
$new_group->breakpoints = $group->breakpoints;
breakpoints_breakpoint_group_save($new_group);
return $new_group;
}
/**
* Override the breakpoints of a group.
*
* @param $group
*/
function breakpoints_breakpoints_group_override($group) {
foreach ($group->breakpoints as $key => $breakpoint) {
$breakpoint = breakpoints_breakpoint_load_by_fullkey($breakpoint);
$old_breakpoint = clone $breakpoint;
if ($breakpoint->source_type == BREAKPOINTS_SOURCE_TYPE_THEME && $breakpoint->source == $group->machine_name) {
unset($breakpoint->id);
$breakpoint->machine_name = 'custom.' . $breakpoint->source . '.' . str_replace('-', '_', drupal_clean_css_identifier($breakpoint->name));
$breakpoint->source_type = BREAKPOINTS_SOURCE_TYPE_CUSTOM;
// make sure it doesn't already exists.
if (breakpoints_breakpoint_load_by_fullkey($breakpoint->machine_name) === FALSE) {
breakpoints_breakpoint_save($breakpoint);
}
// Add to the group and delete old breakpoint.
$group->breakpoints[$key] = $breakpoint->machine_name;
breakpoints_breakpoint_delete($old_breakpoint);
}
}
$group->overridden = 1;
breakpoints_breakpoint_group_save($group);
}
/**
* Export breakpoints ready for theme.info inclusion.