/**
  * get all unread messages forthe loged in user
  * @return [type] [description]
  */
 public function action_unread()
 {
     $messages = Model_Message::get_unread_threads($this->user->id_user);
     //filter results by param, verify field exists and has a value
     $messages->api_filter($this->_filter_params);
     //how many? used in header X-Total-Count
     $count = $messages->count_all();
     //by default sort by status not read and when was created
     if (empty($this->_sort)) {
         $this->_sort['created'] = 'desc';
     }
     //after counting sort values
     $messages->api_sort($this->_sort);
     //pagination with headers
     $pagination = $messages->api_pagination($count, $this->_params['items_per_page']);
     $messages = $messages->cached()->find_all();
     $m = array();
     //convert it to array
     foreach ($messages as $message) {
         $m[] = $message->as_array();
     }
     $this->rest_output(array('messages' => $m), 200, $count, $pagination !== FALSE ? $pagination : NULL);
 }
<?php

defined('SYSPATH') or die('No direct script access.');
?>

<?php 
if (Auth::instance()->logged_in()) {
    ?>
    <?php 
    if (core::config('general.messaging') and $messages = Model_Message::get_unread_threads(Auth::instance()->get_user())) {
        ?>
        <div class="btn-group" role="group">
            <?php 
        if ($messages_count = $messages->count_all() > 0) {
            ?>
                <a class="btn btn-success"
                    href="<?php 
            echo Route::url('oc-panel', array('controller' => 'messages', 'action' => 'index'));
            ?>
"
                    data-toggle="dropdown"
                    data-target="#"
                >
                    <i class="fa fa-bell"></i> <span class="badge"><?php 
            echo $messages_count;
            ?>
</span>
                </a>
                <ul class="dropdown-menu">
                    <li class="dropdown-header"><?php 
            echo sprintf(__('You have %s unread messages'), $messages_count);