2007-10-31 17:50:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2012-01-03 04:36:15 +00:00
|
|
|
* Admin page callbacks for the Comment module.
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
|
|
|
|
2012-05-03 05:49:41 +00:00
|
|
|
use Drupal\comment\Comment;
|
2012-06-02 19:41:40 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2012-05-03 05:49:41 +00:00
|
|
|
|
2007-10-31 17:50:47 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Page callback: Presents an administrative comment listing.
|
|
|
|
*
|
|
|
|
* @param $type
|
|
|
|
* The type of the overview form ('approval' or 'new'). See
|
|
|
|
* comment_admin_overview() for details.
|
|
|
|
*
|
|
|
|
* @see comment_menu()
|
|
|
|
* @see comment_multiple_delete_confirm()
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
|
|
|
function comment_admin($type = 'new') {
|
|
|
|
$edit = $_POST;
|
|
|
|
|
|
|
|
if (isset($edit['operation']) && ($edit['operation'] == 'delete') && isset($edit['comments']) && $edit['comments']) {
|
2009-05-25 10:43:54 +00:00
|
|
|
return drupal_get_form('comment_multiple_delete_confirm');
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-25 21:24:35 +00:00
|
|
|
return drupal_get_form('comment_admin_overview', $type);
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form constructor for the comment overview administration form.
|
2007-10-31 17:50:47 +00:00
|
|
|
*
|
2007-12-16 21:01:45 +00:00
|
|
|
* @param $arg
|
2012-01-03 04:36:15 +00:00
|
|
|
* The type of overview form ('approval' or 'new').
|
2010-06-25 21:24:35 +00:00
|
|
|
*
|
2007-10-31 17:50:47 +00:00
|
|
|
* @ingroup forms
|
2012-01-03 04:36:15 +00:00
|
|
|
* @see comment_admin()
|
2008-01-08 10:35:43 +00:00
|
|
|
* @see comment_admin_overview_validate()
|
|
|
|
* @see comment_admin_overview_submit()
|
|
|
|
* @see theme_comment_admin_overview()
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function comment_admin_overview($form, &$form_state, $arg) {
|
2008-05-14 13:12:41 +00:00
|
|
|
// Build an 'Update options' form.
|
2007-10-31 17:50:47 +00:00
|
|
|
$form['options'] = array(
|
2008-05-14 13:12:41 +00:00
|
|
|
'#type' => 'fieldset',
|
|
|
|
'#title' => t('Update options'),
|
2010-03-03 19:46:26 +00:00
|
|
|
'#attributes' => array('class' => array('container-inline')),
|
2007-10-31 17:50:47 +00:00
|
|
|
);
|
2010-03-07 07:15:28 +00:00
|
|
|
|
|
|
|
if ($arg == 'approval') {
|
|
|
|
$options['publish'] = t('Publish the selected comments');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$options['unpublish'] = t('Unpublish the selected comments');
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
2010-03-07 07:15:28 +00:00
|
|
|
$options['delete'] = t('Delete the selected comments');
|
|
|
|
|
2008-05-14 13:12:41 +00:00
|
|
|
$form['options']['operation'] = array(
|
|
|
|
'#type' => 'select',
|
2010-10-20 01:31:07 +00:00
|
|
|
'#title' => t('Operation'),
|
|
|
|
'#title_display' => 'invisible',
|
2008-05-14 13:12:41 +00:00
|
|
|
'#options' => $options,
|
|
|
|
'#default_value' => 'publish',
|
|
|
|
);
|
|
|
|
$form['options']['submit'] = array(
|
|
|
|
'#type' => 'submit',
|
|
|
|
'#value' => t('Update'),
|
|
|
|
);
|
2007-10-31 17:50:47 +00:00
|
|
|
|
2008-05-14 13:12:41 +00:00
|
|
|
// Load the comments that need to be displayed.
|
2007-10-31 17:50:47 +00:00
|
|
|
$status = ($arg == 'approval') ? COMMENT_NOT_PUBLISHED : COMMENT_PUBLISHED;
|
2009-01-28 07:43:26 +00:00
|
|
|
$header = array(
|
|
|
|
'subject' => array('data' => t('Subject'), 'field' => 'subject'),
|
|
|
|
'author' => array('data' => t('Author'), 'field' => 'name'),
|
|
|
|
'posted_in' => array('data' => t('Posted in'), 'field' => 'node_title'),
|
2010-10-18 15:17:45 +00:00
|
|
|
'changed' => array('data' => t('Updated'), 'field' => 'c.changed', 'sort' => 'desc'),
|
2009-01-28 07:43:26 +00:00
|
|
|
'operations' => array('data' => t('Operations')),
|
|
|
|
);
|
|
|
|
|
2012-06-05 05:42:19 +00:00
|
|
|
$query = db_select('comment', 'c')
|
|
|
|
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
|
|
|
->extend('TableSort');
|
2009-02-22 16:53:41 +00:00
|
|
|
$query->join('node', 'n', 'n.nid = c.nid');
|
|
|
|
$query->addField('n', 'title', 'node_title');
|
2011-01-03 05:18:42 +00:00
|
|
|
$query->addTag('node_access');
|
2009-06-06 10:27:42 +00:00
|
|
|
$result = $query
|
2010-01-07 05:23:52 +00:00
|
|
|
->fields('c', array('cid', 'subject', 'name', 'changed'))
|
2009-02-22 16:53:41 +00:00
|
|
|
->condition('c.status', $status)
|
|
|
|
->limit(50)
|
2009-06-06 10:27:42 +00:00
|
|
|
->orderByHeader($header)
|
|
|
|
->execute();
|
2010-01-30 07:59:26 +00:00
|
|
|
|
2010-01-11 00:17:02 +00:00
|
|
|
$cids = array();
|
2009-02-22 16:53:41 +00:00
|
|
|
|
2010-01-07 05:23:52 +00:00
|
|
|
// We collect a sorted list of node_titles during the query to attach to the
|
|
|
|
// comments later.
|
|
|
|
foreach ($result as $row) {
|
|
|
|
$cids[] = $row->cid;
|
|
|
|
$node_titles[] = $row->node_title;
|
|
|
|
}
|
|
|
|
$comments = comment_load_multiple($cids);
|
2007-10-31 17:50:47 +00:00
|
|
|
|
2008-05-14 13:12:41 +00:00
|
|
|
// Build a table listing the appropriate comments.
|
2009-01-28 07:43:26 +00:00
|
|
|
$options = array();
|
2007-10-31 17:50:47 +00:00
|
|
|
$destination = drupal_get_destination();
|
2009-01-28 07:43:26 +00:00
|
|
|
|
2010-01-07 05:23:52 +00:00
|
|
|
foreach ($comments as $comment) {
|
2010-01-11 00:17:02 +00:00
|
|
|
// Remove the first node title from the node_titles array and attach to
|
2010-01-07 05:23:52 +00:00
|
|
|
// the comment.
|
|
|
|
$comment->node_title = array_shift($node_titles);
|
2009-01-28 07:43:26 +00:00
|
|
|
$options[$comment->cid] = array(
|
2009-11-03 05:27:18 +00:00
|
|
|
'subject' => array(
|
|
|
|
'data' => array(
|
|
|
|
'#type' => 'link',
|
|
|
|
'#title' => $comment->subject,
|
|
|
|
'#href' => 'comment/' . $comment->cid,
|
2012-03-08 15:10:59 +00:00
|
|
|
'#options' => array('attributes' => array('title' => truncate_utf8($comment->comment_body[LANGUAGE_NOT_SPECIFIED][0]['value'], 128)), 'fragment' => 'comment-' . $comment->cid),
|
2009-11-03 05:27:18 +00:00
|
|
|
),
|
|
|
|
),
|
2009-10-09 01:00:08 +00:00
|
|
|
'author' => theme('username', array('account' => $comment)),
|
2009-11-03 05:27:18 +00:00
|
|
|
'posted_in' => array(
|
|
|
|
'data' => array(
|
|
|
|
'#type' => 'link',
|
|
|
|
'#title' => $comment->node_title,
|
|
|
|
'#href' => 'node/' . $comment->nid,
|
|
|
|
),
|
|
|
|
),
|
2009-10-10 13:37:11 +00:00
|
|
|
'changed' => format_date($comment->changed, 'short'),
|
2009-11-03 05:27:18 +00:00
|
|
|
'operations' => array(
|
|
|
|
'data' => array(
|
|
|
|
'#type' => 'link',
|
|
|
|
'#title' => t('edit'),
|
|
|
|
'#href' => 'comment/' . $comment->cid . '/edit',
|
|
|
|
'#options' => array('query' => $destination),
|
|
|
|
),
|
|
|
|
),
|
2008-05-14 13:12:41 +00:00
|
|
|
);
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
2009-01-28 07:43:26 +00:00
|
|
|
|
2008-05-14 13:12:41 +00:00
|
|
|
$form['comments'] = array(
|
2009-01-28 07:43:26 +00:00
|
|
|
'#type' => 'tableselect',
|
|
|
|
'#header' => $header,
|
|
|
|
'#options' => $options,
|
|
|
|
'#empty' => t('No comments available.'),
|
2008-05-14 13:12:41 +00:00
|
|
|
);
|
2009-01-28 07:43:26 +00:00
|
|
|
|
2009-07-29 06:39:35 +00:00
|
|
|
$form['pager'] = array('#theme' => 'pager');
|
2008-05-14 13:12:41 +00:00
|
|
|
|
2007-10-31 17:50:47 +00:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form validation handler for comment_admin_overview().
|
|
|
|
*
|
|
|
|
* @see comment_admin_overview_submit()
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
|
|
|
function comment_admin_overview_validate($form, &$form_state) {
|
|
|
|
$form_state['values']['comments'] = array_diff($form_state['values']['comments'], array(0));
|
2008-05-14 13:12:41 +00:00
|
|
|
// We can't execute any 'Update options' if no comments were selected.
|
2007-10-31 17:50:47 +00:00
|
|
|
if (count($form_state['values']['comments']) == 0) {
|
2010-01-09 23:03:22 +00:00
|
|
|
form_set_error('', t('Select one or more comments to perform the update on.'));
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form submission handler for comment_admin_overview().
|
2007-12-16 21:01:45 +00:00
|
|
|
*
|
2012-01-03 04:36:15 +00:00
|
|
|
* Executes the chosen 'Update option' on the selected comments, such as
|
2007-10-31 17:50:47 +00:00
|
|
|
* publishing, unpublishing or deleting.
|
2012-01-03 04:36:15 +00:00
|
|
|
*
|
|
|
|
* @see comment_admin_overview_validate()
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
|
|
|
function comment_admin_overview_submit($form, &$form_state) {
|
2010-03-07 07:15:28 +00:00
|
|
|
$operation = $form_state['values']['operation'];
|
|
|
|
$cids = $form_state['values']['comments'];
|
|
|
|
|
|
|
|
if ($operation == 'delete') {
|
|
|
|
comment_delete_multiple($cids);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
foreach ($cids as $cid => $value) {
|
|
|
|
$comment = comment_load($value);
|
|
|
|
|
|
|
|
if ($operation == 'unpublish') {
|
|
|
|
$comment->status = COMMENT_NOT_PUBLISHED;
|
|
|
|
}
|
2010-07-24 17:28:27 +00:00
|
|
|
elseif ($operation == 'publish') {
|
2010-03-07 07:15:28 +00:00
|
|
|
$comment->status = COMMENT_PUBLISHED;
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
2010-03-07 07:15:28 +00:00
|
|
|
comment_save($comment);
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-07 07:15:28 +00:00
|
|
|
drupal_set_message(t('The update has been performed.'));
|
|
|
|
$form_state['redirect'] = 'admin/content/comment';
|
2010-03-31 11:49:51 +00:00
|
|
|
cache_clear_all();
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form constructor for the confirmation form for bulk comment deletion.
|
2007-10-31 17:50:47 +00:00
|
|
|
*
|
|
|
|
* @ingroup forms
|
2012-01-03 04:36:15 +00:00
|
|
|
* @see comment_admin()
|
2008-01-08 10:35:43 +00:00
|
|
|
* @see comment_multiple_delete_confirm_submit()
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
2009-09-18 00:12:48 +00:00
|
|
|
function comment_multiple_delete_confirm($form, &$form_state) {
|
2009-03-14 20:13:27 +00:00
|
|
|
$edit = $form_state['input'];
|
2007-10-31 17:50:47 +00:00
|
|
|
|
2008-05-14 13:12:41 +00:00
|
|
|
$form['comments'] = array(
|
|
|
|
'#prefix' => '<ul>',
|
|
|
|
'#suffix' => '</ul>',
|
|
|
|
'#tree' => TRUE,
|
|
|
|
);
|
|
|
|
// array_filter() returns only elements with actual values.
|
2007-10-31 17:50:47 +00:00
|
|
|
$comment_counter = 0;
|
|
|
|
foreach (array_filter($edit['comments']) as $cid => $value) {
|
2008-04-10 10:13:57 +00:00
|
|
|
$comment = comment_load($cid);
|
2007-10-31 17:50:47 +00:00
|
|
|
if (is_object($comment) && is_numeric($comment->cid)) {
|
2009-07-28 19:18:08 +00:00
|
|
|
$subject = db_query('SELECT subject FROM {comment} WHERE cid = :cid', array(':cid' => $cid))->fetchField();
|
2008-04-14 17:48:46 +00:00
|
|
|
$form['comments'][$cid] = array('#type' => 'hidden', '#value' => $cid, '#prefix' => '<li>', '#suffix' => check_plain($subject) . '</li>');
|
2007-10-31 17:50:47 +00:00
|
|
|
$comment_counter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
|
|
|
|
|
|
|
if (!$comment_counter) {
|
2008-05-14 13:12:41 +00:00
|
|
|
drupal_set_message(t('There do not appear to be any comments to delete, or your selected comment was deleted by another administrator.'));
|
2009-07-30 19:24:21 +00:00
|
|
|
drupal_goto('admin/content/comment');
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return confirm_form($form,
|
|
|
|
t('Are you sure you want to delete these comments and all their children?'),
|
2009-07-30 19:24:21 +00:00
|
|
|
'admin/content/comment', t('This action cannot be undone.'),
|
2007-10-31 17:50:47 +00:00
|
|
|
t('Delete comments'), t('Cancel'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form submission handler for comment_multiple_delete_confirm().
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
|
|
|
function comment_multiple_delete_confirm_submit($form, &$form_state) {
|
|
|
|
if ($form_state['values']['confirm']) {
|
2009-07-31 19:44:21 +00:00
|
|
|
comment_delete_multiple(array_keys($form_state['values']['comments']));
|
2007-10-31 17:50:47 +00:00
|
|
|
cache_clear_all();
|
2009-07-31 19:44:21 +00:00
|
|
|
$count = count($form_state['values']['comments']);
|
|
|
|
watchdog('content', 'Deleted @count comments.', array('@count' => $count));
|
2010-12-09 02:16:21 +00:00
|
|
|
drupal_set_message(format_plural($count, 'Deleted 1 comment.', 'Deleted @count comments.'));
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
2009-07-30 19:24:21 +00:00
|
|
|
$form_state['redirect'] = 'admin/content/comment';
|
2007-10-31 17:50:47 +00:00
|
|
|
}
|
|
|
|
|
2010-02-22 15:38:52 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Page callback: Shows a confirmation page for comment deletions.
|
|
|
|
*
|
|
|
|
* @param $cid
|
|
|
|
* The ID of the comment that is about to be deleted.
|
|
|
|
*
|
|
|
|
* @see comment_menu()
|
|
|
|
* @see comment_confirm_delete()
|
2010-02-22 15:38:52 +00:00
|
|
|
*/
|
|
|
|
function comment_confirm_delete_page($cid) {
|
|
|
|
if ($comment = comment_load($cid)) {
|
|
|
|
return drupal_get_form('comment_confirm_delete', $comment);
|
|
|
|
}
|
2012-06-02 19:41:40 +00:00
|
|
|
throw new NotFoundHttpException();
|
2010-02-22 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-31 17:50:47 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form constructor for the confirmation form for comment deletion.
|
|
|
|
*
|
2012-05-03 05:49:41 +00:00
|
|
|
* @param Drupal\comment\Comment $comment
|
2012-01-03 04:36:15 +00:00
|
|
|
* The comment that is about to be deleted.
|
2007-10-31 17:50:47 +00:00
|
|
|
*
|
|
|
|
* @ingroup forms
|
2012-01-03 04:36:15 +00:00
|
|
|
* @see comment_confirm_delete_page()
|
2008-01-08 10:35:43 +00:00
|
|
|
* @see comment_confirm_delete_submit()
|
2012-01-03 04:36:15 +00:00
|
|
|
* @see confirm_form()
|
2007-10-31 17:50:47 +00:00
|
|
|
*/
|
2012-04-13 08:01:13 +00:00
|
|
|
function comment_confirm_delete($form, &$form_state, Comment $comment) {
|
2007-10-31 17:50:47 +00:00
|
|
|
$form['#comment'] = $comment;
|
2010-02-17 05:42:42 +00:00
|
|
|
// Always provide entity id in the same form key as in the entity edit form.
|
|
|
|
$form['cid'] = array('#type' => 'value', '#value' => $comment->cid);
|
2007-10-31 17:50:47 +00:00
|
|
|
return confirm_form(
|
|
|
|
$form,
|
|
|
|
t('Are you sure you want to delete the comment %title?', array('%title' => $comment->subject)),
|
2008-04-14 17:48:46 +00:00
|
|
|
'node/' . $comment->nid,
|
2007-10-31 17:50:47 +00:00
|
|
|
t('Any replies to this comment will be lost. This action cannot be undone.'),
|
|
|
|
t('Delete'),
|
|
|
|
t('Cancel'),
|
|
|
|
'comment_confirm_delete');
|
|
|
|
}
|
|
|
|
|
2007-12-16 21:01:45 +00:00
|
|
|
/**
|
2012-01-03 04:36:15 +00:00
|
|
|
* Form submission handler for comment_confirm_delete().
|
2007-12-16 21:01:45 +00:00
|
|
|
*/
|
2007-10-31 17:50:47 +00:00
|
|
|
function comment_confirm_delete_submit($form, &$form_state) {
|
|
|
|
$comment = $form['#comment'];
|
2008-05-14 13:12:41 +00:00
|
|
|
// Delete the comment and its replies.
|
2009-07-31 19:44:21 +00:00
|
|
|
comment_delete($comment->cid);
|
|
|
|
drupal_set_message(t('The comment and all its replies have been deleted.'));
|
2010-09-11 01:47:14 +00:00
|
|
|
watchdog('content', 'Deleted comment @cid and its replies.', array('@cid' => $comment->cid));
|
2007-10-31 17:50:47 +00:00
|
|
|
// Clear the cache so an anonymous user sees that his comment was deleted.
|
|
|
|
cache_clear_all();
|
|
|
|
|
|
|
|
$form_state['redirect'] = "node/$comment->nid";
|
|
|
|
}
|