function simplenews_nodeapi in Simplenews 6
Same name and namespace in other branches
- 5 simplenews.module \simplenews_nodeapi()
- 6.2 simplenews.module \simplenews_nodeapi()
Implementation of hook_nodeapi().
File
- ./
simplenews.module, line 328 - Simplnews node handling, sent email, newsletter block and general hooks
Code
function simplenews_nodeapi(&$node, $op, $teaser, $page) {
global $user;
// Operate only on node types set in 'simplenews_content_types' variable.
if (!in_array($node->type, variable_get('simplenews_content_types', array(
'simplenews',
)))) {
return;
}
switch ($op) {
case 'alter':
// Don't replace the tokens when node alter is called by simplenews_mail.
if (!isset($node->simplenews_mail)) {
global $language;
$context['node'] = $node;
$variables = simplenews_mail_tokens($user, $context, $language);
if (isset($node->body)) {
$node->body = strtr($node->body, $variables);
}
if (isset($node->teaser)) {
$node->teaser = strtr($node->teaser, $variables);
}
}
break;
case 'validate':
$vid = variable_get('simplenews_vid', '');
if (!isset($node->taxonomy[$vid]) || empty($node->taxonomy[$vid]) || simplenews_validate_taxonomy($node->taxonomy) == FALSE) {
form_set_error('taxonomy', t('No newsletter term is selected, the newsletter taxonomy term is probably not configured correctly.<br /> Check and <strong>save</strong> the <a href="@settings">Simplenews general settings</a>.', array(
'%name' => $vocabulary->name,
'@settings' => url('admin/settings/simplenews/general'),
)));
}
elseif (isset($node->simplenews['send']) && $node->simplenews['send'] == SIMPLENEWS_COMMAND_SEND_TEST) {
if (!empty($node->simplenews['test_address'])) {
$mails = explode(',', $node->simplenews['test_address']);
foreach ($mails as $mail) {
$mail = trim($mail);
if ($mail == '') {
form_set_error('simplenews][test_address', t('Test email address is empty.'));
}
elseif (!valid_email_address($mail)) {
form_set_error('simplenews][test_address', t('Invalid email address %mail.', array(
'%mail' => $mail,
)));
}
}
}
else {
form_set_error('simplenews][test_address', t('Missing test email address.'));
}
}
break;
case 'presave':
$term = simplenews_validate_taxonomy($node->taxonomy);
$tid = is_array($term) ? array_values($term) : FALSE;
$node->simplenews['tid'] = $tid ? $tid[0] : 0;
break;
case 'insert':
case 'update':
// Flatten the simplenews 'advanced' settings array.
// This makes sure the $node array after insert/update is equal to the
// $node array after load.
$node->simplenews = _simplenews_flatten_array($node->simplenews);
$send_with_permission = $node->simplenews['send'] == SIMPLENEWS_COMMAND_SEND_NOW && user_access('send newsletter');
if ($op == 'insert') {
// Insert node
$s_status = $send_with_permission ? SIMPLENEWS_STATUS_SEND_PENDING : SIMPLENEWS_STATUS_SEND_NOT;
db_query("INSERT INTO {simplenews_newsletters} (nid, vid, tid, s_status, s_format, priority, receipt)\n VALUES (%d, %d, %d, %d, '%s', %d, %d)", $node->nid, $node->vid, $node->simplenews['tid'], $s_status, $node->simplenews['s_format'], $node->simplenews['priority'], $node->simplenews['receipt']);
}
else {
// Update node
if ($send_with_permission) {
db_query("UPDATE {simplenews_newsletters} SET vid = %d, tid = %d, s_status = %d, s_format = '%s', priority = %d, receipt = %d\n WHERE nid = %d", $node->vid, $node->simplenews['tid'], SIMPLENEWS_STATUS_SEND_PENDING, $node->simplenews['s_format'], $node->simplenews['priority'], $node->simplenews['receipt'], $node->nid);
}
else {
db_query("UPDATE {simplenews_newsletters} SET tid = %d, s_format = '%s', priority = %d, receipt = %d\n WHERE nid = %d", $node->simplenews['tid'], $node->simplenews['s_format'], $node->simplenews['priority'], $node->simplenews['receipt'], $node->nid);
}
}
// When this node is selected for translation all translation of this node
// will be sent too.
// All translated nodes will receive the same send states (priority, confirmation, format).
if (module_exists('translation') && translation_supported_type($node->type) && $send_with_permission) {
if ($translations = translation_node_get_translations($node->tnid)) {
foreach ($translations as $translation) {
db_query("UPDATE {simplenews_newsletters} SET s_status = %d, s_format = '%s', priority = %d, receipt = %d\n WHERE nid = %d", SIMPLENEWS_STATUS_SEND_PENDING, $node->simplenews['s_format'], $node->simplenews['priority'], $node->simplenews['receipt'], $translation->nid);
}
}
}
// Send newsletter or test newsletter
if ($send_with_permission) {
// Send newsletter to all subscribers
simplenews_send_node($node);
}
elseif ($node->simplenews['send'] == SIMPLENEWS_COMMAND_SEND_TEST) {
// Send test newsletter to test address(es)
simplenews_send_test($node);
}
break;
case 'delete':
$result = db_query('DELETE FROM {simplenews_newsletters} WHERE nid = %d', $node->nid);
if ($result) {
drupal_set_message(t('Newsletter %title was deleted.', array(
'%title' => $node->title,
)));
}
break;
case 'load':
$node->simplenews = db_fetch_array(db_query('SELECT * FROM {simplenews_newsletters} WHERE nid = %d', $node->nid));
break;
}
}