can't get image name in cakephp edit.ctp file - cakephp

$image_name = $this->data['ApkCollection']['apk_image']['name'];
echo $image_name; // it show result as number like 7 or 3 etc...
when am upload image in edit.ctp file means it show image name as above result, but it works fine on add.ctp file
Someone help me...

Is your code from the controller or view (edit.ctp)? If you're inspecting the posted data in the controller, what does this show?
pr($this->request->data['ApkCollection']['apk_image'])
If it's in the view, wouldn't it just be this?
$image_name = $this->data['ApkCollection']['apk_image'];
echo $image_name;
The file details array (that 'name' is part of) only exists as the result of a post, I think.
Toby

Related

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.

Dynamic Header Line with CakePHP

I have a cakePHP application with a header line. This header contains things like the name of the owner, phone number, email, ...
The owner can change this information, so this header has to be dynamic.
Is it possible to include this header into the layout, so I don't have to include it in every view?
thx
EDIT:
I see, I didn't describe my problem right.
The informations in the header aren't user informations. There should be informations like the name of the owner of the website (or companies name), phone number of the owner, and so on. These informations are the same for all logged in users.
Thanks anyway. The problem seems to be solved in an other Question
yes
assumning the User data is stored in session you can - in your layout - do something like
<div id="#header">
<?php
echo $this->Session->read('Auth.User.name');
// echo other User information
?>
</div>
User can edit his information even after logged in. in that case session data won't be helpful in displaying correct information.
For displaying dynamic data
so better you can set the information from controller and display it in view as like this
Controller
public function beforeFilter() {
parent::beforeFilter();
if ($this->Auth->user('id')) {
$this->loadModel('User');
$this->set('user', $this->User->findById($this->Auth->user('id')));
}
}
View
<div class='header'>
<?php
if (!empty($user)) {
echo $user['User']['name']; // display the info whatever you want..
}
?>
</div>

how to make file upload mandatory yii

im a bit new to this,
I have a model with an attribute file_id but it'll be displayed as File. I should make this mandatory..but when user uploads a file, I'm storing the file path and all in file table. and getting id from that table and storing it in my model.
can anyone pls tell me how can i make file upload mandatory for create but not for update
my code for file upload is:
<?php echo $form->labelEx($model,'file'); ?>
<?php echo $form->fileField($model, 'file');?>
<?php echo $form->error($model,'file'); ?>`
its working fine. but i need to make it mandatory for this current model. do i need to make it required in rules function in file model or current model??
Thanks,
The easiest way would be in the model rules:
array('file', 'required','on'=>'insert'),
Then it is only required on insert and not updates.
Another way would be to utilize the before or afterValidate method:
protected function afterValidate(){
if($this->isNewRecord){
if(!isset($this->file)){ //Not sure about handling files if this works.
$this->addError("file","Not file selected. Please choose a file to upload.");
return false;
}
}
return parent::afterValidate();
}

Multiple models and uploading files

At first - I'm new to the Yii Framework. I did some research on my own but I couldn't find a precise solution to my issue.
Assume there are two related models - Product and Image. A single Product may have multiple Images assigned. What is the best approach at creating the create / update forms that would be able to manage this kind of scheme?
The Image model consists of various fields, along with a path to the image file, so it's not just a "container" for the path itself. What's more - I need to have a thumbnail generated for every uploaded image and its path stored within the same model.
What I need to achieve is pretty much similar to the admin inline functionality known from Django - there should be a section in the Product create / update form which would allow users to add / modify / delete Images.
I tried the multimodelform extension but I couldn't get file uploading to work. What's the best way of getting it done and not having to build the whole file-upload-enabled-multiple-model-form structure manually?
The detailed solution depends on if you are using CActiveForm or CHtml form. Since you have 2 related models I assume you are using CActiveForm and will point out some thing you need to keep in mind.
For this example i am gonna assume some definitions
Product with fields id, name
Product with ONE to MANY relation to 'images' on ProductImage
ProductImage with fields id, productId, path
I also assume there gonna be 1 upload / edit, but multi delete
Here's the view:
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
echo $form->labelEx($product, 'name');
echo $form->fileField($product, 'name');
echo $form->error($product, 'name');
echo $form->checkBoxList($product, 'path', $product->images);
echo $form->labelEx($productImage, 'path');
echo $form->fileField($productImage, 'path');
echo $form->error($productImage, 'path');
$this->endWidget();
And your action
public function actionUpdate($productId) {
$product = Product::model()->findByPk($productId)->with('images');
$productImage = new ProductImage();
if(isset($_POST['Item']))
{
$product->attributes=$_POST['Product'];
foreach($product->images as $im) {
if(in_array($im->path, $_POST['Item']['Image']))
$im->delete();
}
$productImage->image=CUploadedFile::getInstance($productImage,'path');
if($productImage->save())
{
$productImage->image->saveAs('some/new/path');
// redirect to success page
}
}
$this->render('update', array(
'product'=>$product,
'productImage'=>$productImage,
));
}
Now note that this solution is not tested so there will be bugs, but it should give you an idea on how to write your own form.
Resources:
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
http://www.yiiframework.com/wiki/384/creating-and-updating-model-and-its-related-models-in-one-form-inc-image

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()

Resources