Skip to content

Instantly share code, notes, and snippets.

@zulfajuniadi
Last active December 28, 2015 07:19
Show Gist options
  • Save zulfajuniadi/7463106 to your computer and use it in GitHub Desktop.
Save zulfajuniadi/7463106 to your computer and use it in GitHub Desktop.
Auto crud generator proposed for GC Replacement. Request for Comments and Volunteers.
<?php
/* Base Controller Example */
class BaseController
{
protected function _format_name($column, $row)
{
return strtoupper($column);
}
protected function _format_currency($column, $row)
{
return number_format($column);
}
protected function _format_number($column, $row)
{
return; //....
}
protected function _human_date($column, $row)
{
return date('d/m/Y H:i:s', strtotime($column));
}
}
/* End Base Controller */
/* Super Minimal, No-frills 9 lines of code for a complete CRUD! */
use \JomWeb\BakulPasar as BakulPasar;
class Products extends BaseController
{
public function __construct()
{
BakulPasar::Table('products');
}
}
/* End of Minimal example */
/* Extended API Example */
class Sales extends BaseController
{
public function __construct()
{
$user = Auth::User();
$where = (!$user->isAdmin()) ? $where = 'promoter_id = ' . $user->id : null;
/* Use the sales table */
BakulPasar::Table('sales');
/* Setup the Library for the sales Module */
BakulPasar::Setup(function(){
/* Using closures, we can set the context to be of BakulPasar */
return $this
/* List all the default fields */
-> fields([
'promoter_id' =>
$this
/**
* By default, all one to many relational data will be selected from a normal <select> list.
* This is how we override the output of a field in a form to our own custom view.
*/
-> display_as('_list_picker')
/**
* This is how we set the labels on the table list columns and on the form
*/
-> label('Promoter Name')
/**
* This is how we set many to one (belongs to) relationships
*/
-> relational('promoters', 'id', null, 'full_name')
/**
* This is how we 'translate' from mysql data to human readable data.
* By default, the context of the function will be of the parent class.
* Since the Sales class extends base controller, it can access the BaseController's
* protected methods.
*/
-> format('_format_name')
/**
* Server-side and client side validation double whammy. BakulPasar Style.
*/
-> validate(['required', 'xss', 'numeric']),
'points' =>
$this
-> label('Total Points')
-> format('_format_number')
-> validate(['required', 'xss', 'decimal']),
'total' =>
$this
-> label('Total Sales')
-> format('_format_currency')
-> validate(['required', 'xss', 'decimal']),
'sales_date' =>
$this
-> format('_human_date')
-> validate(['required', 'xss', 'date'])
])
/**
* These are all the hooks proposed for BakulPasar to call.
*/
-> onEnter('some_function')
-> onLeave('some_function')
-> before_insert('some_function')
-> after_insert('some_function')
-> before_update('some_function')
-> after_update('some_function')
-> before_delete('some_function')
-> after_delete('some_function')
/**
* Proposed simple ACL System enforced by the SQL WHERE query
*/
-> where($where)
/**
* Sorting
*/
-> sort('Promoter Name')
/**
* Pagination
*/
-> paginate('50');
});
/**
* Overriding Routes with BakulPasar. Why we don't use native routing? Because the method below will handle
* BOTH Get and Post requests. DRY, KISS KISS MUAH MUAH
*/
BakulPasar::Override('/sales', function() {
return $this
-> fields([
'promoter_id',
/**
* Summing Columns
*/
'points' => $this -> sum(),
'total' => $this -> sum()
])
/**
* ... and Grouping
*/
-> group_by('promoter_id');
});
BakulPasar::Override('/sales/:user_id', function($user_id) use ($user) {
/**
* Another Example of simple ACL Enforcement.
*/
if($user_id !== $user->id) {
return Redirect::to('/sales');
}
return $this
-> template('sales.individual')
/**
* I'm not quite sure of the partials mechanics. Just putting it out here to see where this will go.
*/
-> partials([
'promoter_details' => '/promoter/' . $user_id,
'promoter_sales' => '/charts/sales/ytd/' . $user_id ])
-> fields([
'promoter_id',
'points',
'total',
'items' =>
$this
-> display_as('_list_picker')
-> label('Items Sold')
/**
* This is how we set one to many (has_many) relationships
*/
-> relational('sales_items', 'sales_id', 'id', 'item_name')
-> format('_format_name'),
'sales_date'
])
-> sort('sales_date', 'desc');
});
}
}
/* End of Extended API Example */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment