[phpmix-cvs] drupal/modules/akismet akismet.module,1.18,1.19
Status: Pre-Alpha
Brought to you by:
markus_petrux
From: <php...@li...> - 2006-06-12 00:48:50
|
Update of /cvsroot/phpmix/drupal/modules/akismet In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30108 Modified Files: akismet.module Log Message: Added mail functionality to notification engine. Index: akismet.module =================================================================== RCS file: /cvsroot/phpmix/drupal/modules/akismet/akismet.module,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** akismet.module 10 Jun 2006 19:01:04 -0000 1.18 --- akismet.module 12 Jun 2006 00:48:43 -0000 1.19 *************** *** 4,11 **** /********************************************************************************\ - @TODO: Implement an opt-in option for administrators to e-mail them when content - is posted (maybe for all posts, or for detected spam or nothing). - @TODO: Review the help page. @TODO: Enhance moderation queue with a form to allow multiple operations. \********************************************************************************/ --- 4,9 ---- /********************************************************************************\ @TODO: Enhance moderation queue with a form to allow multiple operations. + @TODO: Review the help page. \********************************************************************************/ *************** *** 453,698 **** /** - * Implementation of hook_user(). - */ - function akismet_user($op, &$edit, &$account, $category = NULL) { - $moderator_email_for_options = array( - 'all' => t('All new (or updated) content'), - 'approval' => t('Only content needing approval'), - 'never' => t('Never') - ); - switch ($op) { - case 'form': - if ($category == 'account' && variable_get('akismet_email_enabled', 1)) { - $moderator_types = akismet_get_moderator_types($account); - $moderator_types_count = count($moderator_types); - if ($moderator_types_count > 0) { - $form = array(); - $form['akismet_moderator'] = array( - '#type' => 'fieldset', '#title' => t('Akismet moderator settings'), - '#weight' => 5, - '#collapsible' => TRUE, '#collapsed' => FALSE, - '#description' => t('You are currently moderator for the following content types: %types.', array('%types' => implode(', ', $moderator_types))) - ); - $form['akismet_moderator']['akismet_moderator_email_for'] = array( - '#type' => 'radios', '#title' => t('Send me e-mails for'), - '#options' => $moderator_email_for_options, - '#default_value' => (isset($moderator_email_for_options[$edit['akismet_moderator_email_for']]) ? $edit['akismet_moderator_email_for'] : 'approval') - ); - return $form; - } - } - break; - case 'load': - $moderator_types = akismet_get_moderator_types($account); - $moderator_types_count = count($moderator_types); - if ($moderator_types_count > 0) { - $moderator_data = db_fetch_object(db_query('SELECT * FROM {akismet_moderator} WHERE uid = %d', $account->uid)); - $account->akismet_moderator_email_for = (isset($moderator_data->email_for) && isset($moderator_types[$moderator_data->email_for]) ? $moderator_data->email_for : 'approval'); - } - break; - case 'insert': - case 'update': - $moderator_types = akismet_get_moderator_types($account); - $moderator_types_count = count($moderator_types); - if ($moderator_types_count > 0 && isset($edit['akismet_moderator_email_for'])) { - if (!isset($moderator_email_for_options[$edit['akismet_moderator_email_for']])) { - $edit['akismet_moderator_email_for'] = 'approval'; - } - db_query('UPDATE {akismet_moderator} SET email_for = \'%s\' WHERE uid = %d', $edit['akismet_moderator_email_for'], $account->uid); - if (!db_affected_rows()) { - db_query('INSERT INTO {akismet_moderator} (uid, email_for) VALUES (%d, \'%s\')', $account->uid, $edit['akismet_moderator_email_for']); - } - $edit['akismet_moderator_email_for'] = NULL; - break; - } - // Fall through, to remove possible garbage. - case 'delete': - db_query('DELETE FROM {akismet_moderator} WHERE uid = %d', $account->uid); - break; - } - } - - function akismet_notify_users() { - // Proceed only if e-mail notifications are enabled. - if (!variable_get('akismet_email_enabled', 0)) { - return; - } - // @TODO: - } - - /** - * Get the current akismet spam counter. - * - * @return integer - */ - function akismet_get_spam_counter() { - return (int)variable_get('akismet_counter_spam', 0); - } - - /** - * Format the 'Counting since' date. - * - * @return string Counting since date formatted according to module settings. - */ - function akismet_get_counting_since() { - $since = variable_get('akismet_counter_since', array('day' => date('j'), 'month' => date('n'), 'year' => date('Y'))); - $format = variable_get('akismet_counter_date_format', 'F j, Y'); - $replace = array( - 'd' => sprintf('%02d', $since['day']), - 'j' => $since['day'], - 'm' => sprintf('%02d', $since['month']), - 'M' => format_date(gmmktime(0, 0, 0, $since['month'], 2, 1970), 'custom', 'M', 0), - 'F' => format_date(gmmktime(0, 0, 0, $since['month'], 2, 1970), 'custom', 'F', 0), - 'Y' => $since['year'] - ); - return strtr($format, $replace); - } - - /** - * Implementation of hook_block(). - */ - function akismet_block($op = 'list', $delta = 0, $edit = array()) { - static $block_fields = FALSE; - if (!$block_fields) { - $block_fields = array( - 'type' => array( - '#type' => 'radios', - '#title' => t('Display counter as'), - '#options' => array('image' => t('Image'), 'text' => t('Text')), - '#default_value' => 'image' - ), - 'sitename' => array( - '#type' => 'radios', - '#title' => t('Show site name'), - '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), - '#default_value' => '1' - ), - 'newwin' => array( - '#type' => 'radios', - '#title' => t('Open Akismet link in new window'), - '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), - '#default_value' => '1' - ) - ); - } - switch ($op) { - case 'list': - $blocks_counter = variable_get('akismet_blocks_counter', 1); - $blocks = array(); - for ($i = 0; $i < $blocks_counter; $i++) { - $blocks[] = array('info' => t('Akismet spam counter (block:%count)', array('%count' => ($i+1)))); - } - return $blocks; - - case 'configure': - if ($delta < 0 || $delta >= variable_get('akismet_blocks_counter', 1)) { - drupal_set_message(t('Oops! You are requesting a non-existing block, changes will not be saved.'), 'error'); - break; - } - $form = array(); - $form['description'] = array( - '#type' => 'markup', - '#value' => '<div class="description"><p>'. t('These options allow to customize the look of this <em>akismet spam counter</em> block.') .'</p></div>' - ); - $block_settings = variable_get('akismet_blocks_'. $delta, FALSE); - foreach ($block_fields as $field_key => $field_info) { - $field_name = 'akismet_blocks_'. $delta .'_'. $field_key; - $form[$field_name] = array(); - foreach ($field_info as $key => $value) { - $form[$field_name][$key] = $value; - } - if ($block_settings && isset($block_settings[$field_key])) { - $form[$field_name]['#default_value'] = $block_settings[$field_key]; - } - } - return $form; - - case 'save': - if ($delta < 0 || $delta >= variable_get('akismet_blocks_counter', 1)) { - drupal_set_message(t('Oops! You have requested a non-existing block, changes were not saved.'), 'error'); - break; - } - $block_settings = array(); - foreach ($block_fields as $field_key => $field_info) { - $field_name = 'akismet_blocks_'. $delta .'_'. $field_key; - $block_settings[$field_key] = $edit[$field_name]; - } - variable_set('akismet_blocks_'. $delta, $block_settings); - break; - - case 'view': - if ($delta >= 0 && $delta < variable_get('akismet_blocks_counter', 1)) { - $block_settings = variable_get('akismet_blocks_'. $delta, FALSE); - if (!$block_settings) { - $block_settings = array(); - } - - $block_args = array( - 'content' => '', // Built below. - 'counter' => akismet_get_spam_counter(), - 'since' => akismet_get_counting_since(), - 'text' => '', // Built below. - 'image' => '', // Built below. - 'block' => array('delta' => $delta) // completed below with current block settings. - ); - - foreach ($block_fields as $field_key => $field_info) { - if (!isset($block_settings[$field_key])) { - $block_settings[$field_key] = $field_info['#default_value']; - } - $block_args['block'][$field_key] = $block_settings[$field_key]; - } - - $target = ($block_settings['newwin'] ? ' target="_blank"' : ''); - $block_args['text'] = array( - 'plain' => array( - 'short' => check_plain(t('Proudly protected by Akismet, %count spam caught since %since.', array('%site_name' => variable_get('site_name', 'drupal'), '%count' => $block_args['counter'], '%since' => $block_args['since']))), - 'long' => check_plain(t('%site_name is proudly protected by Akismet, %count spam caught since %since.', array('%site_name' => variable_get('site_name', 'drupal'), '%count' => $block_args['counter'], '%since' => $block_args['since']))) - ), - 'html' => array( - 'short' => t('Proudly protected by <a href="%akismet"%target>Akismet</a>, %count spam caught since %since', array('%site_name' => variable_get('site_name', 'drupal'), '%akismet' => 'http://akismet.com', '%target' => $target, '%count' => $block_args['counter'], '%since' => $block_args['since'])), - 'long' => t('%site_name is proudly protected by <a href="%akismet"%target>Akismet</a>, %count spam caught since %since', array('%site_name' => variable_get('site_name', 'drupal'), '%akismet' => 'http://akismet.com', '%target' => $target, '%count' => $block_args['counter'], '%since' => $block_args['since'])) - ) - ); - $text_version = ($block_settings['sitename'] ? 'long' : 'short'); - $title_text = $block_args['text']['plain'][$text_version]; - $image_url = base_path() . drupal_get_path('module', 'akismet') .'/akismet.gif'; - $block_args['image'] = '<img src="'. $image_url .'" title="'. $title_text .'" alt="'. $title_text .'" />'; - - if ($block_settings['type'] == 'image') { - $block_args['content'] = '<a href="http://akismet.com" title="'. $title_text .'"'. $target .'>'. $block_args['image'] .'</a>'; - } - else { - $block_args['content'] = $block_args['text']['html'][$text_version]; - } - - $block = array(); - $block['subject'] = t('Akismet spam counter'); - $block['content'] = theme('akismet_counter_block', $block_args); - return $block; - } - break; - } - } - - /** - * Allow themes customize the content of the akismet spam counter block. - * - * @param array arguments where each element is: - * content: String; the completely built block content. - * counter: Integer; the current spam counter. - * since : String; the formatted 'counting since' date. - * text : Array; with 2 subarrays defined as follows: - * plain: Array with 2 elements ('short' and 'long'). - * html : Array with 2 elements ('short' and 'long'). - * image : String; the completely built IMG tag. - * block : Array; Block settings. - * @return string The content of the block. - */ - function theme_akismet_counter_block($args) { - return $args['content']; - } - - /** * Implementation of hook_menu(). */ --- 451,454 ---- *************** *** 883,886 **** --- 639,643 ---- // If Akismet connections are not enabled, we have nothing else to do here. if (!variable_get('akismet_connection_enabled', 1)) { + akismet_notify_moderators('node', $node, ($node->status ? TRUE : FALSE), FALSE); break; } *************** *** 888,891 **** --- 645,649 ---- // Also quit asap, if current user has administration permission. if (akismet_is_spam_moderator($node->type)) { + akismet_notify_moderators('node', $node, ($node->status ? TRUE : FALSE), FALSE); break; } *************** *** 894,897 **** --- 652,656 ---- $check_nodetypes = variable_get('akismet_check_nodetypes', NULL); if (!is_array($check_nodetypes) || !isset($check_nodetypes[$node->type]) || !$check_nodetypes[$node->type]) { + akismet_notify_moderators('node', $node, ($node->status ? TRUE : FALSE), FALSE); break; } *************** *** 899,903 **** // Ok, let's send a query to Akismet. $akismet_api_result = akismet_api_cmd_comment_check(akismet_prepare_comment_data('node', $node)); ! if ($akismet_api_result != AKISMET_API_RESULT_IS_HAM) { if ($akismet_api_result == AKISMET_API_RESULT_IS_SPAM) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. --- 658,665 ---- // Ok, let's send a query to Akismet. $akismet_api_result = akismet_api_cmd_comment_check(akismet_prepare_comment_data('node', $node)); ! if ($akismet_api_result == AKISMET_API_RESULT_IS_HAM) { ! akismet_notify_moderators('node', $node, ($node->status ? TRUE : FALSE), FALSE); ! } ! else { if ($akismet_api_result == AKISMET_API_RESULT_IS_SPAM) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. *************** *** 905,908 **** --- 667,675 ---- // Increment Akismet spam counter variable_set('akismet_counter_spam', akismet_get_spam_counter() + 1); + + akismet_notify_moderators('node', $node, FALSE, TRUE); + } + else { + akismet_notify_moderators('node', $node, FALSE, FALSE); } *************** *** 935,940 **** */ function akismet_comment(&$comment, $op) { ! if ($op == 'delete') { ! db_query('DELETE FROM {akismet_spam_marks} WHERE content_type = \'comment\' AND content_id = %d', $comment->cid); } } --- 702,715 ---- */ function akismet_comment(&$comment, $op) { ! switch ($op) { ! case 'insert': ! case 'update': ! if (!variable_get('akismet_check_comments', 0) || akismet_is_spam_moderator('comments')) { ! akismet_notify_moderators('comment', $comment, ($comment->status == COMMENT_PUBLISHED ? TRUE : FALSE), FALSE); ! } ! break; ! case 'delete': ! db_query('DELETE FROM {akismet_spam_marks} WHERE content_type = \'comment\' AND content_id = %d', $comment->cid); ! break; } } *************** *** 1007,1011 **** if ($comment) { $akismet_api_result = akismet_api_cmd_comment_check(akismet_prepare_comment_data('comment', $comment)); ! if ($akismet_api_result != AKISMET_API_RESULT_IS_HAM) { if ($akismet_api_result == AKISMET_API_RESULT_IS_SPAM) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. --- 782,789 ---- if ($comment) { $akismet_api_result = akismet_api_cmd_comment_check(akismet_prepare_comment_data('comment', $comment)); ! if ($akismet_api_result == AKISMET_API_RESULT_IS_HAM) { ! akismet_notify_moderators('comment', $comment, ($comment->status == COMMENT_PUBLISHED ? TRUE : FALSE), FALSE); ! } ! else { if ($akismet_api_result == AKISMET_API_RESULT_IS_SPAM) { // Oops! Akismet is telling us we got spammed, let's mark the comment as such. *************** *** 1013,1016 **** --- 791,799 ---- // Increment Akismet spam counter variable_set('akismet_counter_spam', akismet_get_spam_counter() + 1); + + akismet_notify_moderators('comment', $comment, FALSE, TRUE); + } + else { + akismet_notify_moderators('comment', $comment, FALSE, FALSE); } *************** *** 1237,1240 **** --- 1020,1380 ---- /** + * Notify moderators of new/updated content, only content needing approval or nothing at all. + * + * @param string Content type; can be either 'node' or 'comment'. + * @param object Content object. + * @param boolean TRUE if content is in published status. + * @param boolean TRUE if content has been marked as spam. + */ + function akismet_notify_moderators($content_type, $content, $is_published, $is_spam) { + global $base_url; + + // Proceed only if e-mail notifications are enabled. + if (!variable_get('akismet_email_enabled', 0)) { + return; + } + + // Make sure we have an object. + $content = (object)$content; + + // Compute the related moderator permission. + if ($content_type == 'comment') { + $moderator_permission = 'moderate spam in comments'; + $administer_permission = 'administer comments'; + } + else { + $moderator_types = akismet_get_moderator_types($account); + $moderator_permission = 'moderate spam in nodes of type '. $moderator_types[$content->type]; + $administer_permission = 'administer nodes'; + } + + // Obtain list of moderators of the specified content type. + $sql = 'SELECT u.uid, u.name, u.mail, m.email_for'. + ' FROM {permission} p'. + ' INNER JOIN {users_roles} r ON r.rid = p.rid'. + ' INNER JOIN {users} u ON u.uid = r.uid OR u.uid = 1'. + ' LEFT JOIN {akismet_moderator} m ON m.uid = u.uid'. + ' WHERE p.perm LIKE \'%%%s%%\''. + ' OR p.perm LIKE \'%%%s%%\''; + $result = db_query($sql, $moderator_permission, $administer_permission); + $moderators = array(); + while ($u = db_fetch_object($result)) { + $moderators[$u->uid] = array( + 'name' => $u->name, + 'email_to' => $u->mail, + 'email_for' => (!is_null($u->email_for) ? $u->email_for : 'approval') + ); + } + + // Extract unique email addresses and ignore those who have requested to not get e-mail notifications. + $unique_emails = array(); + foreach ($moderators as $uid => $moderator) { + if ($moderator['email_for'] == 'all' || ($moderator['email_for'] == 'approval' && !$is_published)) { + if (!isset($unique_emails[$moderator['email_to']])) { + $unique_emails[$moderator['email_to']] = $uid; + } + } + } + if (count($unique_emails) <= 0) { + return; + } + + // If this is about a comment, try to load the node. + // Also, prepare arguments for notification message. + $site_name = variable_get('site_name', t('Drupal')); + if ($content_type == 'comment') { + if (!($node = akismet_content_load('node', $content->nid))) { + watchdog('akismet', t('An error has ocurred while trying to notify moderators about a comment. The associated node could not be loaded.'), WATCHDOG_NOTICE, l(t('view'), 'node/'. $content->nid, NULL, NULL, 'comment-'. $content->cid)); + return; + } + $message_args = array( + '%title-label' => t('Subject'), + '%content-title' => check_plain($content->subject), + '%content-type' => t('comment'), + '%content-link' => url('node/'. $content->nid, NULL, 'comment-'. $content->cid, TRUE) + ); + $message_title = t('[%site-name] New comment posted on \'%title\'', array('%site-name' => $site_name, '%title' => check_plain($node->title))); + } + else { + $message_args = array( + '%title-label' => t('Title'), + '%content-title' => check_plain($content->title), + '%content-type' => check_plain($moderator_types[$content->type]), + '%content-link' => url('node/'. $content->nid, NULL, NULL, TRUE) + ); + $message_title = t('[%site-name] New %type \'%title\'', array('%site-name' => $site_name, '%type' => check_plain($moderator_types[$content->type]), '%title' => check_plain($content->title))); + } + $message_args['%content-status'] = ($is_published ? t('published') : t('published')) . ($is_spam ? ' ('. t('marked as spam') .')' : ''); + $message_args['%site-name'] = check_plain($site_name); + $message_args['%site-link'] = $base_url . base_path(); + + $message_body = t(<<<EOT + Hello %user-name, + + You can use the following information to review this %content-type: + + %title-label: %content-title + URL: %content-link + Status: %content-status + + Please, do not reply to this e-mail. It is an automated notification you are receiving because you are a moderator at %site-name. If you no longer wish to receive such notifications, you can change your moderator settings in your user profile. + + Thank you + + %site-link + EOT + , $message_args); + + // Log the notification to watchdog. + watchdog('akismet', t('Trying to notify the following recipients: %emails', array('%emails' => implode(', ', array_keys($unique_emails))))); + + // Send e-mails. + $site_mail = variable_get('site_mail', ini_get('sendmail_from')); + $headers = "From: $site_mail\nReply-to: $site_mail\nX-Mailer: Drupal\nReturn-path: $site_mail\nErrors-to: $site_mail"; + foreach ($unique_emails as $email_to => $uid) { + user_mail( + $email_to, + $message_title, + str_replace('%user-name', check_plain($moderators[$uid]['name']), $message_body), + $headers + ); + } + } + + /** + * Implementation of hook_user(). + */ + function akismet_user($op, &$edit, &$account, $category = NULL) { + $moderator_email_for_options = array( + 'all' => t('All new (or updated) content'), + 'approval' => t('Only content needing approval'), + 'never' => t('Never') + ); + switch ($op) { + case 'form': + if ($category == 'account' && variable_get('akismet_email_enabled', 1)) { + $moderator_types = akismet_get_moderator_types($account); + $moderator_types_count = count($moderator_types); + if ($moderator_types_count > 0) { + $form = array(); + $form['akismet_moderator'] = array( + '#type' => 'fieldset', '#title' => t('Akismet moderator settings'), + '#weight' => 5, + '#collapsible' => TRUE, '#collapsed' => FALSE, + '#description' => t('You are currently moderator for the following content types: %types.', array('%types' => implode(', ', $moderator_types))) + ); + $form['akismet_moderator']['akismet_moderator_email_for'] = array( + '#type' => 'radios', '#title' => t('Send me e-mails for'), + '#options' => $moderator_email_for_options, + '#default_value' => (isset($moderator_email_for_options[$edit['akismet_moderator_email_for']]) ? $edit['akismet_moderator_email_for'] : 'approval') + ); + return $form; + } + } + break; + case 'load': + $moderator_types = akismet_get_moderator_types($account); + $moderator_types_count = count($moderator_types); + if ($moderator_types_count > 0) { + $moderator_data = db_fetch_object(db_query('SELECT * FROM {akismet_moderator} WHERE uid = %d', $account->uid)); + $account->akismet_moderator_email_for = (isset($moderator_data->email_for) && isset($moderator_email_for_options[$moderator_data->email_for]) ? $moderator_data->email_for : 'approval'); + } + break; + case 'insert': + case 'update': + $moderator_types = akismet_get_moderator_types($account); + $moderator_types_count = count($moderator_types); + if ($moderator_types_count > 0 && isset($edit['akismet_moderator_email_for'])) { + if (!isset($moderator_email_for_options[$edit['akismet_moderator_email_for']])) { + $edit['akismet_moderator_email_for'] = 'approval'; + } + db_query('UPDATE {akismet_moderator} SET email_for = \'%s\' WHERE uid = %d', $edit['akismet_moderator_email_for'], $account->uid); + if (!db_affected_rows()) { + db_query('INSERT INTO {akismet_moderator} (uid, email_for) VALUES (%d, \'%s\')', $account->uid, $edit['akismet_moderator_email_for']); + } + $edit['akismet_moderator_email_for'] = NULL; + break; + } + // Fall through, to remove possible garbage. + case 'delete': + db_query('DELETE FROM {akismet_moderator} WHERE uid = %d', $account->uid); + break; + } + } + + /** + * Get the current akismet spam counter. + * + * @return integer + */ + function akismet_get_spam_counter() { + return (int)variable_get('akismet_counter_spam', 0); + } + + /** + * Format the 'Counting since' date. + * + * @return string Counting since date formatted according to module settings. + */ + function akismet_get_counting_since() { + $since = variable_get('akismet_counter_since', array('day' => date('j'), 'month' => date('n'), 'year' => date('Y'))); + $format = variable_get('akismet_counter_date_format', 'F j, Y'); + $replace = array( + 'd' => sprintf('%02d', $since['day']), + 'j' => $since['day'], + 'm' => sprintf('%02d', $since['month']), + 'M' => format_date(gmmktime(0, 0, 0, $since['month'], 2, 1970), 'custom', 'M', 0), + 'F' => format_date(gmmktime(0, 0, 0, $since['month'], 2, 1970), 'custom', 'F', 0), + 'Y' => $since['year'] + ); + return strtr($format, $replace); + } + + /** + * Implementation of hook_block(). + */ + function akismet_block($op = 'list', $delta = 0, $edit = array()) { + static $block_fields = FALSE; + if (!$block_fields) { + $block_fields = array( + 'type' => array( + '#type' => 'radios', + '#title' => t('Display counter as'), + '#options' => array('image' => t('Image'), 'text' => t('Text')), + '#default_value' => 'image' + ), + 'sitename' => array( + '#type' => 'radios', + '#title' => t('Show site name'), + '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), + '#default_value' => '1' + ), + 'newwin' => array( + '#type' => 'radios', + '#title' => t('Open Akismet link in new window'), + '#options' => array('1' => t('Enabled'), '0' => t('Disabled')), + '#default_value' => '1' + ) + ); + } + switch ($op) { + case 'list': + $blocks_counter = variable_get('akismet_blocks_counter', 1); + $blocks = array(); + for ($i = 0; $i < $blocks_counter; $i++) { + $blocks[] = array('info' => t('Akismet spam counter (block:%count)', array('%count' => ($i+1)))); + } + return $blocks; + + case 'configure': + if ($delta < 0 || $delta >= variable_get('akismet_blocks_counter', 1)) { + drupal_set_message(t('Oops! You are requesting a non-existing block, changes will not be saved.'), 'error'); + break; + } + $form = array(); + $form['description'] = array( + '#type' => 'markup', + '#value' => '<div class="description"><p>'. t('These options allow to customize the look of this <em>akismet spam counter</em> block.') .'</p></div>' + ); + $block_settings = variable_get('akismet_blocks_'. $delta, FALSE); + foreach ($block_fields as $field_key => $field_info) { + $field_name = 'akismet_blocks_'. $delta .'_'. $field_key; + $form[$field_name] = array(); + foreach ($field_info as $key => $value) { + $form[$field_name][$key] = $value; + } + if ($block_settings && isset($block_settings[$field_key])) { + $form[$field_name]['#default_value'] = $block_settings[$field_key]; + } + } + return $form; + + case 'save': + if ($delta < 0 || $delta >= variable_get('akismet_blocks_counter', 1)) { + drupal_set_message(t('Oops! You have requested a non-existing block, changes were not saved.'), 'error'); + break; + } + $block_settings = array(); + foreach ($block_fields as $field_key => $field_info) { + $field_name = 'akismet_blocks_'. $delta .'_'. $field_key; + $block_settings[$field_key] = $edit[$field_name]; + } + variable_set('akismet_blocks_'. $delta, $block_settings); + break; + + case 'view': + if ($delta >= 0 && $delta < variable_get('akismet_blocks_counter', 1)) { + $block_settings = variable_get('akismet_blocks_'. $delta, FALSE); + if (!$block_settings) { + $block_settings = array(); + } + + $block_args = array( + 'content' => '', // Built below. + 'counter' => akismet_get_spam_counter(), + 'since' => akismet_get_counting_since(), + 'text' => '', // Built below. + 'image' => '', // Built below. + 'block' => array('delta' => $delta) // completed below with current block settings. + ); + + foreach ($block_fields as $field_key => $field_info) { + if (!isset($block_settings[$field_key])) { + $block_settings[$field_key] = $field_info['#default_value']; + } + $block_args['block'][$field_key] = $block_settings[$field_key]; + } + + $target = ($block_settings['newwin'] ? ' target="_blank"' : ''); + $block_args['text'] = array( + 'plain' => array( + 'short' => check_plain(t('Proudly protected by Akismet, %count spam caught since %since.', array('%site_name' => variable_get('site_name', 'drupal'), '%count' => $block_args['counter'], '%since' => $block_args['since']))), + 'long' => check_plain(t('%site_name is proudly protected by Akismet, %count spam caught since %since.', array('%site_name' => variable_get('site_name', 'drupal'), '%count' => $block_args['counter'], '%since' => $block_args['since']))) + ), + 'html' => array( + 'short' => t('Proudly protected by <a href="%akismet"%target>Akismet</a>, %count spam caught since %since', array('%site_name' => variable_get('site_name', 'drupal'), '%akismet' => 'http://akismet.com', '%target' => $target, '%count' => $block_args['counter'], '%since' => $block_args['since'])), + 'long' => t('%site_name is proudly protected by <a href="%akismet"%target>Akismet</a>, %count spam caught since %since', array('%site_name' => variable_get('site_name', 'drupal'), '%akismet' => 'http://akismet.com', '%target' => $target, '%count' => $block_args['counter'], '%since' => $block_args['since'])) + ) + ); + $text_version = ($block_settings['sitename'] ? 'long' : 'short'); + $title_text = $block_args['text']['plain'][$text_version]; + $image_url = base_path() . drupal_get_path('module', 'akismet') .'/akismet.gif'; + $block_args['image'] = '<img src="'. $image_url .'" title="'. $title_text .'" alt="'. $title_text .'" />'; + + if ($block_settings['type'] == 'image') { + $block_args['content'] = '<a href="http://akismet.com" title="'. $title_text .'"'. $target .'>'. $block_args['image'] .'</a>'; + } + else { + $block_args['content'] = $block_args['text']['html'][$text_version]; + } + + $block = array(); + $block['subject'] = t('Akismet spam counter'); + $block['content'] = theme('akismet_counter_block', $block_args); + return $block; + } + break; + } + } + + /** + * Allow themes customize the content of the akismet spam counter block. + * + * @param array arguments where each element is: + * content: String; the completely built block content. + * counter: Integer; the current spam counter. + * since : String; the formatted 'counting since' date. + * text : Array; with 2 subarrays defined as follows: + * plain: Array with 2 elements ('short' and 'long'). + * html : Array with 2 elements ('short' and 'long'). + * image : String; the completely built IMG tag. + * block : Array; Block settings. + * @return string The content of the block. + */ + function theme_akismet_counter_block($args) { + return $args['content']; + } + + /** * Load a node or a comment. * |