CodeIgniter Forums
Require Urgent Help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Require Urgent Help (/showthread.php?tid=29919)



Require Urgent Help - El Forum - 04-26-2010

[eluser]greedyh4mster[/eluser]
Hi!

I am trying to create a notification object that serves to remind my users about certain messages. I am not sure whether am I heading towards the correct direction or not. Would seriously appreciate some help from you guys.

However I have encountered several problems along the way, and really hope that you guys could help me out with it. Assuming that my coding design is correct, the major problem that I have encountered is:

Code:
Fatal error: Call to undefined method stdClass::make_status_links()

What I am trying to achieve is to create a notification class, and a collection of notifications class. So when it is time to grab all the notifications for a specific user, I am able to call the notifications class, collection of notification object, and manipulate the data.

Below are the codes:

This is a test function from the user controller:

Code:
function test(){
      $this->load->library('notifications');

      $notifications = $this->notifications->get_notifications_active(1);
   }

This is the controller class for the notification object:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notification {

   private $ci;
   private $error = '';
  
   function __construct(){
      $this->ci =& get_instance();
      $this->ci->load->model('notification_model');
   }
  
   function add($user_id = '', $message_index = '', $action_path = ''){
      $this->ci->notification_model->user_id = $user_id;
      $this->ci->notification_model->message_index = $message_index;
      $this->ci->notification_model->action_path = $action_path;
      
      return $this->ci->notification_model->add();
   }

   function dismiss($notification_id = ''){
       $this->ci->notification_model->notification_id = $notification_id;

       if($this->ci->notification_model->dismiss()){
          return TRUE;
       } else{
          $this->error = 'notification_id_invalid';
          return FALSE;
       }
   }

   function remindmelater($notification_id = ''){
      $this->ci->notification_model->notification_id = $notification_id;

      if($this->ci->notification_model->remindmelater()){
          return TRUE;
       } else{
          $this->error = 'notification_id_invalid';
          return FALSE;
       }
   }

   function make_links($notification_id = ''){
      $this->ci->notification_model->notification_id = $notification_id;
      
      var_dump($this->ci->notification_model->make_status_links());
      
      return $this->ci->notification_model->make_status_links();
   }

   function error(){
      return $this->ci->lang->line($this->error);
   }
  
}

?>

This is the model class for the notification object:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notification_model extends Model {

   public $notification_id;
   public $user_id;
   public $message_index;
   public $action_path;
   public $status;
   public $time;

   private $links = array();

   function __construct(){
      parent::Model();
      $this->load->config('fr3b_config');
   }

   function add(){
      $this->status = $this->config->item('notification_status_active');
      $this->time = date("Y-m-d H:i:s", time());
      
      $this->db->insert('notifications', $this);
      
      return $this->db->insert_id();
   }

   function dismiss(){
      $this->status = $this->config->item('notification_status_dismiss');
      
      return $this->_set_status();
   }

   function remindmelater(){
      $this->status = $this->config->item('notification_status_remindmelater');

      return $this->_set_status();
   }

   function _set_status(){
      $this->db->update(
         'notifications',
         array('status' => $this->status),
         array('notification_id' => $this->notification_id)
      );

      return $this->db->affected_rows();
   }

   function make_status_links(){
      $this->links['dismiss'] = anchor('account/notifications/'.$this->config->item('notification_status_dismiss').'/'.$this->notification_id, 'Dismiss');
      $this->links['remindmelater'] = anchor('account/notifications/'.$this->config->item('notification_status_remindmelater').'/'.$this->notification_id, 'Remind me later');
  
      return $this->links;
   }
  
}
?>

This is the class for a collection of notification objects:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Notifications extends Notification {

   private $ci;

   private $notifications = array();

   function __construct(){
      $this->ci =& get_instance();
   }

   function get_notifications(){

      //Get all notifications regardless of status

   }

   function get_notifications_active($user_id = ''){
      $this->ci->load->config('fr3b_config');
      
      //Get all notifications that has status of 'active'
      $this->ci->db->where('status', $this->ci->config->item('notification_status_active'));
      $this->ci->db->where('user_id', $user_id);
      $this->notifications = $this->ci->db->get('notifications')->result();

      foreach($this->notifications as $notification){
         $notification->links = $this->make_links($notification->notification_id);
      }

      return $this->notifications;
   }

}

?>



Require Urgent Help - El Forum - 04-27-2010

[eluser]frist44[/eluser]
in the construct function of the Notifications class, you need to load the parent construct because that's where the model is loaded.

Code:
function __construct(){
      parent::__construct();
      $this->ci =& get_instance();
   }



Require Urgent Help - El Forum - 04-27-2010

[eluser]frist44[/eluser]
Also, it's usually best practice to do all your data access to the database in models. So this function would ideally be moved over to the model: get_notifications_active()