simplenews.admin.inc 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  1. <?php
  2. /**
  3. * @file
  4. * Newsletter admin, subscription admin, simplenews settings
  5. *
  6. * @ingroup simplenews
  7. */
  8. /**
  9. * Menu callback: Admin form for sent and draft newsletters.
  10. *
  11. * @see simplenews_admin_newsletter_submit()
  12. */
  13. function simplenews_admin_newsletter_issues($form, &$form_state, $action = 'sent') {
  14. // @todo Fix the delete operation
  15. $form['filter'] = simplenews_issue_filter_form();
  16. $form['#submit'][] = 'simplenews_issue_filter_form_submit';
  17. $form['filter']['#theme'] = 'simplenews_filter_form';
  18. $form['admin'] = simplenews_admin_issues();
  19. return $form;
  20. }
  21. /**
  22. * Generate issue filters
  23. */
  24. function simplenews_issue_filters() {
  25. // Newsletter filter
  26. $filters['category'] = array(
  27. 'title' => t('Subscribed to'),
  28. 'options' => array(
  29. 'all' => t('All newsletters'),
  30. 'tid-0' => t('Unassigned newsletters'),
  31. ),
  32. );
  33. foreach (simplenews_category_list() as $tid => $name) {
  34. $filters['category']['options']['tid-' . $tid] = $name;
  35. }
  36. return $filters;
  37. }
  38. /**
  39. * Return form for issue filters.
  40. *
  41. * @see simplenews_issue_filter_form_submit()
  42. */
  43. function simplenews_issue_filter_form() {
  44. // Current filter selections in $session var; stored at form submission
  45. // Example: array('category' => 'all')
  46. $session = isset($_SESSION['simplenews_issue_filter']) ? $_SESSION['simplenews_issue_filter'] : _simplenews_issue_filter_default();
  47. $filters = simplenews_issue_filters();
  48. $form['filters'] = array(
  49. '#type' => 'fieldset',
  50. '#title' => t('Show only newsletters which'),
  51. );
  52. // Filter values are default
  53. $form['filters']['category'] = array(
  54. '#type' => 'select',
  55. '#title' => $filters['category']['title'],
  56. '#options' => $filters['category']['options'],
  57. '#default_value' => $session['category'],
  58. );
  59. $form['filters']['buttons']['submit'] = array(
  60. '#type' => 'submit',
  61. '#value' => t('Filter'),
  62. '#prefix' => '<span class="spacer" />',
  63. );
  64. // Add Reset button if filter is in use
  65. if ($session != _simplenews_issue_filter_default()) {
  66. $form['filters']['buttons']['reset'] = array(
  67. '#type' => 'submit',
  68. '#value' => t('Reset'),
  69. );
  70. }
  71. return $form;
  72. }
  73. /**
  74. * Helper function: returns issue filter default settings
  75. */
  76. function _simplenews_issue_filter_default() {
  77. return array(
  78. 'category' => 'all',
  79. );
  80. }
  81. /**
  82. * Form submit callback for the newsletter issue filter.
  83. */
  84. function simplenews_issue_filter_form_submit($form, &$form_state) {
  85. switch ($form_state['values']['op']) {
  86. case t('Filter'):
  87. $_SESSION['simplenews_issue_filter'] = array(
  88. 'category' => $form_state['values']['category'],
  89. );
  90. break;
  91. case t('Reset'):
  92. $_SESSION['simplenews_issue_filter'] = _simplenews_issue_filter_default();
  93. break;
  94. }
  95. }
  96. /**
  97. * Form builder: Builds a list of newsletters with operations.
  98. *
  99. * @see simplenews_admin_issues_validate()
  100. * @see simplenews_admin_issues_submit()
  101. */
  102. function simplenews_admin_issues() {
  103. // Build an 'Update options' form.
  104. $form['options'] = array(
  105. '#type' => 'fieldset',
  106. '#title' => t('Update options'),
  107. '#prefix' => '<div class="container-inline">',
  108. '#suffix' => '</div>',
  109. );
  110. $options = array();
  111. foreach (module_invoke_all('simplenews_issue_operations') as $operation => $array) {
  112. $options[$operation] = $array['label'];
  113. }
  114. $form['options']['operation'] = array(
  115. '#type' => 'select',
  116. '#options' => $options,
  117. '#default_value' => 'activate',
  118. );
  119. $form['options']['submit'] = array(
  120. '#type' => 'submit',
  121. '#value' => t('Update'),
  122. '#submit' => array('simplenews_admin_issues_submit'),
  123. '#validate' => array('simplenews_admin_issues_validate'),
  124. );
  125. if (variable_get('simplenews_last_cron', '')) {
  126. $form['last_sent'] = array(
  127. '#markup' => '<p>' . format_plural(variable_get('simplenews_last_sent', 0), 'Last batch: 1 mail sent at !time.', 'Last batch: !count mails sent at !time.', array('!time' => format_date(variable_get('simplenews_last_cron', ''), 'small'), '!count' => variable_get('simplenews_last_sent', 0))) . "</p>\n",
  128. );
  129. }
  130. // Table header. Used as tablesort default
  131. $header = array(
  132. 'title' => array('data' => t('Title'), 'field' => 'n.title'),
  133. 'category' => array('data' => t('Newsletter category'), 'field' => 'sc.name'),
  134. 'created' => array('data' => t('Created'), 'field' => 'n.created', 'sort' => 'desc'),
  135. 'published' => array('data' => t('Published')),
  136. 'sent' => array('data' => t('Sent')),
  137. 'subscribers' => array('data' => t('Subscribers')),
  138. 'operations' => array('data' => t('Operations')),
  139. );
  140. $query = db_select('node', 'n')->extend('PagerDefault')->extend('TableSort');
  141. simplenews_build_issue_filter_query($query);
  142. $query->innerJoin('simplenews_newsletter', 'sn', 'n.nid = sn.nid');
  143. $query->leftJoin('simplenews_category', 'sc', 'sn.tid = sc.tid');
  144. $query->leftJoin('taxonomy_term_data', 't', 'sc.tid = t.tid');
  145. //$query->leftJoin('users', 'u', 'ss.uid = u.uid');
  146. $query->fields('n', array('nid', 'title', 'created', 'status'))
  147. ->fields('sn', array('tid'))
  148. ->fields('t', array('name'))
  149. ->limit(30)
  150. ->orderByHeader($header);
  151. $query->addField('sn', 'status', 'sent_status');
  152. $options = array();
  153. $destination = drupal_get_destination();
  154. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  155. foreach ($query->execute() as $issue) {
  156. $categories = simplenews_category_list();
  157. $subscriber_count = simplenews_count_subscriptions($issue->tid);
  158. $pending_count = simplenews_count_spool(array('nid' => $issue->nid));
  159. $send_status = $issue->sent_status == SIMPLENEWS_STATUS_SEND_PENDING ? $subscriber_count - $pending_count : theme('simplenews_status', array('source' => 'sent', 'status' => $issue->sent_status));
  160. $options[$issue->nid] = array(
  161. 'title' => l($issue->title, 'node/' . $issue->nid),
  162. 'category' => $issue->tid && isset($categories[$issue->tid]) ? $categories[$issue->tid] : t('- Unassigned -'),
  163. 'created' => format_date($issue->created, 'small'),
  164. 'published' => theme('simplenews_status', array('source' => 'published', 'status' => $issue->status)),
  165. 'sent' => $send_status,
  166. 'subscribers' => $subscriber_count,
  167. 'operations' => l(t('edit'), 'node/' . $issue->nid . '/edit', array('query' => drupal_get_destination())),
  168. );
  169. }
  170. $form['issues'] = array(
  171. '#type' => 'tableselect',
  172. '#header' => $header,
  173. '#options' => $options,
  174. '#empty' => t('No newsletters available.'),
  175. );
  176. $form['pager'] = array('#theme' => 'pager');
  177. return $form;
  178. }
  179. /**
  180. * Implements hook_simplenews_issue_operations().
  181. */
  182. function simplenews_simplenews_issue_operations() {
  183. // @todo: future ideas: 'pause' => t('Pause sending'), 'resume' => t('Resume sending'), 'cancel' => t('Cancel sending'), 'publish' => t('Publish'), 'unpublish' => t('Unpublish'));
  184. $operations = array(
  185. 'activate' => array(
  186. 'label' => t('Send'),
  187. 'callback' => 'simplenews_issue_send',
  188. ),
  189. );
  190. return $operations;
  191. }
  192. /**
  193. * Form vaidate callback for the issue list operations.
  194. */
  195. function simplenews_admin_issues_validate($form, &$form_state) {
  196. if (isset($form_state['input']['operation'])) {
  197. $nids = array_keys(array_filter($form_state['input']['issues']));
  198. if (empty($nids)) {
  199. form_set_error('', t('No items selected.'));
  200. }
  201. }
  202. }
  203. /**
  204. * Form submit callback for the issue operations.
  205. */
  206. function simplenews_admin_issues_submit($form, &$form_state) {
  207. // Call operation functions as defined in hook_simplenews_issue_operations().
  208. $operations = module_invoke_all('simplenews_issue_operations');
  209. $operation = $operations[$form_state['values']['operation']];
  210. // Filter out unchecked list issues
  211. $nids = array_filter($form_state['values']['issues']);
  212. if ($function = $operation['callback']) {
  213. // Add in callback arguments if present.
  214. if (isset($operation['callback arguments'])) {
  215. $args = array_merge(array($nids), $operation['callback arguments']);
  216. }
  217. else {
  218. $args = array($nids);
  219. }
  220. call_user_func_array($function, $args);
  221. }
  222. else {
  223. // We need to rebuild the form to go to a second step. For example, to
  224. // show the confirmation form for the deletion of nodes.
  225. $form_state['rebuild'] = TRUE;
  226. }
  227. }
  228. /**
  229. * Callback to send newsletters.
  230. */
  231. function simplenews_issue_send($nids) {
  232. $sent_nodes = array();
  233. foreach (node_load_multiple($nids) as $node) {
  234. $newsletter = simplenews_newsletter_load($node->nid);
  235. if ($newsletter->status != SIMPLENEWS_STATUS_SEND_NOT) {
  236. continue;
  237. }
  238. if ($node->status == NODE_NOT_PUBLISHED) {
  239. simplenews_newsletter_update_sent_status($node, SIMPLENEWS_COMMAND_SEND_PUBLISH);
  240. drupal_set_message(t('Newsletter %title is unpublished and will be sent on publish.', array('%title' => $node->title)));
  241. continue;
  242. }
  243. simplenews_add_node_to_spool($node);
  244. $sent_nodes[$node->nid] = $node->title;
  245. }
  246. // If there were any newsletters sent, display a message.
  247. if (!empty($sent_nodes)) {
  248. $conditions = array('nid' => array_keys($sent_nodes));
  249. // Attempt to send immediatly, if configured to do so.
  250. if (simplenews_mail_attempt_immediate_send($conditions)) {
  251. drupal_set_message(t('Sent the following newsletters: %titles.', array('%titles' => implode(', ', $sent_nodes))));
  252. }
  253. else {
  254. drupal_set_message(t('The following newsletter are now pending: %titles.', array('%titles' => implode(', ', $sent_nodes))));
  255. }
  256. }
  257. }
  258. /**
  259. * Apply filters for subscription filters based on session.
  260. *
  261. * @param $query
  262. * A SelectQuery to which the filters should be applied.
  263. */
  264. function simplenews_build_issue_filter_query(SelectQueryInterface $query) {
  265. if (isset($_SESSION['simplenews_issue_filter'])) {
  266. foreach ($_SESSION['simplenews_issue_filter'] as $key => $value) {
  267. switch ($key) {
  268. case 'list':
  269. case 'category':
  270. if ($value != 'all') {
  271. list($key, $value) = explode('-', $value, 2);
  272. $query->condition('sn.' . $key, $value);
  273. }
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. /**
  280. * Menu callback: list admin form with list of available list categories.
  281. *
  282. * @ingroup forms
  283. * @see simplenews_admin_newsletter_categories_submit()
  284. * @see theme_simplenews_admin_newsletter_categories()
  285. */
  286. function simplenews_admin_categories() {
  287. $form['#tree'] = TRUE;
  288. if ($categories = simplenews_categories_load_multiple(array(), array('show_all' => TRUE))) {
  289. foreach ($categories as $category) {
  290. $form[$category->tid]['#category'] = $category;
  291. $form[$category->tid]['name'] = array('#markup' => check_plain(_simplenews_newsletter_name($category)));
  292. $form[$category->tid]['weight'] = array('#type' => 'weight', '#delta' => 10, '#default_value' => $category->weight);
  293. $form[$category->tid]['edit'] = array(
  294. '#type' => 'link',
  295. '#title' => t('edit newsletter category'),
  296. '#href' => "admin/config/services/simplenews/categories/$category->tid/edit",
  297. );
  298. }
  299. }
  300. // Only make this form include a submit button and weight if more than one
  301. // category exists.
  302. if (count($categories) > 1) {
  303. $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  304. }
  305. elseif (!empty($categories)) {
  306. $form[$category->tid]['weight'] = array('#type' => 'value', '#value' => 0);
  307. }
  308. return $form;
  309. }
  310. /**
  311. * Form submit callback for the simplenews categories.
  312. */
  313. function simplenews_admin_categories_submit($form, &$form_state) {
  314. foreach ($form_state['values'] as $tid => $category) {
  315. if (is_numeric($tid) && $form[$tid]['#category']->weight != $category['weight']) {
  316. $form[$tid]['#category']->weight = $category['weight'];
  317. simplenews_category_save($form[$tid]['#category']);
  318. }
  319. }
  320. drupal_set_message(t('Your configuration has been saved.'));
  321. }
  322. /**
  323. * Form builder function, display a list of simplenews categories.
  324. *
  325. * @ingroup theming
  326. */
  327. function theme_simplenews_admin_categories($variables) {
  328. $form = $variables['form'];
  329. $rows = array();
  330. foreach (element_children($form) as $key) {
  331. if (isset($form[$key]['name'])) {
  332. $category = &$form[$key];
  333. $row = array();
  334. $row[] = drupal_render($category['name']);
  335. if (isset($category['weight'])) {
  336. $category['weight']['#attributes']['class'] = array('simplenews-category-weight');
  337. $row[] = drupal_render($category['weight']);
  338. }
  339. $row[] = drupal_render($category['edit']);
  340. $rows[] = array('data' => $row, 'class' => array('draggable'));
  341. }
  342. }
  343. $header = array(t('Newsletter category name'));
  344. if (isset($form['submit'])) {
  345. $header[] = t('Weight');
  346. drupal_add_tabledrag('newsletter-category', 'order', 'self', 'simplenews-category-weight');
  347. }
  348. $header[] = array('data' => t('Operations'), 'colspan' => '3');
  349. return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('No newsletter categories. <a href="@link">Add category</a>.', array('@link' => url('admin/config/services/simplenews/add'))), 'attributes' => array('id' => 'newsletter-category'))) . drupal_render_children($form);
  350. }
  351. /**
  352. * Menu callback: newsletter admin form for newsletter add/edit.
  353. *
  354. * @see simplenews_admin_category_form_validate()
  355. * @see simplenews_admin_category_form_submit()
  356. */
  357. function simplenews_admin_category_form($form, &$form_state, $edit = array()) {
  358. if (!is_array($edit)) {
  359. $edit = (array) $edit;
  360. }
  361. $edit += array(
  362. 'tid' => 0,
  363. 'name' => '',
  364. 'description' => '',
  365. 'weight' => '0',
  366. 'new_account' => 'none',
  367. 'opt_inout' => 'double',
  368. 'block' => 1,
  369. 'format' => variable_get('simplenews_format', 'plain'),
  370. 'priority' => variable_get('simplenews_priority', SIMPLENEWS_PRIORITY_NONE),
  371. 'receipt' => variable_get('simplenews_receipt', 0),
  372. 'from_name' => variable_get('simplenews_from_name', variable_get('site_name', 'Drupal')),
  373. 'email_subject' => '[[simplenews-category:name]] [node:title]',
  374. 'from_address' => variable_get('simplenews_from_address', variable_get('site_mail', ini_get('sendmail_from'))),
  375. 'hyperlinks' => 1,
  376. );
  377. $form['#category'] = (object) $edit;
  378. // Check whether we need a deletion confirmation form.
  379. if (isset($form_state['confirm_delete']) && isset($form_state['values']['tid'])) {
  380. $category = simplenews_category_load($form_state['values']['tid']);
  381. return simplenews_admin_category_delete($form, $form_state, $category);
  382. }
  383. $form['tid'] = array(
  384. '#type' => 'value',
  385. '#value' => $edit['tid'],
  386. );
  387. $form['name'] = array(
  388. '#type' => 'textfield',
  389. '#title' => t('Name'),
  390. '#default_value' => $edit['name'],
  391. '#maxlength' => 255,
  392. '#required' => TRUE,
  393. );
  394. $form['description'] = array(
  395. '#type' => 'textfield',
  396. '#title' => t('Description'),
  397. '#default_value' => $edit['description'],
  398. );
  399. $form['weight'] = array(
  400. '#type' => 'hidden',
  401. '#value' => $edit['weight'],
  402. );
  403. $form['subscription'] = array(
  404. '#type' => 'fieldset',
  405. '#title' => t('Subscription settings'),
  406. '#collapsible' => FALSE,
  407. );
  408. // Subscribe at account registration time.
  409. $options = array(
  410. 'none' => t('None'),
  411. 'on' => t('Default on'),
  412. 'off' => t('Default off'),
  413. 'silent' => t('Silent'),
  414. );
  415. $form['subscription']['new_account'] = array(
  416. '#type' => 'select',
  417. '#title' => t('Subscribe new account'),
  418. '#options' => $options,
  419. '#default_value' => $edit['new_account'],
  420. '#description' => t('None: This newsletter is not listed on the user registration page.<br />Default on: This newsletter is listed on the user registion page and is selected by default.<br />Default off: This newsletter is listed on the user registion page and is not selected by default.<br />Silent: A new user is automatically subscribed to this newsletter. The newsletter is not listed on the user registration page.'),
  421. );
  422. // Type of (un)subsribe confirmation
  423. $options = array(
  424. SIMPLENEWS_OPT_INOUT_HIDDEN => t('Hidden'),
  425. SIMPLENEWS_OPT_INOUT_SINGLE => t('Single'),
  426. SIMPLENEWS_OPT_INOUT_DOUBLE => t('Double'),
  427. );
  428. $form['subscription']['opt_inout'] = array(
  429. '#type' => 'select',
  430. '#title' => t('Opt-in/out method'),
  431. '#options' => $options,
  432. '#default_value' => $edit['opt_inout'],
  433. '#description' => t('Hidden: This newsletter does not appear on subscription forms. No unsubscription footer in newsletter.<br /> Single: Users are (un)subscribed immediately, no confirmation email is sent.<br />Double: When (un)subscribing at a subscription form, anonymous users receive an (un)subscription confirmation email. Authenticated users are (un)subscribed immediately.'),
  434. );
  435. // Provide subscription block for this category.
  436. $form['subscription']['block'] = array(
  437. '#type' => 'checkbox',
  438. '#title' => t('Subscription block'),
  439. '#default_value' => $edit['block'],
  440. '#description' => t('A subscription block will be provided for this newsletter category. Anonymous and authenticated users can subscribe and unsubscribe using this block.'),
  441. );
  442. $form['email'] = array(
  443. '#type' => 'fieldset',
  444. '#title' => t('Email settings'),
  445. '#collapsible' => FALSE,
  446. );
  447. // Hide format selection if there is nothing to choose.
  448. // The default format is plain text.
  449. $format_options = simplenews_format_options();
  450. if (count($format_options) > 1) {
  451. $form['email']['format'] = array(
  452. '#type' => 'radios',
  453. '#title' => t('Email format'),
  454. '#default_value' => $edit['format'],
  455. '#options' => $format_options,
  456. );
  457. }
  458. else {
  459. $form['email']['format'] = array(
  460. '#type' => 'hidden',
  461. '#value' => key($format_options),
  462. );
  463. $form['email']['format_text'] = array(
  464. '#markup' => t('Newsletter emails will be sent in %format format.', array('%format' => $edit['format'])),
  465. );
  466. }
  467. $form['email']['priority'] = array(
  468. '#type' => 'select',
  469. '#title' => t('Email priority'),
  470. '#default_value' => $edit['priority'],
  471. '#options' => simplenews_get_priority(),
  472. );
  473. $form['email']['receipt'] = array(
  474. '#type' => 'checkbox',
  475. '#title' => t('Request receipt'),
  476. '#return_value' => 1,
  477. '#default_value' => $edit['receipt'],
  478. );
  479. // Email sender name
  480. $form['simplenews_sender_information'] = array(
  481. '#type' => 'fieldset',
  482. '#title' => t('Sender information'),
  483. '#collapsible' => FALSE,
  484. );
  485. $form['simplenews_sender_information']['from_name'] = array(
  486. '#type' => 'textfield',
  487. '#title' => t('From name'),
  488. '#size' => 60,
  489. '#maxlength' => 128,
  490. '#default_value' => $edit['from_name'],
  491. );
  492. // Email subject
  493. $form['simplenews_subject'] = array(
  494. '#type' => 'fieldset',
  495. '#title' => t('Newsletter subject'),
  496. '#collapsible' => FALSE,
  497. );
  498. if (module_exists('token')) {
  499. $form['simplenews_subject']['token_help'] = array(
  500. '#title' => t('Replacement patterns'),
  501. '#type' => 'fieldset',
  502. '#collapsible' => TRUE,
  503. '#collapsed' => TRUE,
  504. );
  505. $form['simplenews_subject']['token_help']['browser'] = array(
  506. '#theme' => 'token_tree',
  507. '#token_types' => array('simplenews-category', 'node', 'simplenews-subscriber'),
  508. );
  509. }
  510. $form['simplenews_subject']['email_subject'] = array(
  511. '#type' => 'textfield',
  512. '#title' => t('Email subject'),
  513. '#size' => 60,
  514. '#maxlength' => 128,
  515. '#required' => TRUE,
  516. '#default_value' => $edit['email_subject'],
  517. );
  518. // Email from address
  519. $form['simplenews_sender_information']['from_address'] = array(
  520. '#type' => 'textfield',
  521. '#title' => t('From email address'),
  522. '#size' => 60,
  523. '#maxlength' => 128,
  524. '#required' => TRUE,
  525. '#default_value' => $edit['from_address'],
  526. );
  527. // Type of hyperlinks
  528. $form['simplenews_hyperlinks'] = array(
  529. '#type' => 'fieldset',
  530. '#title' => t('HTML to text conversion'),
  531. '#collapsible' => FALSE,
  532. '#description' => t('When your newsletter is sent as plain text, these options will determine how the conversion to text is performed.'),
  533. );
  534. $form['simplenews_hyperlinks']['hyperlinks'] = array(
  535. '#type' => 'radios',
  536. '#title' => t('Hyperlink conversion'),
  537. '#options' => array(t('Append hyperlinks as a numbered reference list'), t('Display hyperlinks inline with the text')),
  538. '#default_value' => $edit['hyperlinks'],
  539. );
  540. $form['actions'] = array('#type' => 'actions');
  541. $form['actions']['submit'] = array(
  542. '#type' => 'submit',
  543. '#value' => t('Save'),
  544. '#weight' => 50,
  545. );
  546. if ($edit['tid']) {
  547. $form['actions']['delete'] = array(
  548. '#type' => 'submit',
  549. '#value' => t('Delete'),
  550. '#weight' => 55,
  551. );
  552. }
  553. return $form;
  554. }
  555. /**
  556. * Form validation callback for a newsletter category form.
  557. */
  558. function simplenews_admin_category_form_validate($form, &$form_state) {
  559. if ($form_state['clicked_button']['#value'] != t('Delete')) {
  560. // Check for valid email address.
  561. if (!valid_email_address($form_state['values']['from_address'])) {
  562. form_set_error('from_address', t("The sender's email address you supplied is not valid."));
  563. }
  564. }
  565. }
  566. /**
  567. * Form submit callback for a newsletter category form.
  568. */
  569. function simplenews_admin_category_form_submit($form, &$form_state) {
  570. //dpm($form_state);
  571. $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
  572. if ($op == t('Delete')) {
  573. $form_state['redirect'] = 'admin/config/services/simplenews/categories/' . $form_state['values']['tid'] . '/delete';
  574. return;
  575. }
  576. $category = (object) $form_state['values'];
  577. // Create or update taxonomy term.
  578. $term = new stdClass();
  579. $term->tid = $form_state['values']['tid'];
  580. $term->vocabulary_machine_name = 'newsletter';
  581. $term->vid = taxonomy_vocabulary_machine_name_load('newsletter')->vid;
  582. $term->name = $form_state['values']['name'];
  583. $term->description = $form_state['values']['description'];
  584. $term->weight = $form_state['values']['weight'];
  585. taxonomy_term_save($term);
  586. $category->tid = $term->tid;
  587. switch (simplenews_category_save($category)) {
  588. case SAVED_NEW:
  589. drupal_set_message(t('Created new newsletter category %name.', array('%name' => _simplenews_newsletter_name($category))));
  590. watchdog('simplenews', 'Created new newsletter category %name.', array('%name' => _simplenews_newsletter_name($category), WATCHDOG_NOTICE, l(t('edit'), 'admin/config/services/simplenews/categories/' . $category->tid . '/edit')));
  591. break;
  592. case SAVED_UPDATED:
  593. drupal_set_message(t('Updated newsletter category %name.', array('%name' => _simplenews_newsletter_name($category))));
  594. watchdog('simplenews', 'Updated newsletter category %name.', array('%name' => _simplenews_newsletter_name($category)), WATCHDOG_NOTICE, l(t('edit'), 'admin/config/services/simplenews/categories/' . $category->tid . '/edit'));
  595. break;
  596. }
  597. $form_state['values']['tid'] = $category->tid;
  598. $form_state['tid'] = $category->tid;
  599. $form_state['redirect'] = 'admin/config/services/simplenews';
  600. }
  601. /**
  602. * Menu callback: Delete newsletter category.
  603. *
  604. * @see simplenews_admin_category_delete_submit()
  605. */
  606. function simplenews_admin_category_delete($form, &$form_state, $category) {
  607. // Store some category values for submit handling.
  608. $form = array();
  609. $form['tid'] = array('#type' => 'value', '#value' => $category->tid);
  610. $form['name'] = array('#type' => 'value', '#value' => _simplenews_newsletter_name($category));
  611. $form['notice'] = array(
  612. '#markup' => '<p><strong>' . t('Note: All subscriptions associated with this newsletter will be lost.') . '</strong></p>',
  613. );
  614. return confirm_form($form, t('Are you sure you want to delete category %name?', array('%name' => _simplenews_newsletter_name($category))), 'admin/config/services/simplenews', t('This action cannot be undone.'), t('Delete'), t('Cancel')
  615. );
  616. }
  617. /**
  618. * Form submit callback for deleting a simplenews category.
  619. */
  620. function simplenews_admin_category_delete_submit($form, &$form_state) {
  621. $tid = $form_state['values']['tid'];
  622. $name = $form_state['values']['name'];
  623. // Delete newsletter category and associated taxonomy term.
  624. // Subscriptions are deleted by simplenews_simplenews_category_delete()
  625. simplenews_category_delete($tid);
  626. taxonomy_term_delete($tid);
  627. drupal_set_message(t('Newsletter category %name has been deleted.', array('%name' => $name)));
  628. $form_state['redirect'] = 'admin/config/services/simplenews';
  629. return;
  630. }
  631. /**
  632. * Menu callback: Mass subscribe to newsletters.
  633. *
  634. * @see simplenews_subscription_list_add_submit()
  635. *
  636. * @todo Add 32char description field as subsription source
  637. */
  638. function simplenews_subscription_list_add($form, &$form_state) {
  639. global $language;
  640. $form['emails'] = array(
  641. '#type' => 'textarea',
  642. '#title' => t('Email addresses'),
  643. '#cols' => 60,
  644. '#rows' => 5,
  645. '#description' => t('Email addresses must be separated by comma, space or newline.'),
  646. );
  647. $form['newsletters'] = array(
  648. '#type' => 'fieldset',
  649. '#description' => t('Subscribe to'),
  650. '#tree' => TRUE,
  651. );
  652. foreach (simplenews_categories_load_multiple() as $list) {
  653. $form['newsletters'][$list->tid] = array(
  654. '#type' => 'checkbox',
  655. '#title' => check_plain(_simplenews_newsletter_name($list)),
  656. '#description' => _simplenews_newsletter_description($list),
  657. );
  658. }
  659. $form['resubscribe'] = array(
  660. '#type' => 'checkbox',
  661. '#title' => t('Force resubscription'),
  662. '#description' => t('If checked, previously unsubscribed e-mail addresses will be resubscribed. Consider that this might be against the will of your users.'),
  663. );
  664. // Include language selection when the site is multilingual.
  665. // Default value is the empty string which will result in receiving emails
  666. // in the site's default language.
  667. if (variable_get('language_count', 1) > 1) {
  668. $options[''] = t('Site default language');
  669. $languages = language_list('enabled');
  670. foreach ($languages[1] as $langcode => $item) {
  671. $name = t($item->name);
  672. $options[$langcode] = $name . ($item->native != $name ? ' (' . $item->native . ')' : '');
  673. }
  674. $form['language'] = array(
  675. '#type' => 'radios',
  676. '#title' => t('Anonymous user preferred language'),
  677. '#default_value' => '',
  678. '#options' => $options,
  679. '#description' => t('New subscriptions will be subscribed with the selected preferred language. The language of existing subscribers is unchanged.'),
  680. );
  681. }
  682. else {
  683. $form['language'] = array(
  684. '#type' => 'value',
  685. '#value' => '',
  686. );
  687. }
  688. $form['submit'] = array(
  689. '#type' => 'submit',
  690. '#value' => t('Subscribe'),
  691. );
  692. return $form;
  693. }
  694. /**
  695. * @todo
  696. */
  697. function simplenews_subscription_list_add_submit($form, &$form_state) {
  698. $added = array();
  699. $invalid = array();
  700. $unsubscribed = array();
  701. $checked_categories = array_keys(array_filter($form_state['values']['newsletters']));
  702. $langcode = $form_state['values']['language'];
  703. $emails = preg_split("/[\s,]+/", $form_state['values']['emails']);
  704. foreach ($emails as $email) {
  705. $email = trim($email);
  706. if ($email == '') {
  707. continue;
  708. }
  709. if (valid_email_address($email)) {
  710. $subscriber = simplenews_subscriber_load_by_mail($email);
  711. foreach (simplenews_categories_load_multiple($checked_categories) as $category) {
  712. // If there is a valid subscriber, check if there is a subscription for
  713. // the current category and if this subscription has the status
  714. // unsubscribed.
  715. $is_unsubscribed = $subscriber && array_key_exists($category->tid, $subscriber->newsletter_subscription)
  716. && $subscriber->newsletter_subscription[$category->tid]->status == SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED;
  717. if (!$is_unsubscribed || $form_state['values']['resubscribe'] == TRUE) {
  718. simplenews_subscribe_user($email, $category->tid, FALSE, 'mass subscribe', $langcode);
  719. $added[] = $email;
  720. }
  721. else {
  722. $unsubscribed[check_plain(_simplenews_newsletter_name($category))][] = $email;
  723. }
  724. }
  725. }
  726. else {
  727. $invalid[] = $email;
  728. }
  729. }
  730. if ($added) {
  731. $added = implode(", ", $added);
  732. drupal_set_message(t('The following addresses were added or updated: %added.', array('%added' => $added)));
  733. $list_names = array();
  734. foreach (simplenews_categories_load_multiple($checked_categories) as $category) {
  735. $list_names[] = $category->name;
  736. }
  737. drupal_set_message(t('The addresses were subscribed to the following newsletters: %newsletters.', array('%newsletters' => implode(', ', $list_names))));
  738. }
  739. else {
  740. drupal_set_message(t('No addresses were added.'));
  741. }
  742. if ($invalid) {
  743. $invalid = implode(", ", $invalid);
  744. drupal_set_message(t('The following addresses were invalid: %invalid.', array('%invalid' => $invalid)), 'error');
  745. }
  746. foreach ($unsubscribed as $name => $subscribers) {
  747. $subscribers = implode(", ", $subscribers);
  748. drupal_set_message(t('The following addresses were skipped because they have previously unsubscribed from %name: %unsubscribed.', array('%name' => $name, '%unsubscribed' => $subscribers)), 'warning');
  749. }
  750. if (!empty($unsubscribed)) {
  751. drupal_set_message(t("If you would like to resubscribe them, use the 'Force resubscription' option."), 'warning');
  752. }
  753. // Return to the parent page.
  754. $form_state['redirect'] = 'admin/people/simplenews';
  755. }
  756. /**
  757. * Menu callback: Export email address of subscriptions.
  758. *
  759. * @see simplenews_admin_export_after_build()
  760. */
  761. function simplenews_subscription_list_export($form, &$form_state) {
  762. // Get sensible default values for the form elements in this form.
  763. $default['states'] = isset($_GET['states']) ? $_GET['states'] : array('active' => 'active');
  764. $default['subscribed'] = isset($_GET['subscribed']) ? $_GET['subscribed'] : array('subscribed' => 'subscribed');
  765. $default['newsletters'] = isset($_GET['newsletters']) ? $_GET['newsletters'] : array();
  766. $form['states'] = array(
  767. '#type' => 'checkboxes',
  768. '#title' => t('Status'),
  769. '#options' => array(
  770. 'active' => t('Active users'),
  771. 'inactive' => t('Inactive users'),
  772. ),
  773. '#default_value' => $default['states'],
  774. '#description' => t('Subscriptions matching the selected states will be exported.'),
  775. '#required' => TRUE,
  776. );
  777. $form['subscribed'] = array(
  778. '#type' => 'checkboxes',
  779. '#title' => t('Subscribed'),
  780. '#options' => array(
  781. 'subscribed' => t('Subscribed to the newsletter'),
  782. 'unconfirmed' => t('Unconfirmed to the newsletter'),
  783. 'unsubscribed' => t('Unsubscribed from the newsletter'),
  784. ),
  785. '#default_value' => $default['subscribed'],
  786. '#description' => t('Subscriptions matching the selected subscription states will be exported.'),
  787. '#required' => TRUE,
  788. );
  789. $options = simplenews_category_list();
  790. $form['newsletters'] = array(
  791. '#type' => 'checkboxes',
  792. '#title' => t('Newsletter'),
  793. '#options' => $options,
  794. '#default_value' => $default['newsletters'],
  795. '#description' => t('Subscriptions matching the selected newsletters will be exported.'),
  796. '#required' => TRUE,
  797. );
  798. // Get export results and display them in a text area. Only get the results
  799. // if the form is build after redirect, not after submit.
  800. if (isset($_GET['states']) && empty($form_state['input'])) {
  801. $form['emails'] = array(
  802. '#type' => 'textarea',
  803. '#title' => t('Export results'),
  804. '#cols' => 60,
  805. '#rows' => 5,
  806. '#value' => _simplenews_subscription_list_export_get_emails($_GET['states'], $_GET['subscribed'], $_GET['newsletters']),
  807. );
  808. }
  809. $form['submit'] = array(
  810. '#type' => 'submit',
  811. '#value' => t('Export'),
  812. );
  813. return $form;
  814. }
  815. /**
  816. * @todo
  817. */
  818. function simplenews_subscription_list_export_submit($form, &$form_state) {
  819. $form_values = $form_state['values'];
  820. // Get data for query string and redirect back to the current page.
  821. $options['query']['states'] = array_filter($form_values['states']);
  822. $options['query']['subscribed'] = array_filter($form_values['subscribed']);
  823. $options['query']['newsletters'] = array_keys(array_filter($form_values['newsletters']));
  824. $form_state['redirect'] = array('admin/people/simplenews/export', $options);
  825. }
  826. /**
  827. * Helper function to get comma separated list of emails to be exported.
  828. *
  829. * @param $states
  830. * Array of subscriber states to filter on.
  831. * @param $subscribed
  832. * Array of subscription states to filter on.
  833. * @param $newsletters
  834. * Array of taxonomy ids to fitler on.
  835. * @return string
  836. * Comma separated list of email adresses.
  837. */
  838. function _simplenews_subscription_list_export_get_emails($states, $subscribed, $newsletters) {
  839. // Build conditions for active state, subscribed state and newsletter selection.
  840. if (isset($states['active'])) {
  841. $condition_active[] = 1;
  842. }
  843. if (isset($states['inactive'])) {
  844. $condition_active[] = 0;
  845. }
  846. if (isset($subscribed['subscribed'])) {
  847. $condition_subscribed[] = SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED;
  848. }
  849. if (isset($subscribed['unsubscribed'])) {
  850. $condition_subscribed[] = SIMPLENEWS_SUBSCRIPTION_STATUS_UNSUBSCRIBED;
  851. }
  852. if (isset($subscribed['unconfirmed'])) {
  853. $condition_subscribed[] = SIMPLENEWS_SUBSCRIPTION_STATUS_UNCONFIRMED;
  854. }
  855. // Get emails from the database.
  856. $query = db_select('simplenews_subscriber', 'ss');
  857. $query->innerJoin('simplenews_subscription', 'si', 'si.snid = ss.snid');
  858. $query->fields('ss', array('mail'))
  859. ->condition('ss.activated', $condition_active)
  860. ->condition('si.status', $condition_subscribed)
  861. ->condition('si.tid', $newsletters)
  862. ->distinct();
  863. $mails = $query->execute()->fetchCol(0);
  864. // Return comma separated array of emails or empty text.
  865. if ($mails) {
  866. return implode(", ", $mails);
  867. }
  868. return t('No addresses were found.');
  869. }
  870. /**
  871. * Menu callback: Mass subscribe to newsletters.
  872. *
  873. * @see simplenews_subscription_list_remove_submit()
  874. *
  875. * @todo Add 32char description field as unsubsription source
  876. */
  877. function simplenews_subscription_list_remove($form, &$form_state) {
  878. $form['emails'] = array(
  879. '#type' => 'textarea',
  880. '#title' => t('Email addresses'),
  881. '#cols' => 60,
  882. '#rows' => 5,
  883. '#description' => t('Email addresses must be separated by comma, space or newline.'),
  884. );
  885. $form['newsletters'] = array(
  886. '#type' => 'fieldset',
  887. '#description' => t('Unsubscribe from'),
  888. '#tree' => TRUE,
  889. );
  890. foreach (simplenews_categories_load_multiple() as $category) {
  891. $form['newsletters'][$category->tid] = array(
  892. '#type' => 'checkbox',
  893. '#title' => check_plain(_simplenews_newsletter_name($category)),
  894. '#description' => _simplenews_newsletter_description($category),
  895. );
  896. }
  897. $form['submit'] = array(
  898. '#type' => 'submit',
  899. '#value' => t('Unsubscribe'),
  900. );
  901. return $form;
  902. }
  903. /**
  904. * @todo
  905. */
  906. function simplenews_subscription_list_remove_submit($form, &$form_state) {
  907. $removed = array();
  908. $invalid = array();
  909. $checked_lists = array_keys(array_filter($form_state['values']['newsletters']));
  910. $emails = preg_split("/[\s,]+/", $form_state['values']['emails']);
  911. foreach ($emails as $email) {
  912. $email = trim($email);
  913. if (valid_email_address($email)) {
  914. foreach ($checked_lists as $tid) {
  915. simplenews_unsubscribe_user($email, $tid, FALSE, 'mass unsubscribe');
  916. $removed[] = $email;
  917. }
  918. }
  919. else {
  920. $invalid[] = $email;
  921. }
  922. }
  923. if ($removed) {
  924. $removed = implode(", ", $removed);
  925. drupal_set_message(t('The following addresses were unsubscribed: %removed.', array('%removed' => $removed)));
  926. $lists = simplenews_categories_load_multiple();
  927. $list_names = array();
  928. foreach ($checked_lists as $tid) {
  929. $list_names[] = _simplenews_newsletter_name($lists[$tid]);
  930. }
  931. drupal_set_message(t('The addresses were unsubscribed from the following newsletters: %newsletters.', array('%newsletters' => implode(', ', $list_names))));
  932. }
  933. else {
  934. drupal_set_message(t('No addresses were removed.'));
  935. }
  936. if ($invalid) {
  937. $invalid = implode(", ", $invalid);
  938. drupal_set_message(t('The following addresses were invalid: %invalid.', array('%invalid' => $invalid)), 'error');
  939. }
  940. // Return to the parent page.
  941. $form_state['redirect'] = 'admin/people/simplenews';
  942. }
  943. /**
  944. * Menu callback: subscription administration.
  945. */
  946. function simplenews_admin_subscription() {
  947. // @todo Fix the delete operation
  948. $form['filter'] = simplenews_subscription_filter_form();
  949. $form['#submit'][] = 'simplenews_subscription_filter_form_submit';
  950. $form['filter']['#theme'] = 'simplenews_filter_form';
  951. $form['admin'] = simplenews_subscription_list_form();
  952. return $form;
  953. }
  954. /**
  955. * Menu callback: subscription administration.
  956. *
  957. * @see simplenews_subscription_list_form_validate()
  958. * @see simplenews_subscription_list_form_submit()
  959. *
  960. * @todo Subscriber maintanance needs overhaul now we have more data availabale.
  961. */
  962. function simplenews_subscription_list_form() {
  963. // Build an 'Update options' form.
  964. $form['options'] = array(
  965. '#type' => 'fieldset',
  966. '#title' => t('Update options'),
  967. '#prefix' => '<div class="container-inline">',
  968. '#suffix' => '</div>',
  969. );
  970. $options = array();
  971. foreach (module_invoke_all('simplenews_subscription_operations') as $operation => $array) {
  972. $options[$operation] = $array['label'];
  973. }
  974. $form['options']['operation'] = array(
  975. '#type' => 'select',
  976. '#options' => $options,
  977. '#default_value' => 'activate',
  978. );
  979. $form['options']['submit'] = array(
  980. '#type' => 'submit',
  981. '#value' => t('Update'),
  982. '#submit' => array('simplenews_subscription_list_form_submit'),
  983. '#validate' => array('simplenews_subscription_list_form_validate'),
  984. );
  985. // Table header. Used as tablesort default
  986. $header = array(
  987. 'mail' => array('data' => t('Email'), 'field' => 'sn.mail', 'sort' => 'asc'),
  988. 'username' => array('data' => t('Username'), 'field' => 'u.name'),
  989. 'status' => array('data' => t('Status'), 'field' => 'sn.activated'),
  990. 'language' => array('data' => t('Language'), 'field' => 'sn.language'),
  991. 'operations' => array('data' => t('Operations')),
  992. );
  993. $query = db_select('simplenews_subscriber', 'sn')->extend('PagerDefault')->extend('TableSort');
  994. simplenews_build_subscription_filter_query($query);
  995. $query->leftJoin('users', 'u', 'sn.uid = u.uid');
  996. $query->innerJoin('simplenews_subscription', 'su', 'sn.snid = su.snid');
  997. $query->condition('su.status', SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
  998. $query->addField('u', 'name', 'name');
  999. $result = $query
  1000. ->fields('sn', array('snid', 'activated', 'mail', 'uid', 'language'))
  1001. ->limit(30)
  1002. ->orderByHeader($header)
  1003. ->execute();
  1004. $options = array();
  1005. $destination = drupal_get_destination();
  1006. foreach ($result as $subscriber) {
  1007. $options[$subscriber->snid] = array(
  1008. 'mail' => check_plain($subscriber->mail),
  1009. 'username' => isset($subscriber->uid) ? l($subscriber->name, 'user/' . $subscriber->uid) : check_plain($subscriber->name),
  1010. 'status' => theme('simplenews_status', array('source' => 'activated', 'status' => $subscriber->activated)),
  1011. 'language' => check_plain($subscriber->language),
  1012. 'operations' => l(t('edit'), 'admin/people/simplenews/users/edit/' . $subscriber->snid, array(), $destination),
  1013. );
  1014. }
  1015. $form['subscribers'] = array(
  1016. '#type' => 'tableselect',
  1017. '#header' => $header,
  1018. '#options' => $options,
  1019. '#empty' => t('No subscribers available.'),
  1020. );
  1021. $form['pager'] = array(
  1022. // Calling theme('pager') directly so that it the first call after the
  1023. // pager query executed above.
  1024. '#markup' => theme('pager'),
  1025. );
  1026. return $form;
  1027. }
  1028. /**
  1029. * Implements hook_simplenews_subscription_operations().
  1030. */
  1031. function simplenews_simplenews_subscription_operations() {
  1032. $operations = array(
  1033. 'activate' => array(
  1034. 'label' => t('Activate'),
  1035. 'callback' => 'simplenews_subscription_activate',
  1036. 'callback arguments' => array(SIMPLENEWS_SUBSCRIPTION_ACTIVE),
  1037. ),
  1038. 'inactivate' => array(
  1039. 'label' => t('Inactivate'),
  1040. 'callback' => 'simplenews_subscription_activate',
  1041. 'callback arguments' => array(SIMPLENEWS_SUBSCRIPTION_INACTIVE),
  1042. ),
  1043. 'delete' => array(
  1044. 'label' => t('Delete'),
  1045. 'callback' => 'simplenews_subscription_delete_multiple',
  1046. ),
  1047. );
  1048. return $operations;
  1049. }
  1050. /**
  1051. * @todo
  1052. */
  1053. function simplenews_subscription_list_form_validate($form, &$form_state) {
  1054. if (isset($form_state['values']['operation'])) {
  1055. $snids = array_keys(array_filter($form_state['values']['subscribers']));
  1056. if (empty($snids)) {
  1057. form_set_error('', t('No items selected.'));
  1058. }
  1059. }
  1060. }
  1061. /**
  1062. * @todo
  1063. */
  1064. function simplenews_subscription_list_form_submit($form, &$form_state) {
  1065. // Call operation functions as defined in hook_simplenews_subscription_operations().
  1066. $operations = module_invoke_all('simplenews_subscription_operations');
  1067. $operation = $operations[$form_state['values']['operation']];
  1068. // Filter out unchecked subscribers
  1069. $snids = array_filter($form_state['values']['subscribers']);
  1070. if ($function = $operation['callback']) {
  1071. // Add in callback arguments if present.
  1072. if (isset($operation['callback arguments'])) {
  1073. $args = array_merge(array($snids), $operation['callback arguments']);
  1074. }
  1075. else {
  1076. $args = array($snids);
  1077. }
  1078. call_user_func_array($function, $args);
  1079. drupal_set_message(t('The update has been performed.'));
  1080. }
  1081. else {
  1082. // We need to rebuild the form to go to a second step. For example, to
  1083. // show the confirmation form for the deletion of nodes.
  1084. $form_state['rebuild'] = TRUE;
  1085. }
  1086. }
  1087. /**
  1088. * Callback function to (de-)activate subscriptions.
  1089. *
  1090. * @param $snids
  1091. * Array of snid's to be activated.
  1092. * @param $status
  1093. * Status of the subscription (0, 1).
  1094. */
  1095. function simplenews_subscription_activate($snids, $status) {
  1096. foreach ($snids as $snid) {
  1097. $subscriber = simplenews_subscriber_load($snid);
  1098. $subscriber->activated = $status;
  1099. simplenews_subscriber_save($subscriber);
  1100. }
  1101. }
  1102. /**
  1103. * Callback function to delete subscriptions.
  1104. *
  1105. * @param $snids
  1106. * Array of snid's to be deleted.
  1107. */
  1108. function simplenews_subscription_delete_multiple($snids = array()) {
  1109. foreach ($snids as $snid) {
  1110. // Delete Subscription
  1111. simplenews_subscription_delete(array('snid' => $snid));
  1112. // Delete subscriber
  1113. $subscriber = simplenews_subscriber_load($snid);
  1114. simplenews_subscriber_delete($subscriber);
  1115. }
  1116. }
  1117. /**
  1118. * Menu callback: Simplenews admin settings - Newsletter.
  1119. */
  1120. function simplenews_admin_settings_newsletter($form, &$form_state) {
  1121. $address_default = variable_get('site_mail', ini_get('sendmail_from'));
  1122. $form = array();
  1123. $form['simplenews_default_options'] = array(
  1124. '#type' => 'fieldset',
  1125. '#title' => t('Default newsletter options'),
  1126. '#collapsible' => FALSE,
  1127. '#description' => t('These options will be the defaults for new newsletters, but can be overridden in the newsletter editing form.'),
  1128. );
  1129. $links = array('!mime_mail_url' => 'http://drupal.org/project/mimemail', '!html_url' => 'http://drupal.org/project/htmlmail');
  1130. $description = t('Default newsletter format. Install <a href="!mime_mail_url">Mime Mail</a> module or <a href="!html_url">HTML Mail</a> module to send newsletters in HTML format.', $links);
  1131. $form['simplenews_default_options']['simplenews_format'] = array(
  1132. '#type' => 'select',
  1133. '#title' => t('Format'),
  1134. '#options' => simplenews_format_options(),
  1135. '#description' => $description,
  1136. '#default_value' => variable_get('simplenews_format', 'plain'),
  1137. );
  1138. // @todo Do we need these master defaults for 'priority' and 'receipt'?
  1139. $form['simplenews_default_options']['simplenews_priority'] = array(
  1140. '#type' => 'select',
  1141. '#title' => t('Priority'),
  1142. '#options' => simplenews_get_priority(),
  1143. '#description' => t('Note that email priority is ignored by a lot of email programs.'),
  1144. '#default_value' => variable_get('simplenews_priority', SIMPLENEWS_PRIORITY_NONE),
  1145. );
  1146. $form['simplenews_default_options']['simplenews_receipt'] = array(
  1147. '#type' => 'checkbox',
  1148. '#title' => t('Request receipt'),
  1149. '#default_value' => variable_get('simplenews_receipt', 0),
  1150. '#description' => t('Request a Read Receipt from your newsletters. A lot of email programs ignore these so it is not a definitive indication of how many people have read your newsletter.'),
  1151. );
  1152. $form['simplenews_default_options']['simplenews_send'] = array(
  1153. '#type' => 'radios',
  1154. '#title' => t('Default send action'),
  1155. '#options' => array(
  1156. SIMPLENEWS_COMMAND_SEND_TEST => t('Send one test newsletter to the test address'),
  1157. SIMPLENEWS_COMMAND_SEND_NOW => t('Send newsletter'),
  1158. ),
  1159. '#default_value' => variable_get('simplenews_send', 0),
  1160. );
  1161. $form['simplenews_test_address'] = array(
  1162. '#type' => 'fieldset',
  1163. '#title' => t('Test addresses'),
  1164. '#collapsible' => FALSE,
  1165. '#description' => t('Supply a comma-separated list of email addresses to be used as test addresses. The override function allows to override these addresses in the newsletter editing form.'),
  1166. );
  1167. $form['simplenews_test_address']['simplenews_test_address'] = array(
  1168. '#type' => 'textfield',
  1169. '#title' => t('Email address'),
  1170. '#size' => 60,
  1171. '#maxlength' => 128,
  1172. '#default_value' => variable_get('simplenews_test_address', $address_default),
  1173. );
  1174. $form['simplenews_test_address']['simplenews_test_address_override'] = array(
  1175. '#type' => 'checkbox',
  1176. '#title' => t('Allow test address override'),
  1177. '#default_value' => variable_get('simplenews_test_address_override', 0),
  1178. );
  1179. $form['simplenews_sender_info'] = array(
  1180. '#type' => 'fieldset',
  1181. '#title' => t('Sender information'),
  1182. '#collapsible' => FALSE,
  1183. '#description' => t('Default sender address that will only be used for confirmation emails. You can specify sender information for each newsletter separately on the newsletter\'s settings page.'),
  1184. );
  1185. $form['simplenews_sender_info']['simplenews_from_name'] = array(
  1186. '#type' => 'textfield',
  1187. '#title' => t('From name'),
  1188. '#size' => 60,
  1189. '#maxlength' => 128,
  1190. '#default_value' => variable_get('simplenews_from_name', variable_get('site_name', 'Drupal')),
  1191. );
  1192. $form['simplenews_sender_info']['simplenews_from_address'] = array(
  1193. '#type' => 'textfield',
  1194. '#title' => t('From email address'),
  1195. '#size' => 60,
  1196. '#maxlength' => 128,
  1197. '#required' => TRUE,
  1198. '#default_value' => variable_get('simplenews_from_address', $address_default),
  1199. );
  1200. return system_settings_form($form);
  1201. }
  1202. /**
  1203. * @todo
  1204. */
  1205. function simplenews_admin_settings_newsletter_validate($form, &$form_state) {
  1206. if (!valid_email_address($form_state['values']['simplenews_from_address'])) {
  1207. form_set_error($field_name, t("The sender's email address you supplied is not valid."));
  1208. }
  1209. }
  1210. /**
  1211. * Menu callback: Simplenews admin settings - Email.
  1212. */
  1213. function simplenews_admin_settings_mail($form, &$form_state) {
  1214. $address_default = variable_get('site_mail', ini_get('sendmail_from'));
  1215. $form = array();
  1216. $form['simplenews_mail_backend']['simplenews_use_cron'] = array(
  1217. '#type' => 'checkbox',
  1218. '#title' => t('Use cron to send newsletters'),
  1219. '#default_value' => variable_get('simplenews_use_cron', TRUE),
  1220. '#description' => t('When checked cron will be used to send newsletters (recommended). Test newsletters and confirmation emails will be sent immediately. Leave unchecked for testing purposes.'),
  1221. );
  1222. $sources = simplenews_get_source_caches();
  1223. $sources_labels = array();
  1224. $sources_descriptions = '';
  1225. foreach ($sources as $name => $source) {
  1226. $sources_labels[$name] = $source['label'];
  1227. $sources_descriptions .= t('<strong>@label</strong>: @description <br />', array('@label' => $source['label'], '@description' => $source['description']));
  1228. }
  1229. $form['simplenews_mail_backend']['simplenews_source_cache'] = array(
  1230. '#type' => 'select',
  1231. '#title' => t('Cache'),
  1232. '#description' => t('Chosing a different cache implementation allows for a different behavior during sending mails.') . '<br /><br />' . $sources_descriptions,
  1233. '#options' => $sources_labels,
  1234. '#default_value' => variable_get('simplenews_source_cache', 'SimplenewsSourceCacheBuild'),
  1235. );
  1236. $throttle = drupal_map_assoc(array(1, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000));
  1237. $throttle[SIMPLENEWS_UNLIMITED] = t('Unlimited');
  1238. if (function_exists('getrusage')) {
  1239. $description_extra = '<br />' . t('Cron execution must not exceed the PHP maximum execution time of %max seconds. You find the time spend to send emails in the <a href="/admin/reports/dblog">Recent log entries</a>.', array('%max' => ini_get('max_execution_time')));
  1240. }
  1241. else {
  1242. $description_extra = '<br />' . t('Cron execution must not exceed the PHP maximum execution time of %max seconds.', array('%max' => ini_get('max_execution_time')));
  1243. }
  1244. $form['simplenews_mail_backend']['simplenews_throttle'] = array(
  1245. '#type' => 'select',
  1246. '#title' => t('Cron throttle'),
  1247. '#options' => $throttle,
  1248. '#default_value' => variable_get('simplenews_throttle', 20),
  1249. '#description' => t('Sets the numbers of newsletters sent per cron run. Failure to send will also be counted.') . $description_extra,
  1250. );
  1251. $form['simplenews_mail_backend']['simplenews_spool_expire'] = array(
  1252. '#type' => 'select',
  1253. '#title' => t('Mail spool expiration'),
  1254. '#options' => array(
  1255. 0 => t('Immediate'),
  1256. 1 => format_plural(1, '1 day', '@count days'),
  1257. 7 => format_plural(1, '1 week', '@count weeks'),
  1258. 14 => format_plural(2, '1 week', '@count weeks'),
  1259. ),
  1260. '#default_value' => variable_get('simplenews_spool_expire', 0),
  1261. '#description' => t('Newsletter mails are spooled. How long must messages be retained in the spool after successful sending. Keeping the message in the spool allows mail statistics (which is not yet implemented). If cron is not used, immediate expiration is advised.'),
  1262. );
  1263. $form['simplenews_mail_backend']['simplenews_debug'] = array(
  1264. '#type' => 'checkbox',
  1265. '#title' => t('Log emails'),
  1266. '#default_value' => variable_get('simplenews_debug', FALSE),
  1267. '#description' => t('When checked all outgoing simplenews emails are logged in the system log. A logged email does not guarantee that it is send or will be delivered. It only indicates that a message is sent to the PHP mail() function. No status information is available of delivery by the PHP mail() function.'),
  1268. );
  1269. return system_settings_form($form);
  1270. }
  1271. /**
  1272. * Menu callback: Simplenews admin settings - Subscription.
  1273. */
  1274. function simplenews_admin_settings_subscription($form, &$form_state) {
  1275. $address_default = variable_get('site_mail', ini_get('sendmail_from'));
  1276. $form = array();
  1277. $form['account'] = array(
  1278. '#type' => 'fieldset',
  1279. '#title' => t('User account'),
  1280. '#collapsible' => FALSE,
  1281. );
  1282. $form['account']['simplenews_sync_account'] = array(
  1283. '#type' => 'checkbox',
  1284. '#title' => t('Synchronize with account'),
  1285. '#default_value' => variable_get('simplenews_sync_account', TRUE),
  1286. '#description' => t('When checked subscriptions will be synchronized with site accounts. When accounts are deleted, subscriptions with the same email address will be removed. When site accounts are blocked/unblocked, subscriptions will be deactivated/activated. When not checked subscriptions will be unchanged when associated accounts are deleted or blocked.'),
  1287. );
  1288. $form['subscription_mail'] = array(
  1289. '#type' => 'fieldset',
  1290. '#title' => t('Confirmation emails'),
  1291. '#collapsible' => FALSE,
  1292. );
  1293. $form['subscription_mail']['simplenews_use_combined'] = array(
  1294. '#type' => 'select',
  1295. '#title' => t('Use combined confirmation mails'),
  1296. '#options' => array(
  1297. 'multiple' => t('For multiple changes'),
  1298. 'always' => t('Always'),
  1299. 'never' => t('Never'),
  1300. ),
  1301. '#description' => t('Combined confirmation mails allow subscribers to confirm multiple newsletter changes with single mail.'),
  1302. '#default_value' => variable_get('simplenews_use_combined', 'multiple'),
  1303. );
  1304. if (module_exists('token')) {
  1305. $form['subscription_mail']['token_help'] = array(
  1306. '#title' => t('Replacement patterns'),
  1307. '#type' => 'fieldset',
  1308. '#collapsible' => TRUE,
  1309. '#collapsed' => TRUE,
  1310. );
  1311. $form['subscription_mail']['token_help']['browser'] = array(
  1312. '#theme' => 'token_tree',
  1313. '#token_types' => array('simplenews-category', 'simplenews-subscriber'),
  1314. );
  1315. }
  1316. $form['subscription_mail']['single'] = array(
  1317. '#type' => 'fieldset',
  1318. '#title' => t('Single confirmation mails'),
  1319. '#collapsed' => TRUE,
  1320. '#collapsible' => TRUE,
  1321. '#states' => array(
  1322. 'invisible' => array(
  1323. ':input[name="simplenews_use_combined"]' => array(
  1324. 'value' => 'always',
  1325. ),
  1326. ),
  1327. ),
  1328. );
  1329. $form['subscription_mail']['single']['simplenews_confirm_subscribe_subject'] = array(
  1330. '#type' => 'textfield',
  1331. '#title' => t('Subject'),
  1332. '#default_value' => simplenews_subscription_confirmation_text('subscribe_subject'),
  1333. '#maxlength' => 180,
  1334. );
  1335. $form['subscription_mail']['single']['simplenews_confirm_subscribe_unsubscribed'] = array(
  1336. '#type' => 'textarea',
  1337. '#title' => t('Body text of subscribe email'),
  1338. '#default_value' => simplenews_subscription_confirmation_text('subscribe_unsubscribed'),
  1339. '#rows' => 5,
  1340. );
  1341. $form['subscription_mail']['single']['simplenews_confirm_subscribe_subscribed'] = array(
  1342. '#type' => 'textarea',
  1343. '#title' => t('Body text for already subscribed visitor'),
  1344. '#default_value' => simplenews_subscription_confirmation_text('subscribe_subscribed'),
  1345. '#rows' => 5,
  1346. );
  1347. $form['subscription_mail']['single']['simplenews_confirm_unsubscribe_subscribed'] = array(
  1348. '#type' => 'textarea',
  1349. '#title' => t('Body text of unsubscribe email'),
  1350. '#default_value' => simplenews_subscription_confirmation_text('unsubscribe_subscribed'),
  1351. '#rows' => 5,
  1352. );
  1353. $form['subscription_mail']['single']['simplenews_confirm_unsubscribe_unsubscribed'] = array(
  1354. '#type' => 'textarea',
  1355. '#title' => t('Body text for not yet subscribed visitor'),
  1356. '#default_value' => simplenews_subscription_confirmation_text('unsubscribe_unsubscribed'),
  1357. '#rows' => 5,
  1358. );
  1359. $form['subscription_mail']['combined'] = array(
  1360. '#type' => 'fieldset',
  1361. '#title' => t('Combined confirmation mails'),
  1362. '#collapsed' => TRUE,
  1363. '#collapsible' => TRUE,
  1364. '#states' => array(
  1365. 'invisible' => array(
  1366. ':input[name="simplenews_use_combined"]' => array(
  1367. 'value' => 'never',
  1368. ),
  1369. ),
  1370. ),
  1371. );
  1372. $form['subscription_mail']['combined']['simplenews_confirm_combined_subject'] = array(
  1373. '#type' => 'textfield',
  1374. '#title' => t('Subject text for combined confirmation mail'),
  1375. '#default_value' => simplenews_subscription_confirmation_text('combined_subject'),
  1376. );
  1377. $form['subscription_mail']['combined']['simplenews_confirm_combined_body'] = array(
  1378. '#type' => 'textarea',
  1379. '#title' => t('Body text for combined confirmation mail'),
  1380. '#default_value' => simplenews_subscription_confirmation_text('combined_body'),
  1381. '#rows' => 5,
  1382. );
  1383. $form['subscription_mail']['combined']['simplenews_confirm_combined_body_unchanged'] = array(
  1384. '#type' => 'textarea',
  1385. '#title' => t('Body text for unchanged combined confirmation mail'),
  1386. '#default_value' => simplenews_subscription_confirmation_text('combined_body_unchanged'),
  1387. '#rows' => 5,
  1388. '#description' => t('This body is used when there are no change requests which have no effect, e.g trying to subscribe when already being subscribed to a category.')
  1389. );
  1390. $form['subscription_mail']['combined']['simplenews_confirm_combined_line_subscribe_unsubscribed'] = array(
  1391. '#type' => 'textfield',
  1392. '#title' => t('Change text for a new subscription'),
  1393. '#default_value' => simplenews_subscription_confirmation_text('combined_line_subscribe_unsubscribed'),
  1394. );
  1395. $form['subscription_mail']['combined']['simplenews_confirm_combined_line_subscribe_subscribed'] = array(
  1396. '#type' => 'textfield',
  1397. '#title' => t('Change text when already subscribed'),
  1398. '#default_value' => simplenews_subscription_confirmation_text('combined_line_subscribe_subscribed'),
  1399. );
  1400. $form['subscription_mail']['combined']['simplenews_confirm_combined_line_unsubscribe_subscribed'] = array(
  1401. '#type' => 'textfield',
  1402. '#title' => t('Change text for an unsubscription'),
  1403. '#default_value' => simplenews_subscription_confirmation_text('combined_line_unsubscribe_subscribed'),
  1404. );
  1405. $form['subscription_mail']['combined']['simplenews_confirm_combined_line_unsubscribe_unsubscribed'] = array(
  1406. '#type' => 'textfield',
  1407. '#title' => t('Change text when already unsubscribed'),
  1408. '#default_value' => simplenews_subscription_confirmation_text('combined_line_unsubscribe_unsubscribed'),
  1409. );
  1410. $form['confirm_pages'] = array(
  1411. '#type' => 'fieldset',
  1412. '#title' => t('Confirmation pages'),
  1413. '#collapsible' => FALSE,
  1414. );
  1415. $form['confirm_pages']['simplenews_confirm_subscribe_page'] = array(
  1416. '#type' => 'textfield',
  1417. '#title' => t('Subscribe confirmation'),
  1418. '#description' => t('Drupal path or URL of the destination page where after the subscription is confirmed (e.g. node/123). Leave empty to go to the front page.'),
  1419. '#default_value' => variable_get('simplenews_confirm_subscribe_page', ''),
  1420. );
  1421. $form['confirm_pages']['simplenews_confirm_unsubscribe_page'] = array(
  1422. '#type' => 'textfield',
  1423. '#title' => t('Unsubscribe confirmation'),
  1424. '#description' => t('Drupal path or URL of the destination page when the subscription removal is confirmed (e.g. node/123). Leave empty to go to the front page.'),
  1425. '#default_value' => variable_get('simplenews_confirm_unsubscribe_page', ''),
  1426. );
  1427. return system_settings_form($form);
  1428. }
  1429. /**
  1430. * Generate subscription filters
  1431. */
  1432. function simplenews_subscription_filters() {
  1433. // Newsletter filter
  1434. $filters['list'] = array(
  1435. 'title' => t('Subscribed to'),
  1436. 'options' => array(
  1437. 'all' => t('All newsletters'),
  1438. ),
  1439. );
  1440. foreach (simplenews_categories_load_multiple() as $list) {
  1441. $filters['list']['options']['tid-' . $list->tid] = _simplenews_newsletter_name($list);
  1442. }
  1443. // Email filter
  1444. $filters['email'] = array(
  1445. 'title' => t('Email address'),
  1446. );
  1447. return $filters;
  1448. }
  1449. /**
  1450. * Return form for subscription filters.
  1451. *
  1452. * @see simplenews_subscription_filter_form_submit()
  1453. */
  1454. function simplenews_subscription_filter_form() {
  1455. // Current filter selections in $session var; stored at form submission
  1456. // Example: array('list' => 'all', 'email' => 'hotmail')
  1457. $session = isset($_SESSION['simplenews_subscriptions_filter']) ? $_SESSION['simplenews_subscriptions_filter'] : '';
  1458. $session = is_array($session) ? $session : _simplenews_subscription_filter_default();
  1459. $filters = simplenews_subscription_filters();
  1460. $form['filters'] = array(
  1461. '#type' => 'fieldset',
  1462. '#title' => t('Show only subscription which'),
  1463. '#collapsible' => FALSE,
  1464. );
  1465. // Filter values are default
  1466. $form['filters']['list'] = array(
  1467. '#type' => 'select',
  1468. '#title' => $filters['list']['title'],
  1469. '#options' => $filters['list']['options'],
  1470. '#default_value' => $session['list'],
  1471. );
  1472. $form['filters']['email'] = array(
  1473. '#type' => 'textfield',
  1474. '#title' => $filters['email']['title'],
  1475. '#default_value' => $session['email'],
  1476. );
  1477. $form['filters']['submit'] = array(
  1478. '#type' => 'submit',
  1479. '#value' => t('Filter'),
  1480. '#prefix' => '<span class="spacer" />',
  1481. );
  1482. // Add Reset button if filter is in use
  1483. if ($session != _simplenews_subscription_filter_default()) {
  1484. $form['filters']['reset'] = array(
  1485. '#type' => 'submit',
  1486. '#value' => t('Reset'),
  1487. );
  1488. }
  1489. return $form;
  1490. }
  1491. /**
  1492. * Helper function: returns subscription filter default settings
  1493. */
  1494. function _simplenews_subscription_filter_default() {
  1495. return array(
  1496. 'list' => 'all',
  1497. 'email' => '',
  1498. );
  1499. }
  1500. /**
  1501. * @todo
  1502. */
  1503. function simplenews_subscription_filter_form_submit($form, &$form_state) {
  1504. switch ($form_state['values']['op']) {
  1505. case t('Filter'):
  1506. $_SESSION['simplenews_subscriptions_filter'] = array(
  1507. 'list' => $form_state['values']['list'],
  1508. 'email' => $form_state['values']['email'],
  1509. );
  1510. break;
  1511. case t('Reset'):
  1512. $_SESSION['simplenews_subscriptions_filter'] = _simplenews_subscription_filter_default();
  1513. break;
  1514. }
  1515. }
  1516. /**
  1517. * Apply filters for subscription filters based on session.
  1518. *
  1519. * @param $query
  1520. * A SelectQuery to which the filters should be applied.
  1521. */
  1522. function simplenews_build_subscription_filter_query(SelectQueryInterface $query) {
  1523. if (isset($_SESSION['simplenews_subscriptions_filter'])) {
  1524. foreach ($_SESSION['simplenews_subscriptions_filter'] as $key => $value) {
  1525. switch ($key) {
  1526. case 'list':
  1527. if ($value != 'all') {
  1528. list($key, $value) = explode('-', $value, 2);
  1529. $query->condition('su.' . $key, $value);
  1530. }
  1531. break;
  1532. case 'email':
  1533. if (!empty($value)) {
  1534. $query->condition('sn.mail', '%' . $value . '%', 'LIKE');
  1535. }
  1536. break;
  1537. }
  1538. }
  1539. }
  1540. }
  1541. /**
  1542. * Count number of subscribers per newsletter list.
  1543. *
  1544. * @return number of subscribers.
  1545. */
  1546. function simplenews_count_subscriptions($tid) {
  1547. $subscription_count = &drupal_static(__FUNCTION__);
  1548. if (isset($subscription_count[$tid])) {
  1549. return $subscription_count[$tid];
  1550. }
  1551. $query = db_select('simplenews_subscription', 'ss');
  1552. $query->leftJoin('simplenews_subscriber', 'sn', 'sn.snid = ss.snid');
  1553. $query->condition('tid', $tid)
  1554. ->condition('sn.activated', 1)
  1555. ->condition('status', SIMPLENEWS_SUBSCRIPTION_STATUS_SUBSCRIBED);
  1556. $subscription_count[$tid] = $query->countQuery()->execute()->fetchField();
  1557. return $subscription_count[$tid];
  1558. }
  1559. /**
  1560. * Return a status image.
  1561. *
  1562. * @param $variables An associative array containing:
  1563. * source: Source which status will be displayed ('published', 'activated', 'sent')
  1564. * status: Status of the source (0 or 1)
  1565. *
  1566. * @return string
  1567. * HTML string containing an image tag.
  1568. *
  1569. * @ingroup theming
  1570. */
  1571. function theme_simplenews_status($variables) {
  1572. $source = $variables['source'];
  1573. $status = $variables['status'];
  1574. switch ($source) {
  1575. case 'published':
  1576. $images = array(
  1577. 0 => 'sn-saved.png',
  1578. 1 => 'sn-sent.png',
  1579. );
  1580. $title = array(
  1581. 0 => t('Not published'),
  1582. 1 => t('Published'),
  1583. );
  1584. break;
  1585. case 'activated':
  1586. $images = array(
  1587. 0 => 'sn-saved.png',
  1588. 1 => 'sn-sent.png',
  1589. );
  1590. $title = array(
  1591. 0 => t('Inactive: no newsletters will be sent'),
  1592. 1 => t('Active: user will receive newsletters'),
  1593. );
  1594. break;
  1595. case 'sent':
  1596. $images = array(
  1597. SIMPLENEWS_STATUS_SEND_PENDING => 'sn-cron.png',
  1598. SIMPLENEWS_STATUS_SEND_READY => 'sn-sent.png',
  1599. );
  1600. $title = array(
  1601. SIMPLENEWS_STATUS_SEND_NOT => t('Not yet sent'),
  1602. SIMPLENEWS_STATUS_SEND_PENDING => t('Currently sending by cron'),
  1603. SIMPLENEWS_STATUS_SEND_READY => t('Sent'),
  1604. SIMPLENEWS_STATUS_SEND_PUBLISH => t('Send on publish'),
  1605. );
  1606. break;
  1607. }
  1608. // Build the output
  1609. if (isset($images[$status])) {
  1610. $img_vars = array(
  1611. 'path' => drupal_get_path('module', 'simplenews') . '/' . $images[$status],
  1612. 'alt' => $title[$status],
  1613. 'title' => $title[$status],
  1614. 'getsize' => TRUE,
  1615. );
  1616. $output = theme('image', $img_vars);
  1617. }
  1618. else {
  1619. $output = check_plain($title[$status]);
  1620. }
  1621. return $output;
  1622. }
  1623. /**
  1624. * Theme simplenews issues and subscriptions filter form.
  1625. *
  1626. * @ingroup theming
  1627. */
  1628. function theme_simplenews_filter_form($variables) {
  1629. $form = $variables['form'];
  1630. $output = '<div id="simplenews-admin-filter">';
  1631. $output .= drupal_render($form['filters']);
  1632. $output .= '</div>';
  1633. $output .= drupal_render_children($form);
  1634. return $output;
  1635. }
  1636. /**
  1637. * Theme simplenews subscriptions administration filter form.
  1638. *
  1639. * @ingroup theming
  1640. */
  1641. function theme_simplenews_subscription_filter_form($variables) {
  1642. $form = $variables['form'];
  1643. $output = '<div id="simplenews-subscription-filter">';
  1644. $output .= drupal_render($form['filters']);
  1645. $output .= '</div>';
  1646. $output .= drupal_render_children($form);
  1647. return $output;
  1648. }
  1649. /**
  1650. * Function to provide the various simplenews mail priorities for newsletter categories
  1651. */
  1652. function simplenews_get_priority() {
  1653. return array(
  1654. SIMPLENEWS_PRIORITY_NONE => t('none'),
  1655. SIMPLENEWS_PRIORITY_HIGHEST => t('highest'),
  1656. SIMPLENEWS_PRIORITY_HIGH => t('high'),
  1657. SIMPLENEWS_PRIORITY_NORMAL => t('normal'),
  1658. SIMPLENEWS_PRIORITY_LOW => t('low'),
  1659. SIMPLENEWS_PRIORITY_LOWEST => t('lowest'),
  1660. );
  1661. }
  1662. /**
  1663. * Menu callback; Newsletter tab page.
  1664. */
  1665. function simplenews_node_tab_page($node) {
  1666. drupal_set_title(t('<em>Newsletter</em> @title', array('@title' => $node->title)), PASS_THROUGH);
  1667. return drupal_get_form('simplenews_node_tab_send_form', $node);
  1668. }
  1669. /**
  1670. * @todo
  1671. */
  1672. function simplenews_node_tab_send_form($form, &$form_state, $node) {
  1673. // First check if there already is a loaded simplenews object.
  1674. if (!empty($node->simplenews)) {
  1675. $simplenews_values = $node->simplenews;
  1676. }
  1677. // If not, try to load it based on the node id.
  1678. elseif ($loaded = simplenews_newsletter_load($node->nid)) {
  1679. $simplenews_values = $loaded;
  1680. }
  1681. // If that fails too, fall back to the defaults.
  1682. else {
  1683. $simplenews_values = (object) _simplenews_get_node_form_defaults($node);
  1684. }
  1685. $form = array();
  1686. // We will need the node
  1687. $form['nid'] = array(
  1688. '#type' => 'value',
  1689. '#value' => $node->nid,
  1690. );
  1691. // @todo delete this fieldset?
  1692. $form['simplenews'] = array(
  1693. '#type' => 'fieldset',
  1694. '#title' => t('Send newsletter'),
  1695. '#collapsible' => FALSE,
  1696. '#collapsed' => FALSE,
  1697. '#tree' => TRUE,
  1698. );
  1699. // Translations of newsletters don't have the 'send' option. Only the
  1700. // translation source (and non translated) newsletters will get these options.
  1701. if (module_exists('translation') && translation_supported_type($node->type)
  1702. && (isset($node->translate) && ($node->tnid > 0) && ($node->tnid != $node->nid))) {
  1703. $form['simplenews']['#description'] = t('This newsletter issue is part of a translation set. Sending this set is controlled from the <a href="@link">translation source newsletter</a>.', array('@link' => url('node/' . $node->tnid)));
  1704. // @todo Translated nodes must also have the same Category!
  1705. // Move the category in here and give the user feedback.
  1706. }
  1707. else {
  1708. // Show newsletter sending options if newsletter has not been send yet.
  1709. // If send a notification is shown.
  1710. if ($simplenews_values->status == SIMPLENEWS_STATUS_SEND_NOT || $simplenews_values->status == SIMPLENEWS_STATUS_SEND_PUBLISH) {
  1711. $options = array(
  1712. SIMPLENEWS_COMMAND_SEND_TEST => t('Send one test newsletter to the test address'),
  1713. );
  1714. // Add option to send on publish when the node is unpublished.
  1715. if ($node->status == NODE_NOT_PUBLISHED) {
  1716. $options[SIMPLENEWS_COMMAND_SEND_PUBLISH] = t('Send newsletter when published');
  1717. }
  1718. else {
  1719. $options[SIMPLENEWS_COMMAND_SEND_NOW] = t('Send newsletter');
  1720. }
  1721. if ($simplenews_values->status == SIMPLENEWS_STATUS_SEND_PUBLISH) {
  1722. $send_default = SIMPLENEWS_STATUS_SEND_PUBLISH;
  1723. }
  1724. else {
  1725. $send_default = variable_get('simplenews_send', SIMPLENEWS_COMMAND_SEND_TEST);
  1726. }
  1727. $form['simplenews']['send'] = array(
  1728. '#type' => 'radios',
  1729. '#title' => t('Send newsletter'),
  1730. '#default_value' => isset($simplenews_values->send) ? $simplenews_values->send : $send_default,
  1731. '#options' => $options,
  1732. '#attributes' => array(
  1733. 'class' => array('simplenews-command-send'),
  1734. ),
  1735. );
  1736. $address_default = variable_get('site_mail', ini_get('sendmail_from'));
  1737. if (variable_get('simplenews_test_address_override', 0)) {
  1738. $form['simplenews']['test_address'] = array(
  1739. '#type' => 'textfield',
  1740. '#title' => t('Test email addresses'),
  1741. '#description' => t('A comma-separated list of email addresses to be used as test addresses.'),
  1742. '#default_value' => variable_get('simplenews_test_address', $address_default),
  1743. '#size' => 60,
  1744. '#maxlength' => 128,
  1745. );
  1746. }
  1747. else {
  1748. $form['simplenews']['test_address'] = array(
  1749. '#type' => 'value',
  1750. '#value' => variable_get('simplenews_test_address', $address_default),
  1751. );
  1752. }
  1753. }
  1754. else {
  1755. $form['simplenews']['none'] = array(
  1756. '#type' => 'checkbox',
  1757. '#return_value' => 0,
  1758. '#attributes' => array(
  1759. 'checked' => 'checked',
  1760. 'disabled' => 'disabled',
  1761. ),
  1762. );
  1763. $form['simplenews']['none']['#title'] = ($simplenews_values->status == SIMPLENEWS_STATUS_SEND_READY) ? t('This newsletter has been sent') : t('This newsletter is pending');
  1764. return $form;
  1765. }
  1766. $form['submit'] = array(
  1767. '#type' => 'submit',
  1768. '#value' => t('Submit'),
  1769. );
  1770. }
  1771. return $form;
  1772. }
  1773. /**
  1774. * @todo
  1775. */
  1776. function simplenews_node_tab_send_form_validate($form, &$form_state) {
  1777. $values = $form_state['values'];
  1778. $node = node_load($values['nid']);
  1779. $default_address = variable_get('simplenews_test_address', variable_get('site_mail', ini_get('sendmail_from')));
  1780. $mails = array($default_address);
  1781. if (isset($values['simplenews']['send']) && $values['simplenews']['send'] == SIMPLENEWS_COMMAND_SEND_TEST && variable_get('simplenews_test_address_override', 0)) {
  1782. // @todo Can we simplify and use only two kind of messages?
  1783. if (!empty($values['simplenews']['test_address'])) {
  1784. $mails = explode(',', $values['simplenews']['test_address']);
  1785. foreach ($mails as $mail) {
  1786. $mail = trim($mail);
  1787. if ($mail == '') {
  1788. form_set_error('simplenews][test_address', t('Test email address is empty.'));
  1789. }
  1790. elseif (!valid_email_address($mail)) {
  1791. form_set_error('simplenews][test_address', t('Invalid email address "%mail".', array('%mail' => $mail)));
  1792. }
  1793. }
  1794. }
  1795. else {
  1796. form_set_error('simplenews][test_address', t('Missing test email address.'));
  1797. }
  1798. }
  1799. $form_state['test_addresses'] = $mails;
  1800. }
  1801. /**
  1802. * @todo
  1803. */
  1804. function simplenews_node_tab_send_form_submit($form, &$form_state) {
  1805. $values = $form_state['values'];
  1806. $node = node_load($values['nid']);
  1807. // Send newsletter to all subscribers or send test newsletter
  1808. module_load_include('inc', 'simplenews', 'includes/simplenews.mail');
  1809. if ($values['simplenews']['send'] == SIMPLENEWS_COMMAND_SEND_NOW) {
  1810. simplenews_add_node_to_spool($node);
  1811. // Attempt to send immediatly, if configured to do so.
  1812. if (simplenews_mail_attempt_immediate_send(array('nid' => $node->nid))) {
  1813. drupal_set_message(t('Newsletter %title sent.', array('%title' => $node->title)));
  1814. }
  1815. else {
  1816. drupal_set_message(t('Newsletter %title pending.', array('%title' => $node->title)));
  1817. }
  1818. }
  1819. elseif ($values['simplenews']['send'] == SIMPLENEWS_COMMAND_SEND_TEST) {
  1820. simplenews_send_test($node, $form_state['test_addresses']);
  1821. }
  1822. // If the selected command is send on publish, just set the newsletter status.
  1823. if ($values['simplenews']['send'] == SIMPLENEWS_COMMAND_SEND_PUBLISH) {
  1824. $newsletter = simplenews_newsletter_load($node->nid);
  1825. if (!$newsletter) {
  1826. $newsletter = simplenews_newsletter_defaults($node);
  1827. }
  1828. $newsletter->status = SIMPLENEWS_STATUS_SEND_PUBLISH;
  1829. simplenews_newsletter_save($newsletter);
  1830. drupal_set_message(t('The newsletter will be sent when the content is published.'));
  1831. }
  1832. }