Skip to content

Instantly share code, notes, and snippets.

@rsmarshall
Created May 17, 2012 16:03
Show Gist options
  • Save rsmarshall/2719819 to your computer and use it in GitHub Desktop.
Save rsmarshall/2719819 to your computer and use it in GitHub Desktop.
supplier model
<?php
/**
* Supplier management library
* @package Suppliers
* @author Ryan Marshall <ryan@irealms.co.uk>
*/
class Suppliers_model extends BF_Model {
protected $table = 'supplier_details';
protected $key = 'id';
protected $details_table = 'supplier_details';
protected $details_key = 'id';
protected $pricing_table = 'supplier_pricing';
protected $pricing_key = 'id';
protected $stock_map_table = 'supplier_stock_mapping';
protected $stock_map_key = 'supplier_id';
protected $full_map_table = 'supplier_full_mapping';
protected $full_map_key = 'supplier_id';
protected $soft_deletes = false;
protected $date_format = 'int';
protected $set_created = false;
protected $set_modified = false;
public function __construct() {
// Load CodeIgniter's Array Helper
if (!function_exists('elements'))
{
$this->load->helper('array');
}
}
/**
* Save supplier
* @param integer $id Supplier id
* @return boolean
*/
public function save_supplier($id = NULL)
{
$allowed = array(
'sup_account',
'sup_name',
'ftp_host',
'ftp_port',
'ftp_user',
'ftp_pass',
'stock_ftp_directory',
'stock_comp_file',
'stock_uncomp_file',
'stock_feed_separator',
'full_ftp_directory',
'full_comp_file',
'full_uncomp_file',
'full_separator',
'date_format'
);
$data = elements($allowed, $this->input->post());
if ($id)
{
return $this->update($id, $data);
}
else
{
$insert_id = $this->insert($data);
// Blank supplier linked row in mapping tables
// As seach supplier has to have a mapping to work, a blank ID linked row is inserted here
// This saves having create/edit methods in controller
$mapping = array(
'supplier_id' => $insert_id
);
$this->set_table('stock');
$this->insert($mapping);
$this->set_table('full');
$this->insert($mapping);
$this->set_table();
return $insert_id;
}
}
/**
* Set table method for multiple tables in model
* @link http://blog.s-vizion.com/2012/codeigniter/a-quick-tip-on-codeigniter-and-bonfires-model-for-evil/
* @param boolean $table Set primary or alt table
* @return \Suppliers_model
*/
public function set_table ($table = 'details')
{
switch ($table) {
case 'details':
$this->table = $this->details_table;
$this->key = $this->details_key;
break;
case 'pricing':
$this->table = $this->pricing_table;
$this->key = $this->pricing_key;
break;
case 'stock':
$this->table = $this->stock_map_table;
$this->key = $this->stock_map_key;
break;
case 'full':
$this->table = $this->full_map_table;
$this->key = $this->full_map_key;
break;
default:
$this->table = $this->primary_table;
$this->key = $this->primary_key;
break;
}
return $this;
}
/**
* Search supplier feeds
* @param integer $limit
* @param integer $offset
* @return array returns an array with the search data and a total count
*/
public function search($limit = NULL, $offset = NULL)
{
$this->suppliers_model->set_table(FALSE);
$search_string = $this->input->get_post('search_string');
$where = "supplier_code LIKE '%" . $search_string . "%' OR sku LIKE '%" . $search_string . "%'";
$count = $this->count_by($where);
$this->db->join('supplier_details', 'supplier_pricing.supplier_id = supplier_details.id', 'left');
if ($limit)
{
$this->db->limit($limit, $offset);
}
$results = $this->find_all_by($where);
$this->suppliers_model->set_table();
return array(
'data' => $results,
'count' => $count
);
}
public function get_supplier($id = NULL)
{
return $this->find($id);
}
public function get_suppliers($active = TRUE)
{
return $this->find_all_by('active', TRUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment