Adventure,PHP,framework,page controller,front controller,pattern,object orientated design,software,development,reusability,uml,tutorial,benchmark,brilliant performance,

Search:    
Downloads  |  SVN!  |  Roadmap  |  Forum!  |  Bugtracking  |  Guestbook  |  Backlinks!  |  References!  |  Sitemap  |  Impress  
 
Deutsch | English Adventure PHP Framework  Bookmark @ Technorati Bookmark @ del.icio.us Bookmark @ Mr. Wong Bookmark @ Simpy Bookmark @ Google Bookmark @ Digg.com Adventure PHP Framework Print page 035-Contact-form-tutorial

Contact form

Rank article:
This article has not yet been ranked. Vote this article first of all!

1. Introduction

The present chapter gives another application of the form tag libs and the <core:importdesign /> tag. The contact form described in the following text gives the opportunity to prevent e-mail addresses from beeing grabbed by bots. Moreover, the recipient list is configurable and extensible. The module can be included in any page controller compliant application by using the <core:importdesign /> tag.


2. Basics

This tutorial includes technics that were already used in the previous tutorials. The author assumes, that you have read chapter 2 of the comment function tutorial carefully. This chapter describes the separation of a single software into three different layers, that contain different functionality. Beyond, the section depicts how domain objects are used as an agent of communication and which role is dedicated to the MVC pattern.


2.1. Configuration

To be able to integrate the contact form module in various projects the project specific parts must be outsourced to application configuration files. Configuration is necessary in two flavours: first of all the recipients must be configured, second the texts of the form tag libs (validators) must be defined per project.

At this point of the tutorial we're going to do a little excurse to functionality of the form validation included in the framework:
The tags
  • <form:validate />
  • <valgroup:validate />
need a language dependent configuration file for the validation messages to display. The configuration file required for the taglibs is expected to be storend in
  /config/tools/form/taglib/{CONTEXT}/{ENVIRONMENT}_formconfig.ini
{CONTEXT} is meant to be a place holder for the context of the current application and the value for {ENVIRONMENT} must be read from the registry. In case of the presend documentation page the file must be named like
  /config/tools/form/taglib/sites/demosite/DEFAULT_formconfig.ini
As described in the standard tag libs article in section 2.3.16 and 2.3.17 these tags can be configured using the additional attributes
  • msginputreq=""
  • msginputwrg=""
These attributs define the configuration key that contains a language dependent text to be shown, if the desired field is not filled correctly. This makes it possible to define a single set of validation texts for an entire application. Moreover, it is easy to translate these texts, becaues they are stored in simple text files.

Example:
In order to display the message
  Please fill the field in e-mail address!
if a form control was not filled and to print the text
  Please provide a valid e-mail address!
if the field was not filled with a correct e-mail address, the validator must be configured as follows:
  <form:validate
                  button="send"
                  field="email"
                  type="EMail"
                  msginputreq="Contact.EMail.InputRequired"
                  msginputwrg="Contact.EMail.InputWrong"
  />
The attribute button defines the name of the button, that should initiate validation, field indicates the field, that should be checked and type explains the type of the validator. The keys contained in the two attributes msginputreq and msginputwrg were prefixed with "Contact.EMail." to indicate, that these configuration keys belong to the contact form application and are intended to declare the validation texts of the e-mail field. This convention is common to be able to differentiate between the various configuration keys provided within one configuration file. Besides, it is useful to place some comments in the configuration file.

A configuration file intended to be a basis for a multilanguage application may have the following content:
[de]
Contact.EMail.InputRequired = "Bitte geben Sie eine E-Mail-Adresse sein!"
Contact.EMail.InputWrong = "Bitte geben Sie eine gültige E-Mail-Adresse sein!"

[en]
Contact.EMail.InputRequired = "Please fill the field sender name!"
Contact.EMail.InputWrong = "Please provide a valid email address!"

2.2. Multilanguage applications

The adventure php framework features several options to create multilangual applications or modules. At first, the language is stored within every single DOM node or service layer and hence can be used to display language dependent texts within a document controller. Further the framework provides the XML tags
  • <html:getstring />
  • <template:getstring />
  • <form:getstring />
that display language dependent texts from configuration files. In case of the present module both possibilities are used. As a basis, the configuration file
 /config/modules/kontakt4/{CONTEXT}/{ENVIRONMENT}_language.ini
is used to store the texts to be displayed in the module. The file is filled with the following values:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Language file for the contact form module                                                        ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[de]
; header
header.title = "Kontakt"

; hints on the form
formhint.text = "Wenn Sie mit mir in Kontakt treten möchten, dann benutzen Sie einfach dieses Formular. Geben Sie Ihre Nachricht ein und schon kann es los gehen. Ich werden mich dann umgehend mit Ihnen in Verbindung setzten. Bitte füllen Sie das Formular vollständig aus!"

; form labels
form.person = "Person / Gruppe: "
form.person.margin = "24"
form.name = "Ihr Name:"
form.name.margin = "65"
form.email = "Ihre E-Mail-Adresse:"
form.email.margin = "9"
form.subject = "Ihr Betreff:"
form.subject.margin = "65"
form.comment = "Ihre Nachricht:"
form.button = "Senden"
form.button.style = "margin-left: 335px;"
form.captcha = "Bestätigungscode:"

; confirmation text
message.text = "Vielen Danke für Ihre Anfrage. Ich werde mich umgehend mit Ihnen in Verbindung setzen!"

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

[en]
; header
header.title = "Contact"

; hints on the form
formhint.text = "If you want to contact me, please use the form provided below. Then I will immediately get in contact with you. Please fill all required fields!"

; form labels
form.person = "Person / group: "
form.person.margin = "33"
form.name = "Your name:"
form.name.margin = "57"
form.email = "Your email address:"
form.email.margin = "9"
form.subject = "Your subject:"
form.subject.margin = "49"
form.comment = "Your message:"
form.button = "Send"
form.button.style = "margin-left: 350px;"
form.captcha = "Security code:"

; confirmation text
message.text = "Many thanks for your message. I will get in contact with you immediately!"
The values stored there can be displayed by using the follwing XML tags:
  • <html:getstring namespace="modules::kontakt4" config="language" entry="form.person" />,
  • <template:getstring namespace="modules::kontakt4" config="language" entry="form.person" /> oder
  • <form:getstring namespace="modules::kontakt4" config="language" entry="form.person" />
The second possibility of of displaying language dependent texts can be used to label form controls like buttons. In this case the labelling must be done within a document controller. The following code shows how to achieve this:
// Load language dependent configuration
$Config = &$this->__getConfiguration('modules::kontakt4','language');

// Get a reference on the desired form control
$Button = &$Form->getFormElementByName('KontaktSenden');

// Set the value attribute of the control using the $this->__Language member
$Button->setAttribute('value',$Config->getValue($this->__Language,'form.button')); 

2.3. Structure of the module

According to the structure described in the comment module tutorial the structure of the kontakt4 module looks like this:
  /modules/
           kontakt4/
                    biz/
                    data/
                    pres/
                         documentcontroller/
                         templates/
The biz folder serves the business components (two domain objects and one manager), data contains the mapper class, that reads the recipients, and the pres folder stores the controller and template files needed by the presentation layer. Considered as a whole, the contact form consists of the following files:

  • /modules/kontakt4/pres/templates/kontakt.html:
    The file kontakt.html contains the main design and coordinated the output of the form or the thank-you-page with a <core:importdesign /> tag.
  • /modules/kontakt4/pres/templates/formular.html:
    This file contains the definition of the form view.
  • /modules/kontakt4/pres/templates/meldung.html:
    In the file meldung.html the content of the thank-you-page is defined.
  • /modules/kontakt4/pres/documentcontroller/kontakt_v2_controller.php:
    To fill the form view with dynamically generated content a document controller is needed. The file kontakt_v2_controller.php contains the document controller class definition.
  • /modules/kontakt4/biz/contactManager.php:
    contactManager.php is the home of the business component class, that coordinates the workflow of the application.
  • /modules/kontakt4/biz/oRecipient.php:
    oRecipient.php contains the definition of the recipient domain object.
  • /modules/kontakt4/biz/oFormData.php:
    oFormData.php contains the definition of the form data domain object.
  • /modules/kontakt4/data/contactMapper.php:
    The file contactMapper.php is used to define the data layer component, that loads the recipients out of the configuration file and presents them as domain objects.
As already mentioned the following configuration files must be provided:
  • /config/modules/kontakt4/{CONTEXT}/{ENVIRONMENT}_empfaenger.ini:
    Configuration of the recipients.
  • /config/tools/form/taglib/{CONTEXT}/{ENVIRONMENT}_formconfig.ini:
    Texts for the validator form tags.
  • /config/modules/kontakt4/{CONTEXT}/{ENVIRONMENT}_language.ini:
    Language dependent labels.
The syntax of the configuration files was already described above but the content of these files is discussed later on.


3. Implementation of the module

This tutorial uses the TOP-DOWN approach once again. The current chapter therefore describes the files mentioned above starting with the presentation layer and in the given order:


3.1. File kontakt.html

The template file kontakt.html defines the structure of the module. It contains the headline, that is displayed by use of the <html:getstring /> tag and a <core:importdesign /> tag, that defines the view that either displays the form (default behaviour) of the thank-you-page:
<core:addtaglib namespace="tools::html::taglib" prefix="html" class="getstring" />
<font style="font-size: 26px; font weight: bold;"><html:getstring namespace="modules::kontakt4" config="language" entry="header.title" /></font>
<br />
<br />
<core:importdesign namespace="modules::kontakt4::pres::templates" template="[pagepart=formular]" />
Note: Within this template file, the <core:importdesign /> tag is used with the pagepart option. This means, that the name of the template can be influenced by the URL parameter pagepart. If the parameter is not present in the request string, the name of the template is taken from the XML tag definition. In this case the form is displayed.


3.2. File formular.html

This template file contains four essential blocks:
  • Definition of the document controller (Line 1)
    Here the document controller (MVC controller of the current DOM node) that is taken for transformation is defined.
  • Inclusion of external tag libs: (Lines 2-3)
    To be able to use the "html:form" or "html:getstring" tag libraries they must be introduced by using the "core:addtaglib" tag.
  • Definition of the HTML content: (Lines 5-9)
    The lines 5 to 9 define the HTML frame of the form view. This view features notices, that are included using the <html:getstring /> tag and a <html:placeholder /> tag that is filled with the output of the form within the document controller.
  • Form: (Lines 11-43)
    The lines 11 to 43 define the form. It contains a validator group, that is used to display validation messages. The labeling of the form controls is done by the <form:getstring /> tag.
<@controller namespace="modules::kontakt4::pres::documentcontroller" file="kontakt_v4_controller" class="kontakt_v4_controller" @>
<core:addtaglib namespace="tools::form::taglib" prefix="html" class="form" />
<core:addtaglib namespace="tools::html::taglib" prefix="html" class="getstring" />
<html:getstring namespace="modules::kontakt4" config="language" entry="formhint.text" />
<br />
<div style="text-align: left; padding-left: 80px; font-size: 12px;">
  <html:placeholder name="Inhalt" />
</div>

<html:form name="Kontakt" method="post">
  <form:valgroup name="FormValGroup">
    <br />
    <div style="width: 400px; border: 1px solid red; padding: 5px;">
      <img src="<valgroup:placeholder name="WarnImage" />" /> <font style="font-size: 13px; font-weight: bold;"><valgroup:placeholder name="WarnText" />:</font>
      <br />
      <valgroup:validate type="text" field="AbsenderName" button="KontaktSenden" msginputreq="Contact.Sender.InputRequired" />
      <valgroup:validate type="text" field="AbsenderAdresse" button="KontaktSenden" msginputreq="Contact.EMail.InputRequired" msginputwrg="Contact.EMail.InputWrong" validator="EMail" />
      <valgroup:validate type="text" field="Betreff" button="KontaktSenden" msginputreq="Contact.Subject.InputRequired" />
      <valgroup:validate type="text" field="Text" button="KontaktSenden" msginputreq="Contact.Text.InputRequired" />
    </div>
    <br />
  </form:valgroup>
  <br />
  <span style="margin-right: <form:getstring namespace="modules::kontakt4" config="language" entry="form.person.margin" />px;"><form:getstring namespace="modules::kontakt4" config="language" entry="form.person" /></span><form:select name="Empfaenger" class="eingabe_feld" />
  <br />
  <br />
  <span style="margin-right: <form:getstring namespace="modules::kontakt4" config="language" entry="form.name.margin" />px;"><form:getstring namespace="modules::kontakt4" config="language" entry="form.name" /></span><form:text name="AbsenderName" class="eingabe_feld" style="width: 280px;" validate="true" button="KontaktSenden" />
  <br />
  <br />
  <span style="margin-right: <form:getstring namespace="modules::kontakt4" config="language" entry="form.email.margin" />px;"><form:getstring namespace="modules::kontakt4" config="language" entry="form.email" /></span><form:text name="AbsenderAdresse" class="eingabe_feld" style="width: 280px;" validate="true" validator="EMail" button="KontaktSenden" />
  <br />
  <br />
  <span style="margin-right: <form:getstring namespace="modules::kontakt4" config="language" entry="form.subject.margin" />px;"><form:getstring namespace="modules::kontakt4" config="language" entry="form.subject" /></span><form:text name="Betreff" class="eingabe_feld" style="width: 280px;" validate="true" button="KontaktSenden" />
  <br />
  <br />
  <form:getstring namespace="modules::kontakt4" config="language" entry="form.comment" />
  <br />
  <form:area name="Text" class="eingabe_feld" style="height: 200px; width: 400px; overflow: auto;" validate="true" button="KontaktSenden" />
  <br />
  <br />
  <form:getstring namespace="modules::kontakt4" config="language" entry="form.captcha" />
  <br />
  <br />
  <form:addtaglib namespace="modules::captcha::pres::taglib" prefix="form" class="captcha" />
  <form:captcha text_class="eingabe_feld" validate="true" button="KontaktSenden"/>
  <br />
  <br />
  <form:button name="KontaktSenden" class="eingabe_feld" />
</html:form>

3.3. File meldung.html

Within the file meldung.html the thank-you message is defined, that is displayed on submission of the form. Merely this message contains a sentence that describes, that the recipient will send an answer soon. In this case, the message is taken from a configuration file, that contains the multilanguage texts of the module. Due to the fact, that these texts are statical no more document controller is necessary.
<br />
<core:addtaglib namespace="tools::html::taglib" prefix="html" class="getstring" />
<html:getstring namespace="modules::kontakt4" config="language" entry="message.text" />
<br />
<br />

3.4. Document controller kontakt_v4_controller

The document controller kontakt_v4_controller inherits from the abstract document controller baseController (interface). Because of the DOM structure of the framework each document controller must inherit from the base document controller. The rest of th class can be defined freely. The developer can define own class methods or load classes via import() each time he wants to. In case of the contact form the method __buildForm() is used to build the form. To control the output of the form the abstract method transformContent() is implemented. This function describes, that the form is diplayed as long as the form is not filled as desired by the programmer (see line 60). In case of correct values the business layer is created to send the form and to display the thank-you-page (template file meldung.html).
import('tools::variablen','variablenHandler');
import('modules::kontakt4::biz','contactManager');

class 
kontakt_v4_controller extends baseController
{

   var 
$_LOCALS;

   function 
kontakt_v4_controller(){

      
$this->_LOCALS variablenHandler::registerLocal(array('Empfaenger',
                                                             
'AbsenderName',
                                                             
'AbsenderAdresse',
                                                             
'Betreff',
                                                             
'Text'
                                                            
)
                                                      );

    
// end function
   
}

   function 
transformContent(){

      
// get form object
      
$Form = &$this->__getForm('Kontakt');

      if(
$Form->get('isValid') && $Form->get('isSent')){

         
// what is going to be sent?
         //
         // - contact id
         // - name
         // - email
         // - subject
         // - text
         
$oFD = new oFormData();
         
$oFD->set('RecipientID',$this->_LOCALS['Empfaenger']);
         
$oFD->set('SenderName',$this->_LOCALS['AbsenderName']);
         
$oFD->set('SenderEMail',$this->_LOCALS['AbsenderAdresse']);
         
$oFD->set('Subject',$this->_LOCALS['Betreff']);
         
$oFD->set('Text',$this->_LOCALS['Text']);

         
// submit form
         
$cM = &$this->__getServiceObject('modules::kontakt4::biz','contactManager');
         
$cM->sendContactForm($oFD);

       
// end if
      
}
      else{
         
$this->setPlaceHolder('Inhalt',$this->__buildForm());
       
// end else
      
}

    
// end function
   
}

   function 
__buildForm(){

      
// get the form object
      
$Form__Kontakt = &$this->__getForm('Kontakt');

      
// set action attribute
      
$Form__Kontakt->setAttribute('action',$_SERVER['REQUEST_URI']);

      
// sign and format button
      
$Config = &$this->__getConfiguration('modules::kontakt4','language');
      
$Button = &$Form__Kontakt->getFormElementByName('KontaktSenden');
      
$Button->setAttribute('value',$Config->getValue($this->__Language,'form.button'));
      
$Button->setAttribute('style',$Config->getValue($this->__Language,'form.button.style'));

      
// insert image link for validator group
      
$Config = &$this->__getConfiguration('tools::form::taglib','formconfig');
      
$ValGroup = &$Form__Kontakt->getFormElementByName('FormValGroup');
      
$ValGroup->setPlaceHolder('WarnImage',$Config->getValue($this->__Language,'Contact.Warning.Image'));
      
$ValGroup->setPlaceHolder('WarnText',$Config->getValue($this->__Language,'Contact.Warning.Text'));

      
// get select field
      
$Recipients = & $Form__Kontakt->getFormElementByName('Empfaenger');

      
// load recipient list
      
$cM = &$this->__getServiceObject('modules::kontakt4::biz','contactManager');
      
$RecipientList $cM->loadRecipients();

      for(
$i 0$i count($RecipientList); $i++){
         
$Recipients->addOption($RecipientList[$i]->get('Name'),$RecipientList[$i]->get('oID'));
       
// end if
      
}

      
// transform form
      
return $Form__Kontakt->transformForm();

    
// end function
   
}

 
// end class
Taking a closer look to the source code, you can see, that the controller class uses two more components: the variablenHandler and the contactManager. The first class is a class integrated in the framework, that registers request variables in a locally used array and fills this offsets with default values if not present in the request. Details on this class can be seen on the class reference page. The second component is the business layer class of the contact form. This class provides interfaces to load recipients or to submit the form.


3.4.1. Method transformContent()
The method transformContent() takes over the generation of the view. For this reason a reference on the form object is fetched to check, wheather the form is sent and filled correctly. If not, the form is displayed in the current view. Is the user input considered ok, a form data domain object is created and filled. After that the business component is created by use of the __getServiceObject() method. The function sendContactForm() called with the domain object oFormData as an argument then sends the data of the form via mail. The submission and the redirection to the thank-you-message is completely done by the business layer.


3.4.2. Method __buildForm()
The function __buildForm() is often called a "helper function", due to the fact that this private method only encapsulates the form generation. __buildForm() is in charge of these three tasks: at first, the action attribute of the form must be filled with a URL fitting the module's URL structure, second the URL of the validator group's warning image must be set and last but not least the select field must be filled with recipients. Moreover, the button of the form must be labeled with a language dependent text.

Set the action attribute:
As each object inherits from "coreObject", the form method features the function setAttribute(). With aid of this method the attribut action can be set.

Configuration of the validator group:
To set the validator group's place holder "WarnBild" and "WarnText" the programmer at first gets a reference on the validator group. Within the internal DOM the validator group is a child object of the form DOM node. In order to get a reference the developer needs to have a valid pointer on the form object. The method getFormElementByName() and getFormElementByID() is intended to return a reference on a form child object identified by name or ID. The validator group class offers the setPlaceHolder() function, concerning the API documentation. This method can be taken to set the place holder of the validator group.

In case of the place holders "WarnImage" and "WarnText" the language dependent configuration file is used. The image and the corresponding text are located in thge configuration file
  /config/tools/form/taglib/{CONTEXT}/{ENVIRONMENT}_formconfig.ini
in the section [en] (for englisch language) and the under the configuration key
  Contact.Warning.Image
To get the lanuage dependent value out of the configuration file the section is choosen by the internal member $this->__Language, that allways contains the current language selected on page controller or front controller startup or during front controller action execution. The member variable contains a two letter ISO country code. Within the current document controller this task is done by the following source code:
   $Config = &$this->__getConfiguration('tools::form::taglib','formconfig');
   
$ValGroup = &$Form__Kontakt->getFormElementByName('FormValGroup');
   
$ValGroup->setPlaceHolder('WarnImage',$Config->getValue($this->__Language,'Contact.Warning.Image'));
   
$ValGroup->setPlaceHolder('WarnText',$Config->getValue($this->__Language,'Contact.Warning.Text')); 

At the end of the method the form object is transformed and returnd to the calling method.


3.5. Class contactManager

The contactManager class is an implementation of the business layer. It encapsulates the business logic of the application and communicates with lower tiers. In this case the business layer is in charge of sending the form or loading recipients. Therefore the current layer defines two API methods: sendContactForm() to send the form and loadRecipients() to provide a list of recipients. The interface between the presentation and business layer is formed by the two domain objects oFormData and oRecipient.
import('modules::kontakt4::biz','oFormData');
import('modules::kontakt4::biz','oRecipient');
import('modules::kontakt4::data','contactMapper');
import('tools::mail','mailSender');
import('tools::link','linkHandler');

class 
contactManager extends coreObject
{

   function 
contactManager(){
   }

   function 
sendContactForm($oFD){

      
// get contactMapper
      
$cM = &$this->__getServiceObject('modules::kontakt4::data','contactMapper');


      
// get mail sender
      
$MAIL = &$this->__getAndInitServiceObject('tools::mail','mailSender','Kontaktformular');


      
// fill recipient
      
$Recipient $cM->loadRecipientPerId($oFD->get('RecipientID'));
      
$MAIL->setRecipient($Recipient->get('Adresse'),$Recipient->get('Name'));

      
// add text
      
$Text 'Dear sir or madame,';
      
$Text .= "\n\n";
      
$Text .= $oFD->get('SenderName').' (E-Mail: '.$oFD->get('SenderEMail').') has sent the following notice via the contact form:';
      
$Text .= "\n\n\n";
      
$Text .= $oFD->get('Text');
      
$MAIL->setContent($Text);

      
// add subject
      
$MAIL->setSubject($oFD->get('Subject'));

      
// send mail
      
$MAIL->sendMail();


      
// setup new mail
      
$MAIL->clearRecipients();
      
$MAIL->clearCCRecipients();
      
$MAIL->clearContent();

      
// set recipient
      
$MAIL->setRecipient($oFD->get('SenderEMail'),$oFD->get('SenderName'));

      
// add text
      
$Text 'Dear sir or madame,';
      
$Text .= "\n\n";
      
$Text .= 'your request was forwared to "'.$Recipient->get('Name').'". We will send you an answer as soon as possible!';
      
$Text .= "\n\n";
      
$Text .= 'Your message:';
      
$Text .= "\n";
      
$Text .= $oFD->get('Text');
      
$MAIL->setContent($Text);

      
// add subject
      
$MAIL->setSubject($oFD->get('Subject'));

      
// send mail
      
$MAIL->sendMail();


      
// redirect to thanks page
      
$Link linkHandler::generateLink($_SERVER['REQUEST_URI'],array('pagepart' => 'meldung'));

      
$Reg = &Singleton::getInstance('Registry');
      
$URLRewriting $Reg->retrieve('apf::core','URLRewriting');

      if(
$URLRewriting != true){
         
$Link str_replace('&amp;','&',$Link);
       
// end if
      
}

      
header('Location: '.$Link);

    
// end function
   
}

   function 
loadRecipients(){
      
$cM = & $this->__getServiceObject('modules::kontakt4::data','contactMapper');
      return 
$cM->loadRecipients();
    
// end function
   
}

 
// end class
In the first two lines twoe more framework components are included to assist mail delivery (mailSender) and link generation (linkHandler).
Within the method sendContactForm() two service layers are instanciated, that are necessary for the delivery of the form data: the data layer to load the choosen recipient object and the mailSender to send confirmation and notification mails.
The function loadRecipients() is a service function for the presentation layer, that loads recipient domain objects.


4. Class contactMapper

The contactMapper is a data mapper pattern implementation. It is used as a data provider by the business layer. For this reason the mapper loads the configuraton file, that contains the recipients and maps this data into recipient domain object (oRecipient).
import('modules::kontakt4::biz','oFormData');
import('modules::kontakt4::biz','oRecipient');

class 
contactMapper extends coreObject
{

   function 
contactMapper(){
   }

   function 
loadRecipients(){

      
// read config
      
$Config $this->__getConfiguration('modules::kontakt4','empfaenger');


      
// read relevant section
      
$Sections $Config->getConfiguration();


      
// initialize return array
      
$Recipients = array();


      
// parse config and generate recipients
      
foreach($Config->getConfiguration() as $Key => $Values){

         
$Count count($Recipients);
         
$Recipients[$Count] = new oRecipient();

         
// id
         
preg_match("/Kontakt ([0-9]+)/i",$Key,$Matches);
         
$Recipients[$Count]->set('oID',$Matches[1]);

         
// name
         
$Recipients[$Count]->set('Name',$Values['EmpfaengerName']);

         
// email
         
$Recipients[$Count]->set('Adresse',$Values['EmpfaengerAdresse']);

       
// end foreach
      
}


      
// return recipient list
      
return $Recipients;

    
// end function
   
}

   function 
loadRecipientPerId($Id){

      
$Rec $this->loadRecipients();

      if(!
is_array($Rec)){
         return array();
       
// end if
      
}
      else{

         for(
$i 0$i count($Rec); $i++){
            if(
$Rec[$i]->get('oID') == $Id){
               return 
$Rec[$i];
             
// end if
            
}
          
// end for
         
}

       
// end else
      
}

    
// end function
   
}

 
// end class
The configuration file itself is separated into several sections:
[Kontakt ([0-9]+)]
EmpfaengerName = "([A-Za-z0-9,.-_ ]+)"
EmpfaengerAdresse = "([A-Za-z0-9.-_@]+)"
Each section is mapped into on domain object (oRecipient). loadRecipients() returns a list if oRecipient objects. Every recipient can be identified by an unique ID (see section name) and can thus the loaded by the method loadRecipientById().
If you intend to read the recipients from a database instead of a configuration file, only the implementation of the data layer methods must be changed to read the domain objects from a database.


Comments

Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.


There are no comments belonging to this article.


Powered by WebRing.