If you enable the Multiple delete setting in PHPMaker, then the generated list pages will allow you to execute multiple delete (deleting more than one record at once) from the list page of your web application that generated by PHPMaker. This is really really helpful to delete many records by clicking the Delete button only once. In other words, you don't have to delete record one by one anymore. Quick and so easy, right?
But, hey... wait a minute! Be careful with this Multiple delete feature! The reason I am warning you is because you should check all those records before deleting them whether they already being used in another menu or page. If you do not do this step, then you will break the menus or pages that already have used those records that already being deleted. For example, if you want to delete some records in the Countries list page, then you are strongly recommended have to check whether those countries records are already being used in the Customers page. If not, then the country code that already being stored in the customers table will mean nothing, since that country code does not have the relationship anymore with the related country record in the Countries table. Got the point now? Great!
The question is, how do I check those records and make sure that they are already being used in another menu? In other words, if at least one of them or even they are all already being used in another menu, then the multiple delete should be automatically canceled by the system. The answer is, now you are able to handle the situation. Yes, the system will do that, not you do that manually! In this article I will show you the solution how to add this checking process into your web application that generated by PHPMaker. You will be able to know and implement how the system will check those records and make sure that they are not being used in another table before obviously deleting them. If they are already being used, then the system will never be able to delete them (the multiple delete process will be canceled and it will be never happened). Users will get the message from the system that inform them about that situation.
All we have to do is customizing the PHPMaker template file and adding some code into the Server Events section of PHPMaker. In other words, we do not modify any generated script files.
Please click on the following image below to watch the demo:
[hidepost]
-
Open your C:\Program Files\PHPMaker 8\languages\english.xml file, and find this code:
</global>
before the that line, please insert the following code:
<phrase id="RecordCannotBeDeleted" value="Record <strong>%r</strong> cannot be deleted since it is being used in menu %m.<br>"/>
-
Do the same step for the other language files you are using. For example, I am also using Indonesian language for my web application, so I will insert the following line just like the previous step above into my C:\Program Files\PHPMaker 8\languages\indonesian.xml file:
<phrase id="RecordCannotBeDeleted" value="Record <strong>%r</strong> tidak dapat dihapus karena sedang digunakan di menu %m.<br>"/>
- Open your .pmp file using PHPMaker application.
- Now in the next step, let's use the example above. Suppose we want to check the records in the countries table, then click on the countries table, and then from the right pane of PHPMaker, click on the Server Events/Client Scripts tab.
-
Go to the Table-Specific -> Common -> Row_Deleting, and then add the following code into the function Row_Deleting. Suppose that function has not been modified (you are still using the original one), then the complete code you should have would be like this (Warning: this is only example, you have to adjust the customers, countries, and country words below with your own needs):
function Row_Deleting(&$rs) { // Enter your code here // To cancel, set return value to False global $conn; // <-- don't forget this! global $countries_delete; // <-- use the delete object of the countries that generated by PHPMaker $sKey=""; $sMsg=""; foreach ($countries_delete->RecKeys as $key) { $sKey = ""; $keyvalue = is_array($key) ? implode(EW_COMPOSITE_KEY_SEPARATOR, $key) : $key; $sKey = $keyvalue; $sTable = "customers"; // <-- table name, where you want to check the records in another table $sFields = "country"; // <-- field name, in the table name above $sKeyField = "country"; // <-- field name, where you want to check the value in it $sMenu = "<strong>".$Language->Phrase('YourMenu')."</strong> -> <strong>".$Language->Phrase('YourSubMenu')."</strong>"; if (IsRecordBeingUsed($sTable, $sFields, $sKeyField, $sKey)) { // $sMsg .= "Record <strong>".$sKey."</strong> is already being used in another table and cannot be deleted at the moment.<br>"; $sMsg .= str_replace("%r", $sKey, $Language->Phrase("RecordCannotBeDeleted")); $sMsg = str_replace("%m", $sMenu, $sMsg); } } if ($sMsg != "") { $countries_delete->setFailureMessage($sMsg); return FALSE; } else { return TRUE; } }
-
Still in the Server Events/Client Scripts tab, go to the Server Events -> Global -> All Pages -> Global Code, and then insert the following code:
function IsRecordBeingUsed($sTable, $sFields, $sKeyField, $sKey) { global $conn; $sSql = "SELECT ".$sFields." FROM ".$sTable." WHERE ".$sKeyField." LIKE '%".$sKey."%'"; $rs = $conn->Execute($sSql); if ($rs->RecordCount() > 0) { return TRUE; } else { return FALSE; } }
- Re-generate all of your related files using PHPMaker as usual, and make sure you also re-generate the userfn8.php file to include the IsRecordBeingUsed function in order to check the selected record(s) before deleting them using Multiple delete feature.
[/hidepost]
Leave a Reply
You must be logged in to post a comment.