Simpletest Coverage - modules/contact/contact.pages.inc

1 <?php
2 // $Id: contact.pages.inc,v 1.23 2009/07/20 18:51:33 dries Exp $
3
4 /**
5 * @file
6 * User page callbacks for the contact module.
7 */
8
9
10 /**
11 * Site-wide contact page.
12 */
13 function contact_site_page() {
14 if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) {
15 $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
16 }
17 elseif (!db_query("SELECT COUNT(cid) FROM {contact}")->fetchField()) {
18 if (user_access('administer site-wide contact form')) {
19 $output = t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array('@add' => url('admin/structure/contact/add')));
20 }
21 else {
22 return drupal_not_found();
23 }
24 }
25 else {
26 $output = drupal_get_form('contact_site_form');
27 }
28
29 return $output;
30 }
31
32 /**
33 * Form builder; the site-wide contact form.
34 */
35 function contact_site_form() {
36 global $user;
37
38 $categories = db_query("SELECT cid, category FROM {contact} ORDER BY weight, category")->fetchAllKeyed();
39 $default_category = (int) db_query("SELECT cid FROM {contact} WHERE selected = 1")->fetchField();
40
41 // If there is more than one category available and no default category has been selected,
42 // prepend a default placeholder value.
43 if (!$default_category) {
44 if (count($categories) > 1) {
45 $categories = array(0 => t('- Please choose -')) + $categories;
46 }
47 else {
48 $default_category = key($categories);
49 }
50 }
51
52 $form['#token'] = $user->uid ? $user->name . $user->mail : '';
53 $form['name'] = array(
54 '#type' => 'textfield',
55 '#title' => t('Your name'),
56 '#maxlength' => 255,
57 '#default_value' => $user->uid ? $user->name : '',
58 '#required' => TRUE,
59 );
60 $form['mail'] = array(
61 '#type' => 'textfield',
62 '#title' => t('Your e-mail address'),
63 '#maxlength' => 255,
64 '#default_value' => $user->uid ? $user->mail : '',
65 '#required' => TRUE,
66 );
67 $form['subject'] = array(
68 '#type' => 'textfield',
69 '#title' => t('Subject'),
70 '#maxlength' => 255,
71 '#required' => TRUE,
72 );
73 $form['cid'] = array(
74 '#type' => 'select',
75 '#title' => t('Category'),
76 '#default_value' => $default_category,
77 '#options' => $categories,
78 '#required' => TRUE,
79 '#access' => count($categories) > 1,
80 );
81 $form['message'] = array(
82 '#type' => 'textarea',
83 '#title' => t('Message'),
84 '#required' => TRUE,
85 );
86 // We do not allow anonymous users to send themselves a copy
87 // because it can be abused to spam people.
88 $form['copy'] = array(
89 '#type' => 'checkbox',
90 '#title' => t('Send yourself a copy.'),
91 '#access' => $user->uid,
92 );
93 $form['submit'] = array(
94 '#type' => 'submit',
95 '#value' => t('Send message'),
96 );
97
98 return $form;
99 }
100
101 /**
102 * Form validation handler for contact_site_form().
103 */
104 function contact_site_form_validate($form, &$form_state) {
105 if (!$form_state['values']['cid']) {
106 form_set_error('cid', t('You must select a valid category.'));
107 }
108 if (!valid_email_address($form_state['values']['mail'])) {
109 form_set_error('mail', t('You must enter a valid e-mail address.'));
110 }
111 }
112
113 /**
114 * Form submission handler for contact_site_form().
115 */
116 function contact_site_form_submit($form, &$form_state) {
117 global $language;
118
119 $values = $form_state['values'];
120
121 // E-mail address of the sender: as the form field is a text field,
122 // all instances of \r and \n have been automatically stripped from it.
123 $from = $values['mail'];
124
125 // Load category properties and save form values for email composition.
126 $contact = contact_load($values['cid']);
127 $values['contact'] = $contact;
128
129 // Send the e-mail to the recipients using the site default language.
130 drupal_mail('contact', 'page_mail', $contact['recipients'], language_default(), $values, $from);
131
132 // If the user requests it, send a copy using the current language.
133 if ($values['copy']) {
134 drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
135 }
136
137 // Send an auto-reply if necessary using the current language.
138 if ($contact['reply']) {
139 drupal_mail('contact', 'page_autoreply', $from, $language, $values, $contact['recipients']);
140 }
141
142 flood_register_event('contact');
143 watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $values['name'] . " [$from]", '%category' => $contact['category']));
144 drupal_set_message(t('Your message has been sent.'));
145
146 // Jump to home page rather than back to contact page to avoid
147 // contradictory messages if flood control has been activated.
148 $form_state['redirect'] = '';
149 }
150
151 /**
152 * Personal contact page.
153 */
154 function contact_personal_page($account) {
155 global $user;
156
157 if (!valid_email_address($user->mail)) {
158 $output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array('@url' => url("user/$user->uid/edit", array('query' => 'destination=' . drupal_get_destination()))));
159 }
160 elseif (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) {
161 $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
162 }
163 else {
164 drupal_set_title($account->name);
165 $output = drupal_get_form('contact_personal_form', $account);
166 }
167
168 return $output;
169 }
170
171 /**
172 * Form builder; the personal contact form.
173 */
174 function contact_personal_form(&$form_state, $recipient) {
175 global $user;
176 $form['#token'] = $user->name . $user->mail;
177 $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
178 $form['from'] = array('#type' => 'item',
179 '#title' => t('From'),
180 '#markup' => theme('username', $user) . ' &lt;' . check_plain($user->mail) . '&gt;',
181 );
182 $form['to'] = array('#type' => 'item',
183 '#title' => t('To'),
184 '#markup' => theme('username', $recipient),
185 );
186 $form['subject'] = array('#type' => 'textfield',
187 '#title' => t('Subject'),
188 '#maxlength' => 50,
189 '#required' => TRUE,
190 );
191 $form['message'] = array('#type' => 'textarea',
192 '#title' => t('Message'),
193 '#rows' => 15,
194 '#required' => TRUE,
195 );
196 $form['copy'] = array('#type' => 'checkbox',
197 '#title' => t('Send yourself a copy.'),
198 );
199 $form['submit'] = array('#type' => 'submit',
200 '#value' => t('Send message'),
201 );
202 return $form;
203 }
204
205 /**
206 * Form submission handler for contact_personal_form().
207 */
208 function contact_personal_form_submit($form, &$form_state) {
209 global $user, $language;
210
211 $account = $form_state['values']['recipient'];
212
213 // Send from the current user to the requested user.
214 $to = $account->mail;
215 $from = $user->mail;
216
217 // Save both users and all form values for email composition.
218 $values = $form_state['values'];
219 $values['account'] = $account;
220 $values['user'] = $user;
221
222 // Send the e-mail in the requested user language.
223 drupal_mail('contact', 'user_mail', $to, user_preferred_language($account), $values, $from);
224
225 // Send a copy if requested, using current page language.
226 if ($form_state['values']['copy']) {
227 drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
228 }
229
230 flood_register_event('contact');
231 watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
232 drupal_set_message(t('Your message has been sent.'));
233
234 // Back to the requested users profile page.
235 $form_state['redirect'] = "user/$account->uid";
236 }
237

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.