Test Failed
Push — master ( 647c72...cd42b5 )
by
unknown
10:25
created

ReminderItemModule::delete()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 12
nop 4
dl 0
loc 24
rs 9.0777
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Reminder ItemModule
5
 * Module which saves an item and it
6
 * extends the CreateMailItemModule class.
7
 */
8
class ReminderItemModule extends ItemModule
9
{
10
    /**
11
     * Constructor
12
     * @param int $id unique id.
13
     * @param array $data list of all actions.
14
     */
15
    function __construct($id, $data)
16
    {
17
        parent::__construct($id, $data);
18
    }
19
20
    /**
21
     * Function is use to set message flags for flagged mail item.
22
     * @param object $store MAPI Message Store Object
23
     * @param string $parententryid parent entryid of the message
24
     * @param string $entryid entryid of the message
25
     * @param array $action the action data, sent by the client
26
     * @return boolean true on success or false on failure
27
     */
28
    function save($store, $parententryid, $entryid, $action)
29
    {
30
        $this->properties = $GLOBALS['properties']->getMailProperties();
0 ignored issues
show
Bug Best Practice introduced by
The property properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $result = false;
32
33
        if (!$store) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
34
            $store = $GLOBALS['mapisession']->getDefaultMessageStore();
35
        }
36
37
        if ($store) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
38
            // Reference to an array which will be filled with PR_ENTRYID, PR_STORE_ENTRYID and PR_PARENT_ENTRYID of the message
39
            $messageProps = array();
40
41
            // Set message flags
42
            if (isset($action['props']) && isset($action['props']['message_flags']) && $entryid) {
43
                $msg_action = isset($action['message_action']) ? $action['message_action'] : false;
44
                $result = $GLOBALS['operations']->setMessageFlag($store, $entryid, $action['props']['message_flags'], $msg_action, $messageProps);
45
            }
46
47
            // Feedback for successful save
48
            if ($result) {
49
                $GLOBALS['bus']->notify(bin2hex($messageProps[PR_PARENT_ENTRYID]), TABLE_SAVE, $messageProps);
50
            }
51
52
            $this->sendFeedback($result ? true : false, array(), true);
53
        }
54
    }
55
56
    /**
57
     * Function which is use to dismiss or snooze the reminder item.
58
     * @param object $store MAPI Message Store Object
59
     * @param string $parententryid parent entryid of the message
60
     * @param string $entryid entryid of the message
61
     * @param array $action the action data, sent by the client
62
     * @return boolean true on success or false on failure
63
     */
64
    function delete($store, $parententryid, $entryid, $action)
65
    {
66
        $this->properties = $GLOBALS["properties"]->getReminderProperties();
0 ignored issues
show
Bug Best Practice introduced by
The property properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
68
        if(!$store) {
0 ignored issues
show
introduced by
$store is of type object, thus it always evaluated to true.
Loading history...
69
            $store = $GLOBALS["mapisession"]->getDefaultMessageStore();
70
        }
71
72
        $subActionType = false;
73
        if (isset($action["message_action"]) && isset($action["message_action"]["action_type"])) {
74
            $subActionType = $action["message_action"]["action_type"];
75
        }
76
77
        switch ($subActionType) {
78
            case "snooze":
79
                $entryid = $this->getActionEntryID($action);
80
                $this->snoozeItem($store, $entryid, $action);
0 ignored issues
show
Bug introduced by
$entryid of type object is incompatible with the type string expected by parameter $entryid of ReminderItemModule::snoozeItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
                $this->snoozeItem($store, /** @scrutinizer ignore-type */ $entryid, $action);
Loading history...
81
                break;
82
            case "dismiss":
83
                $entryid = $this->getActionEntryID($action);
84
                $this->dismissItem($store, $entryid);
0 ignored issues
show
Bug introduced by
$entryid of type object is incompatible with the type string expected by parameter $entryid of ReminderItemModule::dismissItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
                $this->dismissItem($store, /** @scrutinizer ignore-type */ $entryid);
Loading history...
85
                break;
86
            default:
87
                $this->handleUnknownActionType($subActionType);
88
        }
89
    }
90
91
    /**
92
     * Function which is use to snooze the reminder for given time
93
     * @param object $store MAPI Message Store Object
94
     * @param string $entryid entryid of the message
95
     * @param array $action the action data, sent by the client
96
     */
97
    function snoozeItem($store, $entryid, $action)
98
    {
99
        $result = false;
100
        $message = mapi_msgstore_openentry($store, $entryid);
101
        if ($message) {
102
            $newProps = array(PR_ENTRYID => $entryid);
103
            $props = mapi_getprops($message, $this->properties);
104
105
            $snoozeTime = $GLOBALS["settings"]->get('zarafa/v1/main/reminder/default_snooze_time', 5);
106
            if (isset($action["message_action"]["snoozeTime"]) && is_numeric($action["message_action"]["snoozeTime"])) {
107
                $snoozeTime = $action["message_action"]["snoozeTime"];
108
            }
109
110
            $reminderTime = time() + ($snoozeTime * 60);
111
            if (stripos($props[$this->properties["message_class"]], "IPM.Appointment") === 0) {
112
                if (isset($props[$this->properties["appointment_recurring"]]) && $props[$this->properties["appointment_recurring"]]) {
113
114
                    $recurrence = new Recurrence($store, $message);
0 ignored issues
show
Bug introduced by
$store of type object is incompatible with the type resource expected by parameter $store of Recurrence::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
                    $recurrence = new Recurrence(/** @scrutinizer ignore-type */ $store, $message);
Loading history...
115
                    $nextReminder = $recurrence->getNextReminderTime(time());
116
117
                    // flagdueby must be the snooze time or the time of the next instance, whichever is earlier
118
                    if ($reminderTime < $nextReminder)
119
                        $newProps[$this->properties["flagdueby"]] = $reminderTime;
120
                    else
121
                        $newProps[$this->properties["flagdueby"]] = $nextReminder;
122
                } else {
123
                    $newProps[$this->properties["flagdueby"]] = $reminderTime;
124
                }
125
            } else {
126
                $newProps[$this->properties["flagdueby"]] = $reminderTime;
127
            }
128
129
            // save props
130
            mapi_setprops($message, $newProps);
131
            mapi_savechanges($message);
132
133
            $result = true;
134
        }
135
136
        if ($result) {
137
            /**
138
             * @FIXME: Fix notifications for reminders.
139
             * Notifications are currently disabled, because deleting multiple items will notify
140
             * hierarchy multiple time but no data is changed with folder item in hierarchy.
141
             * so it will push same data again which will lead to an error.
142
             */
143
            //$props = mapi_getprops($message, array(PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID));
144
            //$GLOBALS["bus"]->notify(bin2hex($props[PR_PARENT_ENTRYID]), TABLE_SAVE, $props);
145
        }
146
        $this->sendFeedback($result);
147
    }
148
149
    /**
150
     * Function which is use to dismiss the reminder
151
     * @param object $store MAPI Message Store Object
152
     * @param string $entryid entryid of the message
153
     */
154
    function dismissItem($store, $entryid)
155
    {
156
        $result = false;
157
        $message = mapi_msgstore_openentry($store, $entryid);
158
        if ($message) {
159
            $newProps = array();
160
            $props = mapi_getprops($message, $this->properties);
161
162
            if (stripos($props[$this->properties["message_class"]], "IPM.Appointment") === 0) {
163
                if (isset($props[$this->properties["appointment_recurring"]]) && $props[$this->properties["appointment_recurring"]]) {
164
165
                    $recurrence = new Recurrence($store, $message);
0 ignored issues
show
Bug introduced by
$store of type object is incompatible with the type resource expected by parameter $store of Recurrence::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
                    $recurrence = new Recurrence(/** @scrutinizer ignore-type */ $store, $message);
Loading history...
166
                    // check for next reminder after "now" for the next instance
167
                    $nextReminder = $recurrence->getNextReminderTime(time());
168
                    if ($nextReminder)
0 ignored issues
show
Bug Best Practice introduced by
The expression $nextReminder of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
169
                        $newProps[$this->properties["flagdueby"]] = $nextReminder;
170
                    else
171
                        $newProps[$this->properties["reminder"]] = false;
172
                } else {
173
                    $newProps[$this->properties["reminder"]] = false;
174
                }
175
            } else if (stripos($props[$this->properties["message_class"]], "IPM.Task") === 0) {
176
                $newProps[$this->properties["reminder"]] = false;
177
178
                if (isset($props[$this->properties['task_recurring']]) && $props[$this->properties['task_recurring']] == 1) {
179
                    $newProps[$this->properties['task_resetreminder']] = true;
180
                }
181
            } else {
182
                $newProps[$this->properties["reminder"]] = false;
183
            }
184
185
            // save props
186
            mapi_setprops($message, $newProps);
187
            mapi_savechanges($message);
188
189
            $result = true;
190
        }
191
192
        if ($result) {
193
            /**
194
             * @FIXME: Fix notifications for reminders.
195
             * Notifications are currently disabled, because deleting multiple items will notify
196
             * hierarchy multiple time but no data is changed with folder item in hierarchy.
197
             * so it will push same data again which will lead to an error.
198
             */
199
            //$props = mapi_getprops($message, array(PR_ENTRYID, PR_PARENT_ENTRYID, PR_STORE_ENTRYID));
200
            //$GLOBALS["bus"]->notify(bin2hex($props[PR_PARENT_ENTRYID]), TABLE_SAVE, $props);
201
        }
202
        $this->sendFeedback($result);
203
    }
204
205
    /**
206
     * Function does customization of exception based on module data.
207
     * like, here it will generate display message based on actionType
208
     * for particular exception.
209
     *
210
     * @param object $e Exception object
211
     * @param string $actionType the action type, sent by the client
212
     * @param MAPIobject $store Store object of message.
0 ignored issues
show
Bug introduced by
The type MAPIobject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
213
     * @param string $parententryid parent entryid of the message.
214
     * @param string $entryid entryid of the message.
215
     * @param array $action the action data, sent by the client
216
     */
217
    function handleException(&$e, $actionType = null, $store = null, $parententryid = null, $entryid = null, $action = null)
218
    {
219
        if (is_null($e->displayMessage)) {
220
            switch ($actionType) {
221
                case 'delete':
222
                    if (!empty($action['message_action']['action_type'])) {
223
                        switch ($action['message_action']['action_type']) {
224
                            case 'snooze':
225
                                if ($e->getCode() == MAPI_E_STORE_FULL) {
226
                                    $e->setDisplayMessage(_('Cannot snooze the reminder. You may be reminded again.') . '<br />' . $this->getOverQuotaMessage($store));
227
                                } else {
228
                                    $e->setDisplayMessage(_('Cannot snooze the reminder. You may be reminded again.'));
229
                                }
230
                                break;
231
                            case 'dismiss':
232
                                if ($e->getCode() == MAPI_E_STORE_FULL) {
233
                                    $e->setDisplayMessage(_('Cannot dismiss the reminder. You may be reminded again.') . '<br />' . $this->getOverQuotaMessage($store));
234
                                } else {
235
                                    $e->setDisplayMessage(_('Cannot dismiss the reminder. You may be reminded again.'));
236
                                }
237
                                break;
238
                        }
239
                    }
240
                    break;
241
            }
242
        }
243
244
        parent::handleException($e, $actionType, $store, $parententryid, $entryid, $action);
245
    }
246
}
247
248
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
249