How To Use Sweet Alert Notification On Appgini Events

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
User avatar
rngoda
Veteran Member
Posts: 124
Joined: 2020-02-05 16:00
Location: KENYA
Contact:

How To Use Sweet Alert Notification On Appgini Events

Post by rngoda » 2021-01-28 15:22

Image

Do you need to have an above Alert when you insert records or update records in Appgini instead of the default Appgini alert?
Then I got you covered.
STEP ONE
Paste the following code inside the head tag of your header.php file

Code: Select all

<script type="text/javascript" src="https://www.jquery-az.com/javascript/alert/dist/sweetalert-dev.js"></script>
 <link rel="stylesheet" type="text/css" href="https://www.jquery-az.com/javascript/alert/dist/sweetalert.css">
 
STEP TWO
Locate the incCommon.php file and find a function named showNotifications() replace it with the following code

Code: Select all

function showNotifications($msg = '', $class = '', $fadeout = true) {
		global $Translation;
		if($error_message = strip_tags($_REQUEST['error_message']))
			$error_message = '<div class="text-bold">' . $error_message . '</div>';

		if(!$msg) { // if no msg, use url to detect message to display
			if($_REQUEST['record-added-ok'] != '') {
				$msg = $Translation['new record saved'];
				$class = 'success';
			} elseif($_REQUEST['record-added-error'] != '') {
				$msg = $Translation['Couldn\'t save the new record'] . $error_message;
				$class = 'error';
				$fadeout = false;
			} elseif($_REQUEST['record-updated-ok'] != '') {
				$msg = $Translation['record updated'];
				$class = 'success';
			} elseif($_REQUEST['record-updated-error'] != '') {
				$msg = $Translation['Couldn\'t save changes to the record'] . $error_message;
				$class = 'error';
				$fadeout = false;
			} elseif($_REQUEST['record-deleted-ok'] != '') {
				$msg = $Translation['The record has been deleted successfully'];
				$class = 'success';
			} elseif($_REQUEST['record-deleted-error'] != '') {
				$msg = $Translation['Couldn\'t delete this record'] . $error_message;
				$class = 'error';
				$fadeout = false;
			} else {
				return '';
			}
		}
		$id = 'notification-' . rand();

		ob_start();
		// notification template
		?>

		<script type="text/javascript">swal("<?php echo $alerttitle = ($class=="success") ? "Congrats!" : "Oops!" ; ?>", "<?php echo $msg ?>", "<?php echo $class ?>");</script>

		<?php
		$out = ob_get_clean();

		return $out;
	}
That's it you can now try inserting or updating your records and you will be seeing the above sweet alert. Cheers!!
IMPORTANT NOTE: The above changes can be overwritten when you regenerate your Appgini code, be careful not to overwrite them.
I'm a software engineer specializing in web database application development for complex scalable web apps.

Buy AdminLTE Plugin For Appgini: HERE
Buy Cloud Storage Plugin For Appgini HERE

Checkout AdminLTE Plugin For Appgini Tutorials On Youtube

For support Email: [email protected] Whatsappp: Me Here

User avatar
landinialejandro
AppGini Super Hero
AppGini Super Hero
Posts: 126
Joined: 2016-03-06 00:59
Location: Argentina
Contact:

Re: How To Use Sweet Alert Notification On Appgini Events

Post by landinialejandro » 2021-01-28 22:43

great! :)
Alejandro.
AppGini 5.98 - Linux OpenSuse Tumblewweed.

Some of my posts that may interest you:
:arrow: Landini Admin Template: Template for Appgini like AdminLTE
:arrow: Profile image plugin: add and changue image user profile
:arrow: Field editor in table view: Configurable fast edit fields in TV
:idea: my personal page

User avatar
aarlauskas
Veteran Member
Posts: 127
Joined: 2019-04-28 18:03
Location: Medway, UK

Re: How To Use Sweet Alert Notification On Appgini Events

Post by aarlauskas » 2021-01-29 09:45

That looks great buddy. I'm not good at coding, but it always makes me nervous when I see a web address in the code (like in header.php on yours).
What role does it play? does it rely on that web address to be alive? Sending/Receiving data etc? what is it? :D

User avatar
rngoda
Veteran Member
Posts: 124
Joined: 2020-02-05 16:00
Location: KENYA
Contact:

Re: How To Use Sweet Alert Notification On Appgini Events

Post by rngoda » 2021-01-29 12:26

aarlauskas wrote:
2021-01-29 09:45
That looks great buddy. I'm not good at coding, but it always makes me nervous when I see a web address in the code (like in header.php on yours).
What role does it play? does it rely on that web address to be alive? Sending/Receiving data etc? what is it? :D
Hello, you don't have to be scared, the links just contain the JS code needed to display the sweet alert. Alternatively, you can just hit the provided links copy the code and save it locally in your project and refer to it from there.
I'm a software engineer specializing in web database application development for complex scalable web apps.

Buy AdminLTE Plugin For Appgini: HERE
Buy Cloud Storage Plugin For Appgini HERE

Checkout AdminLTE Plugin For Appgini Tutorials On Youtube

For support Email: [email protected] Whatsappp: Me Here

User avatar
rngoda
Veteran Member
Posts: 124
Joined: 2020-02-05 16:00
Location: KENYA
Contact:

Re: How To Use Sweet Alert Notification On Appgini Events

Post by rngoda » 2021-01-29 12:27

landinialejandro wrote:
2021-01-28 22:43
great! :)
Thanks for your feedback
I'm a software engineer specializing in web database application development for complex scalable web apps.

Buy AdminLTE Plugin For Appgini: HERE
Buy Cloud Storage Plugin For Appgini HERE

Checkout AdminLTE Plugin For Appgini Tutorials On Youtube

For support Email: [email protected] Whatsappp: Me Here

pfrumkin
Veteran Member
Posts: 157
Joined: 2020-02-18 17:58
Location: Albuquerque, New Mexico USA

Re: How To Use Sweet Alert Notification On Appgini Events

Post by pfrumkin » 2021-01-29 14:51

Hi rngoda,

This is great, and great that you are posting to the community.

That you lose the change when you regenerate is a bigger problem than you may be considering, IMHO, very important to application sustainability. You may want to check this blog post, which explains a method of modifying AppGini-generated files in a more maintainable manner:

Code: Select all

https://bigprof.com/blog/appgini/custom ... dmin-area/
.

~Paul

User avatar
rngoda
Veteran Member
Posts: 124
Joined: 2020-02-05 16:00
Location: KENYA
Contact:

Re: How To Use Sweet Alert Notification On Appgini Events

Post by rngoda » 2021-02-01 09:35

pfrumkin wrote:
2021-01-29 14:51
Hi rngoda,

This is great, and great that you are posting to the community.

That you lose the change when you regenerate is a bigger problem than you may be considering, IMHO, very important to application sustainability. You may want to check this blog post, which explains a method of modifying AppGini-generated files in a more maintainable manner:

Code: Select all

https://bigprof.com/blog/appgini/custom ... dmin-area/
.

~Paul
Thanks for the insight let me update my solution above :D
I'm a software engineer specializing in web database application development for complex scalable web apps.

Buy AdminLTE Plugin For Appgini: HERE
Buy Cloud Storage Plugin For Appgini HERE

Checkout AdminLTE Plugin For Appgini Tutorials On Youtube

For support Email: [email protected] Whatsappp: Me Here

User avatar
rngoda
Veteran Member
Posts: 124
Joined: 2020-02-05 16:00
Location: KENYA
Contact:

Re: UPDATE: How To Use Sweet Alert Notification On Appgini Events

Post by rngoda » 2021-02-01 09:41

Now you can implement my hack in a more maintainable manner than ever before, Now instead of copy-pasting the showNotifications function here is an update.
STEP 1:
Go to your hooks folder and create a file named replace-appgini-functions.php paste in the following code:

Code: Select all

<?php // Save this file as 'hooks/replace-appgini-functions.php'
 
	$hooks_dir = dirname(__FILE__);
	include("{$hooks_dir}/../defaultLang.php");
	include("{$hooks_dir}/../language.php");
	include("{$hooks_dir}/../lib.php");
	
	// Step 1: Specify the file containing the function we want to overwrite
	$appgini_file = "{$hooks_dir}/../incCommon.php";
 
	// Step 2: Specify the file containing our version of the function
	$mod_file = "{$hooks_dir}/mod.showNotifications.php";
 
	// Step 3: Specify the name of the function we want to overwrite
	$func_name = 'showNotifications';
 
	echo "<br>{$func_name}: " . replace_function($appgini_file, $func_name, $mod_file);
 
	#######################################
 
	function replace_function($appgini_file, $function_name, $mod_file) {
		// read the new code from the mod file
		$new_code = @file($mod_file);
		if(empty($new_code)) return 'No custom code found.';
 
		// remove the first line containing PHP opening tag and keep the rest as $new_snippet
		array_shift($new_code);
		$new_snippet = implode('', $new_code);
 
		$pattern1 = '/\s*function\s+' . $function_name . '\s*\(.*\).*(\R.*){200}/';
		$pattern2 = '/\t#+(.*\R)*/';
 
		$entire_code = file_get_contents($appgini_file);
		if(!$entire_code) return 'Invalid AppGini file.';
 
		$m = [];
		if(!preg_match_all($pattern1, $entire_code, $m)) return 'Function to replace not found.';
		$snippet = $m[0][0] . "\n";
 
		if(!preg_match_all($pattern2, $snippet, $m)) return 'Could not find the end of the function.';
		$snippet = str_replace($m[0][0], '', $snippet);
 
		$snippet_nocrlf = str_replace("\r\n", "\n", $snippet);
		$new_snippet_nocrlf = str_replace("\r\n", "\n", $new_snippet);
		if(trim($snippet_nocrlf) == trim($new_snippet_nocrlf)) return 'Function already replaced.';
 
		// back up the file before overwriting
		if(!@copy(
			$appgini_file, 
			preg_replace('/\.php$/', '.backup.' . date('Y.m.d.H.i.s') . '.php', $appgini_file)
		)) return 'Could not make a backup copy of file.';
 
		$new_code = str_replace(trim($snippet), trim($new_snippet), $entire_code);
		if(!@file_put_contents($appgini_file, $new_code)) return "Couldn't overwrite file.";
 
		return 'Function overwritten successfully.';
	}
STEP TWO:
In your hooks folder create a file named mod.showNotifications.php and paste in the following code:

Code: Select all

<?php
function showNotifications($msg = '', $class = '', $fadeout = true) {
		global $Translation;
		if($error_message = strip_tags($_REQUEST['error_message']))
			$error_message = '<div class="text-bold">' . $error_message . '</div>';

		if(!$msg) { // if no msg, use url to detect message to display
			if($_REQUEST['record-added-ok'] != '') {
				$msg = $Translation['new record saved'];
				$class = 'success';
			} elseif($_REQUEST['record-added-error'] != '') {
				$msg = $Translation['Couldn\'t save the new record'] . $error_message;
				$class = 'error';
				$fadeout = false;
			} elseif($_REQUEST['record-updated-ok'] != '') {
				$msg = $Translation['record updated'];
				$class = 'success';
			} elseif($_REQUEST['record-updated-error'] != '') {
				$msg = $Translation['Couldn\'t save changes to the record'] . $error_message;
				$class = 'error';
				$fadeout = false;
			} elseif($_REQUEST['record-deleted-ok'] != '') {
				$msg = $Translation['The record has been deleted successfully'];
				$class = 'success';
			} elseif($_REQUEST['record-deleted-error'] != '') {
				$msg = $Translation['Couldn\'t delete this record'] . $error_message;
				$class = 'error';
				$fadeout = false;
			} else {
				return '';
			}
		}
		$id = 'notification-' . rand();

		ob_start();
		// notification template
		?>

		<script type="text/javascript">swal("<?php echo $alerttitle = ($class=="success") ? "Congrats!" : "Oops!" ; ?>", "<?php echo $msg ?>", "<?php echo $class ?>");</script>

		<?php
		$out = ob_get_clean();

		return $out;
	}
We’ve now created 2 files inside the hooks folder: replace-appgini-functions.php and mod.showNotifications.php. Whenever you want to regenerate your AppGini application, all you need to do is to call replace-appgini-functions.php once to apply your modified code. As a safety measure, the file to be modified will be backed up first. Also, our code above would check if modifications were already applied, and if so they won’t be re-applied. So, it’s actually safe to call this file multiple times. Only the first time will apply modifications, and others would do nothing and just notify you that changes were already applied.

Assuming our app is hosted on example.com/staff-db, to apply the modified showNotifications() function, we should visit this URL from a browser: example.com/staff-db/hooks/replace-appgini-functions.php

For more info on how to replace or overwrite appgini functions reffer HERE
I'm a software engineer specializing in web database application development for complex scalable web apps.

Buy AdminLTE Plugin For Appgini: HERE
Buy Cloud Storage Plugin For Appgini HERE

Checkout AdminLTE Plugin For Appgini Tutorials On Youtube

For support Email: [email protected] Whatsappp: Me Here

Post Reply