'
',
'#weight' => 11
);
}
if ( simplenews_forms_included($form_id) ) {
//SIMPLE NEW FORM ELEMENTS (ADDS SUBSCRIPTION CHECKBOXES TO THE FORM FOR NEWSLETTERS
$options = array();
$defaults = array();
foreach (taxonomy_get_tree(_simplenews_get_vid()) as $newsletter) {
$options[$newsletter->tid] = $newsletter->name;
if(simplenews_user_is_subscribed($user->mail, $newsletter->tid)){$defaults[] = $newsletter->tid;}
}
$form['tid'] = array(
'#type' => 'checkboxes',
'#title' => t('Sign up for a newsletter?'),
'#options' => $options,
'#default_value' => $defaults,
'#weight'=> 5,
'#description' => t('You can sign up for any of our regular newsletters here; an email will be sent asking you to confirm your subscription.'),
);
//END SIMPLE NEWS
//$form['#submit']['doxml_submit'] = current($form['#submit']);
//http://heydon.com.au/node/926
//Add simplenews_forms form_submit to the array of form['#submit'].
$form['#submit'] = array_merge(
array('simplenews_forms_subscribe_user'=> array()),
$form['#submit']
);
}
}
/**
* Use internal simplenews functions to add form mail field to newsletter subscriptions.
*/
function simplenews_forms_subscribe_user($form_id, $form_values) {
global $user;
// find mail field
$mail_field = simplenews_forms_get_mail_field($form_id, $form_values);
if ($mail_field){ // If we have found a valid mail field...
//simple news elements
$account = _simplenews_user_load($form_values[$mail_field]);
} else { // Otherwise try the email address of the current user
$account = $user;
}
if ($account->mail) {
foreach($form_values['tid'] as $tid => $checked) {
if ($checked) {
simplenews_subscribe_user($account->mail, $tid, TRUE);
}
elseif ($account=>uid) {
simplenews_unsubscribe_user($account->mail, $tid, FALSE);
}
}
}
}
/**
* Add menu item for config options (to store form IDs - and mail fields?)
*/
function simplenews_forms_menu($may_cache) {
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/simplenews_forms',
'title' => t('Simplenews Forms'),
'callback' => 'drupal_get_form',
'callback arguments' => array('simplenews_forms_settings_form'),
'access' => user_access('administer newsletters'),
'description' => t("Administers the appearance of newsletter subscription checkboxes on forms."),
);
}
return $items;
}
/**
* Callback function for admin menu option
*/
function simplenews_forms_settings_form() {
/**
* get included form IDs
* split the phrase by newlines \r, \t, \n and \f
*/
$form['simplenews_forms_include'] = array(
'#type' => 'textarea',
'#title' => t('Subscribing forms'),
'#cols' => 60,
'#rows' => 5,
'#default_value' => variable_get("simplenews_forms_include", ''),
'#description' => t("Names (FORM_ID's) of forms that may be used to subscribe to forms. Email (to subscribe) fields may be specified in the format 'form-id|email-field-name'; if no email field name is given then simplenews_forms will look for a field named 'mail' and, if this is not available, the logged-in user's email (if any). If all these fields are missing or empty then no subscriptions will be added. Separate entries with spaces, linebreaks or commas.")
);
$form['simplenews_forms_show_hints'] = array(
'#type' => 'checkbox',
'#title' => t('Show hints'),
'#default_value' => variable_get("simplenews_forms_show_hints", false),
'#description' => t('If this box is checked, the FORM_ID for EVERY form on your site will be displayed. This may disrupt your page layout so it is recommended you use it only during development.')
);
return system_settings_form($form);
}
/**
* Take a form_id and see if it is in the included forms list
*/
function simplenews_forms_included($form_id) {
$included_forms = preg_split("/[\s,]+/", strip_tags(variable_get("simplenews_forms_include", '')));
foreach ($included_forms as $aform) {
$form_array = explode('|',$aform);
if ($form_array[0] == $form_id) {
return true;
}
}
return false;
}
/**
* Take a form_id and return the mail_field field
*/
function simplenews_forms_get_mail_field($form_id, $form_values) {
$included_forms = preg_split("/[\s,]+/", strip_tags(variable_get("simplenews_forms_include", '')));
foreach ($included_forms as $aform) {
$form_array = explode("|",$aform);
if ($form_array[0] == $form_id) {
// FOUND form_id. If this row has a mail_field, use that (if defined).
// Else use 'mail' (if defined).
// Else fail.
if (count($form_array) && $form_values[$form_array[1]]) return $form_array[1];
if ($form_values['mail']) return 'mail';
break; // Don't bother to continue processing form fields
}
}
// Not found or no
return false;
}
?>