Merge branch 'master' of github.com:crunchbutton/crunchbutton

This commit is contained in:
pererinha 2013-06-28 13:01:41 -04:00
commit ae000c8673
12 changed files with 512 additions and 10 deletions

View File

@ -0,0 +1,18 @@
CREATE TABLE `promo_group` (
`id_promo_group` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`date` datetime DEFAULT NULL,
`show_at_metrics` tinyint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id_promo_group`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `promo_group_promo` (
`id_promo_promo_group` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_promo` int(11) unsigned NOT NULL,
`id_promo_group` int(11) unsigned NOT NULL,
PRIMARY KEY (`id_promo_promo_group`),
KEY `id_promo` (`id_promo`),
KEY `id_promo_group` (`id_promo_group`),
CONSTRAINT `promo_promo_group_ibfk_1` FOREIGN KEY (`id_promo`) REFERENCES `promo` (`id_promo`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `promo_promo_group_ibfk_2` FOREIGN KEY (`id_promo_group`) REFERENCES `promo_group` (`id_promo_group`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

View File

@ -0,0 +1,59 @@
<?php
class Controller_giftcards_groups extends Crunchbutton_Controller_Account {
public function init() {
$action = c::getPagePiece(2);
switch ( $action ) {
case 'content':
$this->search();
break;
case 'new':
$this->form();
break;
case 'remove':
$id_promo_group = $_REQUEST[ 'id_promo_group' ];
$group = Crunchbutton_Promo_Group::o( $id_promo_group );
if( $group->id_promo_group ){
$group->delete();
}
echo 'ok';
break;
default:
if( is_numeric( $action ) ){
$this->form();
exit;
}
c::view()->page = 'giftcards';
c::view()->display('giftcards/groups/index');
break;
}
}
private function search(){
$search = [];
if ( $_REQUEST[ 'name' ] ) {
$search[ 'name' ] = $_REQUEST[ 'name' ];
}
c::view()->giftcards_groups = Crunchbutton_Promo_Group::find( $search );
c::view()->layout( 'layout/ajax' );
c::view()->display( 'giftcards/groups/content' );
}
private function form(){
$id_promo_group = c::getPagePiece(2);
if( $id_promo_group != 'new' ){
c::view()->group = Crunchbutton_Promo_Group::o( $id_promo_group );
} else {
c::view()->group = new Crunchbutton_Promo_Group();
}
c::view()->display( 'giftcards/groups/form' );
}
}

View File

@ -0,0 +1,43 @@
<?php
class Controller_api_Giftcard_group extends Crunchbutton_Controller_Rest {
public function init() {
switch ( $this->method() ) {
case 'post':
if ( $_SESSION['admin'] ) {
$id_promo_group = c::getPagePiece( 3 );
$name = $_REQUEST[ 'name' ];
$range = $_REQUEST[ 'range' ];
$show_at_metrics = $_REQUEST[ 'show_at_metrics' ];
if( $id_promo_group ){
$group = Crunchbutton_Promo_Group::o( $id_promo_group );
$group->name = $name;
$group->show_at_metrics = 1;
$group->date = date('Y-m-d H:i:s');
$group->save();
if( trim( $range ) != '' ){
$group->save_giftcards( $range );
}
} else {
$group = new Crunchbutton_Promo_Group();
$group->name = $name;
$group->show_at_metrics = 1;
$group->date = date('Y-m-d H:i:s');
$group->save();
if( trim( $range ) != '' ){
$group->save_giftcards( $range );
}
}
echo json_encode( ['success' => $group->id_promo_group ] );
}
break;
default:
echo json_encode( [ 'error' => 'invalid object' ] );
break;
}
}
}

View File

@ -12,6 +12,7 @@ class Controller_api_Giftcard extends Crunchbutton_Controller_Rest {
case 'generate':
$ids_restaurant = $this->request()['id_restaurant'];
$ids_group = $this->request()['id_group'];
$value = $this->request()['value'];
$total = $this->request()['total'];
$note = $this->request()['note'];
@ -92,6 +93,15 @@ class Controller_api_Giftcard extends Crunchbutton_Controller_Rest {
$giftcard->save();
if( $ids_group ){
foreach ( $ids_group as $id_group ) {
$new = new Crunchbutton_Promo_Group_Promo();
$new->id_promo = $giftcard->id_promo;
$new->id_promo_group = intval( $id_group );
$new->save();
}
}
if( !$idIni ){
$idIni = $giftcard->id_promo;
}

View File

@ -0,0 +1,129 @@
<?php
class Crunchbutton_Promo_Group extends Cana_Table
{
public function __construct($id = null) {
parent::__construct();
$this
->table('promo_group')
->idVar('id_promo_group')
->load($id);
}
public function date() {
if (!isset($this->_date)) {
$this->_date = new DateTime($this->date, new DateTimeZone(c::config()->timezone));
$this->_date->setTimezone(new DateTimeZone( c::config()->timezone ));
}
return $this->_date;
}
public function giftcards(){
if ( !isset( $this->_giftcards ) ) {
$sql = "SELECT * FROM promo p INNER JOIN promo_group_promo pgp ON p.id_promo = pgp.id_promo AND pgp.id_promo_group = {$this->id_promo_group}";
$this->_giftcards = Promo::q($sql, $this->db());
}
return $this->_giftcards;
}
public function remove_giftcards(){
$query = "DELETE FROM promo_group_promo WHERE id_promo_group = {$this->id_promo_group}";
Cana::db()->query( $query );
}
public function giftcards_total(){
return $this->giftcards()->count();
}
public function save_giftcards( $range ){
$this->remove_giftcards();
$ids = $this->range_translator( $range );
$ids = $this->validate_promo_ids( $ids );
foreach ( $ids as $id_promo ) {
$id_promo = intval( $id_promo );
$new = new Crunchbutton_Promo_Group_Promo();
$new->id_promo = $id_promo;
$new->id_promo_group = intval( $this->id_promo_group );
$new->save();
}
}
public function validate_promo_ids( $ids ){
$_ids = [];
foreach ( $ids as $id ) {
$promo = Crunchbutton_Promo::o( $id );
if( $promo->id_promo ){
$_ids[] = $promo->id_promo;
}
}
return $_ids;
}
public function range_translator( $range ){
$groups = explode( ',', $range );
$ids = [];
foreach ( $groups as $group ) {
$numbers = trim( $group );
$numbers = explode( '-', $numbers );
if( sizeof( $numbers ) == 2 ){
$ini = intval( $numbers[ 0 ] );
$end = intval( $numbers[ 1 ] );
for( $i = $ini; $i <= $end; $i++ ){
$ids[] = $i;
}
} else {
$ids[] = intval( $numbers[ 0 ] );
}
}
return $ids;
}
public function range(){
if( $this->giftcards_total() > 0 ){
$giftcards = $this->giftcards();
$ids = [];
foreach ( $giftcards as $giftcard ) {
$ids[] = intval( $giftcard->id_promo );
}
sort( $ids );
$groups = array();
for( $i = 0; $i < count( $ids ); $i++ ){
if( $i > 0 && ( $ids[$i - 1] == $ids[ $i ] - 1 ) ){
array_push( $groups[ count( $groups ) - 1 ], $ids[ $i ] );
} else {
array_push( $groups, array( $ids[ $i ] ) );
}
}
$str_group = '';
$commas = '';
foreach($groups as $group){
if( count( $group ) == 1 ){
$str_group .= $commas . $group[ 0 ];
} else {
$str_group .= $commas . $group[0] . ' - ' . $group[count($group) - 1];
}
$commas = ', ';
}
return $str_group;
} else {
return '';
}
}
public static function find($search = []) {
$query = 'SELECT `promo_group`.* FROM `promo_group` WHERE id_promo_group IS NOT NULL ';
if ( $search[ 'name' ] ) {
$query .= " AND name LIKE '%{$search[ 'name' ]}%' ";
}
$query .= " ORDER BY id_promo_group DESC";
$gifts = self::q($query);
return $gifts;
}
}

View File

@ -0,0 +1,12 @@
<?php
class Crunchbutton_Promo_Group_Promo extends Cana_Table
{
public function __construct($id = null) {
parent::__construct();
$this
->table('promo_group_promo')
->idVar('id_promo_group_promo')
->load($id);
}
}

View File

@ -0,0 +1,29 @@
<?php
$groups = $this->giftcards_groups;
?>
<?php if (!$groups->count()) { ?>
No results found
<?php } else { ?>
<table class="table table-normal">
<thead>
<td>Name</td>
<td>Date</td>
<td>Number of Gift cards</td>
<td></td>
</thead>
<?php foreach ( $groups as $group ) {
$date = $group->date();
?>
<tr>
<td><?php echo $group->name;?> </td>
<td><?php echo $date->format('M jS Y')?> - <?php echo $date->format('g:i:s A')?> </td>
<td><?php echo $group->giftcards_total(); ?></td>
<td style="width:150px;">
<a href="/giftcards/groups/<?php echo $group->id_promo_group; ?>" class="btn btn-green admin-giftcard-group-new"><i class="icon-edit"></i> Edit</a>
&nbsp;
<button data-id="<?php echo $group->id_promo_group; ?>" class="btn admin-giftcard-group-remove btn-red admin-giftcard-group-new"><i class="icon-trash"></i> Remove</button>
</td>
</tr>
<?php } ?>
</table>
<?php } ?>

View File

@ -0,0 +1,95 @@
<?
$this->title = 'Gift cards - Groups';
$this->titleicon = 'gift';
$this->titleLink = '/giftcards/groups';
$this->title2 = 'Create new group';
$this->title2icon = 'group';
$group = $this->group;
if( $group->id_promo_group ){
$range = $group->range();
}
?>
<div class="container-fluid padded">
<div class="row-fluid">
<div class="span6">
<div class="box">
<div class="box-header">
<span class="title">Group</span>
</div>
<div class="box-content ">
<ul class="box-list">
<li>
<span>Name</span>
<span class="pull-right">
<input type="text" name="name" maxlength="250" id="name" value="<?php echo $group->name; ?>" />
</span>
</li>
<?php
if( $group->id_promo_group ){?>
<li>
<span>Number of giftcards</span>
<span class="pull-right">
<?php echo $group->giftcards_total(); ?>
</span>
</li>
<?php } ?>
<li>
<span>Range of giftcards</span>
<span class="pull-right">
<input type="text" name="range" maxlength="250" id="range" value="<?php echo $range; ?>" />
<div class="note large">
Sample: 1 - 3, 10 - 12, 15
</div>
</span>
</li>
<li class="input">
<button type="submit" class="btn btn-blue admin-giftcard-group-save"><i class="icon-save"></i> Save </button>
</li>
</ul>
</div>
</div>
</div>
</div>
<script>
$(function() {
$(document).on('click', '.admin-giftcard-group-save', function() {
App.giftcards.print = true;
sendForm();
} );
});
function sendForm(){
var name = $.trim( $( '#name' ).val() );
var range = $.trim( $( '#range' ).val() );
if( name == '' ){
alert( 'Please type a name!' );
$( '#name' ).focus();
return;
}
var data = { 'name' : name, 'range' : range };
var url = App.service + 'giftcard/group/<?php echo $group->id_promo_group; ?>';
$.ajax({
type: "POST",
dataType: 'json',
data: data,
url: url,
success: function( json ) {
if( json.error ){
alert( 'Error at saving the group!' );
} else {
alert( 'Group saved!' );
location.href = '/giftcards/groups/';
}
},
error: function( ){
alert( 'Error at saving the group!' );
}
});
}
</script>

View File

@ -0,0 +1,67 @@
<?
$this->title = 'Gift cards';
$this->titleicon = 'gift';
$this->titleLink = '/giftcards';
$this->title2 = 'Groups';
$this->title2icon = 'group';
?>
<!-- content -->
<div class="container-fluid padded">
<div class="row-fluid">
<div class="box">
<div class="box-content">
<div class="row-fluid">
<div class="span6 separate-sections">
<ul class="padded separate-sections">
<li class="input">
<input class="span12" name="name" type="text" value="<?=strip_tags($_REQUEST['name'])?>" placeholder="Name">
</li>
</ul>
</div>
<div class="span6 separate-sections">
<ul class="padded separate-sections">
<li><button class="btn btn-blue admin-giftcard-group-search"><i class="icon-search"></i>&nbsp;&nbsp;&nbsp; Search </button>
<a href="/giftcards/groups/new" class="btn btn-green admin-giftcard-group-new"><i class="icon-group"></i>&nbsp;&nbsp;&nbsp; New Group </a>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="box">
<div class="box-header"><span class="title">Results</span></div>
<div class="box-content">
<div class="giftcards-group-loader">
<center><i class="icon-spinner icon-spin" style="font-size: 50px;"></i></center>
</div>
<div class="giftcards-group-content"></div>
</div>
</div>
</div>
</div>
<script>
$(function() {
$(document).on('click', '.admin-giftcard-group-search', function() {
App.giftcardsGroup.load();
} );
$(document).on('keyup', '[name="name"]', function( e ) {
if (e.which == 13) {
App.giftcardsGroup.load();
}
} );
App.giftcardsGroup.load();
$(document).on('click', '.admin-giftcard-group-remove', function() {
var button = $( this );
if( confirm( 'Confirm? This action will not remove the gift cards!' ) ){
var id_promo_group = button.attr( 'data-id' );
App.giftcardsGroup.remove( id_promo_group );
}
} );
});
</script>

View File

@ -13,7 +13,7 @@
<div class="span6 separate-sections">
<ul class="padded separate-sections">
<li class="input">
<input name="id_order" type="text" value="<?=strip_tags($_REQUEST['code'])?>" placeholder="Code">
<input name="code" type="text" value="<?=strip_tags($_REQUEST['code'])?>" placeholder="Code">
</li>
<li>
<label class="span3">Limit</label>
@ -66,12 +66,13 @@
</div>
<div style="text-align: center;">
<button type="submit" class="btn btn-blue admin-giftcard-search"><i class="icon-search"></i>&nbsp;&nbsp;&nbsp; Search </button>
&nbsp;&nbsp;&nbsp;
<a href="/giftcards/groups/" class="btn btn-green admin-giftcard-group-new"><i class="icon-group"></i>&nbsp;&nbsp;&nbsp; Groups </a>
&nbsp;&nbsp;&nbsp;
<div class="btn-group">
<button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
Create / Send &nbsp;&nbsp;&nbsp;<span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a href="/giftcards/new">Create a new code</a></li>
<li class="divider"></li>
@ -79,7 +80,8 @@
<li><a href="/giftcards/sms">Send by sms</a></li>
</ul>
</div>
&nbsp;&nbsp;&nbsp;
</div>
<br />
</div>
@ -100,7 +102,7 @@
App.giftcards.load();
} );
$(document).on('keyup', '[name="id_order"]', function( e ) {
$(document).on('keyup', '[name="code"]', function( e ) {
if (e.which == 13) {
App.giftcards.load();
}

View File

@ -65,6 +65,16 @@
</select>
</span>
</li>
<li>
<label>Group</label>
<span>
<select id="id_group" class="chzn-select" multiple name="id_group" data-placeholder="Choose a group" style="width:100%;">
<? foreach (Crunchbutton_Promo_Group::q('SELECT * FROM promo_group ORDER BY name') as $group) : ?>
<option value="<?=$group->id_promo_group?>"><?=$group->name?></option>
<? endforeach ; ?>
</select>
</span>
</li>
<li>
<span>Paid by</span>
<span class="pull-right">
@ -345,6 +355,7 @@ $(function() {
function sendForm(){
var value = $.trim( $( '#value' ).val() );
var id_restaurant = $( '#id_restaurant' ).val();
var id_group = $( '#id_group' ).val();
var id_user = $( '#id_user' ).val();
var total = $( '#total' ).val();
var print = ( App.giftcards.print ) ? 1 : 0;
@ -392,7 +403,7 @@ function sendForm(){
}
}
var data = { 'value' : value, 'id_user' : id_user, 'id_restaurant' : id_restaurant, 'total' : total,'id_order_reference':id_order_reference, 'paid_by':paid_by, 'id_restaurant_paid_by':id_restaurant_paid_by, 'note' : note, 'created_by' : created_by, 'track' : track, 'notify_phone' : notify_phone, 'name' : name, 'how_delivery' : how_delivery, 'contact' : contact, 'add_as_credit' : add_as_credit, 'notify_by_sms' : notify_by_sms, 'notify_by_email' : notify_by_email, 'chars_to_use' : chars_to_use, 'include_gift_card_id' : include_gift_card_id, 'length' : length, 'print' : print, 'prefix' : prefix };
var data = { 'value' : value, 'id_user' : id_user, 'id_restaurant' : id_restaurant, 'total' : total,'id_order_reference':id_order_reference, 'paid_by':paid_by, 'id_restaurant_paid_by':id_restaurant_paid_by, 'note' : note, 'created_by' : created_by, 'track' : track, 'notify_phone' : notify_phone, 'name' : name, 'how_delivery' : how_delivery, 'contact' : contact, 'add_as_credit' : add_as_credit, 'notify_by_sms' : notify_by_sms, 'notify_by_email' : notify_by_email, 'chars_to_use' : chars_to_use, 'include_gift_card_id' : include_gift_card_id, 'length' : length, 'print' : print, 'prefix' : prefix, 'id_group' : id_group };
var url = App.service + 'giftcard/generate';
$.ajax({
type: "POST",
@ -405,9 +416,9 @@ function sendForm(){
} else {
alert( 'Gift card(s) created!' );
if( App.giftcards.print ){
location.href = '/giftcards/print/' + json.success;
location.href = '/giftcards/print/' + json.success;
} else {
location.href = '/giftcards';
location.href = '/giftcards';
}
}
},

View File

@ -1745,7 +1745,34 @@ function full_post(url, data){
$('#jQueryPostItForm').submit();
}
App.giftcardsGroup = {
params: function() {
return {
name: $('input[name="name"]').val()
};
},
load: function() {
$('.giftcards-group-loader').show();
$('.giftcards-group-content').html('');
$.ajax({
url: '/giftcards/groups/content',
data: App.giftcardsGroup.params(),
complete: function(content) {
$('.giftcards-group-content').html(content.responseText);
$('.giftcards-group-loader').hide();
}
});
},
remove: function( id_promo_group ){
$.ajax({
url: '/giftcards/groups/remove',
type: "POST",
data: { 'id_promo_group': id_promo_group } ,
complete: function() {
App.giftcardsGroup.load();
}
});
},
}