contact.admin.inc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @file
  4. * Admin page callbacks for the Contact module.
  5. */
  6. /**
  7. * Categories/list tab.
  8. */
  9. function contact_category_list() {
  10. $header = array(
  11. t('Category'),
  12. t('Recipients'),
  13. t('Selected'),
  14. array('data' => t('Operations'), 'colspan' => 2),
  15. );
  16. $rows = array();
  17. // Get all the contact categories from the database.
  18. $categories = db_select('contact', 'c')
  19. ->addTag('translatable')
  20. ->fields('c', array('cid', 'category', 'recipients', 'selected'))
  21. ->orderBy('weight')
  22. ->orderBy('category')
  23. ->execute()
  24. ->fetchAll();
  25. // Loop through the categories and add them to the table.
  26. foreach ($categories as $category) {
  27. $rows[] = array(
  28. check_plain($category->category),
  29. check_plain($category->recipients),
  30. ($category->selected ? t('Yes') : t('No')),
  31. l(t('Edit'), 'admin/structure/contact/edit/' . $category->cid),
  32. l(t('Delete'), 'admin/structure/contact/delete/' . $category->cid),
  33. );
  34. }
  35. if (!$rows) {
  36. $rows[] = array(array(
  37. 'data' => t('No categories available.'),
  38. 'colspan' => 5,
  39. ));
  40. }
  41. $build['category_table'] = array(
  42. '#theme' => 'table',
  43. '#header' => $header,
  44. '#rows' => $rows,
  45. );
  46. return $build;
  47. }
  48. /**
  49. * Form constructor for the category edit form.
  50. *
  51. * @param $category
  52. * An array describing the category to be edited. May be empty for new
  53. * categories. Recognized array keys are:
  54. * - category: The name of the category.
  55. * - recipients: A comma-separated list of recipients.
  56. * - reply: (optional) The body of the auto-reply message.
  57. * - weight: The weight of the category.
  58. * - selected: Boolean indicating whether the category should be selected by
  59. * default.
  60. * - cid: The category ID for which the form is to be displayed.
  61. *
  62. * @see contact_category_edit_form_validate()
  63. * @see contact_category_edit_form_submit()
  64. * @ingroup forms
  65. */
  66. function contact_category_edit_form($form, &$form_state, array $category = array()) {
  67. // If this is a new category, add the default values.
  68. $category += array(
  69. 'category' => '',
  70. 'recipients' => '',
  71. 'reply' => '',
  72. 'weight' => 0,
  73. 'selected' => 0,
  74. 'cid' => NULL,
  75. );
  76. $form['category'] = array(
  77. '#type' => 'textfield',
  78. '#title' => t('Category'),
  79. '#maxlength' => 255,
  80. '#default_value' => $category['category'],
  81. '#description' => t("Example: 'website feedback' or 'product information'."),
  82. '#required' => TRUE,
  83. );
  84. $form['recipients'] = array(
  85. '#type' => 'textarea',
  86. '#title' => t('Recipients'),
  87. '#default_value' => $category['recipients'],
  88. '#description' => t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each e-mail address with a comma."),
  89. '#required' => TRUE,
  90. );
  91. $form['reply'] = array(
  92. '#type' => 'textarea',
  93. '#title' => t('Auto-reply'),
  94. '#default_value' => $category['reply'],
  95. '#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
  96. );
  97. $form['weight'] = array(
  98. '#type' => 'weight',
  99. '#title' => t('Weight'),
  100. '#default_value' => $category['weight'],
  101. '#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'),
  102. );
  103. $form['selected'] = array(
  104. '#type' => 'select',
  105. '#title' => t('Selected'),
  106. '#options' => array(
  107. 0 => t('No'),
  108. 1 => t('Yes'),
  109. ),
  110. '#default_value' => $category['selected'],
  111. '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
  112. );
  113. $form['cid'] = array(
  114. '#type' => 'value',
  115. '#value' => $category['cid'],
  116. );
  117. $form['actions'] = array('#type' => 'actions');
  118. $form['actions']['submit'] = array(
  119. '#type' => 'submit',
  120. '#value' => t('Save'),
  121. );
  122. return $form;
  123. }
  124. /**
  125. * Form validation handler for contact_category_edit_form().
  126. *
  127. * @see contact_category_edit_form_submit()
  128. */
  129. function contact_category_edit_form_validate($form, &$form_state) {
  130. // Validate and each e-mail recipient.
  131. $recipients = explode(',', $form_state['values']['recipients']);
  132. // When creating a new contact form, or renaming the category on an existing
  133. // contact form, make sure that the given category is unique.
  134. $category = $form_state['values']['category'];
  135. $query = db_select('contact', 'c')->condition('c.category', $category, '=');
  136. if (!empty($form_state['values']['cid'])) {
  137. $query->condition('c.cid', $form_state['values']['cid'], '<>');
  138. }
  139. if ($query->countQuery()->execute()->fetchField()) {
  140. form_set_error('category', t('A contact form with category %category already exists.', array('%category' => $category)));
  141. }
  142. foreach ($recipients as &$recipient) {
  143. $recipient = trim($recipient);
  144. if (!valid_email_address($recipient)) {
  145. form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
  146. }
  147. }
  148. $form_state['values']['recipients'] = implode(',', $recipients);
  149. }
  150. /**
  151. * Form submission handler for contact_category_edit_form().
  152. *
  153. * @see contact_category_edit_form_validate()
  154. */
  155. function contact_category_edit_form_submit($form, &$form_state) {
  156. if ($form_state['values']['selected']) {
  157. // Unselect all other contact categories.
  158. db_update('contact')
  159. ->fields(array('selected' => '0'))
  160. ->execute();
  161. }
  162. if (empty($form_state['values']['cid'])) {
  163. drupal_write_record('contact', $form_state['values']);
  164. }
  165. else {
  166. drupal_write_record('contact', $form_state['values'], array('cid'));
  167. }
  168. drupal_set_message(t('Category %category has been saved.', array('%category' => $form_state['values']['category'])));
  169. watchdog('contact', 'Category %category has been saved.', array('%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/contact/edit/' . $form_state['values']['cid']));
  170. $form_state['redirect'] = 'admin/structure/contact';
  171. }
  172. /**
  173. * Form constructor for the contact category deletion form.
  174. *
  175. * @param $contact
  176. * Array describing the contact category to be deleted. See the documentation
  177. * of contact_category_edit_form() for the recognized keys.
  178. *
  179. * @see contact_menu()
  180. * @see contact_category_delete_form_submit()
  181. */
  182. function contact_category_delete_form($form, &$form_state, array $contact) {
  183. $form['contact'] = array(
  184. '#type' => 'value',
  185. '#value' => $contact,
  186. );
  187. return confirm_form(
  188. $form,
  189. t('Are you sure you want to delete %category?', array('%category' => $contact['category'])),
  190. 'admin/structure/contact',
  191. t('This action cannot be undone.'),
  192. t('Delete'),
  193. t('Cancel')
  194. );
  195. }
  196. /**
  197. * Form submission handler for contact_category_delete_form().
  198. */
  199. function contact_category_delete_form_submit($form, &$form_state) {
  200. $contact = $form['contact']['#value'];
  201. db_delete('contact')
  202. ->condition('cid', $contact['cid'])
  203. ->execute();
  204. drupal_set_message(t('Category %category has been deleted.', array('%category' => $contact['category'])));
  205. watchdog('contact', 'Category %category has been deleted.', array('%category' => $contact['category']), WATCHDOG_NOTICE);
  206. $form_state['redirect'] = 'admin/structure/contact';
  207. }