Total Complexity | 67 |
Total Lines | 406 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 1 | Features | 0 |
Complex classes like AppointmentItemModule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AppointmentItemModule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class AppointmentItemModule extends ItemModule { |
||
9 | /** |
||
10 | * @var string client or server IANA timezone |
||
11 | */ |
||
12 | protected $tziana; |
||
13 | |||
14 | /** |
||
15 | * @var bool|string client timezone definition |
||
16 | */ |
||
17 | protected $tzdef; |
||
18 | |||
19 | /** |
||
20 | * @var array|bool client timezone definition array |
||
21 | */ |
||
22 | protected $tzdefObj; |
||
23 | |||
24 | /** |
||
25 | * @var mixed client timezone effective rule id |
||
26 | */ |
||
27 | protected $tzEffRuleIdx; |
||
28 | |||
29 | /** |
||
30 | * Constructor. |
||
31 | * |
||
32 | * @param int $id unique id |
||
33 | * @param array $data list of all actions |
||
34 | */ |
||
35 | public function __construct($id, $data) { |
||
51 | } |
||
52 | |||
53 | #[Override] |
||
54 | public function open($store, $entryid, $action) { |
||
55 | if ($store && $entryid) { |
||
56 | $data = []; |
||
57 | |||
58 | $message = $GLOBALS['operations']->openMessage($store, $entryid); |
||
59 | |||
60 | if (empty($message)) { |
||
61 | return; |
||
62 | } |
||
63 | |||
64 | // Open embedded message if requested |
||
65 | $attachNum = !empty($action['attach_num']) ? $action['attach_num'] : false; |
||
66 | if ($attachNum) { |
||
67 | // get message props of sub message |
||
68 | $parentMessage = $message; |
||
69 | $message = $GLOBALS['operations']->openMessage($store, $entryid, $attachNum); |
||
70 | |||
71 | if (empty($message)) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | $data['item'] = $GLOBALS['operations']->getEmbeddedMessageProps($store, $message, $this->properties, $parentMessage, $attachNum); |
||
76 | } |
||
77 | else { |
||
78 | // add all standard properties from the series/normal message |
||
79 | $data['item'] = $GLOBALS['operations']->getMessageProps($store, $message, $this->properties, $this->plaintext); |
||
80 | } |
||
81 | |||
82 | if (!empty($action["timezone_iana"])) { |
||
83 | $this->tziana = $action["timezone_iana"]; |
||
84 | |||
85 | try { |
||
86 | $this->tzdef = mapi_ianatz_to_tzdef($action['timezone_iana']); |
||
87 | } |
||
88 | catch (Exception) { |
||
|
|||
89 | } |
||
90 | } |
||
91 | |||
92 | // if appointment is recurring then only we should get properties of occurrence if basedate is supplied |
||
93 | if ($data['item']['props']['recurring'] === true) { |
||
94 | if (!empty($action['basedate'])) { |
||
95 | // check for occurrence/exception |
||
96 | $basedate = $action['basedate']; |
||
97 | |||
98 | $recur = new Recurrence($store, $message); |
||
99 | |||
100 | $exceptionatt = $recur->getExceptionAttachment($basedate); |
||
101 | |||
102 | // Single occurrences are never recurring |
||
103 | $data['item']['props']['recurring'] = false; |
||
104 | |||
105 | if ($exceptionatt) { |
||
106 | // Existing exception (open existing item, which includes basedate) |
||
107 | $exceptionattProps = mapi_getprops($exceptionatt, [PR_ATTACH_NUM]); |
||
108 | $exception = mapi_attach_openobj($exceptionatt, 0); |
||
109 | |||
110 | // overwrite properties with the ones from the exception |
||
111 | $exceptionProps = $GLOBALS['operations']->getMessageProps($store, $exception, $this->properties, $this->plaintext); |
||
112 | |||
113 | /* |
||
114 | * If recurring item has set reminder to true then |
||
115 | * all occurrences before the 'flagdueby' value(of recurring item) |
||
116 | * should not show that reminder is set. |
||
117 | */ |
||
118 | if (isset($exceptionProps['props']['reminder']) && $data['item']['props']['reminder'] == true) { |
||
119 | $flagDueByDay = $recur->dayStartOf($data['item']['props']['flagdueby']); |
||
120 | |||
121 | if ($flagDueByDay > $basedate) { |
||
122 | $exceptionProps['props']['reminder'] = false; |
||
123 | } |
||
124 | } |
||
125 | |||
126 | // The properties must be merged, if the recipients or attachments are present in the exception |
||
127 | // then that list should be used. Otherwise the list from the series must be applied (this |
||
128 | // corresponds with OL2007). |
||
129 | // @FIXME getMessageProps should not return empty string if exception doesn't contain body |
||
130 | // by this change we can handle a situation where user has set empty string in the body explicitly |
||
131 | if (!empty($exceptionProps['props']['body']) || !empty($exceptionProps['props']['html_body'])) { |
||
132 | if (!empty($exceptionProps['props']['body'])) { |
||
133 | $data['item']['props']['body'] = $exceptionProps['props']['body']; |
||
134 | } |
||
135 | |||
136 | if (!empty($exceptionProps['props']['html_body'])) { |
||
137 | $data['item']['props']['html_body'] = $exceptionProps['props']['html_body']; |
||
138 | } |
||
139 | |||
140 | $data['item']['props']['isHTML'] = $exceptionProps['props']['isHTML']; |
||
141 | } |
||
142 | // remove properties from $exceptionProps so array_merge will not overwrite it |
||
143 | unset($exceptionProps['props']['html_body'], $exceptionProps['props']['body'], $exceptionProps['props']['isHTML']); |
||
144 | |||
145 | $data['item']['props'] = array_merge($data['item']['props'], $exceptionProps['props']); |
||
146 | if (isset($exceptionProps['recipients'])) { |
||
147 | $data['item']['recipients'] = $exceptionProps['recipients']; |
||
148 | } |
||
149 | |||
150 | if (isset($exceptionProps['attachments'])) { |
||
151 | $data['item']['attachments'] = $exceptionProps['attachments']; |
||
152 | } |
||
153 | |||
154 | // Make sure we are using the passed basedate and not something wrong in the opened item |
||
155 | $data['item']['props']['basedate'] = $basedate; |
||
156 | $data['item']['attach_num'] = [$exceptionattProps[PR_ATTACH_NUM]]; |
||
157 | } |
||
158 | elseif ($recur->isDeleteException($basedate)) { |
||
159 | // Exception is deleted, should not happen, but if it the case then give error |
||
160 | $this->sendFeedback( |
||
161 | false, |
||
162 | [ |
||
163 | 'type' => ERROR_ZARAFA, |
||
164 | 'info' => [ |
||
165 | 'original_message' => _('Could not open occurrence.'), |
||
166 | 'display_message' => _('Could not open occurrence, specific occurrence is probably deleted.'), |
||
167 | ], |
||
168 | ] |
||
169 | ); |
||
170 | |||
171 | return; |
||
172 | } |
||
173 | else { |
||
174 | // opening an occurrence of a recurring series (same as normal open, but add basedate, startdate and enddate) |
||
175 | $data['item']['props']['basedate'] = $basedate; |
||
176 | $data['item']['props']['startdate'] = $recur->getOccurrenceStart($basedate); |
||
177 | $data['item']['props']['duedate'] = $recur->getOccurrenceEnd($basedate); |
||
178 | $data['item']['props']['commonstart'] = $data['item']['props']['startdate']; |
||
179 | $data['item']['props']['commonend'] = $data['item']['props']['duedate']; |
||
180 | unset($data['item']['props']['reminder_time']); |
||
181 | |||
182 | /* |
||
183 | * If recurring item has set reminder to true then |
||
184 | * all occurrences before the 'flagdueby' value(of recurring item) |
||
185 | * should not show that reminder is set. |
||
186 | */ |
||
187 | if (isset($exceptionProps['props']['reminder']) && $data['item']['props']['reminder'] == true) { |
||
188 | $flagDueByDay = $recur->dayStartOf($data['item']['props']['flagdueby']); |
||
189 | |||
190 | if ($flagDueByDay > $basedate) { |
||
191 | $exceptionProps['props']['reminder'] = false; |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | else { |
||
197 | // Opening a recurring series, get the recurrence information |
||
198 | $recur = new Recurrence($store, $message); |
||
199 | $recurpattern = $recur->getRecurrence(); |
||
200 | $tz = $recur->tz; // no function to do this at the moment |
||
201 | |||
202 | // Add the recurrence pattern to the data |
||
203 | if (isset($recurpattern) && is_array($recurpattern)) { |
||
204 | $data['item']['props'] += $recurpattern; |
||
205 | } |
||
206 | |||
207 | // Add the timezone information to the data |
||
208 | if (isset($tz) && is_array($tz)) { |
||
209 | $data['item']['props'] += $tz; |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | |||
214 | // Fix for all-day events which have a different timezone than the user's browser |
||
215 | if ($data['item']['props']['alldayevent'] == 1 && $this->tzdef !== false) { |
||
216 | $this->processAllDayItem($store, $data['item'], $message); |
||
217 | } |
||
218 | |||
219 | // Send the data |
||
220 | $this->addActionData('item', $data); |
||
221 | $GLOBALS['bus']->addData($this->getResponseData()); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Function does customization of exception based on module data. |
||
227 | * like, here it will generate display message based on actionType |
||
228 | * for particular exception. |
||
229 | * |
||
230 | * @param object $e Exception object |
||
231 | * @param string $actionType the action type, sent by the client |
||
232 | * @param MAPIobject $store store object of message |
||
233 | * @param string $parententryid parent entryid of the message |
||
234 | * @param string $entryid entryid of the message |
||
235 | * @param array $action the action data, sent by the client |
||
236 | */ |
||
237 | #[Override] |
||
238 | public function handleException(&$e, $actionType = null, $store = null, $parententryid = null, $entryid = null, $action = null) { |
||
239 | if (is_null($e->displayMessage)) { |
||
240 | switch ($actionType) { |
||
241 | case "save": |
||
242 | if ($e->getCode() == MAPI_E_NO_ACCESS) { |
||
243 | $message = mapi_msgstore_openentry($store, $entryid); |
||
244 | $messageProps = mapi_getprops($message, [PR_MESSAGE_CLASS, PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID]); |
||
245 | $messageClass = $messageProps[PR_MESSAGE_CLASS]; |
||
246 | |||
247 | $text = $messageClass !== "IPM.Appointment" ? _('a meeting request') : _('an appointment'); |
||
248 | $msg = _('You have insufficient privileges to move ' . $text . ' in this calendar. The calendar owner can set these using the \'permissions\'-tab of the folder properties (right click the calendar folder > properties > permissions)'); |
||
249 | |||
250 | $e->setDisplayMessage($msg); |
||
251 | $e->setTitle(_('Insufficient privileges')); |
||
252 | |||
253 | // Need this notification to refresh the calendar. |
||
254 | $GLOBALS['bus']->notify(bin2hex((string) $parententryid), TABLE_DELETE, $messageProps); |
||
255 | } |
||
256 | break; |
||
257 | } |
||
258 | } |
||
259 | parent::handleException($e, $actionType, $store, $parententryid, $entryid, $action); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Save the give appointment or meeting request to the calendar. |
||
264 | * |
||
265 | * @param mapistore $store MAPI store of the message |
||
266 | * @param string $parententryid Parent entryid of the message (folder entryid, NOT message entryid) |
||
267 | * @param string $entryid entryid of the message |
||
268 | * @param array $action Action array containing json request |
||
269 | * @param string $actionType The action type which triggered this action |
||
270 | */ |
||
271 | #[Override] |
||
357 | ], |
||
358 | ]); |
||
359 | } |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Processes an all-day item and calculates the correct starttime if necessary. |
||
364 | * |
||
365 | * @param object $store |
||
366 | * @param array $calendaritem |
||
367 | * @param object $message |
||
368 | */ |
||
369 | private function processAllDayItem($store, &$calendaritem, $message) { |
||
416 |