Контроллер
Поскольку для добавления/удаления закладок нам понадобиться отправлять запросы при помощи AJAX, создадим контроллер, который к тому же будет отображать закладки пользователя.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CoGear
*
* Content management system based on CodeIgniter
*
* @package CoGear
* @author CodeMotion, Dmitriy Belyaev
* @copyright Copyright © 2009, CodeMotion
* @license http://cogear.ru/license.html
* @link http://cogear.ru
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Favorites controller
*
* @package CoGear
* @subpackage Favorites
* @category Gears controllers
* @author CodeMotion, Dmitriy Belyaev
* @link http://cogear.ru/user_guide/
*/
class Index extends Controller{
/**
* Constructor
*
* @return void
*/
function __construct(){
parent::Controller();
}
// ------------------------------------------------------------------------
/**
* Show user favorite nodes
*
* @param string $url_name
* @param int $page
* @return void
*/
function index($url_name, $page = 0){
// If user exists
if($url_name && $user = $this->user->info($url_name)){
// Show user profile main info
$this->user->head($user,'favorites');
// Check for acl to view unpublished items
if(acl('blogs allow_view_unpublished') OR $user->id == $this->user->get('id')){
$this->nodes->published = FALSE;
}
// Add active record condition
$this->db->where(array('favorites.uid'=>$user->id));
// Show list of nodes with pagination
$this->nodes->get($page,FALSE,TRUE);
}
else return _404();
}
// ------------------------------------------------------------------------
/**
* Add/remove node to user favorites via ajax
*
* @return json
*/
function action(){
// Set i18n department
d('favorites');
// Get POST variables
$nid = $this->input->post('nid');
$action = $this->input->post('action');
// Check for request
if(!$nid OR !$action) ajax(FALSE);
// Get current user id
$uid = $this->user->get('id');
// Switch action
switch($action){
case 'add':
if($this->db->get_where('favorites',array('nid'=>$nid,'uid'=>$uid))->row()){
ajax(FALSE);
}
if($this->db->insert('favorites',array('nid'=>$nid,'uid'=>$uid))){
ajax(TRUE,t('add_success'));
}
break;
case 'remove':
if($this->db->delete('favorites',array('nid'=>$nid,'uid'=>$uid))){
ajax(TRUE,t('remove_success'));
}
break;
}
// If no switch option matches return false
ajax(FALSE);
}
// ------------------------------------------------------------------------
}
// ------------------------------------------------------------------------