⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

callback_after_ read



semsik

semsik
  • profile picture
  • Member

Posted 19 October 2016 - 11:47 AM

Wel I'm kinda new to Grocery Crud, and I just wanted to share a quick and dirty solution to a small problem I faced in building my app.

Disclaimer: the used fields and table names are fictional and are intended to demonstrate the solution.

 

I have a crud for messages to users  that are logged in (field "user_id_destination"=$user_logged_in)

that aren't read yet (field "read"=0).

 

These messages are generated with callback_after_insert functions in the other cruds.and can be everything from 

'user Jane Doe added a product with product code AA11123 to the products list'

to

'new user John Doe added to the system user list'

 

The crud is set up with only the read option available to the user (unset_delete();uset_edit();unset_add(); and a bunch of other functions :D )

 

Well I needed a way to set the "read" field in database to 1 (true) when the message was displayed on the screen by the read option.

In the API list there was no callback_after_read function so i used a quick and dirty (probably not the best) solution.

 

In my messages controller in the __construct() function I added:

public function __construct()
{
    parent::__construct();
    //make sure the code only gets executed when a message is 'read'!!!
    if($this->uri->segment(2)=='message' AND $this->uri->segment(3)=='read'){
        $data=array(
            'read'=>1,
            'date_read'=>date('Y-m-d'),
         );
    $this->db->where('id', $this->uri->segment(4));
    $this->db->update('notificaties', $data);	
    }
}

I know the codeigniter way would be to set up a function in a model to do this, but you get the idea.

 

If there is a better way of doing this or if there are security risks with this way of doing things I'd love to read your suggestions.

 

regards