Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MessageManager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MessageManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class MessageManager |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Get count new messages for the current user from the database. |
||
| 19 | * @return int |
||
| 20 | */ |
||
| 21 | public static function getCountNewMessages() |
||
| 22 | { |
||
| 23 | if (!api_get_user_id()) { |
||
| 24 | return false; |
||
| 25 | } |
||
| 26 | static $count; |
||
| 27 | if (!isset($count)) { |
||
| 28 | $cacheEnabled = function_exists('apcu_exists'); |
||
| 29 | if ($cacheEnabled) { |
||
| 30 | $var = 'social_messages_unread_u_'.$userId; |
||
| 31 | if (apcu_exists($var)) { |
||
| 32 | $count = apcu_fetch($var); |
||
| 33 | } else { |
||
| 34 | $count = self::getCountNewMessagesFromDB($userId); |
||
| 35 | apcu_store($var, $count, 60); |
||
| 36 | } |
||
| 37 | } else { |
||
| 38 | $count = self::getCountNewMessagesFromDB($userId); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | return $count; |
||
| 43 | } |
||
| 44 | /** |
||
| 45 | * Execute the SQL necessary to know the number of messages in the database |
||
| 46 | * @param int $userId The user for which we need the unread messages count |
||
| 47 | * @return int The number of unread messages in the database for the given user |
||
| 48 | */ |
||
| 49 | View Code Duplication | private static function getCountNewMessagesFromDB($userId) |
|
| 50 | { |
||
| 51 | if (empty($userId)) { |
||
| 52 | return 0; |
||
| 53 | } |
||
| 54 | $table = Database::get_main_table(TABLE_MESSAGE); |
||
| 55 | $sql = "SELECT COUNT(id) as count |
||
| 56 | FROM $table |
||
| 57 | WHERE |
||
| 58 | user_receiver_id=" . api_get_user_id() . " AND |
||
| 59 | msg_status = " . MESSAGE_STATUS_UNREAD; |
||
| 60 | $result = Database::query($sql); |
||
| 61 | $row = Database::fetch_assoc($result); |
||
| 62 | |||
| 63 | return $row['count']; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get the list of user_ids of users who are online. |
||
| 68 | */ |
||
| 69 | public static function users_connected_by_id() |
||
| 70 | { |
||
| 71 | $count = who_is_online_count(); |
||
| 72 | $user_connect = who_is_online(0, $count, null, null, 30, true); |
||
| 73 | $user_id_list = array(); |
||
| 74 | for ($i = 0; $i < count($user_connect); $i++) { |
||
| 75 | $user_id_list[$i] = $user_connect[$i][0]; |
||
| 76 | } |
||
| 77 | |||
| 78 | return $user_id_list; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Gets the total number of messages, used for the inbox sortable table |
||
| 83 | */ |
||
| 84 | public static function get_number_of_messages($unread = false) |
||
| 85 | { |
||
| 86 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 87 | if ($unread) { |
||
| 88 | $condition_msg_status = ' msg_status = ' . MESSAGE_STATUS_UNREAD . ' '; |
||
| 89 | } else { |
||
| 90 | $condition_msg_status = ' msg_status IN(' . MESSAGE_STATUS_NEW . ',' . MESSAGE_STATUS_UNREAD . ') '; |
||
| 91 | } |
||
| 92 | |||
| 93 | $keyword = Session::read('message_search_keyword'); |
||
| 94 | $keywordCondition = ''; |
||
| 95 | if (!empty($keyword)) { |
||
| 96 | $keyword = Database::escape_string($keyword); |
||
| 97 | $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') "; |
||
| 98 | } |
||
| 99 | |||
| 100 | $sql = "SELECT COUNT(id) as number_messages |
||
| 101 | FROM $table_message |
||
| 102 | WHERE $condition_msg_status AND |
||
| 103 | user_receiver_id=" . api_get_user_id() . " |
||
| 104 | $keywordCondition |
||
| 105 | "; |
||
| 106 | $result = Database::query($sql); |
||
| 107 | $result = Database::fetch_array($result); |
||
| 108 | |||
| 109 | return $result['number_messages']; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Gets information about some messages, used for the inbox sortable table |
||
| 114 | * @param int $from |
||
| 115 | * @param int $number_of_items |
||
| 116 | * @param string $direction |
||
| 117 | */ |
||
| 118 | public static function get_message_data($from, $number_of_items, $column, $direction) |
||
| 119 | { |
||
| 120 | $from = intval($from); |
||
| 121 | $number_of_items = intval($number_of_items); |
||
| 122 | |||
| 123 | //forcing this order |
||
| 124 | View Code Duplication | if (!isset($direction)) { |
|
| 125 | $column = 3; |
||
| 126 | $direction = 'DESC'; |
||
| 127 | } else { |
||
| 128 | $column = intval($column); |
||
| 129 | if (!in_array($direction, array('ASC', 'DESC'))) { |
||
| 130 | $direction = 'ASC'; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | $keyword = Session::read('message_search_keyword'); |
||
| 135 | $keywordCondition = ''; |
||
| 136 | if (!empty($keyword)) { |
||
| 137 | $keyword = Database::escape_string($keyword); |
||
| 138 | $keywordCondition = " AND (title like '%$keyword%' OR content LIKE '%$keyword%') "; |
||
| 139 | } |
||
| 140 | |||
| 141 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 142 | |||
| 143 | $sql = "SELECT |
||
| 144 | id as col0, |
||
| 145 | user_sender_id as col1, |
||
| 146 | title as col2, |
||
| 147 | send_date as col3, |
||
| 148 | msg_status as col4 |
||
| 149 | FROM $table_message |
||
| 150 | WHERE |
||
| 151 | user_receiver_id=" . api_get_user_id() . " AND |
||
| 152 | msg_status IN (0,1) |
||
| 153 | $keywordCondition |
||
| 154 | ORDER BY col$column $direction |
||
| 155 | LIMIT $from, $number_of_items"; |
||
| 156 | |||
| 157 | $sql_result = Database::query($sql); |
||
| 158 | $i = 0; |
||
| 159 | $message_list = array(); |
||
| 160 | |||
| 161 | while ($result = Database::fetch_row($sql_result)) { |
||
| 162 | $message[0] = $result[0]; |
||
| 163 | $result[2] = Security::remove_XSS($result[2], STUDENT, true); |
||
| 164 | $result[2] = cut($result[2], 80, true); |
||
| 165 | |||
| 166 | if ($result[4] == 1) { |
||
| 167 | $class = 'class = "unread"'; |
||
| 168 | } else { |
||
| 169 | $class = 'class = "read"'; |
||
| 170 | } |
||
| 171 | $link = ''; |
||
| 172 | if (isset($_GET['f']) && $_GET['f'] == 'social') { |
||
| 173 | $link = '&f=social'; |
||
| 174 | } |
||
| 175 | $userInfo = api_get_user_info($result[1]); |
||
| 176 | $message[1] = '<a ' . $class . ' href="view_message.php?id=' . $result[0] . $link . '">' . $result[2] . '</a><br />' . $userInfo['complete_name']; |
||
| 177 | $message[3] = '<a href="new_message.php?re_id=' . $result[0] . $link . '">' . |
||
| 178 | Display::return_icon('message_reply.png', get_lang('ReplyToMessage')) . '</a>' . |
||
| 179 | ' <a onclick="javascript:if(!confirm(' . "'" . addslashes(api_htmlentities(get_lang('ConfirmDeleteMessage'))) . "'" . ')) return false;" href="inbox.php?action=deleteone&id=' . $result[0] . $link . '">' . Display::return_icon('delete.png', get_lang('DeleteMessage')) . '</a>'; |
||
| 180 | |||
| 181 | $message[2] = api_convert_and_format_date($result[3], DATE_TIME_FORMAT_LONG); //date stays the same |
||
| 182 | foreach ($message as $key => $value) { |
||
| 183 | $message[$key] = api_xml_http_response_encode($value); |
||
| 184 | } |
||
| 185 | $message_list[] = $message; |
||
| 186 | $i++; |
||
| 187 | } |
||
| 188 | |||
| 189 | return $message_list; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Sends a message to a user/group |
||
| 194 | * |
||
| 195 | * @param int $receiver_user_id |
||
| 196 | * @param string $subject |
||
| 197 | * @param string $content |
||
| 198 | * @param array $file_attachments files array($_FILES) (optional) |
||
| 199 | * @param array $file_comments about attachment files (optional) |
||
| 200 | * @param int $group_id (optional) |
||
| 201 | * @param int $parent_id (optional) |
||
| 202 | * @param int $edit_message_id id for updating the message (optional) |
||
| 203 | * @param int $topic_id (optional) the default value is the current user_id |
||
| 204 | * @param int $sender_id |
||
| 205 | * @param bool $directMessage |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public static function send_message( |
||
| 210 | $receiver_user_id, |
||
| 211 | $subject, |
||
| 212 | $content, |
||
| 213 | array $file_attachments = [], |
||
| 214 | array $file_comments = [], |
||
| 215 | $group_id = 0, |
||
| 216 | $parent_id = 0, |
||
| 217 | $edit_message_id = 0, |
||
| 218 | $topic_id = 0, |
||
| 219 | $sender_id = null, |
||
| 220 | $directMessage = false |
||
| 221 | ) { |
||
| 222 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 223 | $group_id = intval($group_id); |
||
| 224 | $receiver_user_id = intval($receiver_user_id); |
||
| 225 | $parent_id = intval($parent_id); |
||
| 226 | $edit_message_id = intval($edit_message_id); |
||
| 227 | $topic_id = intval($topic_id); |
||
| 228 | |||
| 229 | if (!empty($receiver_user_id)) { |
||
| 230 | $receiverUserInfo = api_get_user_info($receiver_user_id); |
||
| 231 | |||
| 232 | // Disabling messages for inactive users. |
||
| 233 | if ($receiverUserInfo['active'] == 0) { |
||
| 234 | return false; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | if (empty($sender_id)) { |
||
| 239 | $user_sender_id = api_get_user_id(); |
||
| 240 | } else { |
||
| 241 | $user_sender_id = intval($sender_id); |
||
| 242 | } |
||
| 243 | |||
| 244 | $total_filesize = 0; |
||
| 245 | if (is_array($file_attachments)) { |
||
| 246 | foreach ($file_attachments as $file_attach) { |
||
| 247 | $fileSize = isset($file_attach['size']) ? $file_attach['size'] : 0; |
||
| 248 | if (is_array($fileSize)) { |
||
| 249 | foreach ($fileSize as $size) { |
||
| 250 | $total_filesize += $size; |
||
| 251 | } |
||
| 252 | } else { |
||
| 253 | $total_filesize += $fileSize; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | // Validating fields |
||
| 259 | if (empty($subject) && empty($group_id)) { |
||
| 260 | Display::addFlash(Display::return_message(get_lang('YouShouldWriteASubject'), 'warning')); |
||
| 261 | return false; |
||
| 262 | } else if ($total_filesize > intval(api_get_setting('message_max_upload_filesize'))) { |
||
| 263 | $warning = sprintf( |
||
| 264 | get_lang("FilesSizeExceedsX"), |
||
| 265 | format_file_size(api_get_setting('message_max_upload_filesize')) |
||
| 266 | ); |
||
| 267 | |||
| 268 | Display::addFlash(Display::return_message($warning, 'warning')); |
||
| 269 | |||
| 270 | return false; |
||
| 271 | } |
||
| 272 | |||
| 273 | $inbox_last_id = null; |
||
| 274 | |||
| 275 | //Just in case we replace the and \n and \n\r while saving in the DB |
||
| 276 | //$content = str_replace(array("\n", "\n\r"), '<br />', $content); |
||
| 277 | |||
| 278 | $now = api_get_utc_datetime(); |
||
| 279 | if (!empty($receiver_user_id) || !empty($group_id)) { |
||
| 280 | // message for user friend |
||
| 281 | //@todo it's possible to edit a message? yes, only for groups |
||
| 282 | if ($edit_message_id) { |
||
| 283 | $query = " UPDATE $table_message SET |
||
| 284 | update_date = '" . $now . "', |
||
| 285 | content = '".Database::escape_string($content)."' |
||
| 286 | WHERE id = '$edit_message_id' "; |
||
| 287 | Database::query($query); |
||
| 288 | $inbox_last_id = $edit_message_id; |
||
| 289 | } else { |
||
| 290 | $params = [ |
||
| 291 | 'user_sender_id' => $user_sender_id, |
||
| 292 | 'user_receiver_id' => $receiver_user_id, |
||
| 293 | 'msg_status' => '1', |
||
| 294 | 'send_date' => $now, |
||
| 295 | 'title' => $subject, |
||
| 296 | 'content' => $content, |
||
| 297 | 'group_id' => $group_id, |
||
| 298 | 'parent_id' => $parent_id, |
||
| 299 | 'update_date' => $now |
||
| 300 | ]; |
||
| 301 | $inbox_last_id = Database::insert($table_message, $params); |
||
| 302 | } |
||
| 303 | |||
| 304 | // Save attachment file for inbox messages |
||
| 305 | if (is_array($file_attachments)) { |
||
| 306 | $i = 0; |
||
| 307 | foreach ($file_attachments as $file_attach) { |
||
| 308 | if ($file_attach['error'] == 0) { |
||
| 309 | self::save_message_attachment_file( |
||
| 310 | $file_attach, |
||
| 311 | isset($file_comments[$i]) ? $file_comments[$i] : null, |
||
| 312 | $inbox_last_id, |
||
| 313 | null, |
||
| 314 | $receiver_user_id, |
||
| 315 | $group_id |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | $i++; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | if (empty($group_id)) { |
||
| 323 | // message in outbox for user friend or group |
||
| 324 | $params = [ |
||
| 325 | 'user_sender_id' => $user_sender_id, |
||
| 326 | 'user_receiver_id' => $receiver_user_id, |
||
| 327 | 'msg_status' => '4', |
||
| 328 | 'send_date' => $now, |
||
| 329 | 'title' => $subject, |
||
| 330 | 'content' => $content, |
||
| 331 | 'group_id' => $group_id, |
||
| 332 | 'parent_id' => $parent_id, |
||
| 333 | 'update_date' => $now |
||
| 334 | ]; |
||
| 335 | $outbox_last_id = Database::insert($table_message, $params); |
||
| 336 | |||
| 337 | // save attachment file for outbox messages |
||
| 338 | if (is_array($file_attachments)) { |
||
| 339 | $o = 0; |
||
| 340 | foreach ($file_attachments as $file_attach) { |
||
| 341 | if ($file_attach['error'] == 0) { |
||
| 342 | $comment = isset($file_comments[$o]) ? $file_comments[$o] : ''; |
||
| 343 | self::save_message_attachment_file( |
||
| 344 | $file_attach, |
||
| 345 | $comment, |
||
| 346 | $outbox_last_id, |
||
| 347 | $user_sender_id |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | $o++; |
||
| 351 | } |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | // Load user settings. |
||
| 356 | $notification = new Notification(); |
||
| 357 | $sender_info = api_get_user_info($user_sender_id); |
||
| 358 | |||
| 359 | // add file attachment additional attributes |
||
| 360 | foreach ($file_attachments as $index => $file_attach) { |
||
| 361 | $file_attachments[$index]['path'] = $file_attach['tmp_name']; |
||
| 362 | $file_attachments[$index]['filename'] = $file_attach['name']; |
||
| 363 | } |
||
| 364 | |||
| 365 | if (empty($group_id)) { |
||
| 366 | $type = Notification::NOTIFICATION_TYPE_MESSAGE; |
||
| 367 | if ($directMessage) { |
||
| 368 | $type = Notification::NOTIFICATION_TYPE_DIRECT_MESSAGE; |
||
| 369 | } |
||
| 370 | $notification->save_notification( |
||
| 371 | $type, |
||
| 372 | array($receiver_user_id), |
||
| 373 | $subject, |
||
| 374 | $content, |
||
| 375 | $sender_info, |
||
| 376 | $file_attachments |
||
| 377 | ); |
||
| 378 | } else { |
||
| 379 | $usergroup = new UserGroup(); |
||
| 380 | $group_info = $usergroup->get($group_id); |
||
| 381 | $group_info['topic_id'] = $topic_id; |
||
| 382 | $group_info['msg_id'] = $inbox_last_id; |
||
| 383 | |||
| 384 | $user_list = $usergroup->get_users_by_group($group_id, false, array(), 0, 1000); |
||
| 385 | |||
| 386 | // Adding more sense to the message group |
||
| 387 | $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']); |
||
| 388 | |||
| 389 | $new_user_list = array(); |
||
| 390 | foreach ($user_list as $user_data) { |
||
| 391 | $new_user_list[] = $user_data['id']; |
||
| 392 | } |
||
| 393 | $group_info = array( |
||
| 394 | 'group_info' => $group_info, |
||
| 395 | 'user_info' => $sender_info, |
||
| 396 | ); |
||
| 397 | $notification->save_notification( |
||
| 398 | Notification::NOTIFICATION_TYPE_GROUP, |
||
| 399 | $new_user_list, |
||
| 400 | $subject, |
||
| 401 | $content, |
||
| 402 | $group_info, |
||
| 403 | $file_attachments |
||
| 404 | ); |
||
| 405 | } |
||
| 406 | |||
| 407 | return $inbox_last_id; |
||
| 408 | } |
||
| 409 | |||
| 410 | return false; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param int $receiver_user_id |
||
| 415 | * @param int $subject |
||
| 416 | * @param string $message |
||
| 417 | * @param int $sender_id |
||
| 418 | * @param bool $sendCopyToDrhUsers send copy to related DRH users |
||
| 419 | * @param bool $directMessage |
||
| 420 | * |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | public static function send_message_simple( |
||
| 424 | $receiver_user_id, |
||
| 425 | $subject, |
||
| 426 | $message, |
||
| 427 | $sender_id = null, |
||
| 428 | $sendCopyToDrhUsers = false, |
||
| 429 | $directMessage = false |
||
| 430 | ) { |
||
| 431 | $result = MessageManager::send_message( |
||
| 432 | $receiver_user_id, |
||
| 433 | $subject, |
||
| 434 | $message, |
||
| 435 | $_FILES ? $_FILES : [], |
||
| 436 | [], |
||
| 437 | null, |
||
| 438 | null, |
||
| 439 | null, |
||
| 440 | null, |
||
| 441 | $sender_id, |
||
| 442 | $directMessage |
||
| 443 | ); |
||
| 444 | |||
| 445 | if ($sendCopyToDrhUsers) { |
||
| 446 | $userInfo = api_get_user_info($receiver_user_id); |
||
| 447 | $drhList = UserManager::getDrhListFromUser($receiver_user_id); |
||
| 448 | if (!empty($drhList)) { |
||
| 449 | foreach ($drhList as $drhInfo) { |
||
| 450 | $message = sprintf( |
||
| 451 | get_lang('CopyOfMessageSentToXUser'), |
||
| 452 | $userInfo['complete_name'] |
||
| 453 | ) . ' <br />' . $message; |
||
| 454 | |||
| 455 | MessageManager::send_message_simple( |
||
| 456 | $drhInfo['user_id'], |
||
| 457 | $subject, |
||
| 458 | $message, |
||
| 459 | $sender_id, |
||
| 460 | false, |
||
| 461 | $directMessage |
||
| 462 | ); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | } |
||
| 466 | |||
| 467 | return $result; |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Update parent ids for other receiver user from current message in groups |
||
| 472 | * @author Christian Fasanando Flores |
||
| 473 | * @param int $parent_id |
||
| 474 | * @param int $receiver_user_id |
||
| 475 | * @param int $message_id |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | public static function update_parent_ids_from_reply($parent_id, $receiver_user_id, $message_id) |
||
| 479 | { |
||
| 480 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 481 | $parent_id = intval($parent_id); |
||
| 482 | $receiver_user_id = intval($receiver_user_id); |
||
| 483 | $message_id = intval($message_id); |
||
| 484 | // first get data from message id (parent) |
||
| 485 | $sql_message = "SELECT * FROM $table_message WHERE id = '$parent_id'"; |
||
| 486 | $rs_message = Database::query($sql_message); |
||
| 487 | $row_message = Database::fetch_array($rs_message); |
||
| 488 | |||
| 489 | // get message id from data found early for other receiver user |
||
| 490 | $sql = "SELECT id FROM $table_message |
||
| 491 | WHERE |
||
| 492 | user_sender_id ='{$row_message['user_sender_id']}' AND |
||
| 493 | title='{$row_message['title']}' AND |
||
| 494 | content='{$row_message['content']}' AND |
||
| 495 | group_id='{$row_message['group_id']}' AND |
||
| 496 | user_receiver_id='$receiver_user_id'"; |
||
| 497 | $rs_msg_id = Database::query($sql); |
||
| 498 | $row = Database::fetch_array($rs_msg_id); |
||
| 499 | |||
| 500 | // update parent_id for other user receiver |
||
| 501 | $sql = "UPDATE $table_message SET parent_id = " . $row['id'] . " |
||
| 502 | WHERE id = $message_id"; |
||
| 503 | Database::query($sql); |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @param int $user_receiver_id |
||
| 508 | * @param int $id |
||
| 509 | * @return bool |
||
| 510 | */ |
||
| 511 | public static function delete_message_by_user_receiver($user_receiver_id, $id) |
||
| 512 | { |
||
| 513 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 514 | if ($id != strval(intval($id))) { |
||
| 515 | return false; |
||
| 516 | } |
||
| 517 | $user_receiver_id = intval($user_receiver_id); |
||
| 518 | $id = intval($id); |
||
| 519 | $sql = "SELECT * FROM $table_message |
||
| 520 | WHERE id=" . $id . " AND msg_status<>4"; |
||
| 521 | $rs = Database::query($sql); |
||
| 522 | |||
| 523 | if (Database::num_rows($rs) > 0) { |
||
| 524 | // delete attachment file |
||
| 525 | self::delete_message_attachment_file($id, $user_receiver_id); |
||
| 526 | // delete message |
||
| 527 | $query = "UPDATE $table_message SET msg_status=3 |
||
| 528 | WHERE user_receiver_id=" . $user_receiver_id . " AND id=" . $id; |
||
| 529 | Database::query($query); |
||
| 530 | |||
| 531 | return true; |
||
| 532 | } else { |
||
| 533 | return false; |
||
| 534 | } |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Set status deleted |
||
| 539 | * @author Isaac FLores Paz <[email protected]> |
||
| 540 | * @param int |
||
| 541 | * @param int |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | public static function delete_message_by_user_sender($user_sender_id, $id) |
||
| 545 | { |
||
| 546 | if ($id != strval(intval($id))) { |
||
| 547 | return false; |
||
| 548 | } |
||
| 549 | |||
| 550 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 551 | |||
| 552 | $id = intval($id); |
||
| 553 | $user_sender_id = intval($user_sender_id); |
||
| 554 | |||
| 555 | $sql = "SELECT * FROM $table_message WHERE id='$id'"; |
||
| 556 | $rs = Database::query($sql); |
||
| 557 | |||
| 558 | if (Database::num_rows($rs) > 0) { |
||
| 559 | // delete attachment file |
||
| 560 | self::delete_message_attachment_file($id, $user_sender_id); |
||
| 561 | // delete message |
||
| 562 | $sql = "UPDATE $table_message |
||
| 563 | SET msg_status=3 |
||
| 564 | WHERE user_sender_id='$user_sender_id' AND id='$id'"; |
||
| 565 | Database::query($sql); |
||
| 566 | |||
| 567 | return true; |
||
| 568 | } |
||
| 569 | |||
| 570 | return false; |
||
| 571 | } |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Saves a message attachment files |
||
| 575 | * @param array $file_attach $_FILES['name'] |
||
| 576 | * @param string a comment about the uploaded file |
||
| 577 | * @param int message id |
||
| 578 | * @param int receiver user id (optional) |
||
| 579 | * @param int sender user id (optional) |
||
| 580 | * @param int group id (optional) |
||
| 581 | * @return void |
||
| 582 | */ |
||
| 583 | public static function save_message_attachment_file( |
||
| 639 | |||
| 640 | /** |
||
| 641 | * Delete message attachment files (logically updating the row with a suffix _DELETE_id) |
||
| 642 | * @param int message id |
||
| 643 | * @param int message user id (receiver user id or sender user id) |
||
| 644 | * @param int group id (optional) |
||
| 645 | * @return void |
||
| 646 | */ |
||
| 647 | public static function delete_message_attachment_file($message_id, $message_uid, $group_id = 0) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * update messages by user id and message id |
||
| 687 | * @param int $user_id |
||
| 688 | * @param int $message_id |
||
| 689 | * @return resource |
||
| 690 | */ |
||
| 691 | public static function update_message($user_id, $message_id) |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param int $user_id |
||
| 707 | * @param int $message_id |
||
| 708 | * @param string $type |
||
| 709 | * @return bool |
||
| 710 | */ |
||
| 711 | View Code Duplication | public static function update_message_status($user_id, $message_id, $type) |
|
| 725 | |||
| 726 | /** |
||
| 727 | * get messages by user id and message id |
||
| 728 | * @param int $user_id |
||
| 729 | * @param int $message_id |
||
| 730 | * @return array |
||
| 731 | */ |
||
| 732 | View Code Duplication | public static function get_message_by_user($user_id, $message_id) |
|
| 733 | { |
||
| 743 | |||
| 744 | /** |
||
| 745 | * get messages by group id |
||
| 746 | * @param int group id |
||
| 747 | * @return array |
||
| 748 | */ |
||
| 749 | public static function get_messages_by_group($group_id) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * get messages by group id |
||
| 774 | * @param int $group_id |
||
| 775 | * @param int $message_id |
||
| 776 | * @return array |
||
| 777 | */ |
||
| 778 | public static function get_messages_by_group_by_message($group_id, $message_id) |
||
| 779 | { |
||
| 780 | if ($group_id != strval(intval($group_id))) { |
||
| 781 | return false; |
||
| 782 | } |
||
| 783 | $table_message = Database::get_main_table(TABLE_MESSAGE); |
||
| 784 | $group_id = intval($group_id); |
||
| 785 | $sql = "SELECT * FROM $table_message |
||
| 786 | WHERE |
||
| 787 | group_id = $group_id AND |
||
| 788 | msg_status NOT IN ('" . MESSAGE_STATUS_OUTBOX . "', '" . MESSAGE_STATUS_DELETED . "') |
||
| 789 | ORDER BY id "; |
||
| 790 | |||
| 791 | $rs = Database::query($sql); |
||
| 792 | $data = array(); |
||
| 793 | $parents = array(); |
||
| 794 | View Code Duplication | if (Database::num_rows($rs) > 0) { |
|
| 795 | while ($row = Database::fetch_array($rs, 'ASSOC')) { |
||
| 796 | if ($message_id == $row['parent_id'] || in_array($row['parent_id'], $parents)) { |
||
| 797 | $parents[] = $row['id']; |
||
| 798 | $data[] = $row; |
||
| 799 | } |
||
| 800 | } |
||
| 801 | } |
||
| 802 | |||
| 803 | return $data; |
||
| 804 | } |
||
| 805 | |||
| 806 | /** |
||
| 807 | * get messages by parent id optionally with limit |
||
| 808 | * @param int parent id |
||
| 809 | * @param int group id (optional) |
||
| 810 | * @param int offset (optional) |
||
| 811 | * @param int limit (optional) |
||
| 812 | * @return array |
||
| 813 | */ |
||
| 814 | public static function get_messages_by_parent($parent_id, $group_id = '', $offset = 0, $limit = 0) |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Gets information about if exist messages |
||
| 853 | * @author Isaac FLores Paz <[email protected]> |
||
| 854 | * @param integer |
||
| 855 | * @param integer |
||
| 856 | * @return boolean |
||
| 857 | */ |
||
| 858 | public static function exist_message($user_id, $id) |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Gets information about messages sent |
||
| 878 | * @param integer |
||
| 879 | * @param integer |
||
| 880 | * @param string |
||
| 881 | * @return array |
||
| 882 | */ |
||
| 883 | public static function get_message_data_sent($from, $number_of_items, $column, $direction) |
||
| 957 | |||
| 958 | /** |
||
| 959 | * Gets information about number messages sent |
||
| 960 | * @author Isaac FLores Paz <[email protected]> |
||
| 961 | * @param void |
||
| 962 | * @return integer |
||
| 963 | */ |
||
| 964 | public static function get_number_of_messages_sent() |
||
| 987 | |||
| 988 | /** |
||
| 989 | * display message box in the inbox |
||
| 990 | * @param int the message id |
||
| 991 | * @param string inbox or outbox strings are available |
||
| 992 | * @todo replace numbers with letters in the $row array pff... |
||
| 993 | * @return string html with the message content |
||
| 994 | */ |
||
| 995 | public static function show_message_box($message_id, $source = 'inbox') |
||
| 1121 | |||
| 1122 | /** |
||
| 1123 | * get user id by user email |
||
| 1124 | * @param string $user_email |
||
| 1125 | * @return int user id |
||
| 1126 | */ |
||
| 1127 | public static function get_user_id_by_email($user_email) |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * Displays messages of a group with nested view |
||
| 1143 | * |
||
| 1144 | * @param int $group_id |
||
| 1145 | */ |
||
| 1146 | public static function display_messages_for_group($group_id) |
||
| 1256 | |||
| 1257 | /** |
||
| 1258 | * Displays messages of a group with nested view |
||
| 1259 | * @param $group_id |
||
| 1260 | * @param $topic_id |
||
| 1261 | * @param $is_member |
||
| 1262 | * @param $message_id |
||
| 1263 | * @return string |
||
| 1264 | */ |
||
| 1265 | public static function display_message_for_group($group_id, $topic_id, $is_member, $message_id) |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * Add children to messages by id is used for nested view messages |
||
| 1484 | * @param array $rows rows of messages |
||
| 1485 | * @return array $first_seed new list adding the item children |
||
| 1486 | */ |
||
| 1487 | public static function calculate_children($rows, $first_seed) |
||
| 1501 | |||
| 1502 | /** |
||
| 1503 | * Sort recursively the messages, is used for for nested view messages |
||
| 1504 | * @param array original rows of messages |
||
| 1505 | * @param array list recursive of messages |
||
| 1506 | * @param int seed for calculate the indent |
||
| 1507 | * @param int indent for nested view |
||
| 1508 | * @return void |
||
| 1509 | */ |
||
| 1510 | public static function message_recursive_sort($rows, &$messages, $seed = 0, $indent = 0) |
||
| 1524 | |||
| 1525 | /** |
||
| 1526 | * Sort date by desc from a multi-dimensional array |
||
| 1527 | * @param array $array1 first array to compare |
||
| 1528 | * @param array $array2 second array to compare |
||
| 1529 | * @return bool |
||
| 1530 | */ |
||
| 1531 | public function order_desc_date($array1, $array2) |
||
| 1535 | |||
| 1536 | /** |
||
| 1537 | * Get array of links (download) for message attachment files |
||
| 1538 | * @param int $message_id |
||
| 1539 | * @param string $type message list (inbox/outbox) |
||
| 1540 | * @return array |
||
| 1541 | */ |
||
| 1542 | public static function get_links_message_attachment_files($message_id, $type = '') |
||
| 1570 | |||
| 1571 | /** |
||
| 1572 | * Get message list by id |
||
| 1573 | * @param int $message_id |
||
| 1574 | * @return array |
||
| 1575 | */ |
||
| 1576 | public static function get_message_by_id($message_id) |
||
| 1591 | |||
| 1592 | /** |
||
| 1593 | * @param $id |
||
| 1594 | * @param array $params |
||
| 1595 | * @return string |
||
| 1596 | */ |
||
| 1597 | public static function generate_message_form($id, $params = array()) |
||
| 1605 | |||
| 1606 | /** |
||
| 1607 | * @param $id |
||
| 1608 | * @param array $params |
||
| 1609 | * @return string |
||
| 1610 | */ |
||
| 1611 | public static function generate_invitation_form($id, $params = array()) |
||
| 1617 | |||
| 1618 | //@todo this functions should be in the message class |
||
| 1619 | /** |
||
| 1620 | * @param string $keyword |
||
| 1621 | * @return string |
||
| 1622 | */ |
||
| 1623 | public static function inbox_display($keyword = '') |
||
| 1697 | |||
| 1698 | /** |
||
| 1699 | * @param string $keyword |
||
| 1700 | * @return null|string |
||
| 1701 | */ |
||
| 1702 | public static function outbox_display($keyword = '') |
||
| 1758 | |||
| 1759 | /** |
||
| 1760 | * Get the count of the last received messages for a user |
||
| 1761 | * @param int $userId The user id |
||
| 1762 | * @param int $lastId The id of the last received message |
||
| 1763 | * @return int The count of new messages |
||
| 1764 | */ |
||
| 1765 | public static function countMessagesFromLastReceivedMessage($userId, $lastId = 0) |
||
| 1794 | |||
| 1795 | /** |
||
| 1796 | * Get the data of the last received messages for a user |
||
| 1797 | * @param int $userId The user id |
||
| 1798 | * @param int $lastId The id of the last received message |
||
| 1799 | * @return array |
||
| 1800 | */ |
||
| 1801 | public static function getMessagesFromLastReceivedMessage($userId, $lastId = 0) |
||
| 1834 | |||
| 1835 | /** |
||
| 1836 | * Check whether a message has attachments |
||
| 1837 | * @param int $messageId The message id |
||
| 1838 | * @return boolean Whether the message has attachments return true. Otherwise return false |
||
| 1839 | */ |
||
| 1840 | public static function hasAttachments($messageId) |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * @param string $url |
||
| 1869 | * |
||
| 1870 | * @return FormValidator |
||
| 1871 | */ |
||
| 1872 | public static function getSearchForm($url) |
||
| 1881 | |||
| 1882 | /** |
||
| 1883 | * Send a notification to all admins when a new user is registered |
||
| 1884 | * @param User $user |
||
| 1885 | */ |
||
| 1886 | public static function sendNotificationByRegisteredUser(User $user) |
||
| 1915 | |||
| 1916 | /** |
||
| 1917 | * Get the error log from failed mailing |
||
| 1918 | * This assumes a complex setup where you have a cron script regularly copying the mail queue log |
||
| 1919 | * into app/cache/mail/mailq. |
||
| 1920 | * This can be done with a cron command like (check the location of your mail log file first): |
||
| 1921 | * @example 0,30 * * * * root cp /var/log/exim4/mainlog /var/www/chamilo/app/cache/mail/mailq |
||
| 1922 | * @return array|bool |
||
| 1923 | */ |
||
| 1924 | public static function failedSentMailErrors() |
||
| 1982 | } |
||
| 1983 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: