Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / form / UserActivityPointOptionForm.class.php
1 <?php
2
3 namespace wcf\acp\form;
4
5 use wcf\data\object\type\ObjectType;
6 use wcf\data\object\type\ObjectTypeCache;
7 use wcf\data\object\type\ObjectTypeEditor;
8 use wcf\form\AbstractForm;
9 use wcf\system\exception\UserInputException;
10 use wcf\system\WCF;
11 use wcf\util\ArrayUtil;
12
13 /**
14 * Provides the user activity point option form.
15 *
16 * @author Tim Duesterhus
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
19 * @package WoltLabSuite\Core\Acp\Form
20 */
21 class UserActivityPointOptionForm extends AbstractForm
22 {
23 /**
24 * @inheritDoc
25 */
26 public $activeMenuItem = 'wcf.acp.menu.link.activityPoint';
27
28 /**
29 * @inheritDoc
30 */
31 public $neededPermissions = ['admin.user.canEditActivityPoints'];
32
33 /**
34 * points to objectType
35 * @var int[]
36 */
37 public $points = [];
38
39 /**
40 * valid object types
41 * @var ObjectType[]
42 */
43 public $objectTypes = [];
44
45 /**
46 * @inheritDoc
47 */
48 public function readFormParameters()
49 {
50 parent::readFormParameters();
51
52 if (isset($_POST['points']) && \is_array($_POST['points'])) {
53 $this->points = ArrayUtil::toIntegerArray($_POST['points']);
54 }
55 }
56
57 /**
58 * @inheritDoc
59 */
60 public function validate()
61 {
62 parent::validate();
63
64 foreach ($this->points as $objectTypeID => $points) {
65 if ($points < 0) {
66 throw new UserInputException($objectTypeID, 'greaterThan');
67 }
68 }
69 }
70
71 /**
72 * @inheritDoc
73 */
74 public function readData()
75 {
76 $this->objectTypes = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.user.activityPointEvent');
77 if (empty($_POST)) {
78 foreach ($this->objectTypes as $objectType) {
79 $this->points[$objectType->objectTypeID] = $objectType->points;
80 }
81 }
82
83 parent::readData();
84 }
85
86 /**
87 * @inheritDoc
88 */
89 public function save()
90 {
91 parent::save();
92
93 foreach ($this->objectTypes as $objectType) {
94 if (!isset($this->points[$objectType->objectTypeID])) {
95 continue;
96 }
97 $editor = new ObjectTypeEditor($objectType);
98 $data = $objectType->additionalData;
99 $data['points'] = $this->points[$objectType->objectTypeID];
100 $editor->update(['additionalData' => \serialize($data)]);
101 }
102
103 ObjectTypeEditor::resetCache();
104
105 $this->saved();
106
107 WCF::getTPL()->assign('success', true);
108 }
109
110 /**
111 * @inheritDoc
112 */
113 public function assignVariables()
114 {
115 parent::assignVariables();
116
117 WCF::getTPL()->assign([
118 'objectTypes' => $this->objectTypes,
119 'points' => $this->points,
120 ]);
121 }
122 }