Conditions | 11 |
Paths | 48 |
Total Lines | 80 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
26 | public function execute() |
||
27 | { |
||
28 | global $sourcedir, $smcFunc, $user_profile; |
||
29 | |||
30 | // Get everyone who could be notified - those are the people who can see the calendar. |
||
31 | require_once($sourcedir . '/Subs-Members.php'); |
||
32 | $members = membersAllowedTo('calendar_view'); |
||
33 | |||
34 | // Don't alert the event creator |
||
35 | if (!empty($this->_details['sender_id'])) |
||
36 | $members = array_diff($members, array($this->_details['sender_id'])); |
||
37 | |||
38 | // Having successfully figured this out, now let's get the preferences of everyone. |
||
39 | require_once($sourcedir . '/Subs-Notify.php'); |
||
40 | $prefs = getNotifyPrefs($members, 'event_new', true); |
||
41 | |||
42 | // Just before we go any further, we may not have the sender's name. Let's just quickly fix that. |
||
43 | // If a guest creates the event, we wouldn't be capturing a username or anything. |
||
44 | if (!empty($this->_details['sender_id']) && empty($this->_details['sender_name'])) |
||
45 | { |
||
46 | loadMemberData($this->_details['sender_id'], 'minimal'); |
||
|
|||
47 | if (!empty($user_profile[$this->_details['sender_id']])) |
||
48 | $this->_details['sender_name'] = $user_profile[$this->_details['sender_id']]['real_name']; |
||
49 | else |
||
50 | $this->_details['sender_id'] = 0; |
||
51 | } |
||
52 | |||
53 | // So now we find out who wants what. |
||
54 | $alert_bits = array( |
||
55 | 'alert' => self::RECEIVE_NOTIFY_ALERT, |
||
56 | 'email' => self::RECEIVE_NOTIFY_EMAIL, |
||
57 | ); |
||
58 | $notifies = array(); |
||
59 | |||
60 | foreach ($prefs as $member => $pref_option) |
||
61 | { |
||
62 | foreach ($alert_bits as $type => $bitvalue) |
||
63 | if ($pref_option['event_new'] & $bitvalue) |
||
64 | $notifies[$type][] = $member; |
||
65 | } |
||
66 | |||
67 | // Firstly, anyone who wants alerts. |
||
68 | if (!empty($notifies['alert'])) |
||
69 | { |
||
70 | // Alerts are relatively easy. |
||
71 | $insert_rows = array(); |
||
72 | foreach ($notifies['alert'] as $member) |
||
73 | { |
||
74 | $insert_rows[] = array( |
||
75 | 'alert_time' => $this->_details['time'], |
||
76 | 'id_member' => $member, |
||
77 | 'id_member_started' => $this->_details['sender_id'], |
||
78 | 'member_name' => $this->_details['sender_name'], |
||
79 | 'content_type' => 'event', |
||
80 | 'content_id' => $this->_details['event_id'], |
||
81 | 'content_action' => empty($this->_details['sender_id']) ? 'new_guest' : 'new', |
||
82 | 'is_read' => 0, |
||
83 | 'extra' => $smcFunc['json_encode']( |
||
84 | array( |
||
85 | "event_id" => $this->_details['event_id'], |
||
86 | "event_title" => $this->_details['event_title'] |
||
87 | ) |
||
88 | ), |
||
89 | ); |
||
90 | } |
||
91 | |||
92 | $smcFunc['db_insert']('insert', |
||
93 | '{db_prefix}user_alerts', |
||
94 | array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', |
||
95 | 'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int', |
||
96 | 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'), |
||
97 | $insert_rows, |
||
98 | array('id_alert') |
||
99 | ); |
||
100 | |||
101 | // And update the count of alerts for those people. |
||
102 | updateMemberData($notifies['alert'], array('alerts' => '+')); |
||
103 | } |
||
104 | |||
105 | return true; |
||
106 | } |
||
109 | ?> |