Задать вопрос
eprivalov
@eprivalov
Учусь

Как переделать форму в SocialEngine (Zend Framework) на email?

Задача такая - добавить несколько полей к существующей форме и при нажатии на кнопку отправлять ее на электронную почту.

Вот форма с уже добавленными полями:

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'
    ));

  }
}


А вот ее контроллер:

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
      );
    }
  }


Как мне нужно дополнить контроллер, чтобы получить все поля формы на почту? Заранее спасибо за помощь.
  • Вопрос задан
  • 306 просмотров
Подписаться 2 Оценить Комментировать
Решения вопроса 1
27cm
@27cm
TODO: Написать статус
Если я правильно понял, попробуйте так:
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
      );
    }
  }
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы