simplenews.subscription.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. <?php
  2. /**
  3. * @file
  4. * (Un)subscription and (un)subscription confirmation
  5. *
  6. * FAPI subscription form cases:
  7. * - ACCOUNT
  8. * self/admin action: authenticated user
  9. * via hook_user form: category=newsletter
  10. *
  11. * - BLOCK
  12. * self action: anonymous / authenticated user
  13. * via hook_block: block
  14. *
  15. * - PAGE
  16. * self action: anonymous / authenticated user
  17. * callback: newsletter/subscriptions
  18. *
  19. * - MULTI BLOCK
  20. * self action: anonymous / authenticated user
  21. * authenticated user
  22. * via hook_block: multi_block
  23. * using PAGE handlers
  24. *
  25. * - ADMIN
  26. * admin action: authenticated user
  27. * via hook_menu: admin
  28. *
  29. * FAPI additional form cases:
  30. * - CONFIRM ADD
  31. * - CONFIRM REMOVAL
  32. *
  33. * @ingroup simplenews
  34. */
  35. /**
  36. * FAPI ACCOUNT subscription form.
  37. *
  38. * Finally _account_ cases inject into hook_user and won't work on its own.
  39. * Note that our basis is:
  40. * drupal_get_form('user_profile_form', ...);
  41. * and NOT:
  42. * drupal_get_form('simplenews_subscriptions_account', ...);
  43. *
  44. * see also user/user.module and user/user.pages.inc
  45. *
  46. * @see simplenews_subscriptions_account_form_validate()
  47. * @see simplenews_subscriptions_account_form_submit()
  48. */
  49. function simplenews_subscriptions_account_form(&$form, &$form_state, $subscriber) {
  50. $options = array();
  51. $default_value = array();
  52. // Get newsletters for subscription form checkboxes.
  53. // Newsletters with opt-in/out method 'hidden' will not be listed.
  54. foreach (simplenews_category_get_visible() as $newsletter) {
  55. $options[$newsletter->tid] = check_plain(_simplenews_newsletter_name($newsletter));
  56. $default_value[$newsletter->tid] = FALSE;
  57. }
  58. if ($subscriber) {
  59. $default_value = array_merge($default_value, $subscriber->tids);
  60. }
  61. $form['subscriptions'] = array(
  62. '#type' => 'fieldset',
  63. '#description' => t('Select your newsletter subscriptions.'),
  64. );
  65. $form['subscriptions']['newsletters'] = array(
  66. '#type' => 'checkboxes',
  67. '#options' => $options,
  68. '#default_value' => $default_value,
  69. );
  70. $form['subscriptions']['#title'] = t('Current newsletter subscriptions');
  71. // if we don't override #validate, see user_profile_form_validate
  72. // adding an own #submit leads to the situation where drupal omits execution of user_profile_form_submit completely
  73. $form['#submit'][] = 'simplenews_subscriptions_account_form_submit';
  74. }
  75. /**
  76. * FAPI ACCOUNT subscription form_submit.
  77. */
  78. function simplenews_subscriptions_account_form_submit($form, &$form_state) {
  79. global $user;
  80. $account = $form['#user'];
  81. // We first subscribe, then unsubscribe. This prevents deletion of subscriptions
  82. // when unsubscribed from the
  83. arsort($form_state['values']['newsletters'], SORT_NUMERIC);
  84. foreach ($form_state['values']['newsletters'] as $tid => $checked) {
  85. if ($checked) {
  86. simplenews_subscribe_user($account->mail, $tid, FALSE, 'website');
  87. }
  88. else {
  89. simplenews_unsubscribe_user($account->mail, $tid, FALSE, 'website');
  90. }
  91. }
  92. if ($user->uid == $account->uid) {
  93. drupal_set_message(t('Your newsletter subscriptions have been updated.'));
  94. }
  95. else {
  96. drupal_set_message(t('The newsletter subscriptions for user %account have been updated.', array('%account' => $account->name)));
  97. }
  98. }
  99. /**
  100. * FAPI BLOCK subscription form.
  101. *
  102. * @param $tid term id of selected newsletter.
  103. *
  104. * @see simplenews_block_form_validate()
  105. * @see simplenews_block_form_submit()
  106. */
  107. function simplenews_block_form($form, &$form_state, $tid) {
  108. global $user;
  109. $form = array();
  110. $submit_text = t('Subscribe');
  111. if ($user->uid) {
  112. if (simplenews_user_is_subscribed($user->mail, $tid)) {
  113. $submit_text = t('Unsubscribe');
  114. $form['action'] = array('#type' => 'value', '#value' => 'unsubscribe');
  115. $form['#attributes'] = array('class' => array('simplenews-unsubscribe'));
  116. }
  117. else {
  118. $form['action'] = array('#type' => 'value', '#value' => 'subscribe');
  119. $form['#attributes'] = array('class' => array('simplenews-subscribe'));
  120. }
  121. $form['mail'] = array('#type' => 'value', '#value' => $user->mail);
  122. }
  123. else {
  124. $form['mail'] = array(
  125. '#type' => 'textfield',
  126. '#title' => t('E-mail'),
  127. '#size' => 20,
  128. '#maxlength' => 128,
  129. '#required' => TRUE,
  130. );
  131. $form['action'] = array('#type' => 'value', '#value' => 'subscribe');
  132. $form['#attributes'] = array('class' => array('simplenews-subscribe'));
  133. }
  134. // All block forms use the same validate and submit function.
  135. // #tid carries the tid for processing of the right newsletter issue term.
  136. $form['#tid'] = $tid;
  137. $form['#validate'][] = 'simplenews_block_form_validate';
  138. $form['#submit'][] = 'simplenews_block_form_submit';
  139. $form['submit'] = array(
  140. '#type' => 'submit',
  141. '#value' => $submit_text,
  142. );
  143. return $form;
  144. }
  145. /*
  146. * FAPI BLOCK subscription form_validate.
  147. */
  148. function simplenews_block_form_validate($form, &$form_state) {
  149. if (!valid_email_address($form_state['values']['mail'])) {
  150. form_set_error('mail', t("The e-mail address you supplied is not valid."));
  151. }
  152. }
  153. /*
  154. * FAPI BLOCK subscription form_submit.
  155. */
  156. function simplenews_block_form_submit($form, &$form_state) {
  157. $tid = $form['#tid'];
  158. $account = simplenews_load_user_by_mail($form_state['values']['mail']);
  159. $confirm = simplenews_require_double_opt_in($tid, $account);
  160. switch ($form_state['values']['action']) {
  161. case 'subscribe':
  162. simplenews_subscribe_user($form_state['values']['mail'], $tid, $confirm, 'website');
  163. if ($confirm) {
  164. drupal_set_message(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
  165. }
  166. else {
  167. drupal_set_message(t('You have been subscribed.'));
  168. }
  169. break;
  170. case 'unsubscribe':
  171. simplenews_unsubscribe_user($form_state['values']['mail'], $tid, $confirm, 'website');
  172. if ($confirm) {
  173. drupal_set_message(t('You will receive a confirmation e-mail shortly containing further instructions on how to cancel your subscription.'));
  174. }
  175. else {
  176. drupal_set_message(t('You have been unsubscribed.'));
  177. }
  178. break;
  179. }
  180. }
  181. /**
  182. * FAPI PAGE subscription form.
  183. *
  184. * @see simplenews_subscriptions_page_form_validate()
  185. * @see simplenews_subscriptions_page_form_submit()
  186. */
  187. function simplenews_subscriptions_page_form($form, &$form_state, $code = NULL) {
  188. global $user;
  189. $subscriber = $mail = FALSE;
  190. if (!empty($user->mail)) {
  191. $subscriber = simplenews_subscriber_load_by_mail($user->mail);
  192. $mail = $user->mail;
  193. }
  194. // If a hash is provided, try to load the corresponding subscriber.
  195. else if ($code) {
  196. if (!$subscriber = simplenews_subscriber_load_by_hash($code)) {
  197. drupal_not_found();
  198. return;
  199. }
  200. $mail = $subscriber->mail;
  201. }
  202. $form = array();
  203. $options = array();
  204. $default_value = array();
  205. // Get newsletters for subscription form checkboxes.
  206. // Newsletters with opt-in/out method 'hidden' will not be listed.
  207. foreach (simplenews_category_get_visible() as $newsletter) {
  208. $options[$newsletter->tid] = check_plain($newsletter->name);
  209. $default_value[$newsletter->tid] = FALSE;
  210. }
  211. if ($subscriber) {
  212. // If there is an existing subscriber object, use the existing settings.
  213. $default_value = array_merge($default_value, $subscriber->tids);
  214. }
  215. $form['subscriptions'] = array(
  216. '#type' => 'fieldset',
  217. );
  218. $form['subscriptions']['newsletters'] = array(
  219. '#type' => 'checkboxes',
  220. '#options' => $options,
  221. '#default_value' => $default_value
  222. );
  223. // If we have a mail address, which is either from a logged in user or a
  224. // subscriber identified through the hash code, display the mail address
  225. // instead of a textfield. Anonymous uses will still have to confirm any
  226. // changes.
  227. if ($mail) {
  228. $form['subscriptions']['#title'] = t('Subscriptions for %mail', array('%mail' => $mail));
  229. $form['subscriptions']['#description'] = t('Check the newsletters you want to subscribe to. Uncheck the ones you want to unsubscribe from.');
  230. $form['subscriptions']['mail'] = array('#type' => 'value', '#value' => $mail);
  231. $form['update'] = array(
  232. '#type' => 'submit',
  233. '#value' => t('Update'),
  234. '#weight' => 20,
  235. // @todo: add clean submit handler
  236. );
  237. }
  238. else {
  239. $form['subscriptions']['#title'] = t('Manage your newsletter subscriptions');
  240. $form['subscriptions']['#description'] = t('Select the newsletter(s) to which you want to subscribe or unsubscribe.');
  241. $form['subscriptions']['mail'] = array(
  242. '#type' => 'textfield',
  243. '#title' => t('E-mail'),
  244. '#size' => 20,
  245. '#maxlength' => 128,
  246. '#weight' => 10,
  247. '#required' => TRUE,
  248. );
  249. $form['subscribe'] = array(
  250. '#type' => 'submit',
  251. '#value' => t('Subscribe'),
  252. '#weight' => 20,
  253. // @todo: add clean submit handler
  254. );
  255. $form['unsubscribe'] = array(
  256. '#type' => 'submit',
  257. '#value' => t('Unsubscribe'),
  258. '#weight' => 30,
  259. // @todo: add clean submit handler
  260. );
  261. }
  262. $form['#validate'][] = 'simplenews_subscriptions_page_form_validate';
  263. $form['#submit'][] = 'simplenews_subscriptions_page_form_submit';
  264. return $form;
  265. }
  266. /**
  267. * FAPI PAGE subscription form_validate.
  268. */
  269. function simplenews_subscriptions_page_form_validate($form, &$form_state) {
  270. $valid_email = valid_email_address($form_state['values']['mail']);
  271. if (!$valid_email) {
  272. form_set_error('mail', t('The e-mail address you supplied is not valid.'));
  273. }
  274. $checked_newsletters = array_filter($form_state['values']['newsletters']);
  275. // Unless we're in update mode, at least one checkbox must be checked.
  276. if (!count($checked_newsletters) && $form_state['values']['op'] != t('Update')) {
  277. form_set_error('newsletters', t('You must select at least one newsletter.'));
  278. }
  279. }
  280. /**
  281. * FAPI PAGE subscription form_submit.
  282. */
  283. function simplenews_subscriptions_page_form_submit($form, &$form_state) {
  284. $mail = $form_state['values']['mail'];
  285. $account = simplenews_load_user_by_mail($mail);
  286. // Group confirmation mails as necessary and configured.
  287. simplenews_confirmation_combine(TRUE);
  288. switch ($form_state['values']['op']) {
  289. case t('Update'):
  290. // We first subscribe, then unsubscribe. This prevents deletion of subscriptions
  291. // when unsubscribed from the
  292. arsort($form_state['values']['newsletters'], SORT_NUMERIC);
  293. foreach ($form_state['values']['newsletters'] as $tid => $checked) {
  294. $confirm = simplenews_require_double_opt_in($tid, $account);
  295. if ($checked) {
  296. simplenews_subscribe_user($mail, $tid, $confirm, 'website');
  297. }
  298. else {
  299. simplenews_unsubscribe_user($mail, $tid, $confirm, 'website');
  300. }
  301. }
  302. if (simplenews_confirmation_send_combined()) {
  303. drupal_set_message(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
  304. }
  305. else {
  306. drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail'])));
  307. }
  308. break;
  309. case t('Subscribe'):
  310. foreach ($form_state['values']['newsletters'] as $tid => $checked) {
  311. if ($checked) {
  312. $confirm = simplenews_require_double_opt_in($tid, $account);
  313. simplenews_subscribe_user($mail, $tid, $confirm, 'website');
  314. }
  315. }
  316. if (simplenews_confirmation_send_combined()) {
  317. drupal_set_message(t('You will receive a confirmation e-mail shortly containing further instructions on how to complete your subscription.'));
  318. }
  319. else {
  320. drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail'])));
  321. }
  322. break;
  323. case t('Unsubscribe'):
  324. foreach ($form_state['values']['newsletters'] as $tid => $checked) {
  325. if ($checked) {
  326. $confirm = simplenews_require_double_opt_in($tid, $account);
  327. simplenews_unsubscribe_user($mail, $tid, $confirm, 'website');
  328. }
  329. }
  330. if (simplenews_confirmation_send_combined()) {
  331. drupal_set_message(t('You will receive a confirmation e-mail shortly containing further instructions on how to cancel your subscription.'));
  332. }
  333. else {
  334. drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail'])));
  335. }
  336. break;
  337. }
  338. }
  339. /**
  340. * FAPI MULTI BLOCK subscription form.
  341. *
  342. * Menu callback: Generates the subscription form for users for the multisignup block.
  343. *
  344. * @see simplenews_subscriptions_multi_block_form_validate()
  345. * @see simplenews_subscriptions_multi_block_form_submit()
  346. */
  347. function simplenews_subscriptions_multi_block_form($form, &$form_state) {
  348. global $user;
  349. $subscriber = !empty($user->mail) ? simplenews_subscriber_load_by_mail($user->mail) : FALSE;
  350. // If someone not authorized to edit their subscription, return empty form.
  351. if (!user_access('subscribe to newsletters')) {
  352. return;
  353. }
  354. $form = array();
  355. $options = array();
  356. $default_value = array();
  357. // Get newsletters for subscription form checkboxes.
  358. // Newsletters with opt-in/out method 'hidden' will not be listed.
  359. foreach (simplenews_category_get_visible() as $newsletter) {
  360. $options[$newsletter->tid] = check_plain($newsletter->name);
  361. $default_value[$newsletter->tid] = FALSE;
  362. }
  363. if ($subscriber) {
  364. // If there is an existing subscriber object, use the existing settings.
  365. $default_value = array_merge($default_value, $subscriber->tids);
  366. }
  367. $form['newsletters'] = array(
  368. '#type' => 'checkboxes',
  369. '#options' => $options,
  370. '#default_value' => $default_value,
  371. );
  372. // If current user is logged in, just display email.
  373. // Anonymous users see an email box and will receive confirmations
  374. if (user_is_logged_in()) {
  375. // @todo why not simply Manage your subscriptions?
  376. $form['mail'] = array('#type' => 'value', '#value' => $user->mail);
  377. $form['update'] = array(
  378. '#type' => 'submit',
  379. '#value' => t('Update'),
  380. '#weight' => 20,
  381. // @todo: add clean submit handler
  382. );
  383. }
  384. else {
  385. $form['mail'] = array(
  386. '#type' => 'textfield',
  387. '#title' => t('E-mail'),
  388. '#size' => 20,
  389. '#maxlength' => 128,
  390. '#weight' => 10,
  391. '#required' => TRUE,
  392. );
  393. $form['subscribe'] = array(
  394. '#type' => 'submit',
  395. '#value' => t('Subscribe'),
  396. '#weight' => 20,
  397. // @todo: add clean submit handler
  398. );
  399. $form['unsubscribe'] = array(
  400. '#type' => 'submit',
  401. '#value' => t('Unsubscribe'),
  402. '#weight' => 30,
  403. // @todo: add clean submit handler
  404. );
  405. }
  406. $form['#validate'][] = 'simplenews_subscriptions_page_form_validate';
  407. $form['#submit'][] = 'simplenews_subscriptions_page_form_submit';
  408. return $form;
  409. }
  410. /**
  411. * Menu callback: confirm the user's (un)subscription request
  412. *
  413. * This function is called by clicking the confirm link in the confirmation
  414. * email or the unsubscribe link in the footer of the newsletter. It handles
  415. * both subscription addition and subscription removal.
  416. *
  417. * Calling URLs are:
  418. * newsletter/confirm/add
  419. * newsletter/confirm/add/$HASH
  420. * newsletter/confirm/remove
  421. * newsletter/confirm/remove/$HASH
  422. *
  423. * @see simplenews_confirm_add_form()
  424. * @see simplenews_confirm_removal_form()
  425. */
  426. /**
  427. * Menu callback: confirm the user's (un)subscription request
  428. *
  429. * This function is called by clicking the confirm link in the confirmation
  430. * email or the unsubscribe link in the footer of the newsletter. It handles
  431. * both subscription addition and subscription removal.
  432. *
  433. * @see simplenews_confirm_add_form()
  434. * @see simplenews_confirm_removal_form()
  435. *
  436. * @todo Add parameter description here.
  437. */
  438. function simplenews_confirm_subscription() {
  439. $arguments = func_get_args();
  440. $op = array_shift($arguments);
  441. $code = array_shift($arguments);
  442. if ($subscriber = simplenews_subscriber_load_by_hash($code)) {
  443. // Extract the category id.
  444. list($snid, $tid) = explode('t', drupal_substr($code, 10));
  445. if ($tid > 0) {
  446. $category = simplenews_category_load($tid);
  447. }
  448. // The confirmation page called with two arguments will display a confirmation question.
  449. // When called with three of more arguments the user will be directed to the
  450. // (un)subscribe confirmation page. The additional arguments will be passed on
  451. // to the confirmation page.
  452. if (empty($arguments)) {
  453. if ($op == 'remove') {
  454. return drupal_get_form('simplenews_confirm_removal_form', $subscriber->mail, $category);
  455. }
  456. elseif ($op == 'add') {
  457. return drupal_get_form('simplenews_confirm_add_form', $subscriber->mail, $category);
  458. }
  459. elseif ($op == 'combined' && !empty($subscriber->changes)) {
  460. return drupal_get_form('simplenews_confirm_multi_form', $subscriber);
  461. }
  462. }
  463. else {
  464. if ($op == 'remove') {
  465. simplenews_unsubscribe_user($subscriber->mail, $tid, FALSE, 'website');
  466. if ($path = variable_get('simplenews_confirm_unsubscribe_page', '')) {
  467. $path = $path . '/' . implode('/', $arguments);
  468. drupal_goto($path);
  469. }
  470. drupal_set_message(t('%user was unsubscribed from the %newsletter mailing list.', array('%user' => $subscriber->mail, '%newsletter' => _simplenews_newsletter_name($category))));
  471. drupal_goto(variable_get('site_frontpage', 'node'));
  472. }
  473. else if ($op == 'add') {
  474. simplenews_subscribe_user($subscriber->mail, $tid, FALSE, 'website');
  475. if ($path = variable_get('simplenews_confirm_subscribe_page', '')) {
  476. $path = $path . '/' . implode('/', $arguments);
  477. drupal_goto($path);
  478. }
  479. drupal_set_message(t('%user was added to the %newsletter mailing list.', array('%user' => $subscriber->mail, '%newsletter' => _simplenews_newsletter_name($category))));
  480. drupal_goto(variable_get('site_frontpage', 'node'));
  481. }
  482. else if ($op == 'combined' && !empty($subscriber->changes)) {
  483. foreach ($subscriber->changes as $tid => $action) {
  484. if ($action == 'subscribe') {
  485. simplenews_subscribe_user($subscriber->mail, $tid, FALSE, 'website');
  486. }
  487. else if ($action == 'subscribe') {
  488. simplenews_unsubscribe_user($subscriber->mail, $tid, FALSE, 'website');
  489. }
  490. }
  491. // Clear changes.
  492. $subscriber->changes = array();
  493. simplenews_subscriber_save($subscriber);
  494. drupal_set_message(t('Subscription changes confirmed for %user.', array('%user' => $subscriberil)));
  495. drupal_goto(variable_get('site_frontpage', 'node'));
  496. }
  497. }
  498. }
  499. // If md5 didn't match, do a not found.
  500. drupal_not_found();
  501. return;
  502. }
  503. /**
  504. * Generate the confirm subscription form.
  505. *
  506. * @see simplenews_confirm_add_form_submit()
  507. */
  508. function simplenews_confirm_add_form($form, &$form_state, $mail, $newsletter) {
  509. $form = array();
  510. $form['question'] = array(
  511. '#markup' => '<p>' . t('Are you sure you want to add %user to the %newsletter mailing list?', array('%user' => $mail, '%newsletter' => _simplenews_newsletter_name($newsletter))) . "<p>\n",
  512. );
  513. $form['mail'] = array(
  514. '#type' => 'value',
  515. '#value' => $mail,
  516. );
  517. $form['newsletter'] = array(
  518. '#type' => 'value',
  519. '#value' => $newsletter,
  520. );
  521. return confirm_form($form,
  522. t('Confirm subscription'),
  523. '',
  524. t('You can always unsubscribe later.'),
  525. t('Subscribe'),
  526. t('Cancel')
  527. );
  528. }
  529. function simplenews_confirm_add_form_submit($form, &$form_state) {
  530. simplenews_subscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE, 'website');
  531. if (!$path = variable_get('simplenews_confirm_subscribe_page', '')){
  532. $path = variable_get('site_frontpage', 'node');
  533. drupal_set_message(t('%user was added to the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => _simplenews_newsletter_name($form_state['values']['newsletter']))));
  534. }
  535. $form_state['redirect'] = $path;
  536. }
  537. /**
  538. * Generate the confirm subscription form.
  539. *
  540. * @see simplenews_confirm_add_form_submit()
  541. */
  542. function simplenews_confirm_multi_form($form, &$form_state, $subscriber) {
  543. $form = array();
  544. $form['question'] = array(
  545. '#markup' => '<p>' . t('Are you sure you want to confirm the following subscription changes for %user?', array('%user' => $subscriber->mail)) . "<p>\n",
  546. );
  547. $form['changes'] = array(
  548. '#theme' => 'item_list',
  549. '#items' => simplenews_confirmation_get_changes_list($subscriber),
  550. );
  551. $form['subscriber'] = array(
  552. '#type' => 'value',
  553. '#value' => $subscriber,
  554. );
  555. return confirm_form($form,
  556. t('Confirm subscription'),
  557. '',
  558. t('You can always change your subscriptions later.'),
  559. t('Confirm'),
  560. t('Cancel')
  561. );
  562. }
  563. function simplenews_confirm_multi_form_submit($form, &$form_state) {
  564. $subscriber = $form_state['values']['subscriber'];
  565. foreach ($subscriber->changes as $tid => $action) {
  566. if ($action == 'subscribe') {
  567. simplenews_subscribe_user($subscriber->mail, $tid, FALSE, 'website');
  568. }
  569. else if ($action == 'unsubscribe') {
  570. simplenews_unsubscribe_user($subscriber->mail, $tid, FALSE, 'website');
  571. }
  572. }
  573. // Clear changes.
  574. $subscriber->changes = array();
  575. simplenews_subscriber_save($subscriber);
  576. drupal_set_message(t('Subscription changes confirmed for %user.', array('%user' => $subscriber->mail)));
  577. $form_state['redirect'] = variable_get('site_frontpage', 'node');
  578. }
  579. /**
  580. * Generate the confirm unsubscription form.
  581. *
  582. * @see simplenews_confirm_removal_form_submit()
  583. */
  584. function simplenews_confirm_removal_form($form, &$form_state, $mail, $newsletter) {
  585. $form = array();
  586. $form['question'] = array(
  587. '#markup' => '<p>' . t('Are you sure you want to remove %user from the %newsletter mailing list?', array('%user' => $mail, '%newsletter' => _simplenews_newsletter_name($newsletter))) . "<p>\n",
  588. );
  589. $form['mail'] = array(
  590. '#type' => 'value',
  591. '#value' => $mail,
  592. );
  593. $form['newsletter'] = array(
  594. '#type' => 'value',
  595. '#value' => $newsletter,
  596. );
  597. return confirm_form($form,
  598. t('Confirm remove subscription'),
  599. '',
  600. t('This action will unsubscribe you from the newsletter mailing list.'),
  601. t('Unsubscribe'),
  602. t('Cancel')
  603. );
  604. }
  605. function simplenews_confirm_removal_form_submit($form, &$form_state) {
  606. simplenews_unsubscribe_user($form_state['values']['mail'], $form_state['values']['newsletter']->tid, FALSE, 'website');
  607. if (!$path = variable_get('simplenews_confirm_unsubscribe_page', '')){
  608. $path = variable_get('site_frontpage', 'node');
  609. drupal_set_message(t('%user was unsubscribed from the %newsletter mailing list.', array('%user' => $form_state['values']['mail'], '%newsletter' => _simplenews_newsletter_name($form_state['values']['newsletter']))));
  610. }
  611. $form_state['redirect'] = $path;
  612. }
  613. /**
  614. * FAPI ADMIN subscription form.
  615. *
  616. * Menu callback: handle the edit subscription page and a subscription
  617. * page for anonymous users.
  618. *
  619. * @see simplenews_subscriptions_admin_form_validate()
  620. * @see simplenews_subscriptions_admin_form_submit()
  621. */
  622. function simplenews_subscriptions_admin_form($form, &$form_state, $snid) {
  623. $subscriber = simplenews_subscriber_load($snid);
  624. $form = array();
  625. $options = array();
  626. $default_value = array();
  627. // Get newsletters for subscription form checkboxes.
  628. // Newsletters with opt-in/out method 'hidden' will not be listed.
  629. foreach (simplenews_category_get_visible() as $newsletter) {
  630. $options[$newsletter->tid] = check_plain($newsletter->name);
  631. $default_value[$newsletter->tid] = FALSE;
  632. }
  633. $form['subscriptions'] = array(
  634. '#title' => t('Subscriptions for %mail', array('%mail' => $subscriber->mail)),
  635. '#type' => 'fieldset',
  636. '#description' => t('Select the newsletter(s) to add/remove from subscription.'),
  637. );
  638. $form['subscriptions']['newsletters'] = array(
  639. '#type' => 'checkboxes',
  640. '#options' => $options,
  641. '#default_value' => array_merge($default_value, $subscriber->tids),
  642. );
  643. $form['activated'] = array(
  644. '#title' => t('Activation'),
  645. '#type' => 'fieldset',
  646. '#description' => t('Activate or inactivate account.'),
  647. );
  648. $form['activated']['activated'] = array(
  649. '#type' => 'checkbox',
  650. '#title' => t('Activated'),
  651. '#default_value' => $subscriber->activated,
  652. );
  653. if ((variable_get('language_count', 1) > 1)) {
  654. // @todo we could allow to switch back to "default", but user_load
  655. //$language_options[''] = t('Site default language');
  656. $languages = language_list('enabled');
  657. foreach ($languages[1] as $langcode => $item) {
  658. $name = t($item->name);
  659. $language_options[$langcode] = $name . ($item->native != $name ? ' ('. $item->native .')' : '');
  660. }
  661. // std users have language in profile. disable
  662. $disabled = $subscriber->uid ? TRUE : FALSE;
  663. $form['language'] = array(
  664. '#type' => 'fieldset',
  665. '#title' => 'Preferred language',
  666. '#description' => t('The e-mails will be localized in language chosen. Real users have their preference in account settings.'),
  667. '#disabled' => FALSE,
  668. );
  669. if ($subscriber->uid) {
  670. // fapi error: disabled not supported for select type. workaround: output markup
  671. $form['language']['language'] = array(
  672. '#type' => 'markup',
  673. '#value' => $language_options[$subscriber->language],
  674. );
  675. }
  676. else {
  677. $form['language']['language'] = array(
  678. '#type' => 'select',
  679. '#default_value' => $subscriber->language,
  680. '#options' => $language_options,
  681. );
  682. }
  683. }
  684. $form['subscriptions']['mail'] = array('#type' => 'value', '#value' => $subscriber->mail);
  685. $form['update'] = array(
  686. '#type' => 'submit',
  687. '#value' => t('Update'),
  688. '#weight' => 20,
  689. );
  690. $form['#validate'][] = 'simplenews_subscriptions_admin_form_validate';
  691. $form['#submit'][] = 'simplenews_subscriptions_admin_form_submit';
  692. $form['#redirect'] = 'admin/content/simplenews/users';
  693. return $form;
  694. }
  695. /**
  696. * FAPI ADMIN subscription form_validate.
  697. */
  698. function simplenews_subscriptions_admin_form_validate($form, &$form_state) {
  699. $subscriber = simplenews_subscriber_load_by_mail($form_state['values']['mail']);
  700. $valid_email = valid_email_address($form_state['values']['mail']);
  701. if (!$valid_email) {
  702. form_set_error('mail', t('The e-mail address you supplied is not valid.'));
  703. }
  704. $checked_newsletters = array_filter($form_state['values']['newsletters']);
  705. if (!count($checked_newsletters) && !$subscriber) {
  706. form_set_error('newsletters', t('You must select at least one newsletter.'));
  707. }
  708. $languages = language_list('enabled');
  709. if (!empty($form_state['values']['language'])
  710. && !isset($languages[1][$form_state['values']['language']])) {
  711. form_set_error('language', t('Please choose a language from the list.'));
  712. }
  713. }
  714. /**
  715. * FAPI ADMIN subscription form_submit.
  716. */
  717. function simplenews_subscriptions_admin_form_submit($form, &$form_state) {
  718. $subscriber = simplenews_subscriber_load_by_mail($form_state['values']['mail']);
  719. // update subscriptions
  720. arsort($form_state['values']['newsletters'], SORT_NUMERIC);
  721. foreach ($form_state['values']['newsletters'] as $tid => $checked) {
  722. if ($checked) {
  723. simplenews_subscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
  724. }
  725. else {
  726. simplenews_unsubscribe_user($form_state['values']['mail'], $tid, FALSE, 'website');
  727. }
  728. }
  729. // update subscriber
  730. $data = array();
  731. $subscriber->activated = $form_state['values']['activated'];
  732. if (!$subscriber->uid) {
  733. if (isset($form_state['values']['language'])) {
  734. $subscriber->language = $form_state['values']['language'];
  735. }
  736. }
  737. simplenews_subscriber_save($subscriber);
  738. drupal_set_message(t('The newsletter subscriptions for %mail have been updated.', array('%mail' => $form_state['values']['mail'])));
  739. }