Project

General

Profile

Paste
Download (7.22 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / contact / contact.admin.inc @ 27e02aed

1
<?php
2

    
3
/**
4
 * @file
5
 * Admin page callbacks for the Contact module.
6
 */
7

    
8
/**
9
 * Categories/list tab.
10
 */
11
function contact_category_list() {
12
  $header = array(
13
    t('Category'),
14
    t('Recipients'),
15
    t('Selected'),
16
    array('data' => t('Operations'), 'colspan' => 2),
17
  );
18
  $rows = array();
19

    
20
  // Get all the contact categories from the database.
21
  $categories = db_select('contact', 'c')
22
    ->addTag('translatable')
23
    ->fields('c', array('cid', 'category', 'recipients', 'selected'))
24
    ->orderBy('weight')
25
    ->orderBy('category')
26
    ->execute()
27
    ->fetchAll();
28

    
29
  // Loop through the categories and add them to the table.
30
  foreach ($categories as $category) {
31
    $rows[] = array(
32
      check_plain($category->category),
33
      check_plain($category->recipients),
34
      ($category->selected ? t('Yes') : t('No')),
35
      l(t('Edit'), 'admin/structure/contact/edit/' . $category->cid),
36
      l(t('Delete'), 'admin/structure/contact/delete/' . $category->cid),
37
    );
38
  }
39

    
40
  if (!$rows) {
41
    $rows[] = array(array(
42
      'data' => t('No categories available.'),
43
      'colspan' => 5,
44
    ));
45
  }
46

    
47
  $build['category_table'] = array(
48
    '#theme' => 'table',
49
    '#header' => $header,
50
    '#rows' => $rows,
51
  );
52
  return $build;
53
}
54

    
55
/**
56
 * Form constructor for the category edit form.
57
 *
58
 * @param $category
59
 *   An array describing the category to be edited. May be empty for new
60
 *   categories. Recognized array keys are:
61
 *   - category: The name of the category.
62
 *   - recipients: A comma-separated list of recipients.
63
 *   - reply: (optional) The body of the auto-reply message.
64
 *   - weight: The weight of the category.
65
 *   - selected: Boolean indicating whether the category should be selected by
66
 *     default.
67
 *   - cid: The category ID for which the form is to be displayed.
68
 *
69
 * @see contact_category_edit_form_validate()
70
 * @see contact_category_edit_form_submit()
71
 * @ingroup forms
72
 */
73
function contact_category_edit_form($form, &$form_state, array $category = array()) {
74
  // If this is a new category, add the default values.
75
  $category += array(
76
    'category' => '',
77
    'recipients' => '',
78
    'reply' => '',
79
    'weight' => 0,
80
    'selected' => 0,
81
    'cid' => NULL,
82
  );
83

    
84
  $form['category'] = array(
85
    '#type' => 'textfield',
86
    '#title' => t('Category'),
87
    '#maxlength' => 255,
88
    '#default_value' => $category['category'],
89
    '#description' => t("Example: 'website feedback' or 'product information'."),
90
    '#required' => TRUE,
91
  );
92
  $form['recipients'] = array(
93
    '#type' => 'textarea',
94
    '#title' => t('Recipients'),
95
    '#default_value' => $category['recipients'],
96
    '#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."),
97
    '#required' => TRUE,
98
  );
99
  $form['reply'] = array(
100
    '#type' => 'textarea',
101
    '#title' => t('Auto-reply'),
102
    '#default_value' => $category['reply'],
103
    '#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
104
  );
105
  $form['weight'] = array(
106
    '#type' => 'weight',
107
    '#title' => t('Weight'),
108
    '#default_value' => $category['weight'],
109
    '#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.'),
110
  );
111
  $form['selected'] = array(
112
    '#type' => 'select',
113
    '#title' => t('Selected'),
114
    '#options' => array(
115
      0 => t('No'),
116
      1 => t('Yes'),
117
    ),
118
    '#default_value' => $category['selected'],
119
    '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
120
  );
121
  $form['cid'] = array(
122
    '#type' => 'value',
123
    '#value' => $category['cid'],
124
  );
125
  $form['actions'] = array('#type' => 'actions');
126
  $form['actions']['submit'] = array(
127
    '#type' => 'submit',
128
    '#value' => t('Save'),
129
  );
130

    
131
  return $form;
132
}
133

    
134
/**
135
 * Form validation handler for contact_category_edit_form().
136
 *
137
 * @see contact_category_edit_form_submit()
138
 */
139
function contact_category_edit_form_validate($form, &$form_state) {
140
  // Validate and each e-mail recipient.
141
  $recipients = explode(',', $form_state['values']['recipients']);
142

    
143
  // When creating a new contact form, or renaming the category on an existing
144
  // contact form, make sure that the given category is unique.
145
  $category = $form_state['values']['category'];
146
  $query = db_select('contact', 'c')->condition('c.category', $category, '=');
147
  if (!empty($form_state['values']['cid'])) {
148
    $query->condition('c.cid', $form_state['values']['cid'], '<>');
149
  }
150
  if ($query->countQuery()->execute()->fetchField()) {
151
    form_set_error('category', t('A contact form with category %category already exists.', array('%category' => $category)));
152
  }
153

    
154
  foreach ($recipients as &$recipient) {
155
    $recipient = trim($recipient);
156
    if (!valid_email_address($recipient)) {
157
      form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
158
    }
159
  }
160
  $form_state['values']['recipients'] = implode(',', $recipients);
161
}
162

    
163
/**
164
 * Form submission handler for contact_category_edit_form().
165
 *
166
 * @see contact_category_edit_form_validate()
167
 */
168
function contact_category_edit_form_submit($form, &$form_state) {
169
  if ($form_state['values']['selected']) {
170
    // Unselect all other contact categories.
171
    db_update('contact')
172
      ->fields(array('selected' => '0'))
173
      ->execute();
174
  }
175

    
176
  if (empty($form_state['values']['cid'])) {
177
    drupal_write_record('contact', $form_state['values']);
178
  }
179
  else {
180
    drupal_write_record('contact', $form_state['values'], array('cid'));
181
  }
182

    
183
  drupal_set_message(t('Category %category has been saved.', array('%category' => $form_state['values']['category'])));
184
  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']));
185
  $form_state['redirect'] = 'admin/structure/contact';
186
}
187

    
188
/**
189
 * Form constructor for the contact category deletion form.
190
 *
191
 * @param $contact
192
 *   Array describing the contact category to be deleted. See the documentation
193
 *   of contact_category_edit_form() for the recognized keys.
194
 *
195
 * @see contact_menu()
196
 * @see contact_category_delete_form_submit()
197
 */
198
function contact_category_delete_form($form, &$form_state, array $contact) {
199
  $form['contact'] = array(
200
    '#type' => 'value',
201
    '#value' => $contact,
202
  );
203

    
204
  return confirm_form(
205
    $form,
206
    t('Are you sure you want to delete %category?', array('%category' => $contact['category'])),
207
    'admin/structure/contact',
208
    t('This action cannot be undone.'),
209
    t('Delete'),
210
    t('Cancel')
211
  );
212
}
213

    
214
/**
215
 * Form submission handler for contact_category_delete_form().
216
 */
217
function contact_category_delete_form_submit($form, &$form_state) {
218
  $contact = $form['contact']['#value'];
219

    
220
  db_delete('contact')
221
    ->condition('cid', $contact['cid'])
222
    ->execute();
223

    
224
  drupal_set_message(t('Category %category has been deleted.', array('%category' => $contact['category'])));
225
  watchdog('contact', 'Category %category has been deleted.', array('%category' => $contact['category']), WATCHDOG_NOTICE);
226

    
227
  $form_state['redirect'] = 'admin/structure/contact';
228
}