boincwork_admin_prefs_presets_form_validate()   F
last analyzed

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 25
nc 8388608
nop 2
dl 0
loc 34
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// $Id$
3
4
/**
5
 * @file
6
 * Administration page callbacks for the boincwork module.
7
 */
8
9
/**
10
 * Allow configuration of general options for preference pages
11
 */
12
function boincwork_admin_prefs_options_form(&$form_state) {
13
  $form = array();
14
  $form['beta'] = array(
15
    '#type' => 'checkbox',
16
    '#title' => t('Allow users to opt in to test beta applications'),
17
    '#default_value' => variable_get('boinc_prefs_options_beta', 0),
18
    '#required' => TRUE
19
  );
20
  $form['submit'] = array(
21
    '#type' => 'submit',
22
    '#value' => t('Submit')
23
  );
24
  // Add the official mechanical things and return
25
  //drupal_prepare_form('boincwork_admin_prefs_upload_form', $form, $form_state);
26
  return $form;
27
}
28
29
/**
30
  * Handle validation of preference general settings form.
31
  */
32
function boincwork_admin_prefs_options_form_validate($form, &$form_state) {
33
}
34
35
/**
36
  * Handle submission of preference general settings form.
37
  */
38
function boincwork_admin_prefs_options_form_submit($form, &$form_state) {
39
  variable_set('boinc_prefs_options_beta', $form_state['values']['beta']);
40
  drupal_set_message('Preference options have been updated.');
41
}
42
43
/**
44
 *
45
 */
46
function boincwork_admin_prefs_upload_form(&$form_state) {
47
  $form = array();
48
  $form['prefs_xml'] = array(
49
    '#type' => 'textarea',
50
    '#title' => t('Project specific preferences XML'),
51
    '#default_value' => variable_get('boinc_project_specific_prefs_config', "<project_specific_preferences>\n\n</project_specific_preferences>"),
52
    '#required' => TRUE
53
  );
54
  $form['submit'] = array(
55
    '#type' => 'submit',
56
    '#value' => t('Submit')
57
  );
58
  // Add the official mechanical things and return
59
  //drupal_prepare_form('boincwork_admin_prefs_upload_form', $form, $form_state);
60
  return $form;
61
}
62
63
/**
64
  * Handle validation of preference upload form.
65
  */
66
function boincwork_admin_prefs_upload_form_validate($form, &$form_state) {
67
68
  $xsd = './' . drupal_get_path('module', 'boincwork') . '/includes/projectprefs.xsd';
69
  libxml_use_internal_errors(true);
70
  $xml = new DomDocument();
71
  $xml->loadXML($form_state['values']['prefs_xml'], LIBXML_NOBLANKS);
72
  if (!$xml->schemaValidate($xsd)) {
73
    $errors = libxml_get_errors();
74
    $lines = explode("\r", $form_state['values']['prefs_xml']);
75
    drupal_set_message("{$errors[0]->message} at line {$errors[0]->line}" .
76
      ': <br/>' . htmlentities($lines[$errors[0]->line - 1]), 'error');
77
    form_set_error('upload', t('XML file failed validation'));
78
  }
79
}
80
81
/**
82
  * Handle submission of preference upload form.
83
  */
84
function boincwork_admin_prefs_upload_form_submit($form, &$form_state) {
85
  variable_set('boinc_project_specific_prefs_config', $form_state['values']['prefs_xml']);
86
  drupal_set_message('The XML has been validated and any changes to
87
      preferences are now in place.');
88
}
89
90
/**
91
 *
92
 */
93
function boincwork_admin_prefs_presets_page($preset = 'standard') {
94
  // Configure preference presets
95
  if (function_exists('jump_quickly')) {
96
    $path = 'admin/boinc/prefs/presets';
97
    $preset_options = array(
98
      "{$path}/standard" => t('Standard'),
99
      "{$path}/maximum" => t('Maximum'),
100
      "{$path}/green" => t('Green'),
101
      "{$path}/minimum" => t('Minimum')
102
    );
103
    variable_set('jump_use_js_presets-Array', 1);
104
    drupal_add_js(drupal_get_path('module', 'jump') . '/jump.js');
105
    $output .= '<div class="simple-form-controls"><div class="form-item venue">';
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output seems to be never defined.
Loading history...
106
    $output .= '<label>Preset:</label>';
107
    $output .= jump_quickly($preset_options, 'presets');
108
    $output .= '</div></div>';
109
  }
110
  $output .= drupal_get_form('boincwork_admin_prefs_presets_form', $preset);
111
112
  $output .= '<div>';
113
  $output .= bts('Usage: \'Save configuration\' will save the above preferences into the drupal database. These will be the preset computing (global) preferences that will fill in the preferences form once a user loads the Account Preferences page.', array(), NULL, 'boinc:admin-boinc-preference-presets');
114
  $output .= '<p>';
115
  $output .= bts('\'Save configuration with disk usage settings from config.xml\' will save the above preferences into the drupal database, but also load disk usage settings from the BOINC project\'s config.xml file. These will overwrite any disk usage settings you have placed above. Caution: A user\'s personal preferences will not change if you load the disk usage settings from config.xml here into the drupal database. They must change their preference settings manually.', array(), NULL, 'boinc:admin-boinc-preference-presets');
116
  $output .= '</div>';
117
  return $output;
118
}
119
120
/**
121
 *
122
 */
123
function boincwork_admin_prefs_presets_form(&$form_state, $preset = 'standard') {
124
125
  // Check database for preset prefs
126
  if (!variable_get('boincwork_preset_prefs', null))
127
    drupal_set_message(bts('No presets found in database, loading a default set of preferences for each preset. These will be saved to the database once you click \'Save configuration\'.', array(), NULL, 'boinc:admin-boinc-preference-presets'), 'status');
128
129
  // Load a copy of the general prefs form
130
  $form = boincwork_generalprefs_form($form_state, NULL, $preset);
131
  if ($key = array_search('boincwork_generalprefs_form_submit', $form['#submit'])) {
132
    unset($form['#submit'][$key]);
133
  }
134
  $form['#submit'][] = 'boincwork_admin_prefs_presets_form_submit';
135
136
  // Just keep the necessary items
137
  $form['processor'] = $form['prefs']['advanced']['processor'];
138
  $form['storage'] = $form['prefs']['advanced']['storage'];
139
  $form['network'] = $form['prefs']['advanced']['network'];
140
  unset($form['prefs']);
141
142
  // Tweak some things so it behaves a little better
143
  $form['#tree'] = TRUE;
144
  $form['processor']['#collapsible'] = TRUE;
145
  $form['storage']['#collapsible'] = TRUE;
146
  $form['network']['#collapsible'] = TRUE;
147
148
  // Add a submit button and a hidden field to pass the preset
149
  $form['submit'] = array(
150
    '#type' => 'submit',
151
    '#value' => t('Save configuration')
152
  );
153
  $form['saveuseconfigxml'] = array (
154
      '#type' => 'submit',
155
      '#value' => t('Save configuration with disk usage settings from config.xml'),
156
      '#validate' => array('boincwork_admin_prefs_preset_saveuseconfigxml'),
157
  );
158
  $form['preset'] = array(
159
    '#type' => 'hidden',
160
    '#value' => $preset
161
  );
162
163
  // Add the official mechanical things and return
164
  drupal_prepare_form('boincwork_admin_prefs_presets_form', $form, $form_state);
165
  return $form;
166
}
167
168
/**
169
  * Validate the preference presets form.
170
  */
171
function boincwork_admin_prefs_presets_form_validate($form, &$form_state) {
172
  require_boinc('util');
173
  $values = $form_state['values'];
174
175
  // Verify all non-boolean user input values and notify form API of failures
176
177
  // Processing preferences
178
  if (!verify_numeric($values['processor']['idle_time_to_run'], 1, 9999)) form_set_error('idle_time_to_run', t('Invalid setting for') . " \"{$form['processor']['idle_time_to_run']['#title']} [x] {$form['processor']['idle_time_to_run']['#field_suffix']}\"");
179
  if (!verify_numeric($values['processor']['suspend_if_no_recent_input'], 0, 9999)) form_set_error('suspend_if_no_recent_input', t('Invalid setting for') . " \"{$form['processor']['suspend_if_no_recent_input']['#title']} [x] {$form['processor']['suspend_if_no_recent_input']['#field_suffix']}\"");
180
  if (!verify_numeric($values['processor']['suspend_cpu_usage'], 0, 100)) form_set_error('suspend_cpu_usage', t('Invalid setting for') . " \"{$form['processor']['suspend_cpu_usage']['#title']} [x] {$form['processor']['suspend_cpu_usage']['#field_suffix']}\"");
181
  if (!verify_numeric($values['processor']['start_hour'], 0, 23)) form_set_error('start_hour', t('Invalid setting for') . " \"{$form['processor']['start_hour']['#title']} [x] {$form['processor']['start_hour']['#field_suffix']}\"");
182
  if (!verify_numeric($values['processor']['end_hour'], 0, 23)) form_set_error('end_hour', t('Invalid setting for') . " \"{$form['processor']['end_hour']['#title']} [x] {$form['processor']['end_hour']['#field_suffix']}\"");
183
  if (!verify_numeric($values['processor']['cpu_scheduling_period_minutes'], 1, 9999)) form_set_error('cpu_scheduling_period_minutes', t('Invalid setting for') . " \"{$form['processor']['cpu_scheduling_period_minutes']['#title']} [x] {$form['processor']['cpu_scheduling_period_minutes']['#field_suffix']}\"");
184
  if (!verify_numeric($values['processor']['max_ncpus_pct'], 0, 100)) form_set_error('max_ncpus_pct', t('Invalid setting for') . " \"{$form['processor']['max_ncpus_pct']['#title']} [x] {$form['processor']['max_ncpus_pct']['#field_suffix']}\"");
185
  if (!verify_numeric($values['processor']['cpu_usage_limit'], 0, 100)) form_set_error('cpu_usage_limit', t('Invalid setting for') . " \"{$form['processor']['cpu_usage_limit']['#title']} [x] {$form['processor']['cpu_usage_limit']['#field_suffix']}\"");
186
187
  // Storage preferences
188
  if (!verify_numeric($values['storage']['disk_max_used_gb'], 0, 9999999)) form_set_error('disk_max_used_gb', t('Invalid setting for') . " \"{$form['storage']['disk_max_used_gb']['#title']} [x] {$form['storage']['disk_max_used_gb']['#field_suffix']}\"");
189
  if (!verify_numeric($values['storage']['disk_min_free_gb'], 0.001, 9999999)) form_set_error('disk_min_free_gb', t('Invalid setting for') . " \"{$form['storage']['disk_min_free_gb']['#title']} [x] {$form['storage']['disk_min_free_gb']['#field_suffix']}\"");
190
  if (!verify_numeric($values['storage']['disk_max_used_pct'], 0, 100)) form_set_error('disk_max_used_pct', t('Invalid setting for') . " \"{$form['storage']['disk_max_used_pct']['#title']} [x] {$form['storage']['disk_max_used_pct']['#field_suffix']}\"");
191
  if (!verify_numeric($values['storage']['disk_interval'], 0, 9999999)) form_set_error('disk_interval', t('Invalid setting for') . " \"{$form['storage']['disk_interval']['#title']} [x] {$form['storage']['disk_interval']['#field_suffix']}\"");
192
  if (!verify_numeric($values['storage']['vm_max_used_pct'], 0, 100)) form_set_error('vm_max_used_pct', t('Invalid setting for') . " \"{$form['storage']['vm_max_used_pct']['#title']} [x] {$form['storage']['vm_max_used_pct']['#field_suffix']}\"");
193
  if (!verify_numeric($values['storage']['ram_max_used_busy_pct'], 0, 100)) form_set_error('ram_max_used_busy_pct', t('Invalid setting for') . " \"{$form['storage']['ram_max_used_busy_pct']['#title']} [x] {$form['storage']['ram_max_used_busy_pct']['#field_suffix']}\"");
194
  if (!verify_numeric($values['storage']['ram_max_used_idle_pct'], 0, 100)) form_set_error('ram_max_used_idle_pct', t('Invalid setting for') . " \"{$form['storage']['ram_max_used_idle_pct']['#title']} [x] {$form['storage']['ram_max_used_idle_pct']['#field_suffix']}\"");
195
196
  // Network preferences
197
  if (!verify_numeric($values['network']['work_buf_min_days'], 0, 10)) form_set_error('work_buf_min_days', t('Invalid setting for') . " \"{$form['network']['work_buf_min_days']['#title']} [x] {$form['network']['work_buf_min_days']['#field_suffix']}\"");
198
  if (!verify_numeric($values['network']['work_buf_additional_days'], 0, 10)) form_set_error('work_buf_additional_days', t('Invalid setting for') . " \"{$form['network']['work_buf_additional_days']['#title']} [x] {$form['network']['work_buf_additional_days']['#field_suffix']}\"");
199
  if (!verify_numeric($values['network']['max_bytes_sec_down'], 0, 9999.999)) form_set_error('max_bytes_sec_down', t('Invalid setting for') . " \"{$form['network']['max_bytes_sec_down']['#title']} [x] {$form['network']['max_bytes_sec_down']['#field_suffix']}\"");
200
  if (!verify_numeric($values['network']['max_bytes_sec_up'], 0, 9999.999)) form_set_error('max_bytes_sec_up', t('Invalid setting for') . " \"{$form['network']['max_bytes_sec_up']['#title']} [x] {$form['network']['max_bytes_sec_up']['#field_suffix']}\"");
201
  if (!verify_numeric($values['network']['net_start_hour'], 0, 23)) form_set_error('net_start_hour', t('Invalid setting for') . " \"{$form['network']['net_start_hour']['#title']} [x] {$form['network']['net_start_hour']['#field_suffix']}\"");
202
  if (!verify_numeric($values['network']['net_end_hour'], 0, 23)) form_set_error('net_end_hour', t('Invalid setting for') . " \"{$form['network']['net_end_hour']['#title']} [x] {$form['network']['net_end_hour']['#field_suffix']}\"");
203
  if (!verify_numeric($values['network']['daily_xfer_limit_mb'], 0, 9999999)) form_set_error('daily_xfer_limit_mb', t('Invalid setting for') . " \"{$form['network']['daily_xfer_limit_mb']['#title']} [x] {$form['network']['daily_xfer_limit_mb']['#field_suffix']}\"");
204
  if (!verify_numeric($values['network']['daily_xfer_period_days'], 0, 9999999)) form_set_error('daily_xfer_period_days', t('Invalid setting for') . " \"{$form['network']['daily_xfer_limit_mb']['#title']} [x] {$form['network']['daily_xfer_limit_mb']['#field_suffix']}\"");
205
}
206
207
/**
208
  * Handle post-validation submission of preference presets form.
209
  */
210
function boincwork_admin_prefs_presets_form_submit($form, &$form_state) {
211
  $values = $form_state['values'];
212
  $preset = $form_state['values']['preset'];
213
214
  // Load baseline settings from configuration
215
  $prefs = boincwork_get_preset_prefs($preset);
216
217
  // Processing preferences
218
  $prefs['run_on_batteries'] = ($values['processor']['run_on_batteries']) ? 0 : 1;
219
  $prefs['run_if_user_active'] = ($values['processor']['run_if_user_active']) ? 0 : 1;
220
  $prefs['run_gpu_if_user_active'] = ($values['processor']['run_gpu_if_user_active']) ? 0: 1;
221
  $prefs['idle_time_to_run'] = $values['processor']['idle_time_to_run'];
222
  $prefs['suspend_if_no_recent_input'] = $values['processor']['suspend_if_no_recent_input'];
223
  $prefs['suspend_cpu_usage'] = $values['processor']['suspend_cpu_usage'];
224
  $prefs['start_hour'] = $values['processor']['start_hour'];
225
  $prefs['end_hour'] = $values['processor']['end_hour'];
226
  $prefs['leave_apps_in_memory'] = ($values['processor']['leave_apps_in_memory']) ? 1 : 0;
227
  $prefs['cpu_scheduling_period_minutes'] = $values['processor']['cpu_scheduling_period_minutes'];
228
  $prefs['max_ncpus_pct'] = $values['processor']['max_ncpus_pct'];
229
  $prefs['cpu_usage_limit'] = $values['processor']['cpu_usage_limit'];
230
231
  // Storage preferences
232
  $prefs['disk_max_used_gb'] = $values['storage']['disk_max_used_gb'];
233
  $prefs['disk_min_free_gb'] = $values['storage']['disk_min_free_gb'];
234
  $prefs['disk_max_used_pct'] = $values['storage']['disk_max_used_pct'];
235
  $prefs['disk_interval'] = $values['storage']['disk_interval'];
236
  $prefs['vm_max_used_pct'] = $values['storage']['vm_max_used_pct'];
237
  $prefs['ram_max_used_busy_pct'] = $values['storage']['ram_max_used_busy_pct'];
238
  $prefs['ram_max_used_idle_pct'] = $values['storage']['ram_max_used_idle_pct'];
239
240
  // Network preferences
241
  $prefs['work_buf_min_days'] = $values['network']['work_buf_min_days'];
242
  $prefs['work_buf_additional_days'] = $values['network']['work_buf_additional_days'];
243
  $prefs['confirm_before_connecting'] = ($values['network']['confirm_before_connecting']) ? 1 : 0;
244
  $prefs['hangup_if_dialed'] = ($values['network']['hangup_if_dialed']) ? 1 : 0;
245
  $prefs['max_bytes_sec_down'] = $values['network']['max_bytes_sec_down']*1000;
246
  $prefs['max_bytes_sec_up'] = $values['network']['max_bytes_sec_up']*1000;
247
  $prefs['net_start_hour'] = $values['network']['net_start_hour'];
248
  $prefs['net_end_hour'] = $values['network']['net_end_hour'];
249
  $prefs['daily_xfer_limit_mb'] = $values['network']['daily_xfer_limit_mb'];
250
  $prefs['daily_xfer_period_days'] = $values['network']['daily_xfer_period_days'];
251
  $prefs['dont_verify_images'] = ($values['network']['dont_verify_images']) ? 1 : 0;
252
253
  //Remove @attributes to match new format (see boincwork.forms.inc
254
  //function boincwork_generalprefs_form)
255
  unset($prefs['@attributes']['preset']);
256
257
  // Update the configuration
258
  boincwork_save_preset_prefs($prefs, $preset);
259
  drupal_set_message(t('The "@name" preset has been updated.',
260
    array('@name' => $preset)));
261
}
262
263
/**
264
 * Save preset preferences configuration
265
 */
266
function boincwork_save_preset_prefs($updated_prefs, $preset = 'standard') {
267
268
  // Get the full configuration
269
  $all_presets = boincwork_get_preset_prefs();
270
  $all_presets = (array) $all_presets['general_preferences'];
271
272
  // Check for sane config
273
  if (isset($all_presets['preset'])) {
274
    if (!is_numeric(key($all_presets['preset']))) {
275
      $all_presets['preset'] = array($all_presets['preset']);
276
    }
277
    foreach ($all_presets['preset'] as $key => $old_preset) {
278
      // Find the preset being updated and... update it
279
      if (isset($old_preset['@attributes']['name']) AND $old_preset['@attributes']['name'] == $preset) {
280
        $all_presets['preset'][$key] = $updated_prefs + $old_preset;
281
      }
282
    }
283
  }
284
285
  // Convert prefs back to XML and save to database
286
  $all_presets = array('general_preferences' => $all_presets);
287
  variable_set('boincwork_preset_prefs', save_configuration($all_presets));
288
}
289
290
/**
291
 * Saves but also loads disk usage settings from config.xml, and
292
 * overwrites whatever values are is in form.
293
 */
294
function boincwork_admin_prefs_preset_saveuseconfigxml($form, &$form_state) {
295
    require_boinc(array('db', 'prefs'));
296
    $disk_space_config = get_disk_space_config();
297
298
    $form_state['values']['storage']['disk_max_used_gb']  = $disk_space_config->disk_max_used_gb;
299
    $form_state['values']['storage']['disk_min_free_gb']  = $disk_space_config->disk_min_free_gb;
300
    $form_state['values']['storage']['disk_max_used_pct'] = $disk_space_config->disk_max_used_pct;
301
302
    //Run the form validate function
303
    boincwork_admin_prefs_presets_form_validate($form, $form_state);
304
}
305