Display a contact form on the site, manage entries on admin

The dmContactPlugin allows to display a typical contact form.
By default, contact requests sent with the form are stored in database.
The plugin packages a Diem front widget and an admin interface to manage contact requests.
Integrates recaptcha validation.

See it in action on the contact page

Installation

  • In a console, from your project root dir, run:
git clone git://github.com/ornicar/dmContactPlugin.git plugins/dmContactPlugin  
  • In config/ProjectConfiguration.class.php, add dmContactPlugin to the list of enabled plugins:
class ProjectConfiguration extends dmProjectConfiguration
{  
  public function setup()  
  {  
    parent::setup();  
 
    $this->enablePlugins(array(  
      // your enabled plugins  
      'dmContactPlugin'  
    ));  
  • In a console, from your project root dir, run:
php symfony doctrine:generate-migrations-diff  

php symfony doctrine:migrate  

php symfony dm:setup  

Display the form on the front

Open the front "Add" menu, and drag&drop a contact/form widget somewhere on your site.

Manage contact requests on admin

Open Content->Feedback->Contacts on admin menu.

Enable captcha validation

This step is facultative, but will help you fighting against spam.
Add this configuration in
apps/front/config/app.yml

all:  
  recaptcha:  
    # get your recaptcha keys on http://recaptcha.net/api/getkey  
    public_key:   6LeefAkAAAAAAF-this-is-a-dummy-public-key  
    private_key:  6LeefAkAAAAAA-this-is-a-dummy-private-key  
    enabled:      true  

You will need to get API keys from the recaptcha website. It's free.

Customize form style

You may just use CSS if the default outputed HTML looks fine to you.

Or you can override the form template by copying
dmContactPlugin/modules/dmContact/templates/_form.php
to
apps/front/modules/dmContact/templates/_form.php

Add fields to the form

The default form is quite basic. Let's say you wanna add a "subject" field.

Override the model schema

Add these lines in your project schema
config/doctrine/schema.yml

DmContact:  
  columns:  
    subject:          { type: string(255), notnull: true }  

Then run the required doctrine migrations and setup your project

php symfony doctrine:generate-migrations-diff  
  
php symfony doctrine:migrate  
  
php symfony dm:setup  

The DmContact model and form are now up to date and contain the subject field. We still have to display the on front and admin.

Update the front template

Copy
dmContactPlugin/modules/dmContact/templates/_form.php
to
apps/front/modules/dmContact/templates/_form.php

then add the "subject" field somewhere, for example just after the form tag.

echo $form->open();
 
echo $form['subject']->label()->field()->error();  

Update the admin interface

Copy
dmContactPlugin/modules/dmContactAdmin/config/generator.yml
to
apps/admin/modules/dmContactAdmin/config/generator.yml

and paste this configuration inside:

generator:  
  param:  
    config:  
      list:  
        display:  
          - '=name'  
          - subject  
          - email  
          - body  
          - created_at  
          - updated_at  
      form:  
        display:  
          NONE: [name, email]  
          Body: [subject, body]  

Learn more about symfony admin generator.

Customize model and form

you can override the DmContact model in your project
lib/model/doctrine/dmContactPlugin/DmContact.class.php

and the DmContactForm form in your project
lib/form/doctrine/dmContactPlugin/DmContactForm.class.php

Do something when a contact request is saved.

Listen to the 'dm_contact.saved' event in
apps/front/config/frontConfiguration.class.php

require_once(dm::getDir().'/dmFrontPlugin/lib/config/dmFrontApplicationConfiguration.php');
 
class frontConfiguration extends dmFrontApplicationConfiguration  
{  
  public function configure()  
  {      
    $this->dispatcher->connect('dm_contact.saved', array($this, 'listenToContactSavedEvent'));  
  }  
 
  public function listenToContactSavedEvent(sfEvent $e)  
  {  
    $contact = $e['contact'];  
    // do something with the freshly saved $contact  
  }  
}  
  • RenatoJuly 1, 2010 4:06 PM

    Hi, I get the error Could not generate migration classes from difference, when running doctrine:generate-migrations-diff

  • JérémyApril 13, 2011 1:49 PM

    It's normal, nothing has changed in your schema.yml

Add a comment

dmContactPlugin, created on January 25, 2010 by Thibault D, used by 1433 projects

Fork Diem on GitHub