Skip to content

Commit

Permalink
Converted Mahana model to use entirely Query builder
Browse files Browse the repository at this point in the history
And also made a little improvement to the message overview page view.
  • Loading branch information
StorytellerCZ committed Jan 22, 2014
1 parent b716176 commit e184bc1
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 48 deletions.
134 changes: 88 additions & 46 deletions application/models/pm/Mahana_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ function reply_to_message($reply_msg_id, $sender_id, $body, $priority)
*/
function get_message($msg_id, $user_id)
{
$this->db->select($this->db->dbprefix . 'msg_messages.*, msg_status.status, msg_threads.subject, a3m_account.username');
$this->db->join($this->db->dbprefix . 'msg_threads', 'msg_messages.thread_id = msg_threads.id');
$this->db->join($this->db->dbprefix . 'a3m_account', 'a3m_account.id = msg_messages.sender_id');
$this->db->join($this->db->dbprefix . 'msg_status', 'msg_status.message_id = msg_messages.id');
return $this->db->get_where('msg_messages', array('msg_messages.id' => $msg_id, 'msg_status.user_id' => $user_id))->result_array();
$this->db->select($this->db->dbprefix . 'msg_messages.*, '.$this->db->dbprefix . 'msg_status.status, '.$this->db->dbprefix . 'msg_threads.subject, '.$this->db->dbprefix . 'a3m_account.username');
$this->db->join($this->db->dbprefix . 'msg_threads', $this->db->dbprefix . 'msg_messages.thread_id = '.$this->db->dbprefix . 'msg_threads.id');
$this->db->join($this->db->dbprefix . 'a3m_account', $this->db->dbprefix . 'a3m_account.id = '.$this->db->dbprefix . 'msg_messages.sender_id');
$this->db->join($this->db->dbprefix . 'msg_status', $this->db->dbprefix . 'msg_status.message_id = '.$this->db->dbprefix . 'msg_messages.id');
return $this->db->get_where($this->db->dbprefix . 'msg_messages', array('msg_messages.id' => $msg_id, 'msg_status.user_id' => $user_id))->result_array();
}

// ------------------------------------------------------------------------
Expand All @@ -127,25 +127,49 @@ function get_message($msg_id, $user_id)
* @param string $order_by
* @return array
*/
function get_full_thread($thread_id, $user_id, $full_thread = FALSE, $order_by = 'asc')
function get_full_thread($thread_id, $user_id, $full_thread = FALSE, $order_by = 'ASC')
{
$sql = 'SELECT m.*, s.status, t.subject, a3m_account.username' .
/*$sql = 'SELECT m.*, s.status, t.subject, a3m_account.username' .
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
' JOIN ' . $this->db->dbprefix . 'a3m_account ON (a3m_account.id = m.sender_id) '.
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
' WHERE p.user_id = ? ' .
' AND p.thread_id = ? ';
*/
$this->db->select($this->db->dbprefix . 'msg_messages.*, ' .
$this->db->dbprefix . 'msg_status.status, ' .
$this->db->dbprefix . 'msg_threads.subject, ' .
$this->db->dbprefix . 'a3m_account.username');

$this->db->join($this->db->dbprefix . 'msg_threads',
$this->db->dbprefix . 'msg_threads.id = ' . $this->db->dbprefix . 'msg_participants.thread_id');

$this->db->join($this->db->dbprefix . 'msg_messages',
$this->db->dbprefix . 'msg_messages.thread_id = ' . $this->db->dbprefix . 'msg_threads.id');

$this->db->join($this->db->dbprefix . 'a3m_account',
$this->db->dbprefix . 'a3m_account.id = ' . $this->db->dbprefix . 'msg_messages.sender_id');

$this->db->join($this->db->dbprefix . 'msg_status',
$this->db->dbprefix . 'msg_status.message_id = ' . $this->db->dbprefix . 'msg_messages.id AND ' .
$this->db->dbprefix.'msg_status.user_id = '. $user_id);

$this->db->where($this->db->dbprefix . 'msg_participants.user_id', $user_id);
$this->db->where($this->db->dbprefix . 'msg_participants.thread_id', $thread_id);

if ( ! $full_thread)
{
$sql .= ' AND m.cdate >= p.cdate';
//$sql .= ' AND m.cdate >= p.cdate';
$this->db->where($this->db->dbprefix . 'msg_messages.cdate >=', $this->db->dbprefix . 'msg_participants.cdate', FALSE);
}

$sql .= ' ORDER BY m.cdate ' . $order_by;
//$sql .= ' ORDER BY m.cdate ' . $order_by;
$this->db->order_by($this->db->dbprefix . 'msg_messages.cdate', $order_by);

$query = $this->db->query($sql, array($user_id, $user_id, $thread_id));
//$query = $this->db->query($sql, array($user_id, $user_id, $thread_id));
$query = $this->db->get($this->db->dbprefix . 'msg_participants');

return $query->result_array();
}
Expand All @@ -160,24 +184,49 @@ function get_full_thread($thread_id, $user_id, $full_thread = FALSE, $order_by =
* @param string $order_by
* @return array
*/
function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'asc')
function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'ASC')
{
$sql = 'SELECT m.*, s.status, t.subject, a.username' .
/*$sql = 'SELECT m.*, s.status, t.subject, a.username' .
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
' JOIN ' . $this->db->dbprefix . 'msg_threads t ON (t.id = p.thread_id) ' .
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.thread_id = t.id) ' .
' JOIN ' . $this->db->dbprefix . 'a3m_account a ON (a.id = m.sender_id) '.
' JOIN ' . $this->db->dbprefix . 'msg_status s ON (s.message_id = m.id AND s.user_id = ? ) ' .
' WHERE p.user_id = ? ' ;
*/
$this->db->select($this->db->dbprefix . 'msg_messages.*, ' .
$this->db->dbprefix . 'msg_status.status, ' .
$this->db->dbprefix . 'msg_threads.subject, ' .
$this->db->dbprefix . 'a3m_account.username', FALSE);

$this->db->join($this->db->dbprefix . 'msg_threads',
$this->db->dbprefix . 'msg_threads.id = ' . $this->db->dbprefix . 'msg_participants.thread_id');

$this->db->join($this->db->dbprefix . 'msg_messages',
$this->db->dbprefix . 'msg_messages.thread_id = ' . $this->db->dbprefix . 'msg_threads.id');

$this->db->join($this->db->dbprefix . 'a3m_account',
$this->db->dbprefix . 'a3m_account.id = ' . $this->db->dbprefix . 'msg_messages.sender_id');

$this->db->join($this->db->dbprefix . 'msg_status',
$this->db->dbprefix . 'msg_status.message_id = ' .
$this->db->dbprefix . 'msg_messages.id AND ' .
$this->db->dbprefix.'msg_status.user_id = '. $user_id);

$this->db->where($this->db->dbprefix . 'msg_participants.user_id', $user_id);

if (!$full_thread)
{
$sql .= ' AND m.cdate >= p.cdate';
//$sql .= ' AND m.cdate >= p.cdate';
$this->db->where($this->db->dbprefix . 'msg_messages.cdate >=', $this->db->dbprefix . 'msg_participants.cdate', FALSE);
}

$sql .= ' ORDER BY t.id ' . $order_by. ', m.cdate '. $order_by;
//$sql .= ' ORDER BY t.id ' . $order_by. ', m.cdate '. $order_by;
$this->db->order_by($this->db->dbprefix . 'msg_threads.id', $order_by);
$this->db->order_by($this->db->dbprefix . 'msg_messages.cdate', $order_by);

$query = $this->db->query($sql, array($user_id, $user_id));
//$query = $this->db->query($sql, array($user_id, $user_id));
$query = $this->db->get($this->db->dbprefix . 'msg_participants');

return $query->result_array();
}
Expand All @@ -195,7 +244,7 @@ function get_all_threads($user_id, $full_thread = FALSE, $order_by = 'asc')
function update_message_status($msg_id, $user_id, $status_id)
{
$this->db->where(array('message_id' => $msg_id, 'user_id' => $user_id ));
$this->db->update('msg_status', array('status' => $status_id ));
$this->db->update($this->db->dbprefix . 'msg_status', array('status' => $status_id ));

return $this->db->affected_rows();
}
Expand Down Expand Up @@ -277,14 +326,11 @@ function remove_participant($thread_id, $user_id)
*/
function valid_new_participant($thread_id, $user_id)
{
$sql = 'SELECT COUNT(*) AS count ' .
' FROM ' . $this->db->dbprefix . 'msg_participants p ' .
' WHERE p.thread_id = ? ' .
' AND p.user_id = ? ';

$query = $this->db->query($sql, array($thread_id, $user_id));
$this->db->from($this->db->dbprefix . 'msg_participants');
$this->db->where('thread_id', $thread_id);
$this->db->where('user_id', $user_id);

if ($query->row()->count)
if($this->db->count_all_results())
{
return FALSE;
}
Expand All @@ -302,13 +348,10 @@ function valid_new_participant($thread_id, $user_id)
*/
function application_user($user_id)
{
$sql = 'SELECT COUNT(*) AS count ' .
' FROM ' . $this->db->dbprefix . 'a3m_account a' .
' WHERE a.id = ?' ;
$this->db->from($this->db->dbprefix . 'a3m_account');
$this->db->where('id', $user_id);

$query = $this->db->query($sql, array($user_id));

if ($query->row()->count)
if($this->db->count_all_results())
{
return TRUE;
}
Expand Down Expand Up @@ -345,7 +388,7 @@ function get_participant_list($thread_id, $sender_id = 0)
*/
function get_msg_count($user_id, $status_id = MSG_STATUS_UNREAD)
{
$query = $this->db->select('COUNT(*) AS msg_count')->where(array('user_id' => $user_id, 'status' => $status_id ))->get('msg_status');
$query = $this->db->select('COUNT(*) AS msg_count')->where(array('user_id' => $user_id, 'status' => $status_id ))->get($this->db->dbprefix . 'msg_status');

return $query->row()->msg_count;
}
Expand All @@ -362,7 +405,7 @@ function get_msg_count($user_id, $status_id = MSG_STATUS_UNREAD)
*/
private function _insert_thread($subject)
{
$insert_id = $this->db->insert('msg_threads', array('subject' => $subject));
$insert_id = $this->db->insert($this->db->dbprefix . 'msg_threads', array('subject' => $subject));

return $this->db->insert_id();
}
Expand All @@ -383,7 +426,7 @@ private function _insert_message($thread_id, $sender_id, $body, $priority)
$insert['body'] = $body;
$insert['priority'] = $priority;

$insert_id = $this->db->insert('msg_messages', $insert);
$insert_id = $this->db->insert($this->db->dbprefix . 'msg_messages', $insert);

return $this->db->insert_id();
}
Expand All @@ -396,7 +439,7 @@ private function _insert_message($thread_id, $sender_id, $body, $priority)
*/
private function _insert_participants($participants)
{
return $this->db->insert_batch('msg_participants', $participants);
return $this->db->insert_batch($this->db->dbprefix . 'msg_participants', $participants);
}

/**
Expand All @@ -407,7 +450,7 @@ private function _insert_participants($participants)
*/
private function _insert_statuses($statuses)
{
return $this->db->insert_batch('msg_status', $statuses);
return $this->db->insert_batch($this->db->dbprefix . 'msg_status', $statuses);
}

/**
Expand All @@ -418,7 +461,7 @@ private function _insert_statuses($statuses)
*/
private function _get_thread_id_from_message($msg_id)
{
$query = $this->db->select('thread_id')->get_where('msg_messages', array('id' => $msg_id));
$query = $this->db->select('thread_id')->get_where($this->db->dbprefix . 'msg_messages', array('id' => $msg_id));

if ($query->num_rows())
{
Expand All @@ -435,7 +478,7 @@ private function _get_thread_id_from_message($msg_id)
*/
private function _get_messages_by_thread_id($thread_id)
{
$query = $this->db->get_where('msg_messages', array('thread_id' => $thread_id));
$query = $this->db->get_where($this->db->dbprefix . 'msg_messages', array('thread_id' => $thread_id));

return $query->result_array();
}
Expand All @@ -457,10 +500,12 @@ private function _get_thread_participants($thread_id, $sender_id = 0)
$array['msg_participants.user_id != '] = $sender_id;
}

$this->db->select('msg_participants.user_id, a3m_account.username', FALSE);
$this->db->join('a3m_account', 'msg_participants.user_id = a3m_account.id');
$this->db->select($this->db->dbprefix . 'msg_participants.user_id, ' .
$this->db->dbprefix . 'a3m_account.username', FALSE);
$this->db->join($this->db->dbprefix . 'a3m_account',
$this->db->dbprefix . 'msg_participants.user_id = '.$this->db->dbprefix . 'a3m_account.id');

$query = $this->db->get_where('msg_participants', $array);
$query = $this->db->get_where($this->db->dbprefix . 'msg_participants', $array);

return $query->result_array();
}
Expand All @@ -474,7 +519,7 @@ private function _get_thread_participants($thread_id, $sender_id = 0)
*/
private function _delete_participant($thread_id, $user_id)
{
$this->db->delete('msg_participants', array('thread_id' => $thread_id, 'user_id' => $user_id));
$this->db->delete($this->db->dbprefix . 'msg_participants', array('thread_id' => $thread_id, 'user_id' => $user_id));

if ($this->db->affected_rows() > 0)
{
Expand All @@ -492,12 +537,9 @@ private function _delete_participant($thread_id, $user_id)
*/
private function _delete_statuses($thread_id, $user_id)
{
$sql = 'DELETE s FROM msg_status s ' .
' JOIN ' . $this->db->dbprefix . 'msg_messages m ON (m.id = s.message_id) ' .
' WHERE m.thread_id = ? ' .
' AND s.user_id = ? ';

$query = $this->db->query($sql, array($thread_id, $user_id));
$this->db->join($this->db->dbprefix . 'msg_messages',
$this->db->dbprefix . 'msg_messages.id = '.$this->db->dbprefix . 'msg_status.message_id');
$this->db->delete($this->db->dbprefix . 'msg_status', array($this->db->dbprefix .'msg_messages.thread_id' => $thread_id, $this->db->dbprefix . 'msg_status.user_id' => $user_id));

return TRUE;
}
Expand Down
4 changes: 2 additions & 2 deletions application/views/pm/overview.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<div class="col-lg-12">
<div class="page-header">
<h1><?php echo lang('pm_welcome'); ?>
<?php echo anchor('#msg_new', lang('pm_new'), array('class' => 'btn btn-success pull-right', 'data-toggle' => 'modal', 'data-target' => '#new-msg')) ?></h1>
<?php echo anchor('#msg_new', '<span class="glyphicon glyphicon-plus"></span> ' . lang('pm_new'), array('class' => 'btn btn-success pull-right', 'data-toggle' => 'modal', 'data-target' => '#new-msg')) ?></h1>
</div>
<?php
if(isset($response)) print_r($response);
if(empty($threads))
{
echo lang('pm_no_msg');
echo '<div class="alert alert-warning text-center">' . lang('pm_no_msg') . '</div>';
}
else
{
Expand Down

0 comments on commit e184bc1

Please sign in to comment.