· 8 years ago · Jun 09, 2018, 10:28 PM
1## messages.php
2<?php
3
4class Messages extends Controller
5{
6
7 function Messages()
8 {
9
10 parent::Controller();
11 $this->load->helper('date');
12 }
13
14 function index()
15 {
16 $this->inbox();
17 }
18
19 function inbox()
20 {
21 $this->auth_lib->check();
22
23 $data['title'] = "Send a private message";
24
25 $template['content'] = $this->load->view('messages/inbox', $data, true);
26 $this->load->view('layouts/member', $template);
27 }
28
29 function outbox()
30 {
31 $this->auth_lib->check();
32 echo "outbox";
33 }
34
35 function show()
36 {
37 $this->auth_lib->check();
38
39 $message_id = $this->uri->segment(3);
40 //check that the user owns the message
41
42 // show a single message
43 echo "show a single message";
44 }
45
46 function send()
47 {
48 $this->auth_lib->check();
49
50 $this->load->library('form_validation');
51
52 $this->form_validation->set_rules('recipient', 'Recipient', 'required|callback__check_recipient|xss_clean');
53 $this->form_validation->set_rules('title', 'Title', 'required|xss_clean');
54 $this->form_validation->set_rules('message', 'Message', 'required|xss_clean');
55
56 $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
57
58
59 if ($this->form_validation->run() == FALSE)
60 {
61 $data['title'] = "Send a private message";
62
63 $template['content'] = $this->load->view('messages/send', $data, true);
64 $this->load->view('layouts/member', $template);
65 }
66 else
67 {
68
69 // get the recipient id
70 $recipient = $this->input->post('recipient');
71
72
73 $info = $this->auth_lib->get_id_email($recipient);
74
75 if(!$info)
76 {
77 $error['message'] = "The user did not exist";
78 $this->load->view('global/error', $error);
79 }
80
81 $recipient_id = $info->id;
82
83 // insert the message into the messages table
84 $message = array(
85 'title' => $this->input->post('message'),
86 'message' => $this->input->post('message'),
87 'sent' => mdate("%Y-%m-%d %h:%i:%s", time()),
88 'sender' => $this->session->userdata('user_id'),
89 'recipient' => $recipient_id
90 );
91
92 $this->db->insert('messages', $message);
93
94 redirect('messages');
95 }
96 }
97
98 function _check_recipient($recipient)
99 {
100 // test if user exists
101 $this->db->select('id');
102 $this->db->from('users');
103 $this->db->where('username', $recipient);
104
105 $query = $this->db->get();
106
107 if($query->num_rows() == 0)
108 {
109 $this->form_validation->set_message('_check_recipient', 'The recipient does not exist');
110 return FALSE;
111 }
112 else
113 {
114 return TRUE;
115 }
116 }
117}
118
119?>
120
121## sql [sql]
122
123CREATE TABLE `messages` (
124 `id` int(11) unsigned NOT NULL auto_increment,
125 `title` varchar(64) default NULL,
126 `message` text,
127 `sent` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
128 `sender` int(11) default NULL,
129 `recipient` int(11) default NULL,
130 `read` int(2) default '0',
131 PRIMARY KEY (`id`)
132) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
133
134## The idea was to have just a controller and model as no real need for a library. Model would have 1 main function for getting messages that accepts params such as inbox, outbox or all (if left blank). There needs to be another function for getting the "owner" of a message. To make sure the logged in user is the one the message is for.