Page 1 of 1

How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-01-28 15:22
by rngoda
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.

Re: How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-01-28 22:43
by landinialejandro
great! :)

Re: How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-01-29 09:45
by aarlauskas
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

Re: How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-01-29 12:26
by rngoda
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.

Re: How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-01-29 12:27
by rngoda
landinialejandro wrote:
2021-01-28 22:43
great! :)
Thanks for your feedback

Re: How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-01-29 14:51
by pfrumkin
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

Re: How To Use Sweet Alert Notification On Appgini Events

Posted: 2021-02-01 09:35
by rngoda
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

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

Posted: 2021-02-01 09:41
by rngoda
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