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