Я отредактировал стандартную контактную форму в SocialEngine. Добавлены новые 4 поля — дата рождения, адрес, страна & поле почтового индекса.
Первый вопрос — как правильно установить значение по умолчанию Country Select = «USA»?
Второй вопрос — как получить опубликованные данные из новых полей формы (Дата рождения, адрес, страна & почтовый индекс) на электронную почту пользователя? Как нужно редактировать мой контроллер?
Мой код:
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', 'name' => 'birthdate'));
$this->addElement('Text', 'address', array('label' => 'Address:',
'class' => 'address_class', 'name' => 'address'));
$this->addElement('Select', 'country', array('label' => 'Country:',
'class' => 'country_class', 'name' => 'country')); //->setValue("USA");
$this->addElement('Text', 'postal', array('label' => 'Postal Code:',
'class' => 'postal_class', 'name' => 'postal'));
$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',
'name' => 'submitted'
));
}
}
контроллер:
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->getRequest()->getPost('birthdate'); // Empty set
$address = $this->getRequest()->getPost('adress'); // Empty set
$country = "USA"; // Works
$postal = $this->getRequest()->getPost('postal'); // Empty set
$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." ".$birthDate." ".$address." ".$country." ".$postal,
);
// send email
Engine_Api::_()->getApi('mail', 'core')->sendSystem(
$email,
'pagecontact_template',
$mail_settings
);
};
}
}
Евгений,
В вашей форме нет поля страны. Но вы можете сделать это в контроллере, где вы вызываете функцию формы, как.
{
$form->getElement('country')->setvalue('USA');
}
Пожалуйста, не стесняйтесь задавать свои вопросы.
Других решений пока нет …