Project

General

Profile

« Previous | Next » 

Revision 1dc9554f

Added by Andreas Kohlbecker over 11 years ago

theme customization improved - layout adapts to image sizes

View differences:

7.x/themes/zen_dataportal/theme-settings.php
43 43
  // custom images for banner, body and page backgrounds
44 44
  //
45 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
46
    zen_dataportal_form_widget_image($form, $which_image);
47
  }
96 48

  
97 49

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

  
101

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

  
......
111 61
 */
112 62
function zen_dataportal_theme_settings_submit($form, &$form_state) {
113 63

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

  
116 65
  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
    }
66
    zen_dataportal_form_widget_image_submit($form_state, $which_image);
67
  }
68

  
69
  // update logo information like wich one to use and size, this is needed to
70
  // offset the banner and menu bar
71
  $values = &$form_state['values'];
72
  if(!theme_get_setting('default_logo')) {
73
    $logo_file = path_to_theme() . '/logo.png';
74
  } else {
75
    $logo_file = theme_get_setting('logo_path');
76
  }
77
  $logo_file_info = image_get_info($logo_file);
78
  if(isset($logo_file_info['width'])) {
79
   $values['logo_size'] = array('width' => $logo_file_info['width'], 'height' => $logo_file_info['height']);
80
  }
81

  
129 82

  
130
  } // END of loop over image names
131 83

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

  
138 89
}
139 90

  
140 91
/**
......
146 97
 */
147 98
function zen_dataportal_theme_settings_validate($form, &$form_state) {
148 99

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

  
151 100
  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
      }
101
    zen_dataportal_form_widget_image_validate($form_state, $which_image);
102
  }
103
}
104

  
105
/*******************************************************************
106
 *                    form widget functions
107
 *******************************************************************/
108

  
109
/**
110
 *
111
 * Enter description here ...
112
 *  @param $form
113
 *   Nested array of form elements that comprise the form.
114
 * @param unknown_type $which_image
115
 */
116
function zen_dataportal_form_widget_image(&$form, $which_image){
117
  $image_label = str_replace('_', ' ', $which_image);
118
  $form['zen_dataportal_' .  $which_image] = array(
119
          '#type' => 'fieldset',
120
          '#title' => ucfirst(t($image_label)) . ' ' . t('image settings'),
121
          '#description' => t('If toggled on, the following image will be displayed.'),
122
          '#attributes' => array('class' => array('theme-settings-bottom')),
123
  );
124
  $form['zen_dataportal_' .  $which_image]['default_' . $which_image] = array(
125
          '#type' => 'checkbox',
126
          '#title' => t('Use the default') . t($which_image),
127
          '#default_value' => theme_get_setting('default_' . $which_image),
128
          '#tree' => FALSE,
129
          '#description' => t('Check here if you want the theme to use the image supplied with it.')
130
  );
131
  $form['zen_dataportal_' .  $which_image]['settings'] = array(
132
          '#type' => 'container',
133
          '#states' => array(
134
            // Hide the logo settings when using the default logo.
135
            'invisible' => array(
136
              'input[name="default_' . $which_image . '"]' => array('checked' => TRUE),
137
            ),
138
          ),
139
  );
140

  
141
  // If $path is a public:// URI, display the path relative to the files
142
  // directory; stream wrappers are not end-user friendly.
143
  $path = theme_get_setting($which_image . '_path');
144
  $image = '';
145
  if (file_uri_scheme($path) == 'public') {
146
    $url = file_create_url($path);
147
    $path = file_uri_target($path);
148
    $form['zen_dataportal_' .  $which_image]['settings'][$which_image . '_preview'] = array(
149
          	'#type' => 'item',
150
            '#title' => t('Preview'),
151
            '#markup' => '<div class="image-preview"><img src="' . $url . '"/></div>',
152
    );
153
  } // FIXME manully entered files without scheme public: are omitted here
154
  $form['zen_dataportal_' .  $which_image]['settings'][$which_image . '_path'] = array(
155
          '#type' => 'textfield',
156
          '#title' => t('Path to custom') . ' ' . t($image_label),
157
          '#description' => t('The path to the file you would like to use as your') . ' ' . t($image_label),
158
          '#default_value' => $path,
159
  );
160
  $form['zen_dataportal_' .  $which_image]['settings'][$which_image . '_upload'] = array(
161
          '#type' => 'file',
162
          '#title' => t('Upload') . ' ' . t($image_label) . t(' image'),
163
          '#maxlength' => 40,
164
          '#description' => t("If you don't have direct file access to the server, use this field to upload your image.")
165
  );
166
}
167

  
168
/**
169
 *
170
 * Enter description here ...
171
 * @param $form_state
172
 *   A keyed array containing the current state of the form.
173
 * @param unknown_type $which_image
174
 */
175
function zen_dataportal_form_widget_image_submit(&$form_state, $which_image){
176
  // If the user uploaded a new image, save it to a permanent location
177
  // and use it in place of the default theme-provided file.
178
  $values = &$form_state['values'];
179
  if ($file = $values[$which_image . '_upload']) {
180
    unset($values[$which_image . '_upload']);
181
    $filename = file_unmanaged_copy($file->uri);
182
    $values['default_' .$which_image ] = 0;
183
    $values[$which_image . '_path'] = $filename;
184
    $values['toggle_' . $which_image] = 1;
185
  }
186
  if (!empty($values[$which_image . '_path'])) {
187
    $values[$which_image . '_path'] = _system_theme_settings_validate_path($values[$which_image . '_path']);
188
  }
189
}
190

  
191
/**
192
*
193
* Enter description here ...
194
 * @param $form_state
195
 *   A keyed array containing the current state of the form.
196
* @param unknown_type $which_image
197
*/
198
function zen_dataportal_form_widget_image_validate(&$form_state, $which_image){
199
  $validators = array('file_validate_is_image' => array('gif', 'jpg', 'png'));
200
  // Check for a new uploaded logo.
201
  $file = file_save_upload($which_image . '_upload', $validators);
202
  if (isset($file)) {
203
    // File upload was attempted.
204
    if ($file) {
205
      // Put the temporary file in form_values so we can save it on submit.
206
      $form_state['values'][$which_image . '_upload'] = $file;
207
    }
208
    else {
209
      // File upload failed.
210
      form_set_error($which_image . '_upload', t('The image could not be uploaded.'));
164 211
    }
165
  } // END of loop over image names
212
  }
166 213
}

Also available in: Unified diff