CakePHP Media Plugin not uploading - cakephp

I am trying to learn about David Persson's CakePHP Media Plugin through the included tutorial.
I get to the point where I can debug $this->data and it shows the 'file' array with the plugin's added keys (name, size and type) and I've added var $actsAs = array('Media.Transfer'); to the model of my choice. The problem is, at this point when I submit the form, it should be uploading the file to the app/webroot/media/transfers, but it is not doing that.
Could the problem be that I haven't added any controller code or is that taken care of by asserting that var $actsAs = array('Media.Transfer');? Any other ideas? Do you need to see any code to diagnose the problem?

You need to also use the Media.Coupler behavior.
var $actsAs = array('Media.Transfer', 'Media.Coupler');

Related

CakePHP default layout for xlsx

I have a method which exports riport data into an xlsx file in my CakePHP 2 project. It is working well. In the same time it seems it just ignores anything what I put into View/Layouts/xlsx/default.ctp
In my routes.php I have
Router::parseExtensions('json', 'xlsx');
In my controller I have
public $components = array('RequestHandler');
My View/Riports/xlsx/export.ctp is rendered, but View/Layouts/xlsx/default.ctp is ignored.
What do I miss?
I think you're out of luck here. From the documentation;
The data view classes don’t support layouts. They assume that the view file will output the serialized content.
http://book.cakephp.org/2.0/en/views/json-and-xml-views.html#using-a-data-view-with-view-files
But I may be wrong of course :)

How to declare two $actsAs variable instances when using multiple plugins in Cake 2.x

Iam using both the 'cakedc search' and 'meioupload' plugins, I currently load them using:
CakePlugin::load('MeioUpload');
CakePlugin::load('Search'); //in the bootstrap.php file.
I need to be able to make one model use both of these plugins simultaneously, can anyone suggest a way to declare the $actsAs variable twice in the same model, to avoid getting the follwing error in the 'error.log' file:
Error: Fatal Error (64): Cannot redeclare Event::$actsAs
Thanks in advance.
you can use as many behaviors per model as you want:
public $actsAs = array('Behavior1', 'PluginName.Behavior2', ...);
I don't quite see what your problem is.

Embed a Cakephp form into Wordpress

I have a Wordpress blog and I'm developing a backend application using Cakephp.
I want to develop the blog's contact form using cake (since the entered information will be available from the backend app).
Right now, I tried including the cake view into wp using ajax. The problem with this approach is that I either use a Js->submit, which makes attaching files to the form quite complicated, or I use a Form->submit, which makes displaying validation errors problematic. Also, it creates problems with the recaptcha plugin not showing up.
Is there a way of integrating the form using php? I don't need authentication (it is a public form), but I need to be able to show validation errors on the form and upload files on the form.
Actually, you can load any website into a string using CURL, if you load en empty page with a placeholder from your wordpress, you can then use it as your layout
<?php
$url = "http://www.yourdomain.com/emptypage";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl)
$placeholder = "{{CONTENT}}"
$placeholderPos = strpos($output,$placeholder);
$beginning = substr($output,0,$placeholderPos);
$end = substr($output,$placeholderPos+strlen($placeholder));
echo $beginning;
////// your form //////
echo $end;
?>
You may have to deal with relative path after that, but this should get you started.
So, I finally found a way I think is the best since it uses cake classes.
I created a file new_file.php on webroot that is basically a copy of index.php but instead of using a generic request it requests the specific page I'm looking for:
$Dispatcher = new Dispatcher();
$Dispatcher->dispatch(new CakeRequest('CONTROLLER/ACTION'), new CakeResponse(array('charset' => Configure::read('App.encoding'))));
Of course, you'll have to change 'CONTROLLER/ACTION' to whatever your controller is.
After that, you only have to include the file in a WordPress plugin.
There is only one more change. There are some methods that conflict with WP declarations. PHP will complain when you include the above file. You have to find those method and wrap them into an if to make sure the method is not redeclared. This can break some functionality like localization (function __()) but it is ok if what you are including is not a very complex cake application.
if (!function_exists('methodName')) {
Hopefully the last issue will be solved once Cake 3 is out with namespaces support.

CakePHP Media Plugin Helper problem

I am trying to display an uploaded image with the Media Plugin of CakePHP.
I added the helper to the controller helper array: var $helpers = array('Media.Media');. Then, in my view, I have this code: echo $media−>file($news['Attachment'][0]['dirname'].DS.$news['Attachment'][0]['basename']);. But the problem is that, it outputs this error:
Undefined variable: media− [APP/views/news/view.ctp, line 3]
What could be the problem?
By the way, if a plugin has a model User in app/plugin/users/models/user.php and i create a new model called User in the app/models folder which one will be loaded?
Thanks in advance for any help!
First off if you are using 1.3.x refer to helpers via $this->HelperName->method(), there could be a variable called $media being set in some method. you can check this by doing var_dump($media);
The other option is that something has maybe unset it. Its very strange that you have the helper set but the variable is not set. It could also be due to adding the $helpers array to the wrong controller, you can try add it to app_controller and see if that works. if it does you had it in the wrong place.
If i got your second question correct, and we are talking about auto loading, a plugin controller will first look for the model in its own plugin directory, if it is not found there it will fall back to the app/models directory.
if you are loading it manually via the $uses array, it depends on the version of cake and how you do it. In previous versions 1.x even $uses = array('User'); would load the plugin model as cake would auto add the plugin prefix. This has changed for 2.0 afaik.
For other methods of loading a model, such as $this->loadModel('User); would load from app/models and $this->loadModel('PluginName.User') would load from the app/plugins/plugin_name/models dir.
Edit:
you are right that is funny having the error show $media- and there is the problem. did you copy that code from some site? − is not - you have a utf8 char in the code which is what its complaining about.

How to do form-based file uploads in CakePHP?

I have been looking into this for a while and can't figure it out. Basically I have an add page for my model which you can add a map from a URL or from a file upload. I have got all the fields and validation in but how and where do I manage the uploaded file?? There must be some easy way to do this. Thanks!
Firstly your form needs to be set up to allow file uploads.
<?php echo $form->create(Model, array('type' => 'file')); ?>
This will allow any file inputs to actually upload the file to your server with $form->file(field) or $form->input(field, array('type' => 'file')).
Once the file has been uploaded you should handle everything else from within the Model:
function beforeSave($created) {
extract($this->data[Model][field]);
if ($size && !$error) {
move_uploaded_file($tmp_name, destination);
$this->data[Model][field] = destination;
}
return true;
}
These are only the basics, so be sure to have a play around to find the solution that best fits your needs.
You can use Zend Components to handle the file upload. There is a good example here on my website:
CakePHP file upload using Zend Components
NOTE: MeioUploadBehavior has been deprecated. Instead jrbasso suggests the Upload Plugin.
In addition to the fine answers already given I want to hint about MeioUploadBehavior, currently maintained by jrbasso at github, which has been a great help for me in my own CakePHP project.
You simply add the behavior to your model using the $actsAs field and at the same time specifying any custom preferences. Then create necessary fields (described by supplied docs in detail) in your database, or configure the model to not use any database table. Finally setup the form in your add page, also described in the supplied documentation. The behavior will then take care of the rest for you.

Resources