~ximo/drupal-fancybox/DRUPAL-6--1

« back to all changes in this revision

Viewing changes to fancybox.admin.inc

  • Committer: Private
  • Date: 2010-02-01 21:33:40 UTC
  • Revision ID: ximo@joakim.local-20100201213340-yyqae3uf60tmwxzx
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id$
 
3
 
 
4
/**
 
5
 * @file
 
6
 * Administration page callbacks for the Fancybox module.
 
7
 */
 
8
 
 
9
 
 
10
/**
 
11
 * Display the Fancybox settings form.
 
12
 */
 
13
function fancybox_admin_settings_form($form_state) {
 
14
  $form = array();
 
15
 
 
16
  // Fancybox path:
 
17
  $form['fancybox_path'] = array(
 
18
    '#type' => 'textfield',
 
19
    '#title' => t('Path to Fancybox jQuery plugin'),
 
20
    '#default_value' => variable_get('fancybox_path', FANCYBOX_DEFAULT_PATH),
 
21
    '#description' => t('Enter the location of the <a href="!url" target="_blank">Fancybox jQuery Plugin</a>. It is recommended to use %path.', array('!url' => 'http://fancybox.net/', '%path' => FANCYBOX_DEFAULT_PATH)),
 
22
  );
 
23
 
 
24
  $settings = variable_get('fancybox_settings', array());
 
25
 
 
26
  $data = array();
 
27
 
 
28
  // Options:
 
29
  $data['options'] = array(
 
30
    '#type' => 'fieldset',
 
31
    '#title' => t('Options'),
 
32
    '#tree' => TRUE,
 
33
  );
 
34
  $data['options'] += fancybox_options($settings['options']);
 
35
 
 
36
  // Activation:
 
37
  $data['activation'] = array(
 
38
    '#type' => 'fieldset',
 
39
    '#title' => t('Activation'),
 
40
    '#collapsible' => TRUE,
 
41
    '#collapsed' => TRUE,
 
42
  );
 
43
  $data['activation']['selector'] = array(
 
44
    '#type' => 'textarea',
 
45
    '#title' => t('jQuery selector'),
 
46
    '#default_value' => isset($settings['activation']['selector']) ? $settings['activation']['selector'] : '',
 
47
  );
 
48
  $data['activation']['activation_type'] = array(
 
49
    '#type' => 'radios',
 
50
    '#title' => t('Enable Fancybox on specific pages'),
 
51
    '#options' => array(
 
52
      'exclude' => t('Enable on every page except the listed pages.'),
 
53
      'include' => t('Enable on only the listed pages.'),
 
54
    ),
 
55
    '#default_value' => isset($settings['activation']['activation_type']) ? $settings['activation']['activation_type'] : 'exclude',
 
56
  );
 
57
  if (user_access('use PHP for block visibility')) {
 
58
    $data['activation']['activation_type']['#options']['php'] = t('Enable if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
 
59
  }
 
60
  $data['activation']['activation_pages'] = array(
 
61
    '#type' => 'textarea',
 
62
    '#title' => t('Pages'),
 
63
    '#default_value' => isset($settings['activation']['activation_pages']) ? $settings['activation']['activation_pages'] : "admin*\nimg_assist*\nnode/add/*\nnode/*/edit",
 
64
    '#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>')),
 
65
  );
 
66
 
 
67
  // Image module:
 
68
  if (module_exists('image')) {
 
69
    $data['image'] = array(
 
70
      '#type' => 'fieldset',
 
71
      '#title' => t('Image module'),
 
72
      '#collapsible' => TRUE,
 
73
      '#collapsed' => TRUE,
 
74
    );
 
75
    $data['image']['status'] = array(
 
76
      '#type' => 'checkbox',
 
77
      '#title' => t('Enable Fancybox on Image nodes'),
 
78
      '#default_value' => isset($settings['image']['auto']) ? $settings['image']['auto'] : FALSE,
 
79
    );
 
80
 
 
81
    $options = array();
 
82
    $sizes = image_get_sizes();
 
83
    foreach ($sizes as $label => $size) {
 
84
      $options[$label] = $size['label'];
 
85
    }
 
86
 
 
87
    $data['image']['size'] = array(
 
88
      '#type' => 'select',
 
89
      '#title' => t('Display size'),
 
90
      '#options' => $options,
 
91
      '#default_value' => isset($settings['image']['size']) ? $settings['image']['size'] : '_original',
 
92
      '#description' => t('The size to be used when displaying an image in a Fancybox.'),
 
93
    );
 
94
  }
 
95
 
 
96
  // ImageField module:
 
97
  if (module_exists('imagefield')) {
 
98
    $data['imagefield'] = array(
 
99
      '#type' => 'fieldset',
 
100
      '#title' => t('ImageField module'),
 
101
      '#collapsible' => TRUE,
 
102
      '#collapsed' => TRUE,
 
103
    );
 
104
 
 
105
    $data['imagefield']['grouping'] = array(
 
106
      '#type' => 'radios',
 
107
      '#title' => t('Grouping'),
 
108
      '#options' => array(1 => t('Group per field'), 2 => t('Group all fields on page'), 0 => t('No grouping')),
 
109
      '#default_value' => isset($settings['imagefield']['grouping']) ? $settings['imagefield']['grouping'] : 1,
 
110
    );
 
111
 
 
112
    // ImageCache module:
 
113
    if (module_exists('imagecache')) {
 
114
      $options = array(0 => t('Original image (no preset)'));
 
115
      foreach (imagecache_presets() as $preset) {
 
116
        $options[$preset['presetname']] = $preset['presetname'];
 
117
      }
 
118
      $data['imagefield']['imagecache_preset'] = array(
 
119
        '#type' => 'select',
 
120
        '#title' => t('ImageCache preset'),
 
121
        '#options' => $options,
 
122
        '#default_value' => isset($settings['imagefield']['imagecache_preset']) ? $settings['imagefield']['imagecache_preset'] : 0,
 
123
        '#description' => t('The ImageCache preset to be use when displaying ImageField images in Fancybox.'),
 
124
      );
 
125
    }
 
126
 
 
127
    $data['imagefield']['use_node_title'] = array(
 
128
      '#type' => 'checkbox',
 
129
      '#title' => t('Use node title as caption'),
 
130
      '#description' => t('By default, the caption for image fields is the title text for the image. If no title is configured, the alt text will be used. Check this to always display the node title as the image caption.'),
 
131
      '#default_value' => isset($settings['imagefield']['use_node_title']) ? $settings['imagefield']['use_node_title'] : FALSE,
 
132
    );
 
133
  }
 
134
 
 
135
  $form['data'] = $data;
 
136
  $form['data']['#tree'] = TRUE;
 
137
 
 
138
  $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
 
139
  $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
 
140
 
 
141
  return $form;
 
142
}
 
143
 
 
144
/**
 
145
 * Validation handler for the Fancybox settings form.
 
146
 */
 
147
function fancybox_admin_settings_form_validate($form, &$form_state) {
 
148
  // TODO: Check fancybox_path for .js files.
 
149
}
 
150
 
 
151
/**
 
152
 * Submit handler for the Fancybox settings form.
 
153
 */
 
154
function fancybox_admin_settings_form_submit($form, &$form_state) {
 
155
  $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
 
156
  if ($op == t('Reset to defaults')) {
 
157
    variable_del('fancybox_settings');
 
158
    variable_del('fancybox_path');
 
159
    drupal_set_message(t('The configuration options have been reset to their default values.'));
 
160
  }
 
161
  else {
 
162
    $form_state['values']['data']['options'] = fancybox_array_flatten($form_state['values']['data']['options']);
 
163
    variable_set('fancybox_settings', $form_state['values']['data']);
 
164
    variable_set('fancybox_path', rtrim($form_state['values']['fancybox_path'], '/ '));
 
165
    drupal_set_message(t('The configuration options have been saved.'));
 
166
  }
 
167
}
 
168
 
 
169
 
 
170
/*****************************************************************************
 
171
 * HELPER FUNCTIONS
 
172
 *****************************************************************************/
 
173
 
 
174
/**
 
175
 * Flatten an array, preserving its keys.
 
176
 */
 
177
function fancybox_array_flatten($array) {
 
178
  $result = array();
 
179
  if (is_array($array)) {
 
180
    foreach ($array as $key => $value) {
 
181
      if (is_array($value)) {
 
182
        $result += fancybox_array_flatten($value);
 
183
      }
 
184
      else {
 
185
        $result[$key] = $value;
 
186
      }
 
187
    }
 
188
  }
 
189
  return $result;
 
190
}