summaryrefslogtreecommitdiffstats
path: root/gallery_settings.inc
blob: a68bfd16c24ab538a9ef269f1342a5f5698110cf (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
<?php
// $Id$

/**
 * gallery.module : gallery_settings.inc
 * Settings functions
 */

/**
 * Function _gallery_settings_general().
 */
function _gallery_settings_general() {
  // Short Status
  gallery_version();
  gallery_plugin_set_status(array('imageblock', 'imageframe', 'search'));
  $status = gallery_get_status();
  
  $form['status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Status'),
    '#description' => gallery_format_status($status, ''),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  
  // Theme Settings
  $form['theme'] = array(
    '#type' => 'fieldset',
    '#title' => t('Theme and Display settings'),
    '#description' => '',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $themes = system_theme_data();
  ksort($themes);
  $options = array('default' => t('System default'));
  foreach ($themes as $theme) {
    $options[$theme->name] = $theme->info['name'];
  }
  $form['theme']['gallery_page_theme'] = array(
    '#type' => 'select',
    '#title' => t('Gallery page theme'),
    '#default_value' => variable_get('gallery_page_theme', 'default'),
    '#options' => $options,
    '#description' => t('Drupal theme to be used for all gallery pages (gallery/*).'),
  );
  $form['theme']['gallery_embed_theme'] = array(
    '#type' => 'select',
    '#title' => t('Gallery embedded theme'),
    '#default_value' => variable_get('gallery_embed_theme', 'default'),
    '#options' => array('default' => t('Gallery default')) + gallery_get_themes(),
    '#description' => t('Gallery2 theme to be used for embedded gallery.'),
  );
  $form['theme']['gallery_root_album'] = _gallery_settings_album_select(
    t('Default root album'),
    variable_get('gallery_root_album', 'default'),
    t('Default root album to use on gallery pages. If you have a separate album for use with nodes (Filter, G2Image, ...)
       it can be helpful to move the root album to hide that album from the browseable gallery.')
  );
  
  // Gallery Block Settings
  $form['blocks'] = array(
    '#type' => 'fieldset',
    '#title' => t('Gallery block settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Use these settings to determine the number of available blocks.<br />
                         <strong>Warning: If you decrease the number of blocks, always the last
                         block (with the highest ID) will be removed.</strong>'),
  );
  $form['blocks']['gallery_block_num'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of blocks'),
    '#default_value' => variable_get('gallery_block_num', 2),
    '#size' => 10,
    '#maxlength' => 10,
    '#description' => t('Select how many blocks should be available.'),
  );
  
  // Gallery2 Sidebar Settings
  $form['sidebar'] = array(
    '#type' => 'fieldset',
    '#title' => t('Sidebar settings'),
    '#description' => '',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['sidebar']['gallery_move_sidebar_to_block'] = array(
    '#type' => 'checkbox',
    '#title' => t('Move Gallery2 sidebar to Drupal Gallery Navigation block'),
    '#default_value' => variable_get('gallery_move_sidebar_to_block', 1),
    '#description' => t('When selected, the Gallery2 sidebar will be moved into
                         the Drupal "Gallery Navigation" block. This typically allows
                         for a much cleaner embedded gallery and is generally
                         recommended. Note that you will need to enable the Gallery
                         Navigation block.'),
  );
  $form['sidebar']['gallery_move_admin_sidebar_to_block'] = array(
    '#type' => 'checkbox',
    '#title' => t('Move Gallery2 Admin sidebar to Drupal Gallery Navigation block'),
    '#default_value' => variable_get('gallery_move_admin_sidebar_to_block', 0),
    '#description' => t('When selected, the Gallery2 Admin sidebar will be moved into
                         the Drupal "Gallery Navigation" block. This is not normally
                         recommended since the Admin Sitebar is very long and the
                         formatting does not always fit with the rest of the block
                         (but cannot be changed in the current Gallery2 versions).'),
    '#disabled' => !variable_get('gallery_move_sidebar_to_block', 1),
  );
  
  // Gallery2 Google Sitemap Settings
  $gallery2_sitemap_status = gallery_single_plugin_status('sitemap');
  $gallery2_sitemap_status_str = theme('gallery_plugin_status_message', $gallery2_sitemap_status);
  $xmlsitemap_status = module_exists('xmlsitemap');
  $xmlsitemap_status_str = theme('gallery_module_status_message', $xmlsitemap_status);
  $xmlsitemap_available = $xmlsitemap_status && ($gallery2_sitemap_status == GALLERY_PLUGIN_ENABLED);
  
  $desc = t('Allows the Gallery2 sitemap to be merged with the Drupal one so that only one URL needs to
             be supplied to search engines. Requires the Drupal XML Sitemap module (!xmlsitemap_status) and
             the Gallery2 sitemap module (!gallery2_sitemap_status) to be installed/activated.',
    array(
      '!xmlsitemap_status' => $xmlsitemap_status_str,
      '!gallery2_sitemap_status' => $gallery2_sitemap_status_str,
    )
  );
  
  $form['sitemap'] = array(
    '#type' => 'fieldset',
    '#title' => t('XML Sitemap settings'),
    '#description' => '',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => $desc,
  );
  $form['sitemap']['gallery_enable_sitemap'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable merge of Gallery2 sitemap with Drupal sitemap'),
    '#default_value' => $xmlsitemap_available ? variable_get('gallery_enable_sitemap', 1) : FALSE,
    '#disabled' => !$xmlsitemap_available,
  );
  
  // Error logging / Debug Settings
  $form['error'] = array(
    '#type' => 'fieldset',
    '#title' => t('Error logging / Debug settings'),
    '#description' => '',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['error']['gallery_error_mode'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Error logging'),
    '#default_value' => variable_get('gallery_error_mode', array(GALLERY_ERROR_WATCHDOG)),
    '#options' => array(GALLERY_ERROR_WATCHDOG => t('Watchdog'),
                        GALLERY_ERROR_BROWSER => t('Output to the browser'),
                        GALLERY_ERROR_VERBOSE => t('Verbose error messages')),
    '#description' => t('Choose where errors are displayed and how detailed they are.'),
  );
  $form['error']['gallery_report'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable bug report assistant'),
    '#default_value' => variable_get('gallery_report', 1),
    '#description' => t('In case of errors the module can automatically assemble useful data (system info and debug
                         traces) to help you with detailed bug reports. Only available to users with \'administer
                         site configuration\' permission.')
  );
  $search_status = module_exists('search') && (gallery_single_plugin_status('search') == GALLERY_PLUGIN_ENABLED);
  $form['error']['gallery_error_redirect'] = array(
    '#type' => 'checkbox',
    '#title' => t('Redirect to Gallery search form for invalid paths'),
    '#default_value' => $search_status ? variable_get('gallery_error_redirect', 0) : 0,
    '#description' => t('Instead of showing a message that the requested Gallery URL does not exist the
                         user is redirected to the search form.'),
    '#disabled' => !$search_status,
  );
  $form['error']['gallery_debug'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable debug mode'),
    '#default_value' => variable_get('gallery_debug', 0),
    '#description' => t('Print out debug variables and verbose error messages. Only visible to users
                         with \'administer site configuration\' permission.')
  );
  
  // Relevant links
  $form['links'] = array(
    '#type' => 'fieldset',
    '#title' => t('Links to the most relevant Drupal & Gallery2 pages'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  // Drupal links
  $desc  = '<ul>';
  $desc .= '<li><a href="@gallery-users">Gallery users</a> : Gallery2 user integration and synchronization.</li>';
  $desc .= '<li><a href="@gallery-report">Report Generator</a> : Collect information about your installation. Useful
            especially for bug reports.</li>';
  $desc .= '</ul>';
  $form['links']['drupal'] = array(
    '#type' => 'fieldset',
    '#title' => t('Links to Drupal paths'),
    '#description' => t($desc, array(
      '@gallery-users' => url('admin/user/gallery'),
      '@gallery-report' => url('admin/settings/gallery/report/download'))),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  // Gallery2 links
  $desc  = '<ul>';
  $desc .= '<li><a href="@g2-modules">Plugins</a> : To install, activate or configure the required and optional plugins
            relating to gallery.module (Image Block, Image Frame, URL Rewrite, Sitemap, Search).</li>';
  $desc .= '<li><a href="@g2-themes">Themes</a> : To add/remove blocks in the sidebar.</li>';
  if (gallery_single_plugin_status('rewrite') == GALLERY_PLUGIN_ENABLED) {
    $desc .= '<li><a href="@g2-rewrite">Embedded URL Rewrite settings</a> : To define the rewrite rules.</li>';
  }
  $desc .= '<li><a href="@g2-cookie">Cookie Settings</a> : Only needed for a small number of site configurations, such
            as the use of different subdomains for Gallery2 and Drupal.</li>';
  $desc .= '</ul>';
  $g2_uri = variable_get('gallery_embed_uri', '?q=gallery');
  $form['links']['gallery'] = array(
    '#type' => 'fieldset',
    '#title' => t('Links to Gallery2 Site Admin sections'),
    '#description' => t($desc, array(
      '@g2-modules' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=core.AdminModules',
      '@g2-themes' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=core.AdminThemes&g2_mode=defaults',
      '@g2-rewrite' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=rewrite.AdminRewrite&g2_mode=setup',
      '@g2-cookie' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=core.AdminCore')),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  
  $form = system_settings_form($form);
  $form['#validate'] = array('_gallery_settings_general_validate');
  $form['#submit'] = array('_gallery_settings_general_submit', 'system_settings_form_submit');
  return $form;
}

/**
 * Function _gallery_settings_general_validate().
 */
function _gallery_settings_general_validate($form, &$form_state) {
  if (!is_numeric($form_state['values']['gallery_block_num']) || $form_state['values']['gallery_block_num'] < 1) {
    form_set_error('gallery_block_num', t('Number of image blocks must be a positiv integer greater zero.'));
  }
}

/**
 * Function _gallery_settings_general_submit().
 */
function _gallery_settings_general_submit($form, &$form_state) {
  if ($form_state['values']['gallery_enable_sitemap'] != variable_get('gallery_enable_sitemap', 0)) {
    drupal_set_message('The XML sitemap setting has been updated, but the new sitemap may not be generated
                        immediately (controlled by XML Sitemap).');
  }
}

/**
 * Function _gallery_settings_filter().
 */
function _gallery_settings_filter() {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_block.inc');
  $plugin_status =  gallery_plugin_status(array('imageblock', 'imageframe'));
  
  $desc = t('Note that changing the defaults here will change the all the gallery filter images
    on the site, not just new images. '); 
  $desc .= t('The Gallery Filter requires the Gallery2 Image Block plugin (!imageblock_status) and
              optionally the Gallery2 Image Frame plugin (!imageframe_status).',
              array(
                '!imageblock_status' => theme('gallery_plugin_status_message', $plugin_status['imageblock']),
                '!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']),
              ));
  if ($plugin_status['imageblock'] != GALLERY_PLUGIN_ENABLED) {
    $g2_uri = variable_get('gallery_embed_uri', '?q=gallery');
    $g2_plugins_page = (variable_get('gallery_valid', 0)) ? 
      t('<a href= "@g2_plugins">Gallery2 Plugins</a>',
        array('@g2_plugins' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=core.AdminModules'))
      : t('Gallery2 Site Admin -> Plugins');
    $desc .= t(' However the Image Block plugin is unavailable, so the Gallery Filter is
      not available and the settings are disabled. To use the filter please go to
      the !g2_plugins page and install/activate the Image Block plugin.',
        array('!g2_plugins' => $g2_plugins_page));
  }    

  $imageframe_desc = ($plugin_status['imageframe'] != GALLERY_PLUGIN_ENABLED) ?
    t('Requires the Gallery2 Image Frame plugin (!imageframe_status).',
      array('!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']))) : '';

  $form['filter'] = array(
    '#type' => 'fieldset',
    '#title' => t('Gallery Filter settings'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#description' => $desc,
  );
  
  $image_frames = gallery_get_image_frames();
  _gallery_block_options($type_map, $param_map);
  $type_map['specificItem'] = t('Specific item');
  
  if ($plugin_status['imageblock'] == GALLERY_PLUGIN_ENABLED) {          
    $form['filter']['gallery_filter_prefix'] = array(
      '#type' => 'textfield',
      '#title' => t('Filter prefix'),
      '#default_value' => variable_get('gallery_filter_prefix', 'G2'),
      '#size' => 10,
      '#maxlength' => 10,
      '#description' => t('Prefix to use with filter. Example: \'G2\' means you use [G2: 999].
        Be careful when changing this as any pages using the previous prefix will not be changed
        and so will not be filtered correctly.'),
    );
    
    $form['filter']['gallery_filter_default_block_type'] = array(
      '#type' => 'select',
      '#title' => t('Default image type'),
      '#default_value' => variable_get('gallery_filter_default_block_type', 'viewedImage'),
      '#options' => $type_map,
      '#description' => t('Pick the default type of image you would like to use.'),
    );
    
    $form['filter']['gallery_filter_can_cache'] = array(
      '#type' => 'checkbox',
      '#title' => t('Cache gallery filter pages'),
      '#default_value' => variable_get('gallery_filter_can_cache', 1),
      '#description' => t('By default the gallery filter output is cached by Drupal to speed up page loading.
                           However, it will not cache the css class info for the frames. The best approach is the
                           make sure that the sidebar image block and the gallery filter images use the same frames.
                           If you are unable to do this you will have to deselect this option to force Drupal not to
                           cache the pages, or else your frames will not appear. If you change this option you will
                           need to go to <a href="@link">admin/settings/filters</a> and re-save the image formats
                           that use gallery filter.', array('@link' => url('admin/settings/filters'))),
    );
    
    $form['filter']['gallery_filter_n_images'] = array(
      '#type' => 'textfield',
      '#title' => t('Default number of images'),
      '#default_value' => variable_get('gallery_filter_n_images', 1),
      '#size' => 3,
      '#maxlength' => 3,
      '#description' => t('How many images you want the default block to show.  Best to keep at 1 and use the n parameter.'),
    );
    
    $form['filter']['gallery_filter_default_show'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Default image data'),
      '#default_value' => variable_get('gallery_filter_default_show', array('none')),
      '#options' => $param_map,
      '#description' => t('Choose the item metadata you would like to display by default.
                           This will change all instances where show parameter was not specified.'),
    );
    
    // Parse gallery_filter.css and extract classes
    $css = implode('', file(drupal_get_path('module', 'gallery') .'/gallery_filter.css'));
    preg_match_all('/div.giImageBlock.(\w+) {/', $css, $matches);
    if (count($matches[1])) {
      $classes = array();
      foreach ($matches[1] as $class) {
        $classes[$class] = $class;
      }
      $form['filter']['gallery_filter_default_div_class'] = array(
        '#type' => 'select',
        '#title' => t('Default class'),
        '#default_value' => variable_get('gallery_filter_default_div_class', 'nowrap'),
        '#options' => $classes,
      );
    }
    else {
      $form['filter']['gallery_filter_default_div_class'] = array(
        '#type' => 'textfield',
        '#title' => t('Default class'),
        '#default_value' => variable_get('gallery_filter_default_div_class', 'nowrap'),
        '#size' => 20,
        '#maxlength' => 20,
        '#description' => t('left, right, or nowrap. (See gallery_filter.css to add more or modify these.)'),
      );
    }
    
    $form['filter']['gallery_filter_default_maxsize'] = array(
      '#type' => 'textfield',
      '#title' => t('Default \'Max Size\' thumbnail size'),
      '#default_value' => variable_get('gallery_filter_default_maxsize', GALLERY_FILTER_MAXSIZE_DEFAULT),
      '#size' => 10,
      '#maxlength' => 10,
    );
    
    $form['filter']['gallery_filter_default_exactsize'] = array(
      '#type' => 'textfield',
      '#title' => t('Default \'Exact Size\' thumbnail size'),
      '#default_value' => variable_get('gallery_filter_default_exactsize', GALLERY_FILTER_EXACTSIZE_DEFAULT),
      '#size' => 10,
      '#maxlength' => 10,
      '#description' => t('If no size is specified when calling the filter, one of
        these sizes (and implied method) will be used. <br />\'Max Size\' gives faster
        image downloading, but the image size 
        may be smaller than the size defined. <br />\'Exact Size\' may be slower
        (as a larger image is downloaded and then scaled by the browser) but the image
        will be guaranteed to be the size defined. Only supported for G2.2+.
        Only fill in one size box (not both) to set the default method.'),
    );
    
    $form['filter']['gallery_filter_default_album_frame'] = array(
      '#type' => 'select',
      '#title' => t('Default album frame'),
      '#default_value' => variable_get('gallery_filter_default_album_frame', 'none'),
      '#options' => $image_frames,
      '#description' => $imageframe_desc,
    );
    
    $form['filter']['gallery_filter_default_item_frame'] = array(
      '#type' => 'select',
      '#title' => t('Default item frame'),
      '#default_value' => variable_get('gallery_filter_default_item_frame', 'none'),
      '#options' => $image_frames,
      '#description' => $imageframe_desc,
    );
    
    $form['filter']['gallery_filter_default_link_target'] = array(
      '#type' => 'textfield',
      '#title' => t('Default link target'),
      '#default_value' => variable_get('gallery_filter_default_link_target', ''),
      '#size' => 20,
      '#maxlength' => 20,
      '#description' => t('Enter a link target (e.g. "_blank", "_new").'),
    );
    
    $form['filter']['gallery_filter_default_link'] = array(
      '#type' => 'textfield',
      '#title' => t('Default Image Link'),
      '#default_value' => variable_get('gallery_filter_default_link', ''),
      '#size' => 20,
      '#maxlength' => 20,
      '#description' => t('By default the image has a link to the item in the Gallery. This
        can be overridden here (or leave empty for the default). Use \'none\' for no link, or a URL
        to link somewhere else instead.'),
    );
  }
  
  $form['array_filter'] = array('#type' => 'value');
  $form = system_settings_form($form);
  $form['#validate'] = array('_gallery_settings_filter_validate');
  $form['#submit'] = array('_gallery_settings_filter_submit', 'system_settings_form_submit');
  return $form;
}

/**
 * Function _gallery_settings_filter_validate().
 */
function _gallery_settings_filter_validate($form, &$form_state) {
  // Only validate if a setting was returned
  if (isset($form_state['values']['gallery_filter_default_maxsize'])) {
    if ((strlen($form_state['values']['gallery_filter_default_maxsize']) > 0) &&
      (strlen($form_state['values']['gallery_filter_default_exactsize']) > 0)) {
      form_set_error('gallery_filter_default_maxsize',
        t('You must set either the Max Size or the Exact Size, not both.'));  
    } 
    elseif ((strlen($form_state['values']['gallery_filter_default_maxsize']) == 0) &&
      (strlen($form_state['values']['gallery_filter_default_exactsize']) == 0)) {
      form_set_error('gallery_filter_default_maxsize',
        t('You must set either the Max Size or the Exact Size.'));  
    }
    if ((strlen($form_state['values']['gallery_filter_default_maxsize']) > 0) &&
      (!is_numeric($form_state['values']['gallery_filter_default_maxsize']) ||
      $form_state['values']['gallery_filter_default_maxsize'] < 1)) {
      form_set_error('gallery_filter_default_maxsize',
        t('Max Size must be a number greater than zero.'));
    }
    if ((strlen($form_state['values']['gallery_filter_default_exactsize']) > 0) &&
      (!is_numeric($form_state['values']['gallery_filter_default_exactsize']) ||
      $form_state['values']['gallery_filter_default_exactsize'] < 1)) {
      form_set_error('gallery_filter_default_exactsize',
        t('Exact Size must be a number greater than zero.'));
    }
    if ((strlen($form_state['values']['gallery_filter_n_images']) > 0) &&
      (!is_numeric($form_state['values']['gallery_filter_n_images']) ||
      $form_state['values']['gallery_filter_n_images'] < 1)) {
      form_set_error('gallery_filter_n_images',
        t('Number of images must be a number greater than zero.'));
    }
  }
}

/**
 * Function _gallery_settings_filter_submit().
 */
function _gallery_settings_filter_submit($form, &$form_state) {
  // The default values have changed, so the pages containing the gallery filter need
  // to be updated (or else it would only occur on an edit), so empty the filter cache.
  
  // Find out which formats are using the gallery filter
  $result = db_query("SELECT format FROM {filters} WHERE module = 'gallery'");
  while ($format = db_fetch_object($result)) {
    cache_clear_all($format->format .':', 'cache_filter', TRUE);
  }
  
  drupal_set_message('The gallery filter cache has been cleared so that the new defaults apply.');
}

/**
 * Function _gallery_settings_g2image().
 */
function _gallery_settings_g2image() {
  $plugin_status =  gallery_plugin_status(array('imageblock'));

  $desc = t('G2Image requires the Gallery2 Image Block plugin (!imageblock_status) and
             the g2image application to be installed.',
              array('!imageblock_status' => theme('gallery_plugin_status_message', $plugin_status['imageblock'])));
  if ($plugin_status['imageblock'] != GALLERY_PLUGIN_ENABLED) {
    $g2_uri = variable_get('gallery_embed_uri', '?q=gallery');
    $g2_plugins_page = (variable_get('gallery_valid', 0)) ?
      t('<a href= "@g2_plugins">Gallery2 Plugins</a>',
        array('@g2_plugins' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=core.AdminModules'))
      : t('Gallery2 Site Admin -> Plugins');
    $desc .= t(' However the Image Block plugin is unavailable, so G2Image is
      not available and the settings are disabled. To use G2Image please go to
      the !g2_plugins page and install/activate the Image Block plugin.', array(
      '!g2_plugins' => $g2_plugins_page));
  }

  $form['g2image'] = array(
    '#type' => 'fieldset',
    '#title' => t('Gallery Image Assist (G2Image) settings'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#description' => $desc,
  );

  if ($plugin_status['imageblock'] == GALLERY_PLUGIN_ENABLED) {
    $form['g2image']['gallery_g2image_mode'] = array(
      '#type' => 'select',
      '#title' => t('Mode'),
      '#default_value' => variable_get('gallery_g2image_mode', 'disabled'),
      '#options' => array(
        'disabled' => t('Disabled'),
        'standalone' => t('Standalone'),
        'tinymce' => t('TinyMCE'),
      ),
      '#description' => t('Determines the mode of operation. For anything other than \'Disabled\' the g2image
                           application has to be installed. See the INSTALL.txt instructions. In \'Standalone\'
                           mode a button will be visible under textfields to launch the g2image window. In
                           \'TinyMCE\' mode the g2image button can be used in the TinyMCE toolbar. Note that the
                           TinyMCE version will NOT work wih Safari - use the standalone version instead.'),
    );
    
    $form['g2image']['gallery_g2image_only_listed_pages'] = array(
      '#type' => 'radios',
      '#title' => t('Show g2image link on specific pages (Standalone mode only)'),
      '#default_value' => variable_get('gallery_g2image_only_listed_pages', 1),
      '#options' => array(
        t('Show on every page except the listed pages.'),
        t('Show on only the listed pages.')
      )
    );
    
    require_once(drupal_get_path('module', 'gallery') .'/gallery_help.inc');
    $form['g2image']['gallery_g2image_std_pages'] = array(
      '#type' => 'textarea',
      '#title' => t('Pages (Standalone mode only)'),
      '#default_value' => variable_get('gallery_g2image_std_pages', gallery_help('admin/settings/gallery_g2image#pages')),
      '#description' => t('Enter one page per line as Drupal paths. The \'*\' character is a wildcard. Example paths are
                           \'%blog\' for the blog page and %blog-wildcard for every personal blog. %front is the front page.',
                           array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'))
    );
    
    $form['g2image']['gallery_g2image_sortby'] = array(
      '#type' => 'select',
      '#title' => t('Default Sort Order'),
      '#default_value' => variable_get('gallery_g2image_sortby', 'title_asc'),
      '#options' => array(
        'title_asc' => t('Gallery2 Title (A-z)'),
        'title_desc' => t('Gallery2 Title (z-A)'),
        'name_asc' => t('Filename (A-z)'),
        'name_desc' => t('Filename (z-A)'),
        'mtime_desc' => t('Last Modification (newest first)'),
        'mtime_asc' => t('Last Modification (oldest first)'),
      ),
      '#description' => t('Determines the default sorting order.'),
    );
    
    $form['g2image']['gallery_g2image_images_per_page'] = array(
      '#type' => 'select',
      '#title' => t('Default Number of images per page'),
      '#default_value' => variable_get('gallery_g2image_images_per_page', 20),
      '#options' => _gallery_range_array(10, 60, 10),
      '#description' => t('Choose the default number of images per page.'),
    );
    
    $form['g2image']['gallery_g2image_display_filenames'] = array(
      '#type' => 'select',
      '#title' => t('Default Display Options'),
      '#default_value' => variable_get('gallery_g2image_display_filenames', 'thumbnails'),
      '#options' => array(
        'thumbnails' => t('Thumbnails only'),
        'filenames' => t('Thumbnails with title and filename'),
      ),
      '#description' => t('Choose the default display option for images.'),
    );
    
    $form['g2image']['gallery_g2image_default_action'] = array(
      '#type' => 'select',
      '#title' => t('Default Action'),
      '#default_value' => variable_get('gallery_g2image_default_action', 'drupal_g2_filter'),
      '#options' => array(
        'drupal_g2_filter' => t('Gallery Filter ID'),
        'thumbnail_image' => t('Thumbnail with link to image'),
        'thumbnail_album' => t('Thumbnail with link to parent album'),
        'thumbnail_custom_url' => t('Thumbnail with link to custom URL'),
        'thumbnail_only' => t('Thumbnail only'),
        'link_image' => t('Text link to image'),
        'link_parent' => t('Text link to parent album'),
      ),
      '#description' => t('Choose the default method of inserting the image info.'),
    );
    
    $form['g2image']['gallery_g2image_custom_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Default custom url'),
      '#default_value' => variable_get('gallery_g2image_custom_url', 'http://'),
      '#size' => 50,
      '#maxlength' => 50,
      '#description' => t('The default custom url. Used with \'Thumbnail with link to custom URL\' action setting'),
    );
    
    $css_options =  array(
      'none' => t('None'),
      'g2image_normal' => t('Normal'),
      'g2image_float_left' => t('Float Left'),
      'g2image_float_right' => t('Float Right'),
      'g2image_centered' => t('Centered'),
    );
    
    // Merge in custom class options
    $gallery_g2image_custom_class = array_filter(variable_get('gallery_g2image_custom_class', array()));
    $css_options = array_merge($css_options, drupal_map_assoc($gallery_g2image_custom_class));
    
    $form['g2image']['gallery_g2image_default_alignment'] = array(
      '#type' => 'select',
      '#title' => t('Default Alignment'),
      '#default_value' => variable_get('gallery_g2image_default_alignment', 'none'),
      '#options' => $css_options,
      '#description' => t('Choose the default alignment of images.'),
    );
    
    $form['g2image']['gallery_g2image_custom_class'] = array('#tree' => TRUE);
    for ($i=1; $i<=4; $i++) {
      $form['g2image']['gallery_g2image_custom_class'][$i] = array(
        '#type' => 'textfield',
        '#title' => t('Custom Class @index', array('@index' => $i)),
        '#default_value' => isset($gallery_g2image_custom_class[$i]) ? $gallery_g2image_custom_class[$i] : '',
        '#size' => 20,
        '#maxlength' => 50,
      );
    }
    $form['g2image']['gallery_g2image_custom_class'][$i-1]['#description'] = t('Additional css classes to be available
                                                                                for selection (must be defined in .css file).');

    $form['g2image']['gallery_g2image_class_mode'] = array(
      '#type' => 'select',
      '#title' => t('Alignment Class Insertion Point'),
      '#default_value' => variable_get('gallery_g2image_class_mode', 'img'),
      '#options' => array(
        'img' => t('<img class=...>'),
        'div' => t('<div class=...><img ...>'),
      ),
      '#description' => t('Determines where the alignment class will be inserted. If you choose \'&lt;div
                           class=...&gt;&lt;img ...&gt;\', you will have to manually delete any &lt;div&gt; tags
                           manually after deleting images from the TinyMCE window.'),
    );
  }
  
  $form['#validate'] = array('_gallery_settings_g2image_validate');
  return system_settings_form($form);
}

/**
 * Function _gallery_settings_g2image_validate().
 * (saves the G2Image config file)
 */
function _gallery_settings_g2image_validate($form, &$form_state) {
  switch ($form_state['values']['gallery_g2image_mode']) {
    case 'tinymce':
      $mode = t('TinyMCE');
      $path = drupal_get_path('module', 'tinymce');
      $path .= '/tinymce/jscripts/tiny_mce/plugins/g2image';
      break;
    case 'standalone':
      $mode = t('Standalone');
      $path = drupal_get_path('module', 'gallery');
      $path .= '/g2image';
      break;
    default:
      return;
  }
  
  // Note that file_check_directory uses &$path and will strip the trailing '/'
  $filename = $path .'/config.php';
  if (is_writable($filename) == FALSE && !file_check_directory($path)) {
    form_set_error('', t('G2Image does not seem to be installed for @mode mode in the required directory (@dir), or the
                          directory is not writable. Please see the INSTALL.txt for instructions.',
                            array('@mode' => $mode, '@dir' => '<drupal_path>/'. $path)));
    return;
  }

  $cr = "\n";
  $g2ic_gallery2_path = str_replace('\\', '/', variable_get('gallery_dir', './gallery2/'));
  $g2ic_use_full_path = (substr($g2ic_gallery2_path, 0, 1) == '/' || substr($g2ic_gallery2_path, $_SERVER['DOCUMENT_ROOT']) !== FALSE) ? 'TRUE' : 'FALSE';
  $form_state['values']['gallery_g2image_custom_url'] = check_url($form_state['values']['gallery_g2image_custom_url']);
  $g2ic_display_filenames =  ($form_state['values']['gallery_g2image_display_filenames'] == 'filenames') ? 'TRUE' : 'FALSE';
  $g2ic_custom_class = array();
  foreach ($form_state['values']['gallery_g2image_custom_class'] as $key => $value) {
    $g2ic_custom_class[$key] = ($value) ? $value : 'not_used';
  }

  $g2ic_custom_class_1 = ($form_state['values']['gallery_g2image_custom_class_1']) ?
    $form_state['values']['gallery_g2image_custom_class_1'] : 'not_used';
  $g2ic_custom_class_2 = ($form_state['values']['gallery_g2image_custom_class_2']) ?
    $form_state['values']['gallery_g2image_custom_class_2'] : 'not_used';
  $g2ic_custom_class_3 = ($form_state['values']['gallery_g2image_custom_class_3']) ?
    $form_state['values']['gallery_g2image_custom_class_3'] : 'not_used';
  $g2ic_custom_class_4 = ($form_state['values']['gallery_g2image_custom_class_4']) ?
    $form_state['values']['gallery_g2image_custom_class_4'] : 'not_used';

  $content = '<?php'. $cr;
  $content .= '/*'. $cr;
  $content .= '    Gallery 2 Image Chooser'. $cr;
  $content .= '    Version 3.0 - updated 16 SEP 2007'. $cr;
  $content .= '    Documentation: http://g2image.steffensenfamily.com/'. $cr;
  $content .= $cr;
  $content .= '    Author: Kirk Steffensen with inspiration, code snippets,'. $cr;
  $content .= '        and assistance as listed in CREDITS.HTML'. $cr;
  $content .= $cr;
  $content .= '    Released under the GPL version 2.'. $cr;
  $content .= '    A copy of the license is in the root folder of this plugin.'. $cr;
  $content .= $cr;
  $content .= '    See README.HTML for installation info.'. $cr;
  $content .= '    See CHANGELOG.HTML for a history of changes.'. $cr;
  $content .= '*/'. $cr;
  $content .= $cr;
  $content .= '//  Automatically generated by gallery.module (admin/settings/gallery/g2image).'. $cr;
  $content .= '//  DO NOT EDIT'. $cr;
  $content .= $cr;

  $content .= '$g2ic_embedded_mode = TRUE;'. $cr;
  $content .= '$g2ic_gallery2_path = \''. $g2ic_gallery2_path .'\';'. $cr;
  $content .= '$g2ic_use_full_path = '. $g2ic_use_full_path .';'. $cr;
  $content .= '$g2ic_gallery2_uri = \''.  variable_get('gallery_uri', '/gallery2/') .'\';'. $cr;
  $content .= '$g2ic_embed_uri = \''. variable_get('gallery_embed_uri', '?q=gallery') .'\';'. $cr;

  $content .= '$g2ic_drupal_g2_filter = TRUE;'. $cr;
  $content .= '$g2ic_drupal_g2_filter_prefix = \''. variable_get('gallery_filter_prefix', 'G2') .'\';'. $cr;

  $content .= '$g2ic_language = \'en\';'. $cr;
  $content .= '$g2ic_images_per_page = '. $form_state['values']['gallery_g2image_images_per_page'] .';'. $cr;
  $content .= '$g2ic_display_filenames = '. $g2ic_display_filenames .';'. $cr;

  $content .= '$g2ic_default_alignment = \''. $form_state['values']['gallery_g2image_default_alignment'] .'\';'. $cr;
  $content .= '$g2ic_custom_class_1 = \''. $g2ic_custom_class[1] .'\';'. $cr;
  $content .= '$g2ic_custom_class_2 = \''. $g2ic_custom_class[2] .'\';'. $cr;
  $content .= '$g2ic_custom_class_3 = \''. $g2ic_custom_class[3] .'\';'. $cr;
  $content .= '$g2ic_custom_class_4 = \''. $g2ic_custom_class[4] .'\';'. $cr;
  $content .= '$g2ic_class_mode = \''. $form_state['values']['gallery_g2image_class_mode'] .'\';'. $cr;

  $content .= '$g2ic_default_action = \''. $form_state['values']['gallery_g2image_default_action'] .'\';'. $cr;
  $content .= '$g2ic_sortby = \''. $form_state['values']['gallery_g2image_sortby'] .'\';'. $cr;
  $content .= '$g2ic_custom_url = \''. $form_state['values']['gallery_g2image_custom_url'] .'\';'. $cr;
  $content .= $cr;
  $content .= '?>'. $cr;

  $filename = $path .'/config.php';
  if (is_writable($filename) == FALSE) {
    form_set_error('', t('Could not write to G2Image config file. Please check the permissions for @filename.',
                            array('@filename' => $filename)));
    return;
  }
  $handle = fopen($filename, "w");
  if (fwrite($handle, $content) === FALSE) {
    form_set_error('', t('Could not write to G2Image config file. Please check the permissions for @filename.',
                            array('@filename' => $filename)));
    return;
  }
  else {
    drupal_set_message(t('G2Image configuration file successfully created.'));
  }
  fclose($handle);
}

/**
 * Function _gallery_settings_search().
 */
function _gallery_settings_search() {
  require_once(drupal_get_path('module', 'gallery') .'/gallery_block.inc');
  $plugin_status =  gallery_plugin_status(array('imageblock', 'imageframe', 'search'));

  $search_desc = t('The Gallery Search requires the Gallery2 Search plugin (!search_status) and
    optionally the Gallery2 Image Block plugin (!imageblock_status) and Gallery2 Image Frame
    plugin (!imageframe_status).',
      array(
        '!search_status' => theme('gallery_plugin_status_message', $plugin_status['search']),
        '!imageblock_status' => theme('gallery_plugin_status_message', $plugin_status['imageblock']), 
        '!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']),
      )
    );
  $g2_uri = variable_get('gallery_embed_uri', '?q=gallery');
  $g2_plugins_page = (variable_get('gallery_valid', 0)) ? 
    t('<a href= "@g2_plugins">Gallery2 Plugins</a>',
      array('@g2_plugins' => $g2_uri .'&g2_view=core.SiteAdmin&g2_subView=core.AdminModules'))
    : t('Gallery2 Site Admin -> Plugins');
  $thumbs_desc = '';
  if ($plugin_status['search'] != GALLERY_PLUGIN_ENABLED) {
    $search_desc .= t(' However the Search plugin is unavailable, so the search is
      not available and these settings are disabled. To use the search feature please go to
      the !g2_plugins page and install/activate the Image Block plugin.',
        array('!g2_plugins' => $g2_plugins_page));
  }   
  if ($plugin_status['imageblock'] != GALLERY_PLUGIN_ENABLED) {
    $thumbs_desc = t('To display search results with thumbnail images requires the Gallery2 Image
      Block plugin (!imageblock_status) and optionally the  and Gallery2 Image Frame 
      plugin (!imageframe_status).',
        array(
          '!imageblock_status' => theme('gallery_plugin_status_message', $plugin_status['imageblock']), 
          '!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']),
        )
      );
    $thumbs_desc .= t(' However the Image Block plugin is unavailable, so the thumbnails are
      not available and these settings are disabled. To use this feature please go to
      the !g2_plugins page and install/activate the Image Block plugin.',
        array('!g2_plugins' => $g2_plugins_page));
  }    

  $imageframe_desc = ($plugin_status['imageframe'] != GALLERY_PLUGIN_ENABLED) ?
    t('Requires the Gallery2 Image Frame plugin (!imageframe_status).',
        array('!imageframe_status' => theme('gallery_plugin_status_message', $plugin_status['imageframe']))) : '';
     
  $form['search'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search settings'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#description' => $search_desc,
  );
  
  $image_frames = gallery_get_image_frames();
  _gallery_block_options($type_map, $param_map);
  
  if ($plugin_status['search'] == GALLERY_PLUGIN_ENABLED) {
    $form['search']['gallery_search_advanced'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display advanced search options'),
      '#default_value' => variable_get('gallery_search_advanced', 1),
      '#description' => t('Adds options to the search form to select which Gallery fields to search in.'),
    );
    
    $form['search']['gallery_search_num_per_row'] = array(
      '#type' => 'select',
      '#title' => t('Number of search results per table row'),
      '#default_value' => variable_get('gallery_search_num_per_row', 3),
      '#options' => _gallery_range_array(1, 5),
      '#description' => t('Select the number of search results per row in the paged table.'),
    );
    
    $form['search']['gallery_search_rows_per_pager'] = array(
      '#type' => 'select',
      '#title' => t('Number of rows per page'),
      '#default_value' => variable_get('gallery_search_rows_per_pager', 4),
      '#options' => _gallery_range_array(1, 10),
      '#description' => t('Select the number of rows in the paged table.'),
    );
    
    $form['search']['thumbs'] = array(
      '#type' => 'fieldset',
      '#title' => t('Search thumbnail settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => $thumbs_desc,
    );
    
    if ($plugin_status['imageblock'] == GALLERY_PLUGIN_ENABLED) {          
      $form['search']['thumbs']['gallery_search_show_thumbs'] = array(
        '#type' => 'checkbox',
        '#title' => t('Display thumbnails'),
        '#default_value' => variable_get('gallery_search_show_thumbs', 1),
        '#description' => t('Display thumbnail images for the search results.'),
      );
      
      $form['search']['thumbs']['gallery_search_block_show'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Image data'),
        '#default_value' => variable_get('gallery_search_block_show', array('title' => t('Title'))),
        '#options' => $param_map,
        '#description' => t('Choose the item metadata you would like to display.'),
        );
        
      $form['search']['thumbs']['gallery_search_size_method'] = array(
        '#type' => 'select',
        '#title' => t('Image size method'),
        '#default_value' => variable_get('gallery_search_size_method', GALLERY_SEARCH_SIZE_METHOD_DEFAULT),
        '#options' => array(
          'maxsize' => t('Max Size'),
          'exactsize' => t('Exact Size'),
        ),
        '#description' => t('\'Max Size\' gives faster image downloading, but the image size 
          may be smaller than the size defined below. <br />\'Exact Size\' may be slower
          (as a larger image is downloaded and then scaled by the browser) but the image
          will be guaranteed to be the size defined below. Only supported for G2.2+.'),
      );  
    
      $form['search']['thumbs']['gallery_search_size'] = array(
        '#type' => 'textfield',
        '#title' => t('Image size'),
        '#default_value' => variable_get('gallery_search_size', GALLERY_SEARCH_SIZE_DEFAULT),
        '#size' => 10,
        '#maxlength' => 10,
        '#description' => t('Sets the size (in pixels) of the longest side of the image
          according to the method defined above.'),
      );
       
      $form['search']['thumbs']['gallery_search_album_frame'] = array(
        '#type' => 'select',
        '#title' => t('Album frame'),
        '#default_value' => variable_get('gallery_search_album_frame', 'none'),
        '#options' => $image_frames,
        '#description' => $imageframe_desc,
      );
      
      $form['search']['thumbs']['gallery_search_item_frame'] = array(
        '#type' => 'select',
        '#title' => t('Item frame'),
        '#default_value' => variable_get('gallery_search_item_frame', 'none'),
        '#options' => $image_frames,
        '#description' => $imageframe_desc,
      );
      
      $form['search']['thumbs']['gallery_search_link_target'] = array(
        '#type' => 'textfield',
        '#title' => t('Link target'),
        '#default_value' => variable_get('gallery_search_link_target', ''),
        '#size' => 20,
        '#maxlength' => 20,
        '#description' => t('Enter a link target (e.g. "_blank", "_new").'),
      );
      
      $form['search']['thumbs']['gallery_search_default_link'] = array(
        '#type' => 'textfield',
        '#title' => t('Image Link'),
        '#default_value' => variable_get('gallery_search_default_link', ''),
        '#size' => 20,
        '#maxlength' => 20,
        '#description' => t('By default the image has a link to the item in the Gallery. This
          can be overridden here (or leave empty for the default). Use \'none\' for no link, or a
          URL to link somewhere else instead.'),
      );
    }
  }
  
  $form['array_filter'] = array('#type' => 'value');
  $form['#validate'] = array('_gallery_settings_search_validate');
  return system_settings_form($form);
}

/**
 * Function _gallery_settings_search_validate().
 */
function _gallery_settings_search_validate($form, &$form_state) {
  if (isset($form_state['values']['gallery_search_size']) &&
    (!is_numeric($form_state['values']['gallery_search_size']) || $form_state['values']['gallery_search_size'] < 1)) {
    form_set_error('gallery_search_size',
      t('Image size must be a number greater than zero.'));
  }
}

/**
 * Function _gallery_settings_album_select().
 */
function _gallery_settings_album_select($title, $value, $description, $root = NULL, $depth = 1, $uid = NULL) {
  $options = array();
  $tree = gallery_album_tree($root, $depth, $uid);
  _gallery_settings_album_traverse($tree, $options);

  return array(
    '#type' => 'select',
    '#title' => $title,
    '#default_value' => $value,
    '#options' => array('default' => t('Default')) + $options,
    '#description' => $description
  );
}

/**
 * Function _gallery_settings_album_traverse().
 */
function _gallery_settings_album_traverse(&$tree, &$items) {
  foreach (array_keys($tree) as $id) {
    $album = gallery_item_details($id);
    $items[$id] = $album['title'];
    if (count($tree[$id])) {
      _gallery_settings_album_traverse($tree[$id], $items);
    }
  }
}

/**
 * Function _gallery_range_array().
 * (create an associative range array
 *  like array($min=>$min, ..., $max=>$max))
 */
function _gallery_range_array($min, $max, $interval = 1) {
  $range_array = array();
  for ($i=$min; $i<=$max; $i+=$interval) {
    $range_array[$i] = $i;
  }

  return $range_array;
}

/**
 * Function _gallery_php_memcheck().
 * (check the amount of PHP memory)
 */
function _gallery_php_memcheck($min_memory = 24) {
  $mem_limit = trim(ini_get('memory_limit'));
  $mem_limit_bytes = parse_size($mem_limit);
  
  if (empty($mem_limit)) {
    return array(
      'status' => TRUE,
      'info' => t('There is no memory limit restricting your PHP installation.'),
    );
  }
  elseif (($mem_limit_bytes / (1024 * 1024)) < $min_memory) {
    return array(
      'status' => FALSE,
      'info' => t('Your PHP is configured to limit the memory to @mem_limit
        (memory_limit parameter in php.ini). You must raise this limit 
        to at least @min_mem_limitM for proper embedded Gallery2 operation.',
        array(
          '@mem_limit' => $mem_limit, 
          '@min_mem_limit' => $min_memory,
        )
      )
    );
  } 
  else {
    return array(
      'status' => TRUE,
      'info' => t('Your PHP is configured to limit the memory to @mem_limit
        (memory_limit parameter in php.ini). This should be fine for embedded Gallery2 operation.',
        array(
          '@mem_limit' => $mem_limit, 
        )
      )
    );
  }
}

/**
 * Implementation of hook_gallery_plugin_info().
 */
function gallery_gallery_plugin_info($type) {
  switch ($type) {
    case GALLERY_PLUGIN_WANTED:
      return array(
        'imageblock' => array(
          'title' => 'Image Block',
          'info' => t('Allows images to be included in the Drupal sidebar or in nodes.'),
          'severity' => GALLERY_SEVERITY_ERROR,
          'status' => gallery_single_plugin_status('imageblock'),
        ),
        'imageframe' => array(
          'title' => 'ImageFrame',
          'info' => t('Provides a variety of frames around the images.'),
          'severity' => GALLERY_SEVERITY_WARNING,
          'status' => gallery_single_plugin_status('imageframe'),
        ),
        'search' => array(
          'title' => 'Search',
          'info' => t('Allow the Drupal search to also search the Gallery.'),
          'severity' => GALLERY_SEVERITY_WARNING,
          'status' => gallery_single_plugin_status('search'),
        ),
        'rewrite' => array(
          'title' => 'URL Rewrite',
          'info' => t('Allow short URLs (clean URLs).'),
          'severity' => GALLERY_SEVERITY_ADVISE,
          'status' => gallery_single_plugin_status('rewrite'),
        ),
      );
    case GALLERY_PLUGIN_UNWANTED:
      return array(
        'register' => array(
          'title' => 'Registration',
          'info' => t('All user registration must take place via Drupal to ensure that the users
            are synchronized between Drupal and Gallery2. This plugin would allow a user to register
            only in Gallery2 and not in Drupal and so should not be used.'),
          'severity' => GALLERY_SEVERITY_WARNING,
          'status' => gallery_single_plugin_status('register'),
        ),
      );
    case GALLERY_PLUGIN_DRUPAL:
      return array(
        'xmlsitemap' => array(
          'title' => 'XML Sitemap',
          'status' => module_exists('xmlsitemap'),
          'severity' => GALLERY_SEVERITY_ADVISE,
          'info' => t('Enables the Drupal and Gallery2 sitemaps to be merged which allows for
                       a single sitemap to be sent to Google and other web search sites.'),
        ),
        'profile' => array(
          'title' => 'Profile',
          'status' => module_exists('profile'),
          'severity' => GALLERY_SEVERITY_ADVISE,
          'info' => t('Allows support of the \'Full Name\' field in Gallery2.'),
        ),
      );
    default:
      return array();
  }
}

/**
 * Function gallery_plugin_set_status().
 */
function gallery_plugin_set_status($plugin_names) {
  $plugins_status = gallery_plugin_status($plugin_names);
  $plugin_info = module_invoke_all('gallery_plugin_info', GALLERY_PLUGIN_WANTED);
  // Generate array containing module status
  $status = array();
  foreach ($plugins_status as $plugin => $plugin_status) {
    if ($plugin_status == GALLERY_PLUGIN_ENABLED) {
      $status[$plugin] = '';
    }
    else {
      $status[$plugin]['title'] = t('Gallery2 plugin \'@plugin\' is not available',
        array('@plugin' => isset($plugin_info[$plugin]) ? $plugin_info[$plugin]['title'] : drupal_ucfirst($plugin)));
      $status[$plugin]['severity'] = isset($plugin_info[$plugin]['severity']) ? $plugin_info[$plugin]['severity'] : GALLERY_SEVERITY_WARNING;
      $status[$plugin]['url'] = url('admin/settings/gallery/install', NULL, 'gallery-install-plugin-'. $plugin);
    }
  }
  gallery_set_status($status);
}

/**
 * Function gallery_format_status().
 */
function gallery_format_status($status = array(), $title = 'Gallery message(s):') {
  $message = $title .'<ul>';
  if (count($status)) {
    foreach ($status as $item) {
      $message .= '<li>';
      $message .= empty($item['url']) ? t($item['title']) : '<a href="'. $item['url'] .'">'. t($item['title']) .'</a>';
      $message .= (isset($item['info']) && !empty($item['info'])) ? (': '. t($item['info'])) : '';
      $message .= isset($item['severity'])  ? (' ['. theme('gallery_severity_message', $item['severity']) .']') : '';
      $message .= '</li>';
    }
  }
  else {
    $message = t('Status not available');
  }
  $message .= '</ul>';
  
  return $message;
}

/**
 * Theme function : theme_gallery_module_status_message().
 */
function theme_gallery_module_status_message($status) {
  if ($status) {
    return '<span class="admin-enabled">'. t('enabled') .'</span>';
  } 
  else {
    return '<span class="admin-disabled">'. t('disabled') .'</span>';
  }
}

/**
 * Theme function : theme_gallery_severity_message().
 */
function theme_gallery_severity_message($severity = NULL) {
  switch ($severity) {
    case GALLERY_SEVERITY_SUCCESS:
      return '<span class=\'g2_embed_success\'>'. t('OK') .'</span>';
    case GALLERY_SEVERITY_ERROR:
      return '<span class=\'g2_embed_error\'>'. t('Error') .'</span>';
    case GALLERY_SEVERITY_WARNING:
      return '<span class=\'g2_embed_warning\'>'. t('Warning') .'</span>';
    case GALLERY_SEVERITY_ADVISE:
      return '<span class=\'g2_embed_warning\'>'. t('Advisory') .'</span>';
    case GALLERY_SEVERITY_UNKNOWN:
      return '<span class=\'g2_embed_warning\'>'. t('Unknown') .'</span>';
    default:
  }      
}

/**
 * Theme function : theme_gallery_plugin_status_message().
 */
function theme_gallery_plugin_status_message($status, $invert = FALSE) {
  $classes = array('enabled' => 'admin-enabled', 'disabled' => 'admin-disabled');
  if ($invert) {
    $classes = array_reverse($classes, TRUE);
  }

  switch ($status) {
    case GALLERY_PLUGIN_ENABLED:
      return '<span class="'. $classes['enabled'] .'">'. t('activated') .'</span>';
    case GALLERY_PLUGIN_DISABLED:
      return '<span class="'. $classes['disabled'] .'">'. t('disabled') .'</span>';
    case GALLERY_PLUGIN_NOT_ACTIVE:
      return '<span class="'. $classes['disabled'] .'">'. t('deactivated') .'</span>';
    case GALLERY_PLUGIN_NOT_INSTALLED:
      return '<span class="'. $classes['disabled'] .'">'. t('not installed') .'</span>';
    case GALLERY_PLUGIN_MISSING:
      return '<span class="'. $classes['disabled'] .'">'. t('missing') .'</span>';
    case GALLERY_PLUGIN_STATUS_UNKNOWN:
    default:
      return '<span class="'. $classes['disabled'] .'">'. t('unknown') .'</span>';
  }    
}

/**
 * Theme function : theme_gallery_severity_status_message().
 */
function theme_gallery_severity_status_message($severity, $status, $full_msg = FALSE, $invert = FALSE) {
  // In some cases (e.g. unwanted plugins) it makes sense to return the full 2 part message
  // even on a success, but in most cases a simple "OK" is sufficient.
  if ($full_msg) {
    return theme('gallery_severity_message', $severity) .' ('. theme('gallery_plugin_status_message', $status, $invert) .')';
  }    
  switch ($severity) {
    case GALLERY_SEVERITY_SUCCESS:
      return theme('gallery_severity_message', $severity);
    default:
      return theme('gallery_severity_message', $severity) .' ('. theme('gallery_plugin_status_message', $status, $invert) .')';
  }      
}