P
P
Petr Volkhanov2015-04-21 22:36:07
Zend Framework
Petr Volkhanov, 2015-04-21 22:36:07

How to convert form in SocialEngine (Zend Framework) to email?

The task is to add several fields to an existing form and, when you click on the button, send it to e-mail.
Here is the form with the fields already added:

class Pagecontact_Form_Contact extends Engine_Form
{
   private $page_id;

  public function __construct($page_id)
  {
    $this->page_id = $page_id;

    parent::__construct();
  }

  public function init()
  {
  	parent::init();
  	
    $this
      ->setTitle('Send Message to Apply for Sponsorship')
      ->setAttrib('id', 'page_edit_form_contact')
      ->setAttrib('class', 'global_form');

    $topicsTbl = Engine_Api::_()->getDbTable('topics', 'pagecontact');
    $topics = $topicsTbl->getTopics($this->page_id);

    $viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity();

    $options[0] = '';
    foreach($topics as $topic)
    {
      $options[$topic['topic_id']] = $topic['topic_name'];
    }
  
  $this->addElement('Date', 'birthdate', array('label' => 'Birthdate:',
      'class' => 'date_class'));

  $this->addElement('Text', 'address', array('label' => 'Address:',
      'class' => 'address_class'));

  $this->addElement('Select', 'country', array('label' => 'Country:',
      'class' => 'country_class'));

  $this->addElement('Text', 'postal', array('label' => 'Postal Code:',
      'class' => 'postal_class'));

    $this->addElement('Select', 'topic', array(
      'label' => 'PAGECONTACT_Topic',
      'class' => 'topic_class',
      'multiOptions' => $options,
    ));

    $this->getElement('topic')->getDecorator('label')->setOption('class','element_label_class topic_label_class');

    $this->addElement('Hidden', 'visitor', array(
        'value' => 0,
        'order' => 3,
    ));

    if ($viewer_id == 0)
    {
      $this->addElement('Text', 'sender_name', array(
        'label' => 'PAGECONTACT_Full name',
        'class' => 'subject_class',
        'allowEmpty' => false,
        'size' => 37,
        'validators' => array(
          array('NotEmpty', true),
          array('StringLength', false, array(1, 64)),
        ),
        'filters' => array(
          'StripTags',
          new Engine_Filter_Censor(),
          new Engine_Filter_EnableLinks(),
        ),
      ));

      $this->getElement('sender_name')->getDecorator('label')->setOption('class','element_label_class sender_name_label_class');

      $this->addElement('Text', 'sender_email', array(
        'label' => 'PAGECONTACT_Email',
        'class' => 'subject_class',
        'allowEmpty' => false,
        'size' => 37,
        'validators' => array(
          array('NotEmpty', true),
          array('StringLength', false, array(1, 64)),
        ),
        'filters' => array(
          'StripTags',
          new Engine_Filter_Censor(),
          new Engine_Filter_EnableLinks(),
        ),
      ));

      $this->getElement('sender_email')->getDecorator('label')->setOption('class','element_label_class sender_email_label_class');

      $this->addElement('Hidden', 'visitor', array(
        'value' => 1,
        'order' => 3,
      ));
    }

    $this->addElement('Text', 'subject', array(
      'label' => 'PAGECONTACT_Subject',
      'class' => 'subject_class',
      'allowEmpty' => false,
      'size' => 37,
      'validators' => array(
        array('NotEmpty', true),
        array('StringLength', false, array(1, 64)),
      ),
      'filters' => array(
        'StripTags',
        new Engine_Filter_Censor(),
        new Engine_Filter_EnableLinks(),
      ),
    ));

    $this->getElement('subject')->getDecorator('label')->setOption('class','element_label_class subject_label_class');

    $this->addElement('Textarea', 'message', array(
      'label' => 'PAGECONTACT_Message',
      'maxlength' => '512',
      'class' => 'message_class',
      'filters' => array(
        new Engine_Filter_Censor(),
        new Engine_Filter_Html(array('AllowedTags' => 'a'))
      ),
    ));

    $this->getElement('message')->getDecorator('label')->setOption('class','element_label_class message_label_class');

    $this->addElement('Hidden', 'page_id', array(
      'value' => $this->page_id,
      'order' => 7,
    ));


    $this->addElement('Button', 'send', array(
      		'label' => 'Send',
      'type' => 'button',
      'class' => 'btn_send_class'
    ));

  }
}

And here is its controller:
public function sendAction()
  {
    $page_id = $this->_getParam('page_id');
    $topic_id = $this->_getParam('topic_id');
    $subject = $this->_getParam('subject');
    $message = $this->_getParam('message');
    $senderName = $this->_getParam('sender_name');
    $senderEmail = $this->_getParam('sender_email');
    $birthDate = $this->_getParam('birthdate'); //
    $address = $this->_getParam('address'); //
    $country = $this->_getParam('country'); //
    $postal = $this->_getParam('postal'); //

    $pagesTbl = Engine_Api::_()->getDbTable('pages', 'page');
    $select = $pagesTbl->select()
        ->from(array($pagesTbl->info('name')), array('displayname'))
        ->where('page_id = ?', $page_id);
    $query = $select->query();
    $result = $query->fetchAll();
    $pageName = $result[0]['displayname'];

    $viewer = $this->_helper->api()->user()->getViewer();
    $user_id = $viewer->getIdentity();

    $topicsTbl = Engine_Api::_()->getDbTable('topics', 'pagecontact');
    $emails = $topicsTbl->getEmails($page_id, $topic_id);

    $i = 0;
    $emails = explode(',',$emails);

    foreach($emails as $email) {
      $emails[$i] = trim($email);
      $i++;
    }

    if ($user_id != 0) {
      $senderName = $viewer['displayname'];
      $senderEmail = $viewer['email'];
    }

    foreach($emails as $email) {
      // Make params
      $mail_settings = array(
        'date' => time(),
        'page_name' => $pageName,
        'sender_name' => $senderName,
        'sender_email' => $senderEmail,
        'subject' => $subject,
        'message' => $message,
      );

      // send email
      Engine_Api::_()->getApi('mail', 'core')->sendSystem(
        $email,
        'pagecontact_template',
        $mail_settings
      );
    }
  }

How do I need to add a controller to get all the form fields to the mail? Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-04-22
@eprivalov

If I understand correctly, try this:

public function sendAction()
  {
    $page_id = $this->_getParam('page_id');
    $topic_id = $this->_getParam('topic_id');
    $subject = $this->_getParam('subject');
    $message = $this->_getParam('message');
    $senderName = $this->_getParam('sender_name');
    $senderEmail = $this->_getParam('sender_email');
    $birthDate = $this->_getParam('birthdate'); //
    $address = $this->_getParam('address'); //
    $country = $this->_getParam('country'); //
    $postal = $this->_getParam('postal'); //

    $pagesTbl = Engine_Api::_()->getDbTable('pages', 'page');
    $select = $pagesTbl->select()
        ->from(array($pagesTbl->info('name')), array('displayname'))
        ->where('page_id = ?', $page_id);
    $query = $select->query();
    $result = $query->fetchAll();
    $pageName = $result[0]['displayname'];

    $viewer = $this->_helper->api()->user()->getViewer();
    $user_id = $viewer->getIdentity();

    $topicsTbl = Engine_Api::_()->getDbTable('topics', 'pagecontact');
    $emails = $topicsTbl->getEmails($page_id, $topic_id);

    $i = 0;
    $emails = explode(',',$emails);

    foreach($emails as $email) {
      $emails[$i] = trim($email);
      $i++;
    }

    if ($user_id != 0) {
      $senderName = $viewer['displayname'];
      $senderEmail = $viewer['email'];
    }

    $message .= "\r\nBirthdate: " . $birthDate 
             .  "\r\nAddress: " . $address 
             .  "\r\nCountry: " . $country 
             .  "\r\nPostal: " . $postal;
    foreach($emails as $email) {
      // Make params
      $mail_settings = array(
        'date' => time(),
        'page_name' => $pageName,
        'sender_name' => $senderName,
        'sender_email' => $senderEmail,
        'subject' => $subject,
        'message' => $message,
      );

      // send email
      Engine_Api::_()->getApi('mail', 'core')->sendSystem(
        $email,
        'pagecontact_template',
        $mail_settings
      );
    }
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question