Skip to content

Instantly share code, notes, and snippets.

@rsmarshall
Created May 15, 2012 13:43
Show Gist options
  • Save rsmarshall/2701880 to your computer and use it in GitHub Desktop.
Save rsmarshall/2701880 to your computer and use it in GitHub Desktop.
content.php
<?php
/**
* Content controller for supplier management
* @package Suppliers
* @author Ryan Marshall <ryan@irealms.co.uk>
*/
class Content extends Admin_Controller {
public function __construct()
{
parent::__construct();
$this->auth->restrict('Suppliers.Content.View');
Template::set('toolbar_title', 'Supplier Management');
Template::set_block('sub_nav', 'content/sub_nav');
$this->load->model('suppliers_model');
}
/**
* Show supplier management page
*/
public function index()
{
if ($action = $this->input->post('submit'))
{
if ($action == 'Delete')
{
$this->auth->restrict('Suppliers.Content.Delete');
$checked = $this->input->post('checked');
if (is_array($checked) && count($checked))
{
$result = FALSE;
foreach ($checked as $id)
{
$result = $this->suppliers_model->delete($id);
}
if ($result)
{
Template::set_message(count($checked) .' Supplier(s) deleted.', 'success');
}
else
{
Template::set_message('Delete failed', 'error');
}
}
else
{
Template::set_message('Delete failed', 'error');
}
}
}
// Pagination
$total = $this->suppliers_model->count_all();
$offset = $this->input->get('per_page');
$this->pager['base_url'] = current_url().'?';
$this->pager['total_rows'] = $total;
$this->pager['per_page'] = $this->limit;
$this->pager['page_query_string'] = true;
$this->load->library('pagination', $this->pager);
Template::set('suppliers', $this->suppliers_model->limit($this->limit, $offset)->find_all());
Template::render();
}
/**
* Create a supplier
*/
public function create()
{
$this->auth->restrict('Suppliers.Content.Create');
if ($this->input->post('submit'))
{
if ($this->save_supplier())
{
Template::set_message('Supplier created', 'success');
Template::redirect(SITE_AREA .'/content/suppliers');
}
}
Template::render();
}
/**
* Edit supplier
* @param integer $id supplier id to edit
*/
public function edit($id = NULL)
{
$this->auth->restrict('Suppliers.Content.Edit');
if ($this->input->post('submit'))
{
if ($this->save_supplier($id))
{
Template::set_message('Supplier updated', 'success');
Template::redirect(SITE_AREA .'/content/suppliers');
}
}
Template::set('post', $this->suppliers_model->find($id));
Template::set('toolbar_title', 'Edit Key');
Template::set_view('/content/create');
Template::render();
}
/**
* Save supplier to database
* @param integer $id
* @return boolean
*/
public function save_supplier($id = NULL)
{
if ($this->form_validation->run($this, 'create_supplier') === FALSE)
{
return FALSE;
}
return $this->suppliers_model->save_supplier($id);
}
/**
* Activate supplier
* @param integer $id
*/
public function activate($id = NULL)
{
if ($id)
{
$data = array(
'active' => 'Y'
);
$this->suppliers_model->update($id, $data);
Template::set_message('Supplier activated.', 'success');
Template::redirect(SITE_AREA .'/content/suppliers');
}
}
/**
* Deactivate supplier
* @param integer $id
*/
public function deactivate($id = NULL)
{
if ($id)
{
$data = array(
'active' => 'N'
);
$this->suppliers_model->update($id, $data);
Template::set_message('Supplier deactivated.', 'error');
Template::redirect(SITE_AREA .'/content/suppliers');
}
}
public function search()
{
$this->suppliers_model->set_table(FALSE);
// Pagination
$offset = $this->input->get('per_page');
$this->pager['base_url'] = current_url().'?search_feeds='. $this->input->get_post('search_feeds') .'&';
$this->pager['total_rows'] = $this->suppliers_model->count_all();
$this->pager['per_page'] = 1;
$this->pager['page_query_string'] = true;
$this->load->library('pagination', $this->pager);
$results = $this->suppliers_model
->limit($this->pager['per_page'], $offset)
->find_all_by(array ('supplier_code LIKE ' => '%'.$this->input->get_post('search_feeds').'%'));
$this->suppliers_model->set_table();
Template::set('results', $results);
Template::set('search_string', $this->input->get_post('search_feeds'));
Template::set('toolbar_title', 'Search Feeds');
Template::set_view('/content/search');
Template::render();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment