This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » plugins » User management plugins » sfDoctrineApplyPlugin - Undocumented steps to make it work, and without Zend_Mail
sfDoctrineApplyPlugin - Undocumented steps to make it work, and without Zend_Mail [message #92774] Tue, 02 February 2010 20:41 Go to previous message
stephenrs  is currently offline stephenrs
Messages: 22
Registered: January 2010
Junior Member
I had some problems getting sfDoctrineApplyPlugin to work - mostly because of some gaps in the documentation and weirdness in the code - so I decided to document what I did to make it work, in case anyone else is having trouble. Also, this solution uses Symfony's bundled mailer (SwiftMailer), so you don't have to install anything from Zend...which seems to make a little more sense to me...

This solution has been tested with Symfony1.4.1 and sfDoctrineApplyPlugin1.1.0. I'm sure it can be improved further but this has worked for me:

1. Installation

Since the plugin's readme says much about how to install Zend_Mailer, and nothing about how to install the plugin itself (and I couldn't find any combination of ./symfony plugin:install... that would fetch the plugin and install it) - and if you're not used to installing plugins manually, try this:

- Download the plugin from the plugins page ( http://www.symfony-project.org/plugins/sfDoctrineApplyPlugin)

- Unpack the archive, and copy the plugin directory to your project's /plugins directory (I called mine sfDoctrineApplyPlugin, removing the version "-1.1.0" from the directory name)

- Important: add this line to the setup() method of your /config/ProjectConfiguration.class.php:

    $this->enablePlugins('sfDoctrineApplyPlugin');


2. Configuration

The readme will tell you to put the following in your doctrine/schema.yml:

sfGuardUserProfile:
  tableName: sf_guard_user_profile
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    user_id:
      type: integer(4)
      notnull: true
    email:
      type: string(80)
    fullname:
      type: string(80)
    validate:
      type: string(17)

...etc...


If you're using the latest version of sfDoctrineGuard, then the above entries in your schema.yml will produce SQL errors when you run your next symfony doctrine:build..., and your foreign key won't work. To fix this, replace the above with the following:

sfGuardUserProfile:
  tableName: sf_guard_user_profile
  columns:
    id:
      type: integer(20)
      primary: true
      autoincrement: true
    user_id:
      type: integer(20)
      notnull: true
    email:
      type: string(80)
    fullname:
      type: string(80)
    validate:
      type: string(17)


Notice the type definitions for id and user_id in the lines above. The user_id type is the most important, because in a foreign key relationship, the columns involved in the relation must have types that match exactly. The user(id) column of sf_guard_user is a BIGINT (integer(20)), so the foreign key user_id must also be a BIGINT. I've also made sf_guard_user_profile(id) an integer(20) so that my system can have as many profiles as users.

3. Getting rid of Zend_Mailer

To do this, you need to override 2 of sfApply's methods. Create a module called sfApply in your application, and put this in the .../modules/sfApply/actions/actions.class.php:

require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineApplyPlugin/modules/sfApply/lib/BasesfApplyActions.class.php');

class sfApplyActions extends BasesfApplyActions
{
  public function executeApply(sfRequest $request)
  {
    $this->form = $this->newForm('sfApplyApplyForm');
    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('sfApplyApply'));
      if ($this->form->isValid())
      {
        $guid = "n" . self::createGuid();
        $this->form->setValidate($guid);
        $this->form->save();
        try
        {
          $profile = $this->form->getObject();
          $this->mail(array('subject' => sfConfig::get('app_sfApplyPlugin_apply_subject',
              sfContext::getInstance()->getI18N()->__("Please verify your account on %1%", array('%1%' => $this->getRequest()->getHost()))),
            'fullname' => $profile->getFullname(),
            'email' => $profile->getEmail(),
            'parameters' => array('fullname' => $profile->getFullname(), 'validate' => $profile->getValidate()),
            'text' => 'sfApply/sendValidateNewText',
            'html' => 'sfApply/sendValidateNew'));
          return 'After';
        }
        catch (Exception $e)
        {
          //$mailer->disconnect();
          $profile = $this->form->getObject();
          $user = $profile->getUser();
          $user->delete();
            // You could re-throw $e here if you want to 
          // make it available for debugging purposes
          return 'MailerError';
        }
      }
    }
  }
  
  protected function mail($options) {
    $required = array('subject', 'parameters', 'email', 'fullname', 'html', 'text');
    foreach ($required as $option)
    {
      if (!isset($options[$option]))
      {
        throw new sfException("Required option $option not supplied to sfApply::mail");
      }
    }
    $message = $this->getMailer()->compose();
    $message->setSubject($options['subject']);
    
    // Render message parts
    $message->setBody($this->getPartial($options['html'], $options['parameters']), 'text/html');
    $message->addPart($this->getPartial($options['text'], $options['parameters']), 'text/plain');
    $address = $this->getFromAddress();
    $message->setFrom(array($address['email'] => $address['fullname']));
    $message->setTo(array($options['email'] => $options['fullname']));
    $this->getMailer()->send($message);
  }
}


This snippet overrides the mail() method to replace Zend_Mailer with the built-in SwifMailer.

It also overrides the executeApply method, simply commenting out the line that says:

$mailer->disconnect();


...because this line will always generate a PHP fatal error...$mailer is never defined...

You may also need to configure SwiftMailer to handle outbound mail, depending on your server environment. The JoeBeet tutorial (http://www.symfony-project.org/jobeet/1_4/Doctrine/en/16) is an excellent source of info on how to do this. For reference, I'm using a gmail account for sending mail, and the relevant section of my factories.yml file looks like this:

all:
  mailer:
    class: sfMailer
    param:
      logging:           %SF_LOGGING_ENABLED%
      charset:           %SF_CHARSET%
      delivery_strategy: realtime
      transport:
        class: Swift_SmtpTransport
        param:
          host:       smtp.gmail.com
          port:       465
          encryption: ssl
          username:   your.username@gmail.com
          password:   your.password


As I mentioned, this setup is working for me - so I hope this helps someone. Better yet, I hope the authors are inspired to update the docs, clean up the plugin, and remove the unnecessary dependency to Zend_Mailer.

Happy coding!

Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic:sfForkedDotrctineApplyPlugin is broken (latest version)
Next Topic:Customize sfGuard or use sfApply
Goto Forum:

  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software