Save associated Model data before Form submission - cakephp

I have a polymorphic attachment model between Article and Image (alias for Attachment model) ala https://github.com/josegonzalez/upload. Article hasMany Image. Image is an Attachment.
When the form is submitted via the Article form's save button, everything saves properly, including associated models.
However, I have set up a dynamic display of attached images so that the user can choose which one to use for the article's preview before saving the article. The dynamic image display works (proved by deleting row in phpMyAdmin), but I cannot figure out how to upload and save the associated Image that has been selected without pressing the submit button. I'd like to upload the image immediately upon selection (or upon a button press that does not submit the article, too!
Here is the relevant form section, and the script that eventually results in the display of all associated Images.
<div id='imagesControl'>
<?php
//TODO gotta make this happen live
echo $this->Form->input('Image.0.attachment', array('type' => 'file', 'label' => 'Image'));
echo $this->Form->input('Image.0.model', array('type' => 'hidden', 'value' => 'Content'));
echo $this->Form->input('Image.0.foreign_key', array('type' => 'hidden', 'value' => $id));
echo $this->Form->input('Image.0.id', array('type' => 'hidden')); //Thought this would help
?>
<div id='attachedImages'>
</div> </div>
<script>
$(document).ready(function() {
$('#imagesControl').change(function() {window.alert("sometext");
$("#attachedImages").load('../imageDisplay/<?php echo $id ?> #attachedImages',
{id: $(this).attr('id')});
})
.change();
});
</script>
Notably, in the view file '../imageDisplay' that gets rendered, $this->data only contains the div id sent via $(this).attr('id'). I cannot find a variable, anywhere, that contains the file I chose in the input section before the form saves (the file I'm trying to upload and save). It's apparently there, because it does save using $this->Article->saveAll() automagic.
However, I cannot even begin to guess where / when / how I can upload the image and add a row to the attachments table containing the new image's data without submitting the article. I've been stuck for days.

Related

Uploading multiple pictures from the same form in CakePHP 1.2

I have a one-to-many relationship where one House can have many Pictures. The models that I have are "House" and "Picture". I am using CakePHP 1.2 and I can upload one picture successfully by using this:
echo $form->input('Picture.filename', array('type' => 'file', 'label' => __l('Image')));
In my houses_controller.php file, I have this code to save the picture and corresponding association with its house:
$this->House->Picture->save($this->data['Picture']);
But I need to save several pictures now. I read the documentation at https://book.cakephp.org/1.2/en/The-Manual/Core-Helpers/Form.html and based on that information, I am trying to use this:
echo $form->input('Picture.0.filename', array('type' => 'file', 'label' => __l('Image 1'));
echo $form->input('Picture.1.filename', array('type' => 'file', 'label' => __l('Image 2'));
Then my houses_controller.php file has this:
$this->House->Picture->saveAll($this->data['Picture']);
I see that one new record is saved in my pictures table, but I was expecting to see two new entries in my table because I should have added two pictures, not only one. Any ideas about why this may not be saving two pictures for me? Thank you.
Solution:
$this->Deal->Picture->save($this->data['Picture'][0]);
$this->Deal->Picture->save($this->data['Picture'][1]);
Using $this->data['Picture'] was not sufficient because the array was returning multiple elements, so I had to reference each with the corresponding index such as $this->data['Picture'][0] for the first element, $this->data['Picture'][1] for the second one, etc.
NOTE: It was not saving two records for me, even thought I was using save() twice. But that was another issue. You can read the answers at Save multiple times in Cakephp for the corresponding solution. Basically you need to use create() before you use save().

Send image email cakephp without storing it

I'm using Cakephp 3 with Cake\Network\Email\Email.
I have a form to send a message with two input fields. Those fields are stored in database.
I'd like to join an image to this form without storing it: only in attachment.
Here is my input file :
<?php echo $this->Form->file('photo', ['class' => 'form-control','value'=>'','accept'=>'image/*']); ?>
My Controller :
$Email = new Email('default');
$Email->theme('Front')
->template('my_template1')
->viewVars(['sender'=> $user, 'recipes'=> $recipes])
->attachments([$this->request->data['photo'] => $this->request->data['photo']['tmp_name']])
->emailFormat('html')
->to('xx#xx.com')
->from($user['email'])
->send();
Text inputs are well stored and sent. But no image attached in my email...
What's wrong?
Thanks!
I was stuck here to i got it, in your create form add
'enctype'=>'multipart/form-data'
So it should be
echo $this->Form->create($email, ['enctype'=>'multipart/form-data']);
This is added in the other controller by default but when you use a model less form it doesn't do this.

blackhole cakephp 2 associated entities

My Goal:
Reuse of a contact form to be related to several different entities I call "Parents" ie Group has contact information, Member has contact info etc....
The way I tried doing it was:
1. Creating a view file for contact, named "form.ctp" which doesn`t create a new form, nor submits, just echo's the contact's fields.
2. Calling this file using requestAction
My Problem:
The form's _Token get crumbled.
Parent add.ctp example
<?php echo $this->Form->create('Group');?>
<fieldset>
echo $this->Form->input($field_prefix.'contact_id',array('type'=>'hidden'));
<?php echo $this->requestAction(array('controller' => 'contacts', 'action' => 'form'), array('named' => array('index'=>'0','parent'=>'Group',
'fields'=>array(
'email'=>array('value'=>'xx#yy.com','hidden'=>1)
))));
inside the form.ctp I have:
//Associated Model
echo $this->Form->input('Contact.0.city',array('type'=>'hidden'));
echo $this->Form->input('Contact.0.postcode');
echo $this->Form->input('Contact.0.phone');
echo $this->Form->input('Contact.0.cellphone');
echo $this->Form->input('Contact.0.email',array('value'=>""));
echo $this->Form->input('Contact.0.id',array('type'=>'hidden'));
?>
Looking at the HTML source code that is generated, I see that whether I use the request action or just copy the contect of the form.ctp into the "Parent's" add file, I get the same HTML result.
HOWEVER!!! when I use the form.ctp Action Request, I get the blackhole, the tokens are being messed up!!!
Any Ideas?
Thanks in advance
Orly
If your problem is solely reusing a form, you can use the form as a Element, and then you could call it multiple times, substituting in the exact values you need.
As for SecurityComponent, I would recommend (at least as a temporary fix) disabling SecurityComponent for that specific action by using $this->Security->unlockedActions(); in your controller's beforeFilter()

Cakephp sizing a new window link

Hi all creating a link that when you click it, it opens a new window. I was wondering if it's possible to restrict the size of the window opening?
this is the code I have for the link that opens the window
<?php echo $this->Html->link(
$this->Html->image($help, array(
"alt" => "eBox")),
'/accounts/help', array('target'=>'_blank', 'escape'=>false)); ?>
You can do this, but only with Javascript.
Raw code would be something like:
Text Link
The only parameters you really need are width and height, but you might find the others useful too. If not, just delete them. If the user doesn't have javascript, it'll just open the link normally.
To do it with html helper, pass your javascript through to the onClick array key:
<?php echo $this->Html->link(
$this->Html->image($help, array(
"alt" => "eBox")),
'/accounts/help', array(
'target'=>'_blank',
'escape'=>false,
'onClick'=>"window.open('/pages/home', 'windowname','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=300,height=290'); return false;"
)); ?>
PS - you can read up on the window.open method here: http://www.w3schools.com/jsref/met_win_open.asp

Checking boxes with associated with HABTM in CakePHP

So, I have models associated via HABTM. In one view, I'm using checkboxes to save associations. I can save data no problem. But what I can't determine is how cleanly check boxes of pre-existing associations, say, when I go onto an Edit page and want to edit associations.
In this case, I have a User model and a Survey model.
Here's the code for my view:
foreach ($users as $user) {
echo $this->Form->input('User.User.', array('type' => 'checkbox', 'value' => $user['User']['id'], 'hiddenField' => false, 'label' => false )) . ' ' . $user['User']['username'];
}
There must be a way to simply mark boxes as selected where appropriate.

Resources