⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Grocery crud with Growl/Ubuntu-like Notifications & redirect



rat

rat
  • profile picture
  • Member

Posted 28 May 2012 - 12:57 PM

First, I wan't to thank creator for this great tool. It made my life a lot easyer :)

I modified grocery crud to include different notifications than default ones. In my project I wanted to use this notifications...
My theme of choice is datatables so my modification is applied to it (i assume that for flexigrid implementation logic would be simmilar).
I also added redirect function to crud so I can redirect to what ever page I want after delete / add / edit (with simple setting $crud->redirect_url($url);).

It would be nice that grocery crud has option to chose notification system.
Also things that I miss most are: multiple cruds on same page, custom query for table, opening crud directly on edit page (or maybe just a read_only view for whole form with edit button which opens editing ? [and of course option to disable edit] ), hiding & adding buttons on edit page, redirect after edit/add,...
I sure hope some of these will be implemented soon :)

Anyway here's the modified parts od code. Maybe it helps someone else too.


[b]assets/grocery_crud/themes/datatables/js/datatables.js[/b]

function delete_row(delete_url , row_id)
{
if(confirm(message_alert_delete))
{
$.ajax({
url: delete_url,
dataType: 'json',
success: function(data)
{
if(data.success)
{
$('#ajax_refresh_and_loading').trigger('click');
/*$('#report-success').html( data.success_message ).slideUp('fast').slideDown('slow');
$('#report-error').html('').slideUp('fast');*/
/***************navihtot_BEGIN*/
if (typeof(data.redirect_url) != "undefined" && data.redirect_url !== null) {
pass_notify_user("notify_success",data.success_message,data.redirect_url);
}
else notify_user("notify_success", {text:data.success_message} );
/***************navihtot_END*/
$('tr#row-'+row_id).addClass('row_selected');
var anSelected = fnGetSelected( oTable );
oTable.fnDeleteRow( anSelected[0] );
}
else
{
/*$('#report-error').html( data.error_message ).slideUp('fast').slideDown('slow');
$('#report-success').html('').slideUp('fast'); */

/***************navihtot_BEGIN*/
notify_user("notify_error", {text:data.error_message});
/***************navihtot_END*/

}
}
});
}

return false;
}


[b]assets/grocery_crud/themes/datatables/js/datatables-add.js[/b]

$('#crudForm').submit(function(){
$(this).ajaxSubmit({
url: validation_url,
dataType: 'json',
beforeSend: function(){
$("#FormLoading").show();
},
success: function(data){
$("#FormLoading").hide();
if(data.success)
{
$('#crudForm').ajaxSubmit({
dataType: 'text',
cache: 'false',
beforeSend: function(){
$("#FormLoading").show();
},
success: function(result){
$("#FormLoading").fadeOut("slow");
data = $.parseJSON( result );
if(data.success)
{
if(save_and_close)
{
window.location = data.success_list_url;
return true;
}

$('#report-error').hide().html('');
$('.field_error').each(function(){
$(this).removeClass('field_error');
});
clearForm();
/*$('#report-success').html(data.success_message);
$('#report-success').slideDown('slow');*/
/***************navihtot_BEGIN*/
if (typeof(data.redirect_url) != "undefined" && data.redirect_url !== null) {
//window.location.href = data.redirect_url;
pass_notify_user("notify_success",data.success_message,data.redirect_url);
}
else pass_notify_user("notify_success",data.success_message,list_url);
/***************navihtot_END*/
}
else
{
/*alert('An error has been occured at the insert.');*/
/***************navihtot_BEGIN*/
pass_notify_user("notify_error",data.error_message,list_url);
/***************navihtot_END*/
}
}
});


[b]assets/grocery_crud/themes/datatables/js/datatables-edit.js[/b]

$('#crudForm').submit(function(){
$(this).ajaxSubmit({
url: validation_url,
dataType: 'json',
beforeSend: function(){
$("#FormLoading").show();
},
cache: false,
success: function(data){
$("#FormLoading").hide();
if(data.success)
{
$('#crudForm').ajaxSubmit({
dataType: 'text',
cache: false,
beforeSend: function(){
$("#FormLoading").show();
},
success: function(result){
$("#FormLoading").fadeOut("slow");
data = $.parseJSON( result );
if(data.success)
{
if(save_and_close)
{
window.location = data.success_list_url;
return true;
}

$('#report-error').hide().html('');
$('.field_error').each(function(){
$(this).removeClass('field_error');
});

/*$('#report-success').html(data.success_message);
$('#report-success').slideDown('slow');*/
/***************navihtot_BEGIN*/
if (typeof(data.redirect_url) != "undefined" && data.redirect_url !== null) {
pass_notify_user("notify_success",data.success_message,data.redirect_url);
}
else pass_notify_user("notify_success",data.success_message,list_url);
/***************navihtot_END*/
}
else
{
/*alert(message_update_error);*/
/***************navihtot_BEGIN*/
if (typeof(data.redirect_url) != "undefined" && data.redirect_url !== null) {
pass_notify_user("notify_success",data.success_message,data.redirect_url);
}
else pass_notify_user("notify_error",data.error_message,list_url);
/***************navihtot_END*/
}
},


[b]assets/grocery_crud/themes/datatables/views/add.php & edit.php[/b]

/*******navihtot_BEGIN*/
var list_url = '<?php if (isset($this->redirect_url)) echo $this->redirect_url; else echo $list_url?>';
/*******navihtot_END*/


[b]application/libraries/grocery_crud.php[/b]

class grocery_CRUD_Layout extends grocery_CRUD_Model_Driver
{
/**************navihtot_BEGIN*/
protected $redirect_url = null;
/**************navihtot_END*/



protected function insert_layout($insert_result = false)
{
if($insert_result === false)
{
echo json_encode(array('success' => false));
}
else
{
$success_message = '<p>'.$this->l('insert_success_message');

/*if(!$this->unset_back_to_list && !empty($insert_result) && !$this->unset_edit)
{
$success_message .= " <a href='".$this->getEditUrl($insert_result)."'>".$this->l('form_edit')." {$this->subject}</a> ".$this->l('form_or');
}

if(!$this->unset_back_to_list)
{
$success_message .= " <a href='".$this->getListUrl()."'>".$this->l('form_go_back_to_list')."</a>";
}*/

$success_message .= '</p>';

echo "<textarea>".json_encode(array(
'success' => true ,
'insert_primary_key' => $insert_result,
'success_message' => $success_message,
'success_list_url' => $this->getListSuccessUrl($insert_result),
/*********navihtot_BEGIN*/
'redirect_url' => $this->redirect_url
/*********navihtot_END*/
))."</textarea>";
}
$this->set_echo_and_die();
}



/*********navihtot_BEGIN*/
public function redirect_url($url) {
$this->redirect_url=$url;
}
/*********navihtot_END*/



protected function update_layout($update_result = false, $state_info = null)
{
if($update_result === false)
{
echo json_encode(array('success' => $update_result));
}
else
{
$success_message = '<p>'.$this->l('update_success_message');
/*if(!$this->unset_back_to_list)
{
$success_message .= " <a href='".$this->getListUrl()."'>".$this->l('form_go_back_to_list')."</a>";
}*/
$success_message .= '</p>';

/* The textarea is only because of a BUG of the jquery form plugin with the combination of multipart forms */
echo "<textarea>".json_encode(array(
'success' => true ,
'insert_primary_key' => $update_result,
'success_message' => $success_message,
'success_list_url' => $this->getListSuccessUrl($state_info->primary_key),
'redirect_url' => $this->redirect_url
))."</textarea>";
}
$this->set_echo_and_die();
}



[b]notifications.php[/b] which you need to include at begining of every page (eg. header view thats loaded on every page)

<!-- growl/ubuntu like notifications BEGIN -->

<script src="<?php echo base_url().'js/jquery.notify.js'; ?>" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="<?php echo base_url().'css/web/ui.notify.css';?>" />
<script type="text/javascript">
//function to call for every notification
function notify_user( template, vars, opts ){
return $container.notify("create", template, vars, opts);
}


//passing notifications to another page
function pass_notify_user(type,text,url) {
$("#notification_passing_type").val(type);
//$("#notification_passing_title").val(title);
$("#notification_passing_text").val(text);
$("#notification_passing").attr("action",url);
$("#notification_passing").submit();
}
$(function(){


// initialize widget on a container, passing in all the defaults.
// the defaults will apply to any notification created within this
// container, but can be overwritten on notification-by-notification
// basis.
$container = $("#notification_container").notify();


var $_POST = <?php echo json_encode($_POST); ?>;
if ( typeof($_POST['notification_passing_type'])!== "undefined" && $_POST['notification_passing_type']) {
if ($_POST['notification_passing_type']!="none") {
notify_user($_POST['notification_passing_type'], { text:$_POST['notification_passing_text'] });
}
}



});

</script>
<form style="display: hidden" action="" method="POST" id="notification_passing">
<input type="hidden" id="notification_passing_type" name="notification_passing_type" value="none"/>
<input type="hidden" id="notification_passing_text" name="notification_passing_text" value="none"/>
<input type="hidden" id="notification_passing_title" name="notification_passing_title" value="none"/>
</form>
<div id="notification_container" style="display:none">
<div id="notify_info">
<a class="ui-notify-close ui-notify-cross" href="#">x</a>
<div style="float:left;margin:0 10px 0 0; "><img src="<?php echo base_url().'images/notify-info.png';?>" alt="" /></div>
<h1><?php echo $this->lang->line('messages_info_title'); ?></h1>
<p>#{text}</p>

</div>
<div id="notify_warning">
<a class="ui-notify-close ui-notify-cross" href="#">x</a>
<div style="float:left;margin:0 10px 0 0"><img src="<?php echo base_url().'images/notify-warning.png';?>" alt="" /></div>
<h1><?php echo $this->lang->line('messages_warning_title'); ?></h1>
<p>#{text}</p>
</div>
<div id="notify_question">
<a class="ui-notify-close ui-notify-cross" href="#">x</a>
<div style="float:left;margin:0 10px 0 0"><img src="<?php echo base_url().'images/notify-question.png';?>" alt="" /></div>
<h1><?php echo $this->lang->line('messages_question_title'); ?></h1>
<p>#{text}</p>
</div>
<div id="notify_error">
<a class="ui-notify-close ui-notify-cross" href="#">x</a>
<div style="float:left;margin:0 10px 0 0"><img src="<?php echo base_url().'images/notify-error.png';?>" alt="" /></div>
<h1><?php echo $this->lang->line('messages_error_title'); ?></h1>
<p>#{text}</p>
</div>
<div id="notify_success">
<a class="ui-notify-close ui-notify-cross" href="#">x</a>
<div style="float:left;margin:0 10px 0 0;"><img src="<?php echo base_url().'images/notify-success.png';?>" alt="" /></div>
<h1><?php echo $this->lang->line('messages_success_title'); ?></h1>
<p>#{text}</p>
</div>
</div>
<!-- growl/ubuntu like notifications END -->