Skip to content

Instantly share code, notes, and snippets.

@draeton
Last active April 6, 2021 02:15
Show Gist options
  • Save draeton/313490416f6e0585e1ce to your computer and use it in GitHub Desktop.
Save draeton/313490416f6e0585e1ce to your computer and use it in GitHub Desktop.
Mercury Studio » My Local KFC code sample
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Coupons extends Controller
{
private $view = 'admin/layout';
private $limit = 10;
function __construct()
{
parent::__construct();
$this->output->enable_profiler((bool) get_option('profiler_enabled'));
if (is_ajax()) {
$this->view = 'admin/layout_ajax';
}
$this->limit = $this->config->item('items_per_page');
$this->load->library(array('table', 'form_validation'));
$this->load->model('coupons_model');
}
function index($slug = null)
{
}
/**
* view admin table
*
* @param int $offset
* @return void
*/
function admin($offset = 0)
{
admin_check();
$data['page_title'] = 'Coupons';
$data['page'] = 'admin/table';
$data['table_key'] = 'coupons';
$data['new_button'] = anchor("coupons/form", 'New Coupon', array('class' => 'button'));
// set args
$args = array();
// load data
$coupons = $this->coupons_model->get_paged_list($this->limit, $offset, $args);
// generate table
$data['table'] = $this->_table($coupons);
$this->load->view($this->view, $data);
}
/**
* generate the data table
*
* @return string
*/
function _table($coupons)
{
if ($coupons) {
$this->table->set_empty("&nbsp;");
$this->table->set_template(array('table_open' => '<table class="view sort">'));
$this->table->set_heading('zindex', 'id', 'name', 'description', 'plu', 'price', 'active_date', 'expires_date', 'Actions');
foreach ($coupons as $coupon){
$actions = array();
$actions[0] = anchor('coupons/form/'.$coupon->id,'edit',array('class'=>'edit'));
$actions[1] = anchor('coupons/delete/'.$coupon->id,'delete',array('class'=>'delete','onclick'=>"return confirm('Are you sure want to delete this coupon?')"));
$actions = implode(' | ', $actions);
$this->table->add_row(
$coupon->zindex,
$coupon->id,
$coupon->name,
$coupon->description,
$coupon->plu,
$coupon->price,
date('Y-m-d', strtotime($coupon->active_date)),
date('Y-m-d', strtotime($coupon->expires_date)),
"<div class='text-center no-wrap'>$actions</div>"
);
}
return $this->table->generate();
}
return null;
}
/**
* deletion route
*
* @param int $id
* @return void
*/
function delete($id)
{
admin_check();
$success = $this->coupons_model->delete($id);
if ($success) {
set_flasharray('messages', 'Coupon deleted.');
} else {
set_flasharray('errors', 'Coupon deletion failed.');
}
redirect('coupons/admin');
}
/**
* add/edit form
*
* @param int $id
* @return void
*/
function form($id = null)
{
admin_check();
$action = ($id) ? 'Edit' : 'New';
$data['action'] = $action;
$data['page_title'] = "$action Coupon";
$data['page'] = 'admin/form/coupons';
$data['id'] = $id;
if ($this->form_validation->run('coupons/form') == false) // validation hasn't been passed
{
if ($id) {
$coupon = $this->coupons_model->get_by_id($id);
$fields = $this->_fields($coupon);
$data['page_title'] .= ": $coupon->name";
} else {
$fields = $this->_fields();
}
$this->load->vars($fields);
$this->load->view($this->view, $data);
}
else // passed validation proceed to post success logic
{
$form_data = array(
'id' => $id,
'name' => set_value('name'),
'description' => set_value('description'),
'disclaimer' => set_value('disclaimer'),
'image' => set_value('image'),
'plu' => set_value('plu'),
'price' => set_value('price'),
'zindex' => set_value('zindex'),
'active_date' => set_value('active_date'),
'expires_date' => set_value('expires_date')
);
$stores = set_value('stores[]');
$groups = set_value('groups[]');
// upload file
if ($_FILES['image_file']['name']) {
list($w, $h, $kb) = array_values(get_image_settings('coupon'));
$form_data['image'] = upload_image('image_file', $w, $h, $kb);
}
if ($this->coupons_model->save($form_data, $stores, $groups) == true) {
set_flasharray('messages', 'Coupon saved.');
} else {
set_flasharray('errors', 'Coupon save failed.');
}
$submit = $this->input->post('submit');
$redirect = ($submit == 'Save') ? $this->uri->uri_string() : 'coupons/admin';
redirect($redirect);
}
}
/**
* setup form fields
*
* @return array
*/
function _fields($coupon = null)
{
list($w, $h, $kb) = array_values(get_image_settings('coupon'));
$fields = array(
'name' => array(
'title' => 'Name',
'name' => 'name',
'id' => 'name',
'value' => @field(set_value('name', $coupon->name)),
'class' => 'text'
),
'description' => array(
'title' => 'Description',
'name' => 'description',
'id' => 'description',
'value' => @field(set_value('description', $coupon->description)),
'class' => 'text'
),
'disclaimer' => array(
'title' => 'Disclaimer',
'name' => 'disclaimer',
'id' => 'disclaimer',
'value' => @field(set_value('disclaimer', $coupon->disclaimer)),
'class' => 'text'
),
'plu' => array(
'title' => 'PLU',
'name' => 'plu',
'id' => 'plu',
'value' => @field(set_value('plu', $coupon->plu)),
'class' => 'text'
),
'price' => array(
'title' => 'Price',
'name' => 'price',
'id' => 'price',
'value' => @field(set_value('price', $coupon->price)),
'class' => 'text'
),
'image_file' => array(
'title' => "Image File ({$w}w {$h}h, {$kb}kb, gif|jpg|png)",
'name' => 'image_file',
'id' => 'image_file',
'type' => 'file'
),
'image' => array(
'title' => 'Image',
'name' => 'image',
'id' => 'image',
'value' => @field(set_value('image', $coupon->image)),
'class' => 'text'
),
'zindex' => array(
'title' => 'Z-index',
'name' => 'zindex',
'id' => 'zindex',
'value' => @field(set_value('zindex', $coupon->zindex), 0, true),
'class' => 'text'
),
'active_date' => array(
'title' => 'Active Date',
'name' => 'active_date',
'id' => 'active_date',
'value' => @field(set_value('active_date', $coupon->active_date)),
'class' => 'date'
),
'expires_date' => array(
'title' => 'Expires Date',
'name' => 'expires_date',
'id' => 'expires_date',
'value' => @field(set_value('expires_date', $coupon->expires_date)),
'class' => 'date'
),
'stores' => array(
'title' => 'Select Stores',
'name' => 'stores[]',
'id' => 'stores',
'options' => get_stores_array(),
'value' => @field(set_value('stores[]', $coupon->stores)),
'class' => 'text'
),
'groups' => array(
'title' => 'Select Groups',
'name' => 'groups[]',
'id' => 'groups',
'options' => get_groups_array(),
'value' => @field(set_value('groups[]', $coupon->groups)),
'class' => 'text'
),
);
return $fields;
}
/**
* add/edit override form
*
* @return void
*/
function form_override($coupon_id = null, $node_type = null, $node_id = null, $override_id = null)
{
admin_check();
if ($coupon_id && $node_id && $override_id) { // only 'create' for now
$action = ($override_id) ? 'Edit' : 'New';
$data['action'] = $action;
$data['page_title'] = "$action Coupon Override";
$data['page'] = 'admin/form/coupons_override';
$data['id'] = $override_id;
$data['node_type'] = $node_type;
$data['node_id'] = $node_id;
if ($this->form_validation->run('coupons/form/override') == false) // validation hasn't been passed
{
$coupon = $this->coupons_model->get_by_id($coupon_id);
$data['page_title'] .= ": $coupon->name";
if ($override_id) {
$override = $this->coupons_model->get_override_by_id($override_id);
$fields = $this->_fields_override($coupon, $override);
} else {
$fields = $this->_fields_override($coupon);
}
$this->load->vars($fields);
$this->load->view($this->view, $data);
}
else // passed validation proceed to post success logic
{
$form_data = array(
'id' => $override_id,
'coupon_id' => $coupon_id,
'node_type' => $node_type,
'node_id' => $node_id,
'or_price' => set_value('or_price'),
);
if ($this->coupons_model->save_override($form_data) == true) {
set_flasharray('messages', 'Coupon override saved.');
} else {
set_flasharray('errors', 'Coupon override save failed.');
}
$submit = $this->input->post('submit');
$redirect = ($submit == 'Save') ? $this->uri->uri_string() : "{$node_type}s/form/$node_id";
redirect($redirect);
}
}
}
/**
* setup form override fields
*
* @return array
*/
function _fields_override($coupon = null, $override = null)
{
if ($coupon) {
list($w, $h, $kb) = array_values(get_image_settings('coupon'));
$fields = array(
'name' => array(
'title' => 'Name',
'name' => 'name',
'id' => 'name',
'value' => $coupon->name,
'class' => 'text',
'disabled' => 'disabled',
),
'description' => array(
'title' => 'Description',
'name' => 'description',
'id' => 'description',
'value' => $coupon->description,
'class' => 'text',
'disabled' => 'disabled',
),
'plu' => array(
'title' => 'PLU',
'name' => 'plu',
'id' => 'plu',
'value' => $coupon->plu,
'class' => 'text',
'disabled' => 'disabled',
),
'or_price' => array(
'title' => 'Price',
'name' => 'or_price',
'id' => 'or_price',
'value' => @field(set_value('or_price', $override->or_price), $coupon->price, true),
'class' => 'text'
),
'image' => array(
'title' => 'Image',
'name' => 'image',
'id' => 'image',
'value' => $coupon->image,
'class' => 'text',
'disabled' => 'disabled',
),
'zindex' => array(
'title' => 'Z-index',
'name' => 'zindex',
'id' => 'zindex',
'value' => $coupon->zindex,
'class' => 'text',
'disabled' => 'disabled',
),
'active_date' => array(
'title' => 'Active Date',
'name' => 'active_date',
'id' => 'active_date',
'value' => $coupon->active_date,
'class' => 'date',
'disabled' => 'disabled',
),
'expires_date' => array(
'title' => 'Expires Date',
'name' => 'expires_date',
'id' => 'expires_date',
'value' => $coupon->expires_date,
'class' => 'date',
'disabled' => 'disabled',
),
);
return $fields;
}
}
}
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Coupons_model
*
* Class description. It operates the following tables:
* - coupons
* - coupons_nodes
*
*/
class Coupons_model extends Model
{
private $table_name = 'coupons';
private $nodes_table_name = 'coupons_nodes';
function __construct()
{
parent::__construct();
}
function count_all()
{
return $this->db->count_all($this->table_name);
}
function get_paged_list($limit = 10, $offset = 0)
{
$fields = array('id', 'name', 'description', 'plu', 'price', 'zindex', 'active_date', 'expires_date');
$this->db->select($fields);
$this->db->from($this->table_name);
$this->db->order_by('zindex DESC');
if ($limit) $this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
function get_by_id($id)
{
$fields = array('id', 'name', 'description', 'disclaimer', 'image', 'plu', 'price', 'zindex', 'active_date', 'expires_date');
$this->db->select($fields);
$this->db->from($this->table_name);
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$coupon = $query->row();
$coupon->stores = $this->get_nodes('store', $id);
$coupon->groups = $this->get_nodes('group', $id);
return $coupon;
}
return null;
}
function get_list_by_node($type, $id, $active_only = false)
{
if ($type == 'store') {
// get group nodes with this store id
$this->db->select('coupons_nodes.id, coupons_nodes.coupon_id');
$this->db->from('coupons_nodes');
$this->db->join('groups', "coupons_nodes.node_id = groups.id");
$this->db->join('group_stores', "group_stores.group_id = groups.id");
$this->db->where('node_type', 'group');
$this->db->where('group_stores.store_id', $id);
$this->db->order_by('groups.zindex, groups.modified');
$query = $this->db->get();
$group_nodes = $query->result();
// get store nodes with this store id
$this->db->select('coupons_nodes.id, coupons_nodes.coupon_id');
$this->db->from('coupons_nodes');
$this->db->where('node_id', $id);
$this->db->where('node_type', 'store');
$query = $this->db->get();
$store_nodes = $query->result();
// walk through groups, and then stores, setting keys on a hash
// the key will be overriden by groups sorted by zindex
// then stores
$nodes = array();
foreach ($group_nodes as $node) {
$nodes[$node->coupon_id] = $node->id;
}
foreach ($store_nodes as $node) {
$nodes[$node->coupon_id] = $node->id;
}
// pull out the node ids to pass into the next query
$nodes = array_values($nodes);
// pass some value to prevent where_in error on blank array
if (empty($nodes)) $nodes = array(-1);
}
$fields = array('coupons_nodes.id', 'coupons.name', 'coupons.description', 'coupons.plu', 'coupons.price', 'coupons.disclaimer',
'coupons.zindex', 'coupons.active_date', 'coupons.expires_date', 'coupons.image',
'or_price', 'coupons_nodes.node_type', 'coupons_nodes.node_id', 'coupons_nodes.coupon_id');
$this->db->select($fields);
$this->db->from($this->nodes_table_name);
$this->db->join($this->table_name, "$this->table_name.id = $this->nodes_table_name.coupon_id");
if ($type == 'group') {
$this->db->where('node_id', $id);
$this->db->where('node_type', $type);
} else {
$this->db->where_in('coupons_nodes.id', $nodes);
}
if ($active_only) {
$today = date('Y-m-d');
$this->db->where("active_date <= '$today'");
$this->db->where("expires_date >= '$today'");
}
$this->db->order_by('zindex DESC');
$query = $this->db->get();
return $query->result();
}
function get_nodes($type, $id)
{
$this->db->select('node_id');
$this->db->from($this->nodes_table_name);
$this->db->where('coupon_id', $id);
$this->db->where('node_type', $type);
$nodes = array_flatten_no_keys($this->db->get()->result_array());
return $nodes;
}
function get_override_by_id($id)
{
$fields = array('id', 'or_price');
$this->db->select($fields);
$this->db->from($this->nodes_table_name);
$this->db->where('id', $id);
$query = $this->db->get();
if ($query->num_rows() == 1) return $query->row();
return null;
}
function save($data, $stores, $groups)
{
if ($data['id']) {
$id = $data['id'];
$this->db->where('id', $data['id']);
$this->db->update($this->table_name, $data);
} else {
$data['created'] = date('Y-m-d h:i:s');
$this->db->insert($this->table_name, $data);
$id = $this->db->insert_id();
}
$this->save_nodes('store', $id, $stores, $data);
$this->save_nodes('group', $id, $groups, $data);
return $id;
}
function save_nodes($type, $id, $nodes, $data)
{
// save nodes
$nodes = ($nodes) ? $nodes : array(); // set nodes to empty array if blank
if (is_array($nodes)) {
$current = $this->get_nodes($type, $id);
if (!is_array($current)) {
$current = array();
}
$sub = array_diff($current, $nodes);
$add = array_diff($nodes, $current);
// remove items
$this->db->where('coupon_id', $id);
$this->db->where('node_type', $type);
$this->db->where_in('node_id', $sub);
$this->db->delete($this->nodes_table_name);
// add items
foreach ($add as $node) {
$this->db->insert($this->nodes_table_name, array(
'coupon_id' => $id,
'node_type' => $type,
'node_id' => $node,
'or_price' => $data['price'],
'created' => date('Y:m:d h:i:s'),
));
}
}
}
function delete($id)
{
$this->db->where('coupon_id', $id);
$this->db->delete($this->nodes_table_name);
$this->db->where('id', $id);
$this->db->delete($this->table_name);
return is_null($this->get_by_id($id));
}
function save_override($data)
{
if ($data['id']) {
$this->db->where('id', $data['id']);
$this->db->update($this->nodes_table_name, $data);
return $data['id'];
} else {
$this->db->insert($this->nodes_table_name, $data);
return $this->db->insert_id();
}
return false;
}
}
<?php echo form_open_multipart($this->uri->uri_string(), null, array('id' => $id)); ?>
<div class="tabs">
<ul>
<li><a href="#tabs-1">General</a></li>
<li><a href="#tabs-2">Groups</a></li>
<li><a href="#tabs-3">Stores</a></li>
</ul>
<div id="tabs-1">
<table>
<tr>
<td>
<p>
<?php echo form_label($name['title'], $name['id']); ?>
<?php echo form_input($name); ?>
<?php echo form_error($name['name']); ?>
</p>
<p>
<?php echo form_label($description['title'], $description['id']); ?>
<?php echo form_input($description); ?>
<?php echo form_error($description['name']); ?>
</p>
<p>
<?php echo form_label($disclaimer['title'], $disclaimer['id']); ?>
<?php echo form_input($disclaimer); ?>
<?php echo form_error($disclaimer['name']); ?>
</p>
<p>
<?php echo form_label($plu['title'], $plu['id']); ?>
<?php echo form_input($plu); ?>
<?php echo form_error($plu['name']); ?>
</p>
<p>
<?php echo form_label($price['title'], $price['id']); ?>
<?php echo form_input($price); ?>
<?php echo form_error($price['name']); ?>
</p>
<p>
<?php echo form_label($zindex['title'], $zindex['id']); ?>
<?php echo form_input($zindex); ?>
<?php echo form_error($zindex['name']); ?>
</p>
<p>
<?php echo form_label($active_date['title'], $active_date['id']); ?>
<?php echo form_input($active_date); ?>
<?php echo form_error($active_date['name']); ?>
</p>
<p>
<?php echo form_label($expires_date['title'], $expires_date['id']); ?>
<?php echo form_input($expires_date); ?>
<?php echo form_error($expires_date['name']); ?>
</p>
</td>
<td>
<p>
<?php echo form_label($image['title'], $image['id']); ?>
<?php echo form_input($image); ?>
<?php echo form_error($image['name']); ?>
</p>
<p>
<?php echo form_label($image_file['title'], $image_file['id']); ?>
<?php echo form_input($image_file); ?>
<?php echo form_error($image_file['name']); ?>
</p>
<?php if (isset($image['value']) && $image['value']) : ?>
<p>
<img src="<?php echo $image['value']; ?>" alt="<?php echo $image['title']; ?>" />
</p>
<?php endif; ?>
</td>
</tr>
</table>
</div>
<div id="tabs-2">
<p>
<?php echo form_label($groups['title'], $groups['id']); ?>
<?php echo form_multiselect($groups['name'], $groups['options'], $groups['value'], 'id = "'.$groups['id'].'" class="multiselect"'); ?>
<?php echo form_error($groups['name']); ?>
</p>
</div>
<div id="tabs-3">
<p>
<?php echo form_label($stores['title'], $stores['id']); ?>
<?php echo form_multiselect($stores['name'], $stores['options'], $stores['value'], 'id = "'.$stores['id'].'" class="multiselect"'); ?>
<?php echo form_error($stores['name']); ?>
</p>
</div>
</div>
<br />
<p>
<?php if ($action == 'New') : ?>
<?php echo anchor('coupons/admin', 'Cancel') ?> <?php echo form_submit('submit', 'Create'); ?>
<?php else : ?>
<?php echo anchor('coupons/admin', 'Cancel') ?> <?php echo form_submit('submit', 'Save'); ?> <?php echo form_submit('submit', 'Save & Go Back'); ?>
<?php endif; ?>
</p>
<?php echo form_close(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment