I've created a component that allows to use a Webform to subscribe to a newsletters (simplenews). The basic need was to acquire more information about the people that were anonymously subscribing to a newsletter, and I think this might interest others.

I don't know if this is the right place to submit this, but anyway, here it is :

<?php

function _webform_submit_simplenews(&$data, $component) {
  $news_vid = $data[0];
  $email = $data[1];
  if($email && $news_vid) {
    simplenews_subscribe_user($email, $news);
  }
}

function _webform_edit_simplenews($currfield) {
  if (!module_exists("simplenews")) {
    drupal_set_message(t("Using simplenews components in webform requires the <a href='http://drupal.org/project/simplenews'>Simpnews</a> module."), "error");
  }

  $edit_fields = array();
  $options = array();

  foreach( taxonomy_get_tree(_simplenews_get_vid()) as $newsletter) {
    $options[$newsletter->tid] = $newsletter->name;
  }

  $edit_fields['extra']['newsletter'] = array(
    '#type' => 'select',
    '#title' => t("Newsletter"),
    '#default_value' =>  $currfield['extra']['newsletter'],
    '#description' => t('Select which newsletter can be chosen'),
    '#required' => TRUE,
    '#multiple' => FALSE,
    '#size' => sizeof($options),
    '#options' => $options,
  );

  $edit_fields['mandatory'] = array(
    '#type' => 'hidden',
    '#value' => 1,
  );
  $edit_fields['extra']['description'] = array(); // Hide the description box

  return $edit_fields;
}

function _webform_render_simplenews($component) {
  $form_item[] = array(
    '#type'          => 'hidden',
    '#value'         => $component['extra']['newsletter'],
  );
  $form_item[] = array(
    '#title'    => htmlspecialchars($component['name'], ENT_QUOTES),
    '#type'     => 'textfield',
    '#required' => 1,
    '#validate' => array('_webform_validate_email' => array('submitted]['. $component['cid'])),
  );
  $form_item['#weight'] = $component['weight'];

  return $form_item;
}

function _webform_submission_display_simplenews($data, $component) {
  $form_item = _webform_render_hidden($component);
  $form_item['#value']         = $data['value']['0'];
  $form_item['#type']          = 'textfield';
  $form_item['#title']         = htmlspecialchars($component['name'], ENT_QUOTES) ." (hidden)";
  $form_item['#attributes']    = array("disabled" => "disabled");
  return $form_item;
}

function _webform_help_simplenews($section) {
  switch ($section) {
    case 'admin/settings/webform#simplenews_description':
      $output = t("Subscribe to newsletters.");
      break;
  }
  return $output;
}

function _webform_analysis_rows_simplenews($component) {  
  $query = 'SELECT data '.
    ' FROM {webform_submitted_data} '.
    ' WHERE nid = %d '.
    ' AND cid = %d';
  $nonblanks = 0;
  $submissions = 0;
  $wordcount = 0;

  $result = db_query($query, $component['nid'], $component['cid']);
  while ($data = db_fetch_array($result)) {
    if ( strlen(trim($data['data'])) > 0 ) {
      $nonblanks++;
      $wordcount += str_word_count(trim($data['data']));
    }
    $submissions++;
  }
  $rows[0] = array( t('Submissions'), $submissions);
  return $rows;
}

function _webform_table_data_simplenews($data) {
  return check_plain(empty($data['value']['1']) ? "" : $data['value']['1']);
}

function _webform_csv_headers_simplenews($component) {
  $header = array();
  $header[0] = '';
  $header[1] = '';
  $header[2] = $component['name'];
  return $header;
}

function _webform_csv_data_simplenews($data) {
  return empty($data['value']['1']) ? "" : $data['value']['1'];
}

Comments

davemybes’s picture

That's exactly what I've been thinking about doing for several clients. Thanks! I'll give it a go and see how it goes.

quicksketch’s picture

Status: Active » Closed (fixed)

Thanks for sharing! I think the implementation is too specific to be included in webform, but it'll be handy to have kept in the issue queue.

Phillip Mc’s picture

looks good.

Can i use this snippet to add CIty and Country fields to the simplenews subscriptions?

Phil

hermanns’s picture

Version: 5.x-1.4 » 5.x-2.1

Sounds great!! Just what i looking for.
But how to install it?
I've made a new file named simplenews.inc and copy it to the webform/components-folder. But i get an error like this: Fatal error: Call to undefined function _simplenews_get_vid() in /xxxl/sites/all/modules/webform/components/simplenews.inc on line 19
Anyone an idea?

Greetings
Dirk

pelicani’s picture

As of February, the simplenews module changed how the call the newsletter vid.
See http://lists.drupal.org/pipermail/drupal-cvs/2008-February/123992.html for an explanation.

To solve this, on line 19, I changed to variable_get('simplenews_vid', 0)

pelicani’s picture

also note line 9 has an option to not require confirmation
it also may contain a bug
original line 9 ...
simplenews_subscribe_user($email, $news);
i changed the line to this ...
simplenews_subscribe_user($email, $news_vid, FALSE);

sgabe’s picture

Version: 5.x-2.1 » 5.x-2.3
Status: Closed (fixed) » Active

I made a checkbox and an e-mail field like in yraber's code, but here comes the problem. The preg_match() function wait for a string, and in the _webform_render_simplenews function we have to create a form_item[] array with a textfield, wich contains the e-mail address, with a checkbox that controls the subscribe, and a hidden field with the newsletter taxonomy id. Because of the array, the e-mail doesn't contain the value of the e-mail address field and you can't set up the sender's e-mail address with this component...

There is some more information about the problem: http://drupal.org/node/127178

The php error message when the webform is submitted:
preg_match() expects parameter 2 to be string, array given - /home/ftp/site/public_html/includes/bootstrap.inc - 684. line.

Any idea how to solve this?

seren10pity’s picture

Thank you very mutch !
That's exactly what I needed !

quicksketch’s picture

Status: Active » Closed (duplicate)

This patch is a bit further along, let's consolidate there: #525446: Public API for allowing other modules to provide components