webform_simplenews.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /**
  3. * @file: Webform component that allows to suscribe to a simplenews newsletter.
  4. * This way you can subscribe to a newsletter using a webform, so you can add extra options.
  5. */
  6. /**
  7. * Create a default newsletter_email component.
  8. */
  9. function _webform_defaults_newsletter_email() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'user_email' => 1,
  17. 'title_display' => 0,
  18. 'mandatory' => 0,
  19. 'email' => 1,
  20. 'extra' => array(
  21. 'width' => '',
  22. 'description' => '',
  23. 'attributes' => array(),
  24. 'news_vid' => 0,
  25. 'action' => '',
  26. 'private' => 0,
  27. ),
  28. );
  29. }
  30. /**
  31. * Implementation of _webform_theme_component().
  32. */
  33. function _webform_theme_newsletter_email() {
  34. return array(
  35. 'webform_display_newsletter_email' => array(
  36. 'render element' => 'element',
  37. ),
  38. );
  39. }
  40. /**
  41. * Implementation of _webform_edit_component().
  42. */
  43. function _webform_edit_newsletter_email($component) {
  44. $options = array();
  45. foreach (taxonomy_get_tree(variable_get('simplenews_vid', '')) as $newsletter) {
  46. $options[$newsletter->tid] = $newsletter->name;
  47. }
  48. $form['user_email'] = array(
  49. '#type' => 'checkbox',
  50. '#title' => t('User email as default'),
  51. '#default_value' => $component['user_email'],
  52. '#description' => t('Set the default value of this field to the user email, if he/she is logged in.'),
  53. '#weight' => 0,
  54. '#element_validate' => array('_webform_edit_newsletter_email_validate'),
  55. );
  56. $form['extra']['news_vid'] = array(
  57. '#type' => 'select',
  58. '#title' => t("Newsletter"),
  59. '#default_value' => $component['extra']['news_vid'],
  60. '#description' => t('Select the newsletter this form will handle.'),
  61. '#required' => TRUE,
  62. '#multiple' => TRUE,
  63. '#size' => sizeof($options),
  64. '#options' => $options,
  65. );
  66. $form['extra']['individual_newsletter_selection'] = array(
  67. '#type' => 'checkbox',
  68. '#title' => t('Allow individual selection of newsletters'),
  69. '#default_value' => isset($component['extra']['individual_newsletter_selection']) ? $component['extra']['individual_newsletter_selection'] : '',
  70. '#description' => t('If this box is checked and multiple newsletters are selected above, the user will be presented with a list of invdividual newsletters that they can subscribe to.'),
  71. );
  72. $form['extra']['action'] = array(
  73. '#type' => 'radios',
  74. '#title' => t('Action'),
  75. '#default_value' => $component['extra']['action'] ? $component['extra']['action'] : 'subscribe',
  76. '#description' => t('Choose the action to be taken on submit.'),
  77. '#required' => TRUE,
  78. '#options' => array(
  79. 'subscribe' => t('Subscription'),
  80. 'unsubscribe' => t('Unsubscription'),
  81. )
  82. );
  83. $form['extra']['width'] = array(
  84. '#type' => 'textfield',
  85. '#title' => t('Width'),
  86. '#default_value' => $component['extra']['width'],
  87. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  88. '#size' => 5,
  89. '#maxlength' => 10,
  90. );
  91. return $form;
  92. }
  93. /**
  94. * Validation function for the email edit form.
  95. */
  96. function _webform_edit_newsletter_email_validate($element, &$form_state) {
  97. if ($form_state['values']['user_email']) {
  98. $form_state['values']['value'] = '%useremail';
  99. }
  100. }
  101. /**
  102. * Implementation of _webform_render_component().
  103. */
  104. function _webform_render_newsletter_email($component, $value = NULL, $filter = TRUE) {
  105. $element = array(
  106. '#title' => $filter ? _webform_filter_xss($component['name']) : $component['name'],
  107. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  108. '#required' => $component['mandatory'],
  109. '#weight' => $component['weight'],
  110. '#attributes' => $component['extra']['attributes'],
  111. '#element_validate' => array('_webform_validate_newsletter_email'),
  112. '#theme_wrappers' => array('webform_element'),
  113. '#webform_component' => $component,
  114. '#description' => $filter ? _webform_filter_descriptions($component['extra']['description']) : $component['extra']['description']
  115. );
  116. $element['newsletter_email_address'] = array(
  117. '#type' => 'textfield',
  118. '#default_value' => _webform_filter_values($component['value']),
  119. );
  120. if (isset($value)) {
  121. $element['email']['#default_value'] = $value[0];
  122. }
  123. // Change the 'width' option to the correct 'size' option.
  124. if ($component['extra']['width'] > 0) {
  125. $element['newsletter_email_address']['#size'] = $component['extra']['width'];
  126. }
  127. $news_vid = $component['extra']['news_vid'];
  128. if (count($news_vid) > 1 && $component['extra']['individual_newsletter_selection']) {
  129. $options = array();
  130. foreach (taxonomy_get_tree(variable_get('simplenews_vid', '')) as $newsletter) {
  131. if ($news_vid[$newsletter->tid] != 0) {
  132. $options[$newsletter->tid] = $newsletter->name;
  133. }
  134. }
  135. $element['newsletter_selection'] = array(
  136. '#type' => 'checkboxes',
  137. '#default_value' => array(),
  138. '#options' => $options,
  139. );
  140. }
  141. return $element;
  142. }
  143. /**
  144. * A Drupal Forms API Validation function. Validates the entered values from
  145. * email components on the client-side form.
  146. * @param $form_element
  147. * The e-mail form element.
  148. * @param $form_state
  149. * The full form state for the webform.
  150. * @return
  151. * None. Calls a form_set_error if the e-mail is not valid.
  152. */
  153. function _webform_validate_newsletter_email($form_element, $form_state) {
  154. $component = $form_element['#webform_component'];
  155. if (!empty($form_element['newsletter_email_address']['#value']) && !valid_email_address($form_element['newsletter_email_address']['#value'])) {
  156. form_error($form_element, t('%value is not a valid email address.', array('%value' => $form_element['newsletter_email_address']['#value'])));
  157. }
  158. elseif (empty($form_element['newsletter_email_address']['#value']) && $form_element['#required']) {
  159. form_error($form_element, t('E-mail address for newsletter "%name" is required.', array('%name' => $component['name'])));
  160. }
  161. }
  162. /**
  163. * Implementation of _webform_submit_component().
  164. */
  165. function _webform_submit_newsletter_email($component, $value) {
  166. global $user;
  167. $mail = $value['newsletter_email_address'];
  168. $return_val = array(0 => $value['newsletter_email_address']);
  169. $selected_subscriptions = array();
  170. if (!empty($value['newsletter_selection'])) {
  171. foreach ($value['newsletter_selection'] as $k => $v) {
  172. if ($v > 0) {
  173. $selected_subscriptions[(int) $k] = (int) $k;
  174. }
  175. }
  176. }
  177. $news_vid = $component['extra']['news_vid'];
  178. if (count($selected_subscriptions) > 0) {
  179. $news_vid = $selected_subscriptions;
  180. }
  181. else {
  182. $news_vid = array();
  183. }
  184. $action = $component['extra']['action'];
  185. $account = simplenews_load_user_by_mail($mail);
  186. // If email belongs to the current registered user, don't send confirmation.
  187. // Other addresses receive a confirmation if double opt-in is selected.
  188. if ($account->uid && $account->uid == $user->uid) {
  189. $confirm = FALSE;
  190. }
  191. else {
  192. $confirm = variable_get('simplenews_opt_inout_' . $news_vid, 'double') == 'double';
  193. }
  194. if ($mail && isset($news_vid)) {
  195. if ($action == 'subscribe') {
  196. if (count($news_vid) > 0) {
  197. foreach ($news_vid as $k => $v) {
  198. simplenews_subscribe_user($mail, $v, $confirm);
  199. }
  200. }
  201. }
  202. else {
  203. if (count($news_vid) > 0) {
  204. foreach ($news_vid as $k => $v) {
  205. simplenews_unsubscribe_user($mail, $v, $confirm);
  206. }
  207. }
  208. }
  209. }
  210. return $return_val;
  211. }
  212. /**
  213. * Implementation of _webform_display_component().
  214. */
  215. function _webform_display_newsletter_email($component, $value, $format = 'html') {
  216. return array(
  217. '#title' => $component['name'],
  218. '#weight' => $component['weight'],
  219. '#theme' => 'webform_display_newsletter_email',
  220. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  221. '#post_render' => array('webform_element_wrapper'),
  222. '#webform_component' => $component,
  223. '#format' => $format,
  224. '#value' => isset($value[0]) ? $value[0] : '',
  225. );
  226. }
  227. /**
  228. * Format the text output for this component.
  229. */
  230. function theme_webform_display_newsletter_email($variables) {
  231. $element = $variables['element'];
  232. $element['#value'] = empty($element['#value']) ? ' ' : $element['#value'];
  233. return $element['#format'] == 'html' ? check_plain($element['#value']) : $element['#value'];
  234. }
  235. /**
  236. * Implementation of _webform_form_builder_save_component().
  237. */
  238. function _webform_form_builder_save_newsletter_email($component, $form_element) {
  239. $component['extra']['width'] = isset($form_element['#size']) ? $form_element['#size'] : NULL;
  240. $component['extra']['news_vid'] = isset($form_element['#news_vid']) ? $form_element['#news_vid'] : NULL;
  241. $component['extra']['action'] = isset($form_element['#action']) ? $form_element['#action'] : NULL;
  242. return $component;
  243. }
  244. /**
  245. * Implementation of _webform_form_builder_types_component().
  246. */
  247. function _webform_form_builder_types_newsletter_email() {
  248. $fields = array();
  249. $fields['newsletter_email'] = array(
  250. 'title' => t('Newsletter email'),
  251. 'properties' => array(
  252. 'title',
  253. 'description',
  254. 'default_value',
  255. 'required',
  256. 'size',
  257. 'key',
  258. ),
  259. 'default' => array(
  260. '#title' => t('New e-mail for newsletters'),
  261. '#type' => 'textfield',
  262. '#form_builder' => array('element_type' => 'newsletter_email'),
  263. ),
  264. );
  265. return $fields;
  266. }
  267. /**
  268. * Implementation of _webform_analysis_component().
  269. */
  270. function _webform_analysis_newsletter_email($component) {
  271. $rows = array();
  272. $nonblanks = 0;
  273. $submissions = 0;
  274. $wordcount = 0;
  275. $result = db_query("SELECT data FROM {webform_submitted_data} WHERE nid = :nid AND cid = :cid", array(':nid' => $component['nid'], ':cid' => $component['cid']))->fetchAll();
  276. foreach ($result as $data) {
  277. if (drupal_strlen(trim($data->data)) > 0) {
  278. $nonblanks++;
  279. $wordcount += str_word_count(trim($data->data));
  280. }
  281. $submissions++;
  282. }
  283. $rows[0] = array(t('Submissions'), $submissions);
  284. return $rows;
  285. }
  286. /**
  287. * Implementation of _webform_csv_headers_component().
  288. */
  289. function _webform_csv_headers_newsletter_email($component, $export_options) {
  290. $header = array();
  291. $header[0] = '';
  292. $header[1] = '';
  293. $header[2] = $component['name'];
  294. return $header;
  295. }
  296. /**
  297. * Implementation of _webform_csv_data_component().
  298. */
  299. function _webform_csv_data_newsletter_email($component, $export_options, $value) {
  300. return empty($value[0]) ? '' : $value[0];
  301. }
  302. /**
  303. * Implementation of _webform_table_component().
  304. */
  305. function _webform_table_newsletter_email($component, $value) {
  306. return check_plain(empty($value[0]) ? '' : $value[0]);
  307. }