mailsystem.admin.inc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * @file
  4. * Administrative form for setting the mail_system variable.
  5. */
  6. function mailsystem_admin_settings() {
  7. $args = array(
  8. '!interface' => url('http://api.drupal.org/api/drupal/includes--mail.inc/interface/MailSystemInterface/7'),
  9. '@interface' => 'MailSystemInterface',
  10. '!format' => url('http://api.drupal.org/api/drupal/includes--mail.inc/function/MailSystemInterface%3A%3Aformat/7'),
  11. '@format' => 'format()',
  12. '!mail' => url('http://api.drupal.org/api/drupal/includes--mail.inc/function/MailSystemInterface%3A%3Amail/7'),
  13. '@mail' => 'mail()',
  14. '!default_class' => url('http://api.drupal.org/api/drupal/modules--system--system.mail.inc/class/DefaultMailSystem/7'),
  15. '@default_class' => mailsystem_default_value(),
  16. '%module' => 'module',
  17. '%key' => 'key',
  18. );
  19. $form = array('#submit' => array('mailsystem_admin_settings_submit'));
  20. $mail_system = mailsystem_get();
  21. $mail_defaults = mailsystem_defaults();
  22. $mailsystem_classes = mailsystem_get_classes();
  23. $descriptions = array();
  24. foreach (system_rebuild_module_data() as $item) {
  25. if ($item->status) {
  26. $descriptions[$item->name] = (
  27. empty($item->info['package'])
  28. ? '' : $item->info['package']
  29. ) . ' » ' . t('!module module', array('!module' => $item->info['name']));
  30. }
  31. }
  32. asort($descriptions);
  33. $form['mailsystem'] = array(
  34. '#type' => 'fieldset',
  35. '#title' => t('Mail System Settings'),
  36. '#description' => t(
  37. 'Drupal provides a default <a href="!interface"><code>@interface</code></a> class called <a href="!default_class"><code>@default_class</code></a>. Modules may provide additional classes. Each <a href="!interface"><code>@interface</code></a> class may be associated with one or more identifiers, composed of a %module and an optional %key. Each email being sent also has a %module and a %key. To decide which class to use, Drupal uses the following search order: <ol><li>The class associated with the %module and %key, if any.</li><li>The class associated with the %module, if any.</li><li>The site-wide default <a href="!interface"><code>@interface</code></a> class.</li></ol>', $args
  38. ),
  39. '#collapsible' => FALSE,
  40. '#tree' => TRUE,
  41. );
  42. $form['mailsystem'][mailsystem_default_id()] = array(
  43. '#type' => 'select',
  44. '#title' => t(
  45. 'Site-wide default <a href="!interface"><code>@interface</code></a> class', $args
  46. ),
  47. '#options' => $mailsystem_classes,
  48. '#default_value' => $mail_system[mailsystem_default_id()],
  49. );
  50. $mailsystem_classes = array(
  51. mailsystem_default_id() => t('Remove this setting.'),
  52. ) + $mailsystem_classes;
  53. foreach (array_diff_key($mail_system, $mail_defaults) as $id => $class) {
  54. // Separate $id into $module and $key.
  55. $module = $id;
  56. while ($module && empty($descriptions[$module])) {
  57. // Remove a key from the end.
  58. $module = implode('_', explode('_', $module, -1));
  59. }
  60. // If an array key of the $mail_system variable is neither "default-system"
  61. // nor begins with a module name, then it should be unset.
  62. if (empty($module)) {
  63. watchdog('mailsystem', "Removing bogus mail_system key %id.", array('%id' => $id), WATCHDOG_WARNING);
  64. unset($mail_system[$id]);
  65. continue;
  66. }
  67. // Set $title to the human-readable module name.
  68. $title = preg_replace('/^.* » /', '', $descriptions[$module]);
  69. if ($key = substr($id, strlen($module) + 1)) {
  70. $title .= " ($key key)";
  71. }
  72. $title .= ' class';
  73. $form['mailsystem'][$id] = array(
  74. '#type' => 'select',
  75. '#title' => $title,
  76. '#options' => $mailsystem_classes,
  77. '#default_value' => $class,
  78. );
  79. }
  80. // Generate a list of themes which may used to render emails.
  81. $theme_options = array('current' => t('Current'), 'default' => t('Default'));
  82. if (module_exists('domain_theme')) {
  83. $theme_options['domain'] = t('Domain Theme');
  84. }
  85. // Get a list of all themes.
  86. $themes = list_themes();
  87. foreach ($themes as $name => $theme) {
  88. if ($theme->status == 1) {
  89. $theme_options[$name] = $theme->info['name'];
  90. }
  91. }
  92. $form['mailsystem']['mailsystem_theme'] = array(
  93. '#type' => 'select',
  94. '#title' => t('Theme to render the emails'),
  95. '#description' => t('Select the theme that will be used to render the emails. This can be either the current theme, the default theme, the domain theme or any active theme.'),
  96. '#options' => $theme_options,
  97. '#default_value' => variable_get('mailsystem_theme', 'current'),
  98. );
  99. $form['class'] = array(
  100. '#type' => 'fieldset',
  101. '#title' => t('New Class'),
  102. '#description' => t(
  103. 'Create a new <a href="!interface"><code>@interface</code></a> that inherits its methods from other classes. The new class will be named after the other classes it uses.', $args
  104. ),
  105. '#collapsible' => TRUE,
  106. '#collapsed' => TRUE,
  107. '#tree' => TRUE,
  108. );
  109. $mailsystem_classes[mailsystem_default_id()] = '--Select--';
  110. $form['class']['format'] = array(
  111. '#type' => 'select',
  112. '#title' => t(
  113. 'Class to use for the <a href="!format"><code>@format</code></a> method', $args
  114. ),
  115. '#options' => $mailsystem_classes,
  116. );
  117. $form['class']['mail'] = array(
  118. '#type' => 'select',
  119. '#title' => t(
  120. 'Class to use for the <a href="!mail"><code>@mail</code></a> method', $args
  121. ),
  122. '#options' => $mailsystem_classes,
  123. );
  124. $form['identifier'] = array(
  125. '#type' => 'fieldset',
  126. '#title' => t('New Setting'),
  127. '#description' => t('Add a new %module and %key to the settings list.',
  128. array(
  129. '%module' => 'module',
  130. '%key' => 'key',
  131. )
  132. ),
  133. '#collapsible' => TRUE,
  134. '#collapsed' => TRUE,
  135. '#tree' => TRUE,
  136. );
  137. array_unshift($descriptions, t('-- Select --'));
  138. $form['identifier']['module'] = array(
  139. '#type' => 'select',
  140. '#title' => t('Module'),
  141. '#options' => $descriptions,
  142. );
  143. $form['identifier']['key'] = array(
  144. '#type' => 'textfield',
  145. '#title' => t('Key'),
  146. '#size' => 80,
  147. );
  148. $form['submit'] = array(
  149. '#type' => 'submit',
  150. '#value' => t('Save settings'),
  151. );
  152. return $form;
  153. }
  154. /**
  155. * Processes mailsystem_admin_settings form.
  156. */
  157. function mailsystem_admin_settings_submit($form, &$form_state) {
  158. variable_set('mailsystem_theme', $form_state['values']['mailsystem']['mailsystem_theme']);
  159. // Rebuild the theme registry to make changes needed by theme rendering.
  160. drupal_theme_rebuild();
  161. unset($form_state['values']['mailsystem']['mailsystem_theme']);
  162. $default_id = mailsystem_default_id();
  163. $mail_system = array(
  164. $default_id => (
  165. empty($form_state['values'][$default_id])
  166. ? mailsystem_default_value()
  167. : $form_state['values'][$default_id]
  168. ),
  169. );
  170. foreach (element_children($form_state['values']['mailsystem']) as $module) {
  171. $class = $form_state['values']['mailsystem'][$module];
  172. if (!empty($class) && $class != $default_id) {
  173. $mail_system[$module] = $class;
  174. }
  175. }
  176. unset($form_state['values']['mailsystem']);
  177. if ($form_state['values']['class']['format'] === mailsystem_default_id()) {
  178. unset($form_state['values']['class']['format']);
  179. }
  180. if ($form_state['values']['class']['mail'] === mailsystem_default_id()) {
  181. unset($form_state['values']['class']['mail']);
  182. }
  183. if ($form_state['values']['class']) {
  184. $new_class = mailsystem_create_class($form_state['values']['class']);
  185. }
  186. else {
  187. $new_class = $mail_system[mailsystem_default_id()];
  188. }
  189. unset($form_state['values']['class']);
  190. if ($id = $form_state['values']['identifier']['module']) {
  191. if (!empty($form_state['values']['identifier']['key'])) {
  192. $id .= '_' . $form_state['values']['identifier']['key'];
  193. }
  194. $mail_system[$id] = $new_class;
  195. }
  196. unset($form_state['values']['identifier']);
  197. variable_set('mail_system', $mail_system);
  198. drupal_set_message(t('The configuration options have been saved.'));
  199. }