1. creating a directory after inserting a record
Important notice: the following approach has only been successfully tested with "single record" insert forms, in other words: it´s not likely to work when using ADDT´s "Dynamic Form" to insert multiple records !
If you´re like me, you occasionally might want to create a directory on the server alongside with inserting a new record into the database table -- for purposes such as generating a unique "assets" folder for each user or for making sure that your newly created CMS article will instantly have a corresponding directory for storing related images or files.
Very fortunately ADDT lets us even automatize such complementary tasks by passing custom code to its "Custom Trigger" engine, and here´s a rather trivial PHP code snippet which would automatically create such a directory after the new record has been successfully inserted:
mkdir ("../../existing_folder_name/".KT_escapeForSql($tNG->getPrimaryKeyValue(),$tNG->getColumnType($tNG->getPrimaryKey()))."", 0755);
Needless to mention that this code will have to be encapsulated in an AFTER trigger !
Please let me tear this code snippet apart in order to explain its functionality:
mkdir()
This native PHP function attempts to create a directory -- of course it needs some more information like the relative path to the new folder and the name you´d like to assign to it, but the technical details are already well explained within the official PHP function reference.
"../../existing_folder_name/"
This is the relative path (../../) from your current document to an already existing directory (existing_folder_name) which is supposed to contain the new subdirectory.
KT_escapeForSql($tNG->getPrimaryKeyValue(),$tNG->getColumnType($tNG->getPrimaryKey()))
Well, even if this looooooooooong piece of PHP code might seem suspicious to you, you can buy my explanation unseen -- it´s a standard ADDT "shortcut" (not sure if the term "longcut" exists :-) which represents the Primary Key of the newly created record.
Q: what is it here for ?
A: this shortcut respectively the Primary Key value of the newly inserted record will define the name of the to-be-created directory
Q: why not using a simple static folder name instead ?
A 1: are you nuts ? :-)
A 2: when trying to create *unique* folders for each newly inserted record, you need to ensure *that* they´re indeed going to be unique, and you need to make sure that the folder name will be a valid one -- and so far the only unique information a database table holds, is the numeric Primary Key.
0755
This octal number will set the so-called mode of the new folder to "Read/no execute/Write" on Unix servers, whereas Windows servers will happily ignore this parameter.
Regardless if it´s a true file (e.g. jpg,swf) or a directory, a Unix server makes no difference and treats them all as "file" -- and the specified "mode" will define its execute,write and read - permissions. As such a "mode" can have *lots* of different permission settings, and as I´d like to wrap things up at this point, I recommend reading some related information on w3schools.com
That´s basically all there is to say. Of course you should always make a test run on your (or your client´s) server to make sure that...
a) the new directory has indeed been created at the desired location within the server´s file system
b) the defined "mode" is also correct
Other than that I have to confess that this very Custom Trigger has made my life as developer much easier -- imagine you´d have to manually create such a directory on the server everytime you insert a new record ;-)
This tutorial and all related material (files, images etc) are licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
Contact:
info @ guenter-schenk.com