in a current project I use XML files to determine whether certain options are available on a page-by-page basis. an example snippet of the XML file looks like so:
<options>
<formularID>grabstaettennachauswahl_printdialog</formularID>
<module_path>%%PROJECT_PATH%%/modul/drucken/grabstaettennachauswahl</module_path>
<show_cemeteries>true</show_cemeteries>
<show_nachAuswahlkriterien>true</show_nachAuswahlkriterien>
<show_aktuelleGrabstaette>false</show_aktuelleGrabstaette>
<show_stichtag>false</show_stichtag>
<show_alleGrabstaetten>false</show_alleGrabstaetten>
.
.
. and so on...
</options>
below is the section of PHP code that reads the options in the XML file (stored in an array prior to this point) and does something when a match is found looks like so:
class TPrint
{
function TPrint($options)
{
// $this->options contains an array of all <options> from the XML file mentioned above
$this->options = $options;
}
function getBereich()
{
switch ($this->options["formularID"]) {
case "grabstaettennachauswahl_printdialog":
if ($this->options['show_alleVerstorbenen'] == 'true') { $this->show_alleVerstorbenen( $checked='no', $active='yes' ); }
if ($this->options['show_nachAuswahlkriterien'] == 'true') { $this->show_nachAuswahlkriterien( $checked='yes', $active='yes' ); }
if ($this->options['show_aktuelleGrabstaette'] == 'true') { $this->show_aktuelleGrabstaette( $checked='yes', $active='yes' ); }
if ($this->options['show_alleGrabstaetten'] == 'true') { $this->show_alleGrabstaetten( $checked='yes', $active='yes' ); }
break;
case "ablaufendenutzungsrechte_printdialog":
if ($this->options['show_alleVerstorbenen'] == 'true') { $this->show_alleVerstorbenen( $checked='no', $active='yes' ); }
if ($this->options['show_nachAuswahlkriterien'] == 'true') { $this->show_nachAuswahlkriterien( $checked='yes', $active='yes' ); }
if ($this->options['show_aktuelleGrabstaette'] == 'true') { $this->show_aktuelleGrabstaette( $checked='yes', $active='yes' ); }
if ($this->options['show_alleGrabstaetten'] == 'true') { $this->show_alleGrabstaetten( $checked='yes', $active='yes' ); }
break;
}
}
while this works quite well, it is easy to see that as I add more pages ('formularID') or even more options ('show_whatever'), this is gonna get very long and out of hand as the project grows. I've tried to simplfy things by using the called function name to be the same as the XML option name ('show_whatever') to reduce some confusion, but I know there has to be a more efficient way.
in this project the client might ask for another report page which would require me to modify the above bereich function (adding a new case to the switch statement and it's corresponding IFs) and adding a new XML file for that page. while the later is necessary, it would be nice to write up a function that loops through all the available options (minus the first three, which are set), and if an XML option is set to true, then fire-off the function of the same name. something like:
function bereich() {
count the number of options in the current XML file;
starting after the third option, loop through each {
if $this->option[name_of_option] is 'true' {
execute name_of_option_function
}
}
}
I guess what I am asking is this: is there some method of passing the contents of an array ($this->option['name_of_option'] in the above example) as the name of an existing function?
I'm looking for a generic or dynamic solution that simplifies further development. ideally I would just want to build up the XML file (putting in as many or as few of the options as needed), drop it in place, and let the code take care of determining which options to 'turn on'.
it feels like the solution is on the 'tip of my tongue', but I'm a bit stuck getting around to discovering it. any help or nudge in the right direction would be appreciated!
thanks!