anonymous_publishing_lr.module in Anonymous Publishing 7
Hooks for the Anonymous Publishing LR module.
File
modules/lr/anonymous_publishing_lr.moduleView source
<?php
/**
* @file
* Hooks for the Anonymous Publishing LR module.
*/
/**
* Implements hook_menu().
*/
function anonymous_publishing_lr_menu() {
$items['user/%user/cap'] = array(
'title' => 'Claim anonymous posts',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'anonymous_publishing_lr_form',
1,
),
'access callback' => 'anonymous_publishing_lr_user_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'weight' => 1,
);
return $items;
}
/**
* Access callback to check for access to claim tab.
*/
function anonymous_publishing_lr_user_access($account = NULL) {
global $user;
$nodes = FALSE;
if ($user->uid == $account->uid) {
$mail = $account->mail;
$sql = "SELECT nid, email FROM {anonymous_publishing} WHERE email = :mail";
$nodes = db_query($sql, array(
':mail' => $mail,
))
->rowCount();
}
return $nodes;
}
/**
* Define a form.
*/
function anonymous_publishing_lr_form() {
$form['anonymous_publishing_lr_options'] = array(
'#type' => 'radios',
'#title' => t("What do you want to do with the contents you've previously published anonymously?"),
'#default_value' => 'leave',
'#options' => array(
'leave' => t('Leave it as it is.'),
'claim' => t('Claim it.'),
),
'#description' => t("If you check “Claim it” and press the “Submit”-button, you will become the owner and author of the content you've posted previously that is associated with your e-mail address."),
);
$form[' submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Handle post-validation form submission.
*/
function anonymous_publishing_lr_form_submit($form, &$form_state) {
global $user;
$claim = $form_state['values']['anonymous_publishing_lr_options'];
if ('leave' == $claim) {
drupal_goto('<front>');
}
$sql = "SELECT a.nid, a.cid, n.title FROM {anonymous_publishing} a JOIN {node} n ON a.nid = n.nid WHERE email = :mail ORDER BY n.created ASC";
$rows = db_query($sql, array(
':mail' => $user->mail,
))
->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
if ($row['cid']) {
$comment = comment_load($row['cid']);
$comment->uid = $user->uid;
$comment->name = $user->name;
$comment->mail = $user->mail;
comment_save($comment);
}
else {
$context = array(
'owner_uid' => $user->uid,
);
$node = node_load($row['nid']);
node_assign_owner_action($node, $context);
node_save($node);
}
db_delete('anonymous_publishing')
->condition('nid', $row['nid'], '=')
->condition('cid', $row['cid'], '=')
->execute();
}
drupal_goto('<front>');
}
Functions
Name | Description |
---|---|
anonymous_publishing_lr_form | Define a form. |
anonymous_publishing_lr_form_submit | Handle post-validation form submission. |
anonymous_publishing_lr_menu | Implements hook_menu(). |
anonymous_publishing_lr_user_access | Access callback to check for access to claim tab. |