$us) { if ($key == 'id_user') { $group_user[$us] = $us; } } } foreach ($group_user as $user) { $return = messages_create_message($usuario_origen, get_user_id($user), $subject, $mensaje); if ($return === false) { // Error sending message return false; } } return true; } /** * Deletes a private message * * @param integer $id_message * * @return boolean true when deleted, false in case of error */ function messages_delete_message($id_message) { global $config; $where = [ // 'id_usuario_destino' => $config["id_user"], 'id_mensaje' => $id_message, ]; return (bool) db_process_sql_delete('tmensajes', $where); } /** * Marks a private message as read/unread * * @param integer $message_id The message to modify * @param boolean $read To set unread pass 0, false or empty value * * @return boolean true when marked, false in case of error */ function messages_process_read($message_id, $read=true) { if (empty($read)) { $read = 0; } else { $read = 1; } return (bool) db_process_sql_update('tmensajes', ['estado' => $read], ['id_mensaje' => $message_id]); } /** * Gets a private message * * This function abstracts the database backend so it can simply be replaced with another system * * @param integer $message_id * * @return mixed False if it doesn't exist or a filled array otherwise */ function messages_get_message($message_id) { global $config; $sql = sprintf( "SELECT id_usuario_origen, id_usuario_destino, subject, mensaje, timestamp FROM tmensajes WHERE id_usuario_destino='%s' AND id_mensaje=%d", $config['id_user'], $message_id ); $row = db_get_row_sql($sql); if (empty($row)) { return false; } return $row; } /** * Gets a sent message * * This function abstracts the database backend so it can simply be replaced with another system * * @param integer $message_id * * @return mixed False if it doesn't exist or a filled array otherwise */ function messages_get_message_sent($message_id) { global $config; $sql = sprintf( "SELECT id_usuario_origen, id_usuario_destino, subject, mensaje, timestamp FROM tmensajes WHERE id_usuario_origen='%s' AND id_mensaje=%d", $config['id_user'], $message_id ); $row = db_get_row_sql($sql); if (empty($row)) { return false; } return $row; } /** * Counts private messages * * @param string $user * @param boolean $incl_read Whether or not to include read messages * * @return integer The number of messages this user has */ function messages_get_count($user=false, $incl_read=false) { if (empty($user)) { global $config; $user = $config['id_user']; } if (empty($incl_read)) { $filter = 'AND estado = 0'; } else { $filter = ''; } $sql = sprintf( "SELECT COUNT(*) FROM tmensajes WHERE id_usuario_destino='%s' %s", $user, $filter ); return (int) db_get_sql($sql); } /** * Counts sended messages * * @param string $user * * @return integer The number of messages this user has sent */ function messages_get_count_sent($user=false) { if (empty($user)) { global $config; $user = $config['id_user']; } $sql = sprintf( "SELECT COUNT(*) FROM tmensajes WHERE id_usuario_origen='%s'", $user ); return (int) db_get_sql($sql); } /** * Get message overview in array * * @param string $order How to order them valid: * (status (default), subject, timestamp, sender) * @param string $order_dir Direction of order (ASC = Ascending, DESC = Descending) * * @return integer The number of messages this user has */ function messages_get_overview($order='status', $order_dir='ASC') { global $config; switch ($order) { case 'timestamp': case 'sender': case 'subject': break; case 'status': default: $order = 'estado, timestamp'; break; } if ($order_dir != 'ASC') { $order .= ' DESC'; } $result = []; $return = db_get_all_rows_field_filter('tmensajes', 'id_usuario_destino', $config['id_user'], $order); if ($return === false) { return $result; } foreach ($return as $message) { $result[$message['id_mensaje']]['sender'] = $message['id_usuario_origen']; $result[$message['id_mensaje']]['subject'] = $message['subject']; $result[$message['id_mensaje']]['timestamp'] = $message['timestamp']; $result[$message['id_mensaje']]['status'] = $message['estado']; } return $result; } /** * Get sent message overview in array * * @param string $order How to order them valid: * (status (default), subject, timestamp, sender) * @param string $order_dir Direction of order (ASC = Ascending, DESC = Descending) * * @return integer The number of messages this user has */ function messages_get_overview_sent($order='timestamp', $order_dir='ASC') { global $config; switch ($order) { case 'timestamp': case 'sender': case 'subject': break; case 'status': default: $order = 'estado, timestamp'; break; } if ($order_dir != 'ASC') { $order .= ' DESC'; } $result = []; $return = db_get_all_rows_field_filter('tmensajes', 'id_usuario_origen', $config['id_user'], $order); if ($return === false) { return $result; } foreach ($return as $message) { $result[$message['id_mensaje']]['dest'] = $message['id_usuario_destino']; $result[$message['id_mensaje']]['subject'] = $message['subject']; $result[$message['id_mensaje']]['timestamp'] = $message['timestamp']; $result[$message['id_mensaje']]['status'] = $message['estado']; } return $result; }