Project

General

Profile

Download (6.19 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * Provides the image names which can be configured in the theme settings to all
5
 * theme setting functions.
6
 *
7
 * @return multitype:string
8
 *     the list of image names which can be configured in the theme settings
9
 */
10
function _zen_dataportal_imagenames() {
11
  static $image_list = array('banner', 'body_background', 'page_background');
12
  return $image_list;
13
}
14

    
15
/**
16
 * Implements hook_form_system_theme_settings_alter().
17
 *
18
 * @param $form
19
 *   Nested array of form elements that comprise the form.
20
 * @param $form_state
21
 *   A keyed array containing the current state of the form.
22
 */
23
function zen_dataportal_form_system_theme_settings_alter(&$form, &$form_state, $form_id = NULL)  {
24
  // Work-around for a core bug affecting admin themes. See issue #943212.
25
  if (isset($form_id)) {
26
    return;
27
  }
28

    
29
  $form['zen_dataportal_colors'] = array(
30
          '#type' => 'fieldset',
31
          '#title' => t('Colors'),
32
          '#description' => t('Configure colors where.'),
33
          '#attributes' => array('class' => array('theme-settings-bottom')),
34
  );
35
  $form['zen_dataportal_colors']['site_name_color'] = array(
36
      '#type'          => 'textfield',
37
      '#title'         => t('Site name color'),
38
      '#default_value' => theme_get_setting('site_name_color'),
39
      '#description'   => t('Set the color of the site name which is shown in the header. Must be a css color value like: #000000'),
40
  );
41

    
42
  //
43
  // custom images for banner, body and page backgrounds
44
  //
45
  foreach(_zen_dataportal_imagenames() as $which_image) {
46
    $image_label = str_replace('_', ' ', $which_image);
47
    $form['zen_dataportal_' .  $which_image] = array(
48
        '#type' => 'fieldset',
49
        '#title' => ucfirst(t($image_label)) . ' ' . t('image settings'),
50
        '#description' => t('If toggled on, the following image will be displayed.'),
51
        '#attributes' => array('class' => array('theme-settings-bottom')),
52
      );
53
      $form['zen_dataportal_' .  $which_image]['default_' . $which_image] = array(
54
        '#type' => 'checkbox',
55
        '#title' => t('Use the default') . t($which_image),
56
        '#default_value' => theme_get_setting('default_' . $which_image),
57
        '#tree' => FALSE,
58
        '#description' => t('Check here if you want the theme to use the image supplied with it.')
59
      );
60
      $form['zen_dataportal_' .  $which_image]['settings'] = array(
61
        '#type' => 'container',
62
        '#states' => array(
63
          // Hide the logo settings when using the default logo.
64
          'invisible' => array(
65
            'input[name="default_' . $which_image . '"]' => array('checked' => TRUE),
66
          ),
67
        ),
68
      );
69

    
70
      // If $path is a public:// URI, display the path relative to the files
71
      // directory; stream wrappers are not end-user friendly.
72
      $path = theme_get_setting($which_image . '_path');
73
      $image = '';
74
      if (file_uri_scheme($path) == 'public') {
75
        $url = file_create_url($path);
76
        $path = file_uri_target($path);
77
        $form['zen_dataportal_' .  $which_image]['settings'][$which_image . '_preview'] = array(
78
        	'#type' => 'item',
79
          '#title' => t('Preview'),
80
          '#markup' => '<div class="image-preview"><img src="' . $url . '"/></div>',
81
        );
82
      }
83
      $form['zen_dataportal_' .  $which_image]['settings'][$which_image . '_path'] = array(
84
        '#type' => 'textfield',
85
        '#title' => t('Path to custom') . ' ' . t($image_label),
86
        '#description' => t('The path to the file you would like to use as your') . ' ' . t($image_label),
87
        '#default_value' => $path,
88
      );
89
      $form['zen_dataportal_' .  $which_image]['settings'][$which_image . '_upload'] = array(
90
        '#type' => 'file',
91
        '#title' => t('Upload') . ' ' . t($image_label) . t(' image'),
92
        '#maxlength' => 40,
93
        '#description' => t("If you don't have direct file access to the server, use this field to upload your image.")
94
      );
95
  } // END of loop over image names
96

    
97

    
98
  $form['#validate'][] = 'zen_dataportal_theme_settings_validate';
99
  $form['#submit'][] = 'zen_dataportal_theme_settings_submit';
100

    
101

    
102
  // We are editing the $form in place, so we don't need to return anything.
103
}
104

    
105
/**
106
 * Process zen_dataportal admin form submissions.
107
 * @param $form
108
 *   Nested array of form elements that comprise the form.
109
 * @param $form_state
110
 *   A keyed array containing the current state of the form.
111
 */
112
function zen_dataportal_theme_settings_submit($form, &$form_state) {
113

    
114
  $values = &$form_state['values'];
115

    
116
  foreach(_zen_dataportal_imagenames() as $which_image) {
117
    // If the user uploaded a new image, save it to a permanent location
118
    // and use it in place of the default theme-provided file.
119
    if ($file = $values[$which_image . '_upload']) {
120
      unset($values[$which_image . '_upload']);
121
      $filename = file_unmanaged_copy($file->uri);
122
      $values['default_' .$which_image ] = 0;
123
      $values[$which_image . '_path'] = $filename;
124
      $values['toggle_' . $which_image] = 1;
125
    }
126
    if (!empty($values[$which_image . '_path'])) {
127
      $values[$which_image . '_path'] = _system_theme_settings_validate_path($values[$which_image . '_path']);
128
    }
129

    
130
  } // END of loop over image names
131

    
132
  /*
133
   * Ok, we are done here the $values will be saved in the theme
134
   * variable by system_theme_settings_submit($form, &$form_state)
135
   * in modules/system/system.admin.inc
136
   */
137

    
138
}
139

    
140
/**
141
 * Validator for the system_theme_settings() form.
142
 *  @param $form
143
 *   Nested array of form elements that comprise the form.
144
 * @param $form_state
145
 *   A keyed array containing the current state of the form.
146
 */
147
function zen_dataportal_theme_settings_validate($form, &$form_state) {
148

    
149
  $validators = array('file_validate_is_image' => array());
150

    
151
  foreach(_zen_dataportal_imagenames() as $which_image) {
152
    // Check for a new uploaded logo.
153
    $file = file_save_upload($which_image . '_upload', $validators);
154
    if (isset($file)) {
155
      // File upload was attempted.
156
      if ($file) {
157
        // Put the temporary file in form_values so we can save it on submit.
158
        $form_state['values'][$which_image . '_upload'] = $file;
159
      }
160
      else {
161
        // File upload failed.
162
        form_set_error($which_image . '_upload', t('The image could not be uploaded.'));
163
      }
164
    }
165
  } // END of loop over image names
166
}
(7-7/8)