vendredi 27 février 2015

CakePHP sending HABTM checkboxes values through email

I am developing a Poll Form to be filled by our customers. In that form there are radio options, textboxes, and a group of checkboxes. I am only having trouble getting the values of the checkboxes being send through email. The checkboxes are displayed by using a HABTM relationship between the tables Polls and Listexcursions. I am using ViewVars like the following manner(PollsController.ctp):



public function add() {
App::uses('String', 'Utility');

if ($this->request->is(array('post', 'put'))) {
$this->Poll->create();
if ($this->Poll->saveAll($this->request->data)) {
$email = new CakeEmail(); // Create a new instance
$email->template('poll', 'default') //Use poll for the view (app/View/Emails/html/poll.ctp), and default for the layout (app/View/Layouts/Emails/html/default.ctp)
->emailFormat('html')
->from('no-reply@hovertours.com')
->to('google@hovertours.com')
->subject('Valoracion de Servicios - Encuesta')
->replyTo($this->request->data['Poll']['email'])
->viewVars(array(
'name' => $this->request->data['Poll']['name'],
'email' => $this->request->data['Poll']['email'],
'question01' => $this->request->data['Poll']['question01'],
'question02' => $this->request->data['Poll']['question02'],
'comments' => $this->request->data['Poll']['comments'],['comments04'],
'listexcursion' => $this->request->data['Listexcursion'],
))
->send();
return $this->redirect(array('action' => 'poll_received'));
} else {
$this->Session->setFlash(__('The poll could not be saved. Please, try again.'));
}
}

$listexcursions = $this->Poll->Listexcursion->find('list');
$this->set(compact('listexcursions'));
$this->layout = 'design';
}


My poll email layout (app/View/Emails/html/poll.ctp) is the following:



<table cellpadding=10 cellspacing="0" width="30%" border="1" style="border-color: #666;">
<tr>
<th width="40%">Excursiones</th>
<td>
<?php if (!empty($poll['Listexcursion'])): ?>
<?php foreach ($poll['Listexcursion'] as $listexcursion): ?>
<?php $listexcursions[] = $listexcursion['name'] ;?>
<?php endforeach; ?>
<?php echo String::toList($listexcursions, 'y '); ?>
<?php endif; ?>
<?php
echo "-----";
echo #String::toList($listexcursion);
print_r($listexcursion);
?>
</td></table>


My Model Poll.ctp is:



class Poll extends AppModel {
public $displayField = 'name';
public $hasAndBelongsToMany = array(
'Listexcursion' => array(
'className' => 'Listexcursion',
'joinTable' => 'polls_listexcursions',
'foreignKey' => 'poll_id',
'associationForeignKey' => 'listexcursion_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);

function beforeValidate($options = array()) {
if(!empty($this->request->data['Listexcursion'])) {
$this->data['Listexcursion'] = implode(',', $this->data['Listexcursion']);
}
return true;
}


I've tried using implode function in my email layout and in the model by using the beforeValidate function, but I get a blank results. I've used a foreach function as well. Most of the time I get a notice (8) array to string conversion error. I've been trying all kinds of solutions found here in SO but I can't get to the solution I need.


Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire