Projet

Général

Profil

Paste
Télécharger (4,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / themes / kanji / theme-settings.php @ 4b706e38

1 85ad3d82 Assos Assos
<?php
2
/**
3
 * Implements hook_form_system_theme_settings_alter().
4
 */
5
function kanji_form_system_theme_settings_alter(&$form, &$form_state, $form_id = NULL) {
6
  if (isset($form_id)) {
7
    return;
8
  }
9
10
  // Layout settings
11
  $form['kanji'] = array(
12
13
    '#default_tab' => 'defaults',
14
  );
15
16
  // Breadcrumbs
17
  $form['kanji']['breadcrumb'] = array(
18
    '#type' => 'fieldset',
19
    '#weight' => '5',
20
    '#title' => t('Breadcrumbs'),
21
  );
22
  $form['kanji']['breadcrumb']['bd']['breadcrumb_display'] = array(
23
    '#type' => 'select',
24
    '#title' => t('Show breadcrumbs'),
25
    '#default_value' => theme_get_setting('breadcrumb_display'),
26
    '#options' => array(
27
      'yes' => t('Yes'),
28
      'no' => t('No'),
29
    ),
30
  );
31
32
        $form['background'] = array(
33
    '#type' => 'fieldset',
34
    '#title' => t('Background settings'),
35
  );
36
  
37
  $form['background']['default_background'] = array(
38
    '#type' => 'checkbox',
39
    '#title' => t('Use the default background-image'),
40
    '#default_value' => (theme_get_setting('default_background') == 0 ? theme_get_setting('default_background') : 1),
41
  );
42
  
43
  $form['background']['settings'] = array(
44
    '#type' => 'container',
45
    '#states' => array(
46
      'invisible' => array(
47
        'input[name="default_background"]' => array('checked' => TRUE),
48
      ),
49
    ),
50
  );
51
  
52
  $form['background']['settings']['path_background'] = array(
53
    '#type' => 'textfield',
54
    '#title' => t('Current used background-image'),
55
    '#default_value' => variable_get('kanji:path_background', ''),
56
  );
57
58
  $form['background']['settings']['upload_background'] = array(
59
    '#type' => 'file',
60
    '#title' => t('Upload a background image'),
61
  );
62
63
  $form['background']['settings']['background_color'] = array(
64
    '#type' => 'checkbox',
65
    '#title' => t('Use background color'),
66
    '#description' => t('This will remove the background-image'),
67
    '#default_value' => (theme_get_setting('background_color') == 0 ? theme_get_setting('background_color') : 1),
68
  );
69
  
70
  $form['background']['settings']['background_color_value'] = array(
71
    '#type' => 'textfield',
72
    '#title' => t('Provide the backgroundcolor'),
73
    '#description' => t('Give the value for the background-color, example: FFF, 000'),
74
    '#default_value' => variable_get('kanji:background_color_value', ''),
75
    '#field_prefix' => '#',
76
    '#states' => array(
77
      'invisible' => array(
78
        'input[name="background_color"]' => array('checked' => FALSE),
79
      ),
80
    ),
81
  );
82
  
83
  $form['#validate'][] = 'kanji_theme_settings_validate';
84
  $form['#submit'][] = 'kanji_theme_settings_submit';
85
}
86
87
function kanji_theme_settings_validate($form, &$form_state) {
88
// Handle file uploads.
89
  $validators = array('file_validate_is_image' => array());
90
91
  // Check for a new uploaded logo.
92
  $file = file_save_upload('upload_background', $validators, FALSE, FILE_EXISTS_REPLACE);
93
94
  if (isset($file)) {
95
    // File upload was attempted.
96
    if ($file) {
97
      // Put the temporary file in form_values so we can save it on submit.
98
      $form_state['values']['upload_background'] = $file;
99
    }
100
    else {
101
      // File upload failed.
102
      form_set_error('upload_background', t('The header could not be uploaded.'));
103
    }
104
  }
105
106
  // If the user provided a path for a logo or favicon file, make sure a file
107
  // exists at that path.
108
  if ($form_state['values']['path_background']) {
109
    $path = _system_theme_settings_validate_path($form_state['values']['path_background']);
110
    if (!$path) {
111
      form_set_error('path_background', t('The custom header path is invalid.'));
112
    }
113
  }
114
  
115
  if ($form_state['values']['background_color'] == true && !empty($form_state['values']['background_color_value'])) {
116
    if (!preg_match("/^[A-Za-z0-9]+$/", $form_state['values']['background_color_value'])) {
117
      form_set_error('background_color_value', t('The color must be a HEX value, example ff0000'));
118
    }
119
  }
120
}
121
122
function kanji_theme_settings_submit($form, &$form_state) {
123
  $values = $form_state['values'];
124
  
125
  // If the user uploaded a new header image, save it to a permanent location
126
  if ($file = $values['upload_background']) {
127
    unset($values['upload_background']);
128
    $filename = file_unmanaged_copy($file->uri, NULL, FILE_EXISTS_REPLACE);
129
    $values['path_background'] = $filename;
130
  }
131
132
  // If the user entered a path relative to the system files directory for
133
  // a header image, store a public:// URI so the theme system can handle it.
134
  if (!empty($values['path_background'])) {
135
    $values['path_background'] = _system_theme_settings_validate_path($values['path_background']);
136
    variable_set('kanji:path_background', $values['path_background']);
137
  }
138
  
139
  if (!empty($values['background_color_value'])) {
140
    variable_set('kanji:background_color_value', $values['background_color_value']);
141
  }
142
  
143
  //Delete background settings when the default background is used
144
  if ($values['default_background'] == 1) {
145
          variable_del('kanji:path_background');
146
    variable_del('kanji:background_color_value');
147
  }
148
}