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