settings.inc 8.7 KB

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