webform.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * @file
  4. * Administration pages provided by Webform module.
  5. */
  6. /**
  7. * Menu callback for admin/config/content/webform.
  8. */
  9. function webform_admin_settings() {
  10. module_load_include('inc', 'webform', 'includes/webform.export');
  11. $node_types = node_type_get_names();
  12. $form['node_types'] = array(
  13. '#type' => 'checkboxes',
  14. '#title' => t('Webform-enabled content types'),
  15. '#description' => t('Webform allows you to enable the webform components for any content type. Choose the types on which you would like to associate webform components.'),
  16. '#options' => $node_types,
  17. '#default_value' => webform_variable_get('webform_node_types'),
  18. );
  19. $form['components'] = array(
  20. '#type' => 'fieldset',
  21. '#title' => t('Available components'),
  22. '#collapsible' => TRUE,
  23. '#collapsed' => FALSE,
  24. '#description' => t('These are the available field types for your installation of Webform. You may disable any of these components by unchecking its corresponding box. Only checked components will be available in existing or new webforms.'),
  25. );
  26. // Add each component to the form:
  27. $form['components'] = array('#tree' => TRUE);
  28. $component_types = webform_components(TRUE);
  29. foreach ($component_types as $key => $component) {
  30. $form['components'][$key] = array(
  31. '#title' => $component['label'],
  32. '#description' => $component['description'],
  33. '#type' => 'checkbox',
  34. '#return_value' => 1,
  35. '#default_value' => $component['enabled'],
  36. );
  37. }
  38. $form['email'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Default e-mail values'),
  41. '#collapsible' => TRUE,
  42. '#collapsed' => FALSE,
  43. );
  44. $form['email']['webform_default_from_address'] = array(
  45. '#type' => 'textfield',
  46. '#title' => t('From address'),
  47. '#default_value' => variable_get('webform_default_from_address', variable_get('site_mail', ini_get('sendmail_from'))),
  48. '#description' => t('The default sender address for emailed webform results; often the e-mail address of the maintainer of your forms.'),
  49. );
  50. $form['email']['webform_default_from_name'] = array(
  51. '#type' => 'textfield',
  52. '#title' => t('From name'),
  53. '#default_value' => variable_get('webform_default_from_name', variable_get('site_name', '')),
  54. '#description' => t('The default sender name which is used along with the default from address.'),
  55. );
  56. $form['email']['webform_default_subject'] = array(
  57. '#type' => 'textfield',
  58. '#title' => t('Default subject'),
  59. '#default_value' => variable_get('webform_default_subject', t('Form submission from: %title')),
  60. '#description' => t('The default subject line of any e-mailed results.'),
  61. );
  62. $form['email']['webform_default_format'] = array(
  63. '#type' => 'radios',
  64. '#title' => t('Format'),
  65. '#options' => array(
  66. 0 => t('Plain text'),
  67. 1 => t('HTML'),
  68. ),
  69. '#default_value' => variable_get('webform_default_format', 0),
  70. '#description' => t('The default format for new e-mail settings. Webform e-mail options take precedence over the settings for system-wide e-mails configured in MIME mail.'),
  71. '#access' => webform_email_html_capable(),
  72. );
  73. $form['email']['webform_format_override'] = array(
  74. '#type' => 'radios',
  75. '#title' => t('Format override'),
  76. '#options' => array(
  77. 0 => t('Per-webform configuration of e-mail format'),
  78. 1 => t('Send all e-mails in the default format'),
  79. ),
  80. '#default_value' => variable_get('webform_format_override', 0),
  81. '#description' => t('Force all webform e-mails to be sent in the default format.'),
  82. '#access' => webform_email_html_capable(),
  83. );
  84. $form['advanced'] = array(
  85. '#type' => 'fieldset',
  86. '#title' => t('Advanced options'),
  87. '#collapsible' => TRUE,
  88. '#collapsed' => TRUE,
  89. );
  90. $form['advanced']['webform_use_cookies'] = array(
  91. '#type' => 'checkbox',
  92. '#checked_value' => 1,
  93. '#title' => t('Allow cookies for tracking submissions'),
  94. '#default_value' => variable_get('webform_use_cookies', 0),
  95. '#description' => t('<a href="http://www.wikipedia.org/wiki/HTTP_cookie">Cookies</a> can be used to help prevent the same user from repeatedly submitting a webform. This feature is not needed for limiting submissions per user, though it can increase accuracy in some situations. Besides cookies, Webform also uses IP addresses and site usernames to prevent repeated submissions.'),
  96. );
  97. $form['advanced']['webform_search_index'] = array(
  98. '#type' => 'checkbox',
  99. '#checked_value' => 1,
  100. '#title' => t('Include webform forms in search index'),
  101. '#default_value' => variable_get('webform_search_index', 1),
  102. '#description' => t('When selected, all Webform nodes will have their form components indexed by the search engine.'),
  103. '#access' => module_exists('search'),
  104. );
  105. $form['advanced']['webform_email_address_format'] = array(
  106. '#type' => 'radios',
  107. '#title' => t('E-mail address format'),
  108. '#options' => array(
  109. 'long' => t('Long format: "Example Name" &lt;name@example.com&gt;'),
  110. 'short' => t('Short format: name@example.com'),
  111. ),
  112. '#default_value' => variable_get('webform_email_address_format', 'long'),
  113. '#description' => t('Most servers support the "long" format which will allow for more friendly From addresses in e-mails sent. However many Windows-based servers are unable to send in the long format. Change this option if experiencing problems sending e-mails with Webform.'),
  114. );
  115. $form['advanced']['webform_export_format'] = array(
  116. '#type' => 'radios',
  117. '#title' => t('Default export format'),
  118. '#options' => webform_export_list(),
  119. '#default_value' => variable_get('webform_export_format', 'delimited'),
  120. );
  121. $form['advanced']['webform_csv_delimiter'] = array(
  122. '#type' => 'select',
  123. '#title' => t('Default export delimiter'),
  124. '#description' => t('This is the delimiter used in the CSV/TSV file when downloading Webform results. Using tabs in the export is the most reliable method for preserving non-latin characters. You may want to change this to another character depending on the program with which you anticipate importing results.'),
  125. '#default_value' => variable_get('webform_csv_delimiter', '\t'),
  126. '#options' => array(
  127. ',' => t('Comma (,)'),
  128. '\t' => t('Tab (\t)'),
  129. ';' => t('Semicolon (;)'),
  130. ':' => t('Colon (:)'),
  131. '|' => t('Pipe (|)'),
  132. '.' => t('Period (.)'),
  133. ' ' => t('Space ( )'),
  134. ),
  135. );
  136. $form['advanced']['webform_submission_access_control'] = array(
  137. '#type' => 'radios',
  138. '#title' => t('Submission access control'),
  139. '#options' => array(
  140. '1' => t('Select the user roles that may submit each individual webform'),
  141. '0' => t('Disable Webform submission access control'),
  142. ),
  143. '#default_value' => variable_get('webform_submission_access_control', 1),
  144. '#description' => t('By default, the configuration form for each webform allows the administrator to choose which roles may submit the form. You may want to allow users to always submit the form if you are using a separate node access module to control access to webform nodes themselves.'),
  145. );
  146. $form = system_settings_form($form);
  147. $form['#theme'] = 'webform_admin_settings';
  148. array_unshift($form['#submit'], 'webform_admin_settings_submit');
  149. return $form;
  150. }
  151. /**
  152. * Submit handler for the webform_admin_settings() form.
  153. */
  154. function webform_admin_settings_submit($form, &$form_state) {
  155. $disabled_components = array();
  156. foreach ($form_state['values']['components'] as $name => $enabled) {
  157. if (!$enabled) {
  158. $disabled_components[] = $name;
  159. }
  160. }
  161. // Update $form_state and let system_settings_form_submit() handle saving.
  162. $form_state['values']['webform_disabled_components'] = $disabled_components;
  163. unset($form_state['values']['components']);
  164. // Change the name of the node type variable and clean it up.
  165. $form_state['values']['webform_node_types'] = array_keys(array_filter($form_state['values']['node_types']));
  166. unset($form_state['values']['node_types']);
  167. }
  168. /**
  169. * Theme the output of the webform_admin_settings() form.
  170. */
  171. function theme_webform_admin_settings($variables) {
  172. $form = $variables['form'];
  173. // Format the components into a table.
  174. foreach (element_children($form['components']) as $key) {
  175. $row = array();
  176. $row[] = $form['components'][$key]['#title'];
  177. $row[] = $form['components'][$key]['#description'];
  178. $form['components'][$key]['#title'] = NULL;
  179. $form['components'][$key]['#description'] = NULL;
  180. $row[] = array('data' => drupal_render($form['components'][$key]), 'align' => 'center');
  181. $rows[] = $row;
  182. }
  183. $header = array(t('Name'), t('Description'), array('data' => t('Enabled'), 'class' => array('checkbox')));
  184. // Create the table inside the form.
  185. $form['components']['table'] = array(
  186. '#theme' => 'table',
  187. '#header' => $header,
  188. '#rows' => $rows,
  189. );
  190. return drupal_render_children($form);
  191. }
  192. /**
  193. * Menu callback for admin/content/webform. Displays all webforms on the site.
  194. */
  195. function webform_admin_content() {
  196. $query = db_select('webform', 'w');
  197. $query->join('node', 'n', 'w.nid = n.nid');
  198. $query->fields('n');
  199. $nodes = $query->execute()->fetchAllAssoc('nid');
  200. return theme('webform_admin_content', array('nodes' => $nodes));
  201. }
  202. /**
  203. * Create a comma-separate list of content types that are webform enabled.
  204. */
  205. function webform_admin_type_list() {
  206. $webform_types = webform_variable_get('webform_node_types');
  207. $webform_type_list = '';
  208. $webform_type_count = count($webform_types);
  209. foreach ($webform_types as $n => $type) {
  210. $webform_type_list .= l(node_type_get_name($type), 'node/add/' . $type);
  211. if ($n + 1 < $webform_type_count) {
  212. $webform_type_list .= $webform_type_count == 2 ? ' ' : ', ';
  213. }
  214. if ($n + 2 == $webform_type_count) {
  215. $webform_type_list .= t('or') . ' ';
  216. }
  217. }
  218. return $webform_type_list;
  219. }
  220. /**
  221. * Generate a list of all webforms avaliable on this site.
  222. */
  223. function theme_webform_admin_content($variables) {
  224. $nodes = $variables['nodes'];
  225. $header = array(
  226. t('Title'),
  227. array('data' => t('View'), 'colspan' => '4'),
  228. array('data' => t('Operations'), 'colspan' => '3')
  229. );
  230. $rows = array();
  231. foreach ($nodes as $node) {
  232. $rows[] = array(
  233. l($node->title, 'node/' . $node->nid),
  234. l(t('Submissions'), 'node/' . $node->nid . '/webform-results'),
  235. l(t('Analysis'), 'node/' . $node->nid . '/webform-results/analysis'),
  236. l(t('Table'), 'node/' . $node->nid . '/webform-results/table'),
  237. l(t('Download'), 'node/' . $node->nid . '/webform-results/download'),
  238. node_access('update', $node) ? l(t('Edit'), 'node/' . $node->nid . '/edit') : '',
  239. node_access('update', $node) ? l(t('Components'), 'node/' . $node->nid . '/webform') : '',
  240. user_access('delete all webform submissions') ? l(t('Clear'), 'node/' . $node->nid . '/webform-results/clear') : '',
  241. );
  242. }
  243. if (empty($rows)) {
  244. $webform_types = webform_variable_get('webform_node_types');
  245. if (empty($webform_types)) {
  246. $message = t('Webform is currently not enabled on any content types.') . ' ' . t('Visit the <a href="!url">Webform settings</a> page and enable Webform on at least one content type.', array('!url' => url('admin/config/content/webform')));
  247. }
  248. else {
  249. $webform_type_list = webform_admin_type_list();
  250. $message = t('There are currently no webforms on your site. Create a !types piece of content.', array('!types' => $webform_type_list));
  251. }
  252. $rows[] = array(
  253. array('data' => $message, 'colspan' => 7),
  254. );
  255. }
  256. return theme('table', array('header' => $header, 'rows' => $rows));
  257. }