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