Fix CodeSniffing errors
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / form / ConversationAddForm.class.php
1 <?php
2 namespace wcf\form;
3 use wcf\data\conversation\Conversation;
4 use wcf\data\conversation\ConversationAction;
5 use wcf\data\user\UserProfile;
6 use wcf\system\breadcrumb\Breadcrumb;
7 use wcf\system\conversation\ConversationHandler;
8 use wcf\system\exception\IllegalLinkException;
9 use wcf\system\exception\NamedUserException;
10 use wcf\system\exception\UserInputException;
11 use wcf\system\request\LinkHandler;
12 use wcf\system\WCF;
13 use wcf\util\HeaderUtil;
14 use wcf\util\StringUtil;
15
16 /**
17 * Shows the conversation form.
18 *
19 * @author Marcel Werk
20 * @copyright 2009-2012 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 * @package com.woltlab.wcf.conversation
23 * @subpackage form
24 * @category Community Framework
25 */
26 class ConversationAddForm extends MessageForm {
27 /**
28 * @see wcf\page\AbstractPage::$enableTracking
29 */
30 public $enableTracking = true;
31
32 /**
33 * @see wcf\form\MessageForm::$attachmentObjectType
34 */
35 public $attachmentObjectType = 'com.woltlab.wcf.conversation.message';
36
37 /**
38 * @see wcf\page\AbstractPage::$loginRequired
39 */
40 public $loginRequired = true;
41
42 /**
43 * @see wcf\page\AbstractPage::$neededModules
44 */
45 public $neededModules = array('MODULE_CONVERSATION');
46
47 /**
48 * @see wcf\page\AbstractPage::$neededPermissions
49 */
50 public $neededPermissions = array('user.conversation.canUseConversation');
51
52 /**
53 * participants (comma separated user names)
54 * @var string
55 */
56 public $participants = '';
57
58 /**
59 * invisible participants (comma separated user names)
60 * @var string
61 */
62 public $invisibleParticipants = '';
63
64 /**
65 * draft status
66 * @var integer
67 */
68 public $draft = 0;
69
70 /**
71 * true, if participants can add new participants
72 * @var integer
73 */
74 public $participantCanInvite = 0;
75
76 /**
77 * participants (user ids)
78 * @var array<integer>
79 */
80 public $participantIDs = array();
81
82 /**
83 * invisible participants (user ids)
84 * @var array<integer>
85 */
86 public $invisibleParticipantIDs = array();
87
88 /**
89 * @see wcf\page\IPage::readParameters()
90 */
91 public function readParameters() {
92 parent::readParameters();
93
94 // check max pc permission
95 if (ConversationHandler::getInstance()->getConversationCount() >= WCF::getSession()->getPermission('user.conversation.maxConversations')) {
96 throw new NamedUserException(WCF::getLanguage()->get('wcf.conversation.error.mailboxIsFull'));
97 }
98
99 if (isset($_REQUEST['userID'])) {
100 $userID = intval($_REQUEST['userID']);
101 $user = UserProfile::getUserProfile($userID);
102 if ($user === null || $user->userID == WCF::getUser()->userID) {
103 throw new IllegalLinkException();
104 }
105
106 // validate user
107 try {
108 Conversation::validateParticipant($user);
109 }
110 catch (UserInputException $e) {
111 throw new NamedUserException(WCF::getLanguage()->getDynamicVariable('wcf.conversation.participants.error.'.$e->getType(), array('errorData' => array('username' => $user->username))));
112 }
113
114 $this->participants = $user->username;
115 }
116 }
117
118 /**
119 * @see wcf\form\IForm::readFormParameters()
120 */
121 public function readFormParameters() {
122 parent::readFormParameters();
123
124 if (isset($_POST['draft'])) $this->draft = (bool) $_POST['draft'];
125 if (isset($_POST['participantCanInvite'])) $this->participantCanInvite = (bool) $_POST['participantCanInvite'];
126 if (isset($_POST['participants'])) $this->participants = StringUtil::trim($_POST['participants']);
127 if (isset($_POST['invisibleParticipants'])) $this->invisibleParticipants = StringUtil::trim($_POST['invisibleParticipants']);
128 }
129
130 /**
131 * @see wcf\form\IForm::validate()
132 */
133 public function validate() {
134 if (empty($this->participants) && empty($this->invisibleParticipants) && !$this->draft) {
135 throw new UserInputException('participants');
136 }
137
138 $this->participantIDs = Conversation::validateParticipants($this->participants);
139 $this->invisibleParticipantIDs = Conversation::validateParticipants($this->invisibleParticipants, 'invisibleParticipants');
140
141 // remove duplicates
142 $intersection = array_intersect($this->participantIDs, $this->invisibleParticipantIDs);
143 if (!empty($intersection)) $this->invisibleParticipantIDs = array_diff($this->invisibleParticipantIDs, $intersection);
144
145 if (empty($this->participantIDs) && empty($this->invisibleParticipantIDs) && !$this->draft) {
146 throw new UserInputException('participants');
147 }
148
149 // check number of participants
150 if (count($this->participantIDs) + count($this->invisibleParticipantIDs) > WCF::getSession()->getPermission('user.conversation.maxParticipants')) {
151 throw new UserInputException('participants', 'tooManyParticipants');
152 }
153
154 parent::validate();
155 }
156
157 /**
158 * @see wcf\form\IForm::save()
159 */
160 public function save() {
161 parent::save();
162
163 // save conversation
164 $data = array(
165 'subject' => $this->subject,
166 'time' => TIME_NOW,
167 'userID' => WCF::getUser()->userID,
168 'username' => WCF::getUser()->username,
169 'isDraft' => ($this->draft ? 1 : 0),
170 'participantCanInvite' => $this->participantCanInvite
171 );
172 if ($this->draft) {
173 $data['draftData'] = serialize(array(
174 'participants' => $this->participantIDs,
175 'invisibleParticipants' => $this->invisibleParticipantIDs
176 ));
177 }
178
179 $conversationData = array(
180 'data' => $data,
181 'attachmentHandler' => $this->attachmentHandler,
182 'messageData' => array(
183 'message' => $this->text
184 )
185 );
186 if (!$this->draft) {
187 $conversationData['participants'] = $this->participantIDs;
188 $conversationData['invisibleParticipants'] = $this->invisibleParticipantIDs;
189 }
190
191 $this->objectAction = new ConversationAction(array(), 'create', $conversationData);
192 $resultValues = $this->objectAction->executeAction();
193 $this->saved();
194
195 // forward
196 HeaderUtil::redirect(LinkHandler::getInstance()->getLink('Conversation', array(
197 'object' => $resultValues['returnValues']
198 )));
199 exit;
200 }
201
202 /**
203 * @see wcf\page\IPage::readData()
204 */
205 public function readData() {
206 parent::readData();
207
208 // add breadcrumbs
209 WCF::getBreadcrumbs()->add(new Breadcrumb(WCF::getLanguage()->get('wcf.conversation.conversations'), LinkHandler::getInstance()->getLink('ConversationList')));
210 }
211
212 /**
213 * @see wcf\page\IPage::assignVariables()
214 */
215 public function assignVariables() {
216 parent::assignVariables();
217
218 WCF::getTPL()->assign(array(
219 'participantCanInvite' => $this->participantCanInvite,
220 'participants' => $this->participants,
221 'invisibleParticipants' => $this->invisibleParticipants
222 ));
223 }
224 }