Enable Multiple User Feeds In SweetCron (Part 2)

In part 1, we have gone through on how to modify the database to enable multiple user in SweetCron. Because the structure of the database has been altered, the next step that we needs to do is to modify the SweetCron model.

Model

Location: sweetcron/system/application/models

Firstly, we need to add a new model which is the user_model. This model will be used to access the users table from the database. Like all other models, this model must also inherits from the Model class. The function that we will write in this model are:

  1. __construct() //class’ constructor
  2. _process($users) //for final processing of the database result
  3. get_all_users() //return all existing user
  4. add_user($user) //add a new user
  5. delete_user($user_name) //delete user

After that, we need to modify the feed_model to add a relationship between feeds table and users table. In order to do that, we must join the query result in the accessor functions get_feeds() and get_active_feeds() with the users table. The modified functions should look like as shown below:

function get_feeds()
{

return $this->_process($this->db

->join(users‘, ‘users.ID = feeds.feed_user_id‘, ‘left outer)

->result());

}

function get_active_feeds($group = FALSE)
{

if ($group) {

return $this->_process($this->db

->join(‘users‘, users.user_id = feeds.feed_user_id‘, ‘left outer‘)
->group_by(‘feed_domain‘)
->get_where(‘feeds‘, array(‘feed_status‘ => ‘active‘))
->result());

} else {

return $this->_process($this->db

->join(‘users‘, ‘users. user_id = feeds.feed_user_id‘, ‘left outer‘)
->get_where(‘feeds‘, array(‘feed_status‘ => ‘active‘))
->result());

}

}

The last model to be modified is the item_model. In this model, we also have to join the query results in all of its accessor functions (get…()) with the users table. Other than that, we must add 3 new attributes in Sweetcron_Item object declared in the _process($items, $return_single = FALSE) function inside the for loop. The attributes are as shown below:

$new_item->feed_user_id = $items[$key]->feed_user_id;
$new_item->feed_user_name = $items[$key]->user_login;
$new_item->feed_user_avatar = $items[$key]->user_avatar;

In order to enable viewers to view the feeds for a specific user, we must also add the 2 functions shown below inside the item_model

function count_items_by_feed_user($feed_user, $public = FALSE){

if ($public) {

$where = array(‘item_status‘ => ‘publish‘, ‘user_login‘ => $feed_user,’feed_status’ => ‘active‘);

} else {

$where = array(‘item_status !=‘ => ‘deleted‘, ‘user_login‘ => $feed_user,’feed_status‘ => ‘active‘);

}

return $this->db->select(‘ID‘)
->join(‘feeds‘, ‘feeds.feed_id = items.item_feed_id‘, ‘left outer‘)
->join(‘users‘, ‘users.ID = feeds.feed_user_id‘, ‘left outer‘)
->get_where(‘items‘, $where)
->num_rows();

}

function get_items_by_feed_user($offset = 0, $limit = 10, $feed_user, $public = FALSE){

if ($public) {

$where = array(‘item_status‘ => ‘publish‘, ‘user_login‘ => $feed_user, ‘feed_status‘ => ‘active‘);

} else {

$where = array(‘item_status !=‘ => ‘deleted‘, ‘user_login‘ => $feed_user, ‘feed_status‘ => ‘active‘);

}
$items = $this->db->limit($limit)
->offset($offset)
->join(‘feeds‘, ‘feeds.feed_id = items.item_feed_id‘, ‘left outer‘)
->join(‘users‘, ‘users.ID = feeds.feed_user_id‘, ‘left outer‘)
->order_by(‘item_date‘, ‘DESC‘)
->get_where(‘items‘, $where)
->result();

return $this->_process($items);

}

You can download the source code for all the added and modified model here.

Lastly, we needs to register the newly added user_model into SweetCron. In order to do that, we must add the code shown below into the file ‘sweetcron/system/application/libraries/MY_Controller.php’

$this->load->model(‘user_model‘);

That’s all for now. Next time I will teach you how to modify the third element, which is the controller. If you want to view the final product of multiple user sweetcron go to http://www.brainstorm.pro/lifestream/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s