5332c561c990987fb987af873672ce2d929f6d5d
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\bulk\processing\user;
4
5 use wcf\data\DatabaseObjectList;
6 use wcf\data\user\UserList;
7 use wcf\system\WCF;
8 use wcf\util\StringUtil;
9
10 /**
11 * Bulk processing action implementation for exporting mail addresses of users.
12 *
13 * @author Matthias Schmidt
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
16 * @package WoltLabSuite\Core\System\Bulk\Processing\User
17 * @since 3.0
18 */
19 class ExportMailAddressUserBulkProcessingAction extends AbstractUserBulkProcessingAction
20 {
21 /**
22 * type of the file the email addresses will be saved in (csv or xml)
23 * @var string
24 */
25 public $fileType = 'csv';
26
27 /**
28 * separates the exported email addresses
29 * @var string
30 */
31 public $separator = ',';
32
33 /**
34 * encloses the exported email addresses
35 * @var string
36 */
37 public $textSeparator = '"';
38
39 /**
40 * indicates whether output was generated (i.e. executeAction was called)
41 * @var bool
42 */
43 private $executed = false;
44
45 /**
46 * @inheritDoc
47 */
48 public function executeAction(DatabaseObjectList $objectList)
49 {
50 if (!($objectList instanceof UserList)) {
51 throw new \InvalidArgumentException("Object list is no instance of '" . UserList::class . "', instance of '" . \get_class($objectList) . "' given.");
52 }
53
54 $this->executed = true;
55
56 // send content type
57 \header('Content-Type: text/' . $this->fileType . '; charset=UTF-8');
58 \header('Content-Disposition: attachment; filename="export.' . $this->fileType . '"');
59
60 if ($this->fileType == 'xml') {
61 echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<addresses>\n";
62 }
63
64 $userCount = \count($objectList);
65 $i = 0;
66 foreach ($objectList as $user) {
67 if ($this->fileType == 'xml') {
68 echo "<address><![CDATA[" . StringUtil::escapeCDATA($user->email) . "]]></address>\n";
69 } else {
70 echo $this->textSeparator . $user->email . $this->textSeparator . ($i < $userCount ? $this->separator : '');
71 }
72
73 $i++;
74 }
75
76 if ($this->fileType == 'xml') {
77 echo "</addresses>";
78 }
79 }
80
81 /**
82 * @inheritDoc
83 */
84 public function getHTML()
85 {
86 return WCF::getTPL()->fetch('exportMailAddressUserBulkProcessing', 'wcf', [
87 'fileType' => $this->fileType,
88 'separator' => $this->separator,
89 'textSeparator' => $this->textSeparator,
90 ]);
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public function getObjectList()
97 {
98 $userList = parent::getObjectList();
99
100 $userList->sqlOrderBy = 'user_table.email';
101
102 return $userList;
103 }
104
105 /**
106 * @inheritDoc
107 */
108 public function readFormParameters()
109 {
110 if (isset($_POST['fileType']) && $_POST['fileType'] == 'xml') {
111 $this->fileType = $_POST['fileType'];
112 }
113 if (isset($_POST['separator'])) {
114 $this->separator = $_POST['separator'];
115 }
116 if (isset($_POST['textSeparator'])) {
117 $this->textSeparator = $_POST['textSeparator'];
118 }
119 }
120
121 /**
122 * @inheritDoc
123 */
124 public function reset()
125 {
126 if (!$this->executed) {
127 return;
128 }
129 $this->executed = false;
130
131 exit;
132 }
133 }