Simpletest Coverage - modules/comment/comment.admin.inc

1 <?php
2 // $Id: comment.admin.inc,v 1.30 2009/07/31 19:44:09 dries Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the comment module.
7 */
8
9 /**
10 * Menu callback; present an administrative comment listing.
11 */
12 function comment_admin($type = 'new') {
13 $edit = $_POST;
14
15 if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
16 return drupal_get_form('comment_multiple_delete_confirm');
17 }
18 else {
19 return drupal_get_form('comment_admin_overview', $type, arg(4));
20 }
21 }
22
23 /**
24 * Form builder; Builds the comment overview form for the admin.
25 *
26 * @param $type
27 * Not used.
28 * @param $arg
29 * Current path's fourth component deciding the form type (Published comments/Approval queue).
30 * @return
31 * The form structure.
32 * @ingroup forms
33 * @see comment_admin_overview_validate()
34 * @see comment_admin_overview_submit()
35 * @see theme_comment_admin_overview()
36 */
37 function comment_admin_overview($type = 'new', $arg) {
38 // Build an 'Update options' form.
39 $form['options'] = array(
40 '#type' => 'fieldset',
41 '#title' => t('Update options'),
42 '#prefix' => '<div class="container-inline">',
43 '#suffix' => '</div>',
44 );
45 $options = array();
46 foreach (comment_operations($arg == 'approval' ? 'publish' : 'unpublish') as $key => $value) {
47 $options[$key] = $value[0];
48 }
49 $form['options']['operation'] = array(
50 '#type' => 'select',
51 '#options' => $options,
52 '#default_value' => 'publish',
53 );
54 $form['options']['submit'] = array(
55 '#type' => 'submit',
56 '#value' => t('Update'),
57 );
58
59 // Load the comments that need to be displayed.
60 $status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
61 $header = array(
62 'subject' => array('data' => t('Subject'), 'field' => 'subject'),
63 'author' => array('data' => t('Author'), 'field' => 'name'),
64 'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title'),
65 'time' => array('data' => t('Time'), 'field' => 'timestamp', 'sort' => 'desc'),
66 'operations' => array('data' => t('Operations')),
67 );
68
69 $query = db_select('comment', 'c')->extend('PagerDefault')->extend('TableSort');
70 $query->join('users', 'u', 'u.uid = c.uid');
71 $query->join('node', 'n', 'n.nid = c.nid');
72 $query->addField('u', 'name', 'registered_name');
73 $query->addField('n', 'title', 'node_title');
74 $result = $query
75 ->fields('c', array('subject', 'nid', 'cid', 'comment', 'timestamp', 'status', 'name', 'homepage'))
76 ->fields('u', array('uid'))
77 ->condition('c.status', $status)
78 ->limit(50)
79 ->orderByHeader($header)
80 ->execute();
81
82
83 // Build a table listing the appropriate comments.
84 $options = array();
85 $destination = drupal_get_destination();
86
87 foreach ($result as $comment) {
88 $options[$comment->cid] = array(
89 'subject' => l($comment->subject, 'comment/' . $comment->cid, array('attributes' => array('title' => truncate_utf8($comment->comment, 128)), 'fragment' => 'comment-' . $comment->cid)),
90 'author' => theme('username', $comment),
91 'posted_in' => l($comment->node_title, 'node/' . $comment->nid),
92 'time' => format_date($comment->timestamp, 'small'),
93 'operations' => l(t('edit'), 'comment/edit/' . $comment->cid, array('query' => $destination)),
94 );
95 }
96
97 $form['comments'] = array(
98 '#type' => 'tableselect',
99 '#header' => $header,
100 '#options' => $options,
101 '#empty' => t('No comments available.'),
102 );
103
104 $form['pager'] = array('#theme' => 'pager');
105
106 return $form;
107 }
108
109 /**
110 * Validate comment_admin_overview form submissions.
111 */
112 function comment_admin_overview_validate($form, &$form_state) {
113 $form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
114 // We can't execute any 'Update options' if no comments were selected.
115 if (count($form_state['values']['comments']) == 0) {
116 form_set_error('', t('Please select one or more comments to perform the update on.'));
117 drupal_goto('admin/content/comment');
118 }
119 }
120
121 /**
122 * Process comment_admin_overview form submissions.
123 *
124 * Execute the chosen 'Update option' on the selected comments, such as
125 * publishing, unpublishing or deleting.
126 */
127 function comment_admin_overview_submit($form, &$form_state) {
128 $operations = comment_operations();
129 if ($operations[$form_state['values']['operation']][1]) {
130 // Extract the appropriate database query operation.
131 $query = $operations[$form_state['values']['operation']][1];
132 foreach ($form_state['values']['comments'] as $cid => $value) {
133 if ($value) {
134 // Perform the update action, then refresh node statistics.
135 $query
136 ->condition('cid', $cid )
137 ->execute();
138 $comment = comment_load($cid);
139 _comment_update_node_statistics($comment->nid);
140 // Allow modules to respond to the updating of a comment.
141 module_invoke_all('comment_' . $form_state['values']['operation'], $comment);
142 // Add an entry to the watchdog log.
143 watchdog('content', 'Comment: updated %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'node/' . $comment->nid, array('fragment' => 'comment-' . $comment->cid)));
144 }
145 }
146 cache_clear_all();
147 drupal_set_message(t('The update has been performed.'));
148 $form_state['redirect'] = 'admin/content/comment';
149 }
150 }
151
152 /**
153 * List the selected comments and verify that the admin wants to delete them.
154 *
155 * @param $form_state
156 * An associative array containing the current state of the form.
157 * @return
158 * TRUE if the comments should be deleted, FALSE otherwise.
159 * @ingroup forms
160 * @see comment_multiple_delete_confirm_submit()
161 */
162 function comment_multiple_delete_confirm(&$form_state) {
163 $edit = $form_state['input'];
164
165 $form['comments'] = array(
166 '#prefix' => '<ul>',
167 '#suffix' => '</ul>',
168 '#tree' => TRUE,
169 );
170 // array_filter() returns only elements with actual values.
171 $comment_counter = 0;
172 foreach (array_filter($edit['comments']) as $cid => $value) {
173 $comment = comment_load($cid);
174 if (is_object($comment) && is_numeric($comment->cid)) {
175 $subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
176 $form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
177 $comment_counter++;
178 }
179 }
180 $form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
181
182 if (!$comment_counter) {
183 drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
184 drupal_goto('admin/content/comment');
185 }
186 else {
187 return confirm_form($form,
188 t('Are you sure you want to delete these comments and all their children?'),
189 'admin/content/comment', t('This action cannot be undone.'),
190 t('Delete comments'), t('Cancel'));
191 }
192 }
193
194 /**
195 * Process comment_multiple_delete_confirm form submissions.
196 */
197 function comment_multiple_delete_confirm_submit($form, &$form_state) {
198 if ($form_state['values']['confirm']) {
199 comment_delete_multiple(array_keys($form_state['values']['comments']));
200 cache_clear_all();
201 $count = count($form_state['values']['comments']);
202 watchdog('content', 'Deleted @count comments.', array('@count' => $count));
203 drupal_set_message(t('Deleted @count comments.', array('@count' => $count)));
204 }
205 $form_state['redirect'] = 'admin/content/comment';
206 }
207
208 /**
209 * Menu callback; delete a comment.
210 *
211 * @param $cid
212 * The comment to be deleted.
213 */
214 function comment_delete_page($cid = NULL) {
215 $comment = db_query('SELECT c.*, u.name AS registered_name, u.uid FROM {comment} c INNER JOIN {users} u ON u.uid = c.uid WHERE c.cid = :cid', array(':cid' => $cid))->fetch();
216 $comment->name = $comment->uid ? $comment->registered_name : $comment->name;
217 $output = '';
218
219 if (is_object($comment) && is_numeric($comment->cid)) {
220 $output = drupal_get_form('comment_confirm_delete', $comment);
221 }
222 else {
223 drupal_set_message(t('The comment no longer exists.'));
224 }
225
226 return $output;
227 }
228
229 /**
230 * Form builder; Builds the confirmation form for deleting a single comment.
231 *
232 * @ingroup forms
233 * @see comment_confirm_delete_submit()
234 */
235 function comment_confirm_delete(&$form_state, $comment) {
236 $form = array();
237 $form['#comment'] = $comment;
238 return confirm_form(
239 $form,
240 t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
241 'node/' . $comment->nid,
242 t('Any replies to this comment will be lost. This action cannot be undone.'),
243 t('Delete'),
244 t('Cancel'),
245 'comment_confirm_delete');
246 }
247
248 /**
249 * Process comment_confirm_delete form submissions.
250 */
251 function comment_confirm_delete_submit($form, &$form_state) {
252 $comment = $form['#comment'];
253 // Delete the comment and its replies.
254 comment_delete($comment->cid);
255 drupal_set_message(t('The comment and all its replies have been deleted.'));
256 watchdog('content', t('Deleted comment @cid and its replies.', array('@cid' => $comment->cid)));
257 // Clear the cache so an anonymous user sees that his comment was deleted.
258 cache_clear_all();
259
260 $form_state['redirect'] = "node/$comment->nid";
261 }
262

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.