1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11$inputConfiguration = [
12	[
13		'staticKeyFilters' => [
14			'editionId' => 'int',
15		],
16	],
17	[
18		'catchAllUnset' => null,
19	],
20];
21
22include('tiki-setup.php');
23
24$access->check_feature('feature_newsletters');
25
26if (php_sapi_name() == 'cli') {
27	new Perms_Context('admin'); // run as admin when executed from command line
28} else {
29	$access->check_permission('tiki_p_send_newsletters');
30}
31
32global $nllib;
33
34include_once('lib/newsletters/nllib.php');
35
36function display_usage()
37{
38	$helpMsg = "\nUsage: php tiki-batch_send_newsletter.php editionId=X\n"
39		. "Usage: http://path_to_tiki/tiki-batch_send_newsletter.php?editionId=X\n";
40
41	if (php_sapi_name() == 'cli') {
42		echo $helpMsg;
43	} else {
44		echo nl2br($helpMsg);
45	}
46	die;
47}
48
49error_reporting(E_ALL);
50
51$request = new Tiki_Request();
52
53$editionId = $request->getProperty('editionId');
54
55if (empty($editionId)) {
56	display_usage();
57}
58
59if (! ($edition_info = $nllib->get_edition($editionId))) {
60	echo "Incorrect editionId: $editionId";
61	die;
62}
63if (! ($nl_info = $nllib->get_newsletter($edition_info['nlId']))) {
64	echo 'Incorrect nlId: ' . $edition_info['nlId'];
65}
66$edition_info['editionId'] = 0;
67$sent = $errors = [];
68$logFileName = '';
69$edition_info['begin'] = 'y';
70$nllib->send($nl_info, $edition_info, false, $sent, $errors, $logFileName);
71if (! empty($errors)) {
72	echo "Errors\n";
73	foreach ($errors as $error) {
74		echo $error . "\n";
75	}
76	die;
77}
78echo "Sent to\n";
79foreach ($sent as $s) {
80	echo $s . "\n";
81}
82echo "Log: $logFileName\n";
83