| 1 |
<?php
|
| 2 |
// $Id: swfupload.info, v 0.1, 2008/10/13 10:02:46, skilip, jojomaniac Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
*
|
| 7 |
* The swfupload settings form.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
*
|
| 12 |
*/
|
| 13 |
function swfupload_settings_form() {
|
| 14 |
|
| 15 |
// Add js and css
|
| 16 |
$path = drupal_get_path('module', 'swfupload');
|
| 17 |
drupal_add_css($path.'/settings/settings.css');
|
| 18 |
drupal_add_js($path.'/settings/settings.js');
|
| 19 |
|
| 20 |
// Load all roles
|
| 21 |
$roles = user_roles();
|
| 22 |
$node_types = node_get_types();
|
| 23 |
|
| 24 |
// The first loop we make goes trough all the node types
|
| 25 |
foreach($node_types as $node_type) {
|
| 26 |
|
| 27 |
// Get the default values
|
| 28 |
$settings = variable_get('swfupload_setting_'.$node_type->type, array());
|
| 29 |
|
| 30 |
// Now we are in the loop of every node type, we can loop every role :-)
|
| 31 |
foreach($roles as $key => $role) {
|
| 32 |
$roles[$key] = $role;
|
| 33 |
|
| 34 |
// Set a default value if array is empty
|
| 35 |
if(empty($settings[$role])) {
|
| 36 |
$settings[$role]->filepath = '%file_directory_path';
|
| 37 |
$settings[$role]->max_img_resolution = '800x600';
|
| 38 |
$settings[$role]->list = 1;
|
| 39 |
$settings[$role]->file_extensions = 'jpg jpeg gif png txt';
|
| 40 |
$settings[$role]->max_file_size = 1;
|
| 41 |
$settings[$role]->node_max_file_size = 0;
|
| 42 |
$settings[$role]->max_files = 1;
|
| 43 |
$settings[$role]->upload_usersize = 10;
|
| 44 |
|
| 45 |
if ($role == t('authenticated user')) {
|
| 46 |
$settings[$role]->upload_usersize = round((file_upload_max_size() / (1024 * 1024)));
|
| 47 |
}
|
| 48 |
}
|
| 49 |
|
| 50 |
// Create the prefix for each role
|
| 51 |
$form_prefix = $node_type->type .'_'. str_replace(' ', '_', $role);
|
| 52 |
|
| 53 |
// Path settings
|
| 54 |
$form[$form_prefix .'_filepath'] = array(
|
| 55 |
'#type' => 'textfield',
|
| 56 |
'#title' => t('Path settings'),
|
| 57 |
'#default_value' => $settings[$role]->filepath,
|
| 58 |
'#maxlength' => 255,
|
| 59 |
);
|
| 60 |
// Resolution
|
| 61 |
$form[$form_prefix .'_max_img_resolution'] = array(
|
| 62 |
'#type' => 'textfield',
|
| 63 |
'#title' => t('Maximum resolution for uploaded images'),
|
| 64 |
'#default_value' => $settings[$role]->max_img_resolution,
|
| 65 |
'#size' => 15,
|
| 66 |
'#maxlength' => 10,
|
| 67 |
'#field_suffix' => '<kbd>'. t('WIDTHxHEIGHT') .'</kbd>'
|
| 68 |
);
|
| 69 |
// Display attachments
|
| 70 |
$form[$form_prefix .'_list'] = array(
|
| 71 |
'#type' => 'checkbox',
|
| 72 |
'#title' => t('List files by default'),
|
| 73 |
'#default_value' => $settings[$role]->list,
|
| 74 |
);
|
| 75 |
// Total files per node
|
| 76 |
$form[$form_prefix .'_max_files'] = array(
|
| 77 |
'#type' => 'select',
|
| 78 |
'#title' => t('Maximum files per node'),
|
| 79 |
'#default_value' => $settings[$role]->max_files,
|
| 80 |
'#options' => array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 10 => 10, 20 => 20, 50 => 50),
|
| 81 |
);
|
| 82 |
// Allowed extentions
|
| 83 |
$form[$form_prefix .'_file_extensions'] = array(
|
| 84 |
'#type' => 'textfield',
|
| 85 |
'#title' => t('Permitted file extensions'),
|
| 86 |
'#default_value' => $settings[$role]->file_extensions,
|
| 87 |
'#maxlength' => 255,
|
| 88 |
);
|
| 89 |
$form[$form_prefix .'_max_file_size'] = array(
|
| 90 |
'#type' => 'textfield',
|
| 91 |
'#title' => t('Maximum file size per upload'),
|
| 92 |
'#default_value' => $settings[$role]->max_file_size,
|
| 93 |
'#size' => 5,
|
| 94 |
'#maxlength' => 5,
|
| 95 |
'#field_suffix' => t('MB'),
|
| 96 |
);
|
| 97 |
$form[$form_prefix .'_node_max_file_size'] = array(
|
| 98 |
'#type' => 'textfield',
|
| 99 |
'#title' => t('Total file size per node'),
|
| 100 |
'#default_value' => $settings[$role]->node_max_file_size,
|
| 101 |
'#size' => 5,
|
| 102 |
'#maxlength' => 5,
|
| 103 |
'#field_suffix' => t('MB'),
|
| 104 |
);
|
| 105 |
|
| 106 |
$form[$form_prefix .'_upload_usersize'] = array(
|
| 107 |
'#type' => 'textfield',
|
| 108 |
'#title' => t('Total file size per user'),
|
| 109 |
'#default_value' => $settings[$role]->upload_usersize,
|
| 110 |
'#size' => 5,
|
| 111 |
'#maxlength' => 5,
|
| 112 |
'#field_suffix' => t('MB'),
|
| 113 |
);
|
| 114 |
}
|
| 115 |
}
|
| 116 |
$form['node_types'] = array('#type' => 'value', '#value' => $node_types);
|
| 117 |
$form['roles'] = array('#type' => 'value', '#value' => $roles);
|
| 118 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
| 119 |
|
| 120 |
return $form;
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Theme function for the file settings form
|
| 125 |
*/
|
| 126 |
function theme_swfupload_settings_form($form) {
|
| 127 |
// Create an array for all fields we want to render.
|
| 128 |
$fields = array('filepath', 'max_img_resolution', 'list', 'max_files', 'file_extensions' ,'max_file_size', 'node_max_file_size' ,'upload_usersize');
|
| 129 |
|
| 130 |
// Loop trough all node types
|
| 131 |
foreach ($form['node_types']['#value'] as $node_type) {
|
| 132 |
// Reset the output array
|
| 133 |
$output = array('<div class="swf-nodetype-wrapper">');
|
| 134 |
|
| 135 |
// Loop trough all roles
|
| 136 |
foreach ($form['roles']['#value'] as $role) {
|
| 137 |
// Reset the rows array
|
| 138 |
$rows = array();
|
| 139 |
|
| 140 |
// Create the prefix for each role
|
| 141 |
$form_prefix = $node_type->type .'_'. str_replace(' ', '_', $role);
|
| 142 |
|
| 143 |
// Loop trough the fields we want to render
|
| 144 |
foreach ($fields as $field) {
|
| 145 |
$title = $form[$form_prefix ."_". $field]['#title'];
|
| 146 |
unset($form[$form_prefix ."_". $field]['#title']);
|
| 147 |
$rows[] = array(
|
| 148 |
array(
|
| 149 |
'data' => $title,
|
| 150 |
'class' => 'left'
|
| 151 |
),
|
| 152 |
drupal_render($form[$form_prefix ."_". $field])
|
| 153 |
);
|
| 154 |
}
|
| 155 |
$output[] = theme('table', array(array('data' => $role, 'colspan' => 2, 'class' => 'left2')), $rows);
|
| 156 |
}
|
| 157 |
$output[] = '</div><div class="swf-harmonica-arrow"></div>';
|
| 158 |
$lis[] = "<label><span>$node_type->name</span></label>".join("\n", $output);
|
| 159 |
}
|
| 160 |
return theme('item_list', $lis).drupal_render($form) . '<div style="clear:both"></div>';
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* validation of the file settings form
|
| 165 |
*/
|
| 166 |
function swfupload_settings_form_validate($form, &$form_state) {
|
| 167 |
|
| 168 |
// Loop trough all node types
|
| 169 |
foreach($form['node_types']['#value'] as $node_type) {
|
| 170 |
|
| 171 |
// Loop trough all the roles
|
| 172 |
foreach($form['roles']['#value'] as $role) {
|
| 173 |
|
| 174 |
// Create the prefix for each role
|
| 175 |
$form_prefix = $node_type->type .'_'. str_replace(' ', '_', $role);
|
| 176 |
|
| 177 |
// Max resolution
|
| 178 |
if($form_state['values'][$form_prefix .'_max_img_resolution'] != 0) {
|
| 179 |
$max_resolution = explode('x', $form_state['values'][$form_prefix .'_max_img_resolution']);
|
| 180 |
if(count($max_resolution) != 2) {
|
| 181 |
form_set_error($form_prefix .'_max_img_resolution', t('Maximum resolution for uploaded images is not valid'));
|
| 182 |
} else {
|
| 183 |
if(!is_numeric($max_resolution[0]) or !is_numeric($max_resolution[1])) {
|
| 184 |
form_set_error($form_prefix .'_max_img_resolution', t('Maximum resolution for uploaded images is not valid'));
|
| 185 |
}
|
| 186 |
}
|
| 187 |
}
|
| 188 |
|
| 189 |
// Permited extentions
|
| 190 |
$extensions = explode(' ', $form_state['values'][$form_prefix .'_file_extensions']);
|
| 191 |
foreach($extensions as $extention) {
|
| 192 |
if(!preg_match("/^([A-Z0-9]){2,10}$/i", $extention)) {
|
| 193 |
form_set_error($form_prefix .'_file_extensions', t('One of more extentions are not valid'));
|
| 194 |
break;
|
| 195 |
}
|
| 196 |
}
|
| 197 |
|
| 198 |
// Maximum file size
|
| 199 |
if(!is_numeric($form_state['values'][$form_prefix .'_max_file_size'])) {
|
| 200 |
form_set_error($form_prefix .'_max_file_size', t('Maximum file size per upload is not valid'));
|
| 201 |
}
|
| 202 |
|
| 203 |
// Total node size
|
| 204 |
if(!is_numeric($form_state['values'][$form_prefix .'_node_max_file_size'])) {
|
| 205 |
form_set_error($form_prefix .'_node_max_file_size', t('Total file size per node is not valid'));
|
| 206 |
}
|
| 207 |
|
| 208 |
// Total user size
|
| 209 |
if(!is_numeric($form_state['values'][$form_prefix .'_upload_usersize'])) {
|
| 210 |
form_set_error($form_prefix .'_upload_usersize', t('Total file size per user is not valid'));
|
| 211 |
}
|
| 212 |
}
|
| 213 |
}
|
| 214 |
}
|
| 215 |
|
| 216 |
/**
|
| 217 |
* Submittion of the file settings form
|
| 218 |
*/
|
| 219 |
function swfupload_settings_form_submit($form, &$form_state) {
|
| 220 |
|
| 221 |
// Loop trough all node types
|
| 222 |
foreach($form['node_types']['#value'] as $node_type) {
|
| 223 |
|
| 224 |
// Loop trough all the roles
|
| 225 |
foreach($form['roles']['#value'] as $role) {
|
| 226 |
|
| 227 |
// Create the prefix for each role
|
| 228 |
$form_prefix = $node_type->type .'_'. str_replace(' ', '_', $role);
|
| 229 |
|
| 230 |
$settings[$role]->filepath = $form_state['values'][$form_prefix .'_filepath'];
|
| 231 |
$settings[$role]->max_img_resolution = $form_state['values'][$form_prefix .'_max_img_resolution'];
|
| 232 |
$settings[$role]->list = $form_state['values'][$form_prefix .'_list'];
|
| 233 |
$settings[$role]->file_extensions = strtolower($form_state['values'][$form_prefix .'_file_extensions']);
|
| 234 |
$settings[$role]->max_file_size = $form_state['values'][$form_prefix .'_max_file_size'];
|
| 235 |
$settings[$role]->upload_usersize = $form_state['values'][$form_prefix .'_upload_usersize'];
|
| 236 |
$settings[$role]->node_max_file_size = $form_state['values'][$form_prefix .'_node_max_file_size'];
|
| 237 |
$settings[$role]->max_files = $form_state['values'][$form_prefix .'_max_files'];
|
| 238 |
}
|
| 239 |
variable_set('swfupload_setting_'.$node_type->type, $settings);
|
| 240 |
}
|
| 241 |
drupal_set_message(t('The configuration options have been saved.'));
|
| 242 |
}
|