On my files model:
var $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'filesize' => 5242880,
'required' => true
)
),
'Uploader.Attachment' => array(
'file' => array(
'uploadDir' => 'upload/', // Where to upload to, relative to app webroot
'dbColumn' => 'path', // The database column name to save the path to
'maxNameLength' => 30, // Max file name length
'overwrite' => true, // Overwrite file with same name if it exists
'name' => '', // The name to give the file (should be done right before a save)
'transforms' => array() // What transformations to do on images: scale, resize, etc
)
)
);
And on the controller:
$this->File->Behaviors->Attachment->update('File', 'file', array('name' => 'testing')));
if ($this->File->save($this->data)) {
The file is uploaded fine, and the record is saved on the database. But I wanted to rename the file to avoid people finding the archives by mistake.
Thanks!
I have run into this same problem. You can accomplish the same task by doing this before you save.
$this->data['File']['file']['name'] = 'myNewName';
Related
I am trying to implement Miles Johnsons excellent Uploader Component into my app. But here is the problem: Depending on the dimensions of the uploaded image, I need to change the resize dimensions.
I tried to modify the transformation in the callback:
public $actsAs = array(
'Uploader.Attachment' => array(
'image' => array(
'nameCallback' => 'formatName',
'tempDir' => TMP,
'uploadDir' => UPLOADDIR,
'finalPath' => '/img/photos/',
'overwrite' => false,
'stopSave' => true,
'allowEmpty' => false,
'transforms' => array(
array(
'class' => 'exif',
'self' => true
),
'image_sized' => array(
'class' => 'resize',
'nameCallback' => 'transformSizedNameCallback',
'width' => 1680,
'height' => 980,
'aspect' => true
)
)
)
),
'Uploader.FileValidation' => array(
'image' => array(
'extension' => array(
'value' => array('jpg', 'png', 'jpeg'),
'error' => 'Nicht unterstütztes Dateiformat - bitte JPG- oder PNG-Datei hochladen.'
),
'minHeight' => array(
'value' => 980,
'error' => 'Das Bild muss mindestens 980 Pixel hoch sein.'
),
'minWidth' => array(
'value' => 980,
'error' => 'Das Bild muss mindestens 980 Pixel breit sein.'
),
'required' => array(
'value' => true,
'error' => 'Bitte wählen Sie ein Bild aus.'
)
)
)
);
public function beforeTransform($options) {
if($options['dbColumn'] == 'image_sized') {
if($height > $width) {
$options['width'] = 980;
$options['height'] = 1680;
}
}
return $options;
}
I am able to pinpoint the correct transform, but how do I access the dimensions of the image to be transformed inside of beforeTransform? Where do I get $width and $height from?
I'm not familiar with it, but from looking at the code it seems like the only option you have at that point is using the dbColumn value to access the currently processed field data, something like
$file = $this->data[$this->alias][$options['dbColumn']];
Of course this requires the dbColumn value to match the input field name! If that's not the case, then you'll need an additional option that holds the field name and use that one instead.
Now $file is just the raw data, most probably a file upload array. Assuming a single file, check tmp_name for its dimensions, either by yourself, or utilize the Transite\File class which can handle file upload arrays and exposes a method for retrieving the dimensions of a possible image:
$transitFile = new File($file);
$dimensions = $transitFile->dimensions();
https://github.com/milesj/transit/blob/1.5.1/src/Transit/File.php#L121
So finally you could do something like this:
public function beforeTransform($options) {
if($options['dbColumn'] == 'image_sized') {
$file = $this->data[$this->alias][$options['dbColumn']];
$transitFile = new \Transit\File($file);
$dimensions = $transitFile->dimensions();
if($dimensions === null) {
// not an image or something else gone wrong,
// maybe throw an exception or wave your arms and call for help
} elseif($dimensions['height'] > $dimensions['width']) {
$options['width'] = 980;
$options['height'] = 1680;
}
}
return $options;
}
Please not that this is all untested example code.
I am trying to use CakePdf to generate a pdf file within CakePHP. I have a function in the CourseGradesController called viewReport. I want viewReport to allow you to select a student from a select field, and then upon submitting the form, it will generate a PDF with the appropriate data. If I put the data into a table and do not try to make a PDF, the page will display correctly, so I don't think that is the problem. In bootstrap.php, I have
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf',
'options' => array(
'print-media-type' => false,
'outline' => true,
'dpi' => 96
),
'margin' => array(
'bottom' => 15,
'left' => 50,
'right' => 30,
'top' => 45
),
'binary' => '/var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf',
'orientation' => 'landscape',
'download' => false
));
I have the WkHtmlToPdf folder moved into /var/www/cakephp/app/Plugin/CakePdf/Vendor/WkHtmlToPdf, so the lib and include folders are in that directory.
In the viewReport function of CourseGradesController, I have
function viewReport($id = null)
{
$this->CourseGrade->id = $id;
if (!$this->CourseGrade->exists())
{
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->CourseGrade->read(null, $id));
...
}
If I navigate to /courseGrades/viewReport, I get the error "Error: The requested address '/cakephp/courseGrades/viewReport' was not found on this server."
If I naviate to /courseGrades/viewReport/1.pdf, then I just see a completely blank screen.
I would strongly recommend the use of this plugin for this kind of work.
https://github.com/ceeram/CakePdf
Trying to get the CakePHP Upload Plug-in to work. The file uploads fine, the thumbnails are created...etc, but having a few issues:
-the 'name' field in the 'uploads' table is empty
-the 'upload' field in the 'uploads' table is empty ('attachment' field in the doc example)
-if I use {model} in the 'path' set in the Upload model, it uses 'upload' as the model folder - it should go in a folder of the associated model, not the upload model every time
//Upload MODEL
public $actsAs = array(
'Upload.Upload' => array(
'photo' => array(
'thumbnailSizes' => array(
'xvga' => '1024x768',
'vga' => '640x480',
'thumb' => '80x80',
),
'thumbnailMethod' => 'php',
'path' => '{ROOT}webroot{DS}uploads{DS}{model}{DS}{field}{DS}',
'maxSize' => '5242880', //5MB
'mimetypes' => array('image/jpeg', 'image/png', 'image/gif', 'image/bmp'),
'extensions' => array('jpg', 'gif', 'png'),
),
)
);
//ArticleData MODEL
public $hasMany = array(
'Upload' => array(
'className' => 'Upload',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Upload.model' => 'ArticleData',
),
),
);
//CONTROLLER
public function admin_upload() {
if(!empty($this->request->data)) {
$this->loadModel('Upload');
debug($this->request->data);
if($this->Upload->save($this->request->data)) {
$this->Session->setFlash('SAVED!!!!!!!!');
} else {
$this->Session->setFlash('NOT SAVED!!!!!!!!');
}
}
}
// VIEW
echo $this->Form->create('ArticleData', array('type'=>'file'));
echo $this->Form->input('Upload.model', array('type'=>'hidden', 'value'=>'ArticleData'));
echo $this->Form->input('Upload.foreign_key', array('type'=>'hidden', 'value'=>'4f93676e-347c-4e0c-8e6c-0a3cadcd7f7c'));
echo $this->Form->input('Upload.photo', array('type'=>'file'));
echo $this->Form->end('Submit');
Dave:
-the 'name' field in the 'uploads' table is empty
This is normal, I think is is more of a "display" name field as opposed to "filename" field. The name should be stored in the "photo" field in your example.
-if I use {model} in the 'path' set in the Upload model, it uses 'upload' as the model folder - it should go in a folder of the associated model, not the upload model every time
You'll want to alias the model, like so:
//ArticleData MODEL
public $hasMany = array(
'AliasModelHere' => array(
'className' => 'Upload',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Upload.model' => 'ArticleData',
),
),
);
// Then
$this->ArticleData->AliasModelHere->save($data)
For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:
function get_business_units()
{
$options = entity_load('business_unit', FALSE, NULL, FALSE);
$opt = bu_to_list_values($options);
return $opt;
}
function MYMODULE_enable()
{
if (!field_info_field('field_user_business_unit')) {
$field = array(
'field_name' => 'field_user_business_unit',
'type' => 'text',
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'get_business_units',
)
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_user_business_unit',
'entity_type' => 'user',
'label' => 'Business Unit',
'bundle' => 'user',
'required' => FALSE,
'settings' => array(
'user_register_form' => 1,
),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.
The problem is: 'type' => 'text'.
You have to use: 'type' => 'list_text'.
Allowed values is meaningless for a text type.
Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.
Have you tried
drush features-revert MYMODULE ?
I am currently trying to use Miles J plugin located here http://milesj.me/code/cakephp/uploader Although I have made great progress learning CakePhp, I am currently having a problem using the plugin and I would appreciate any help provided.
I have followed all the necessary steps to use the plugin. It has been downloaded put on Plugin folder, I bootstrapped with CakePlugin::loadAll().
So far so good.
Next I have proceed to set up the table as indicated by the plugin developer.
Ok, now back to my own code. I have the following set up:
images_controller.php , image.php and their views.
My goal now is to use the plugin inside those files as such:
App::import('Vendor', 'Uploader.Uploader');
Class ImagesController extends AppController {
var $components = array('Auth');
var $helpers = array('Design');
var $uses = array('Image', 'Uploader.Upload');
function manage(){
//here I show a simple upload form that uses the action saveimage
}
function saveimage(){
$this->Uploader = new Uploader();
if(!empty($this->data)){
$this->Upload->save($this->data);
}
}
}
Now, my model is set as follows
Class Image extends AppModel {
public $useTable = 'uploads';
public $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array(
'value' => array('gif', 'jpg', 'jpeg'),
'error' => 'Only gif, jpg and jpeg images are allowed!'
),
'minWidth' => 500,
'minHeight' => 500,
'required' => true
),
'import' => array(
'required' => false
)
),
'Uploader.Attachment' => array(
'file' => array(
'name' => 'uploaderFilename',
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path',
'maxNameLength' => 30,
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
// Save additional images in the databases after transforming
array(
'method' => 'resize',
'width' => 100,
'height' => 100,
'dbColumn' => 'path_alt'
)
),
'metaColumns' => array(
'size' => 'filesize', // The size value will be saved to the filesize column
'type' => 'type' // And the same for the mimetype
)
),
'import' => array(
'uploadDir' => '/files/uploads/',
'name' => 'uploaderFilename',
'dbColumn' => 'path',
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
array(
'method' => 'scale',
'percent' => .5,
'dbColumn' => 'path' // Overwrite the original image
)
)
)
)
);
}
}
On my model for testing purposes I have not changed anything but just copied and pasted the very same array as shown in the the Test/Model folder inside the plugin, which is meant to show the functionality of the plugin.
The following confusion, errors or lack of understanding is taking place:
My file is not being uploaded to the webroot/files/uploads folder
My data is being inserted in the database, but not in a complete manner by leaving empty as shown:
id | caption | path | path_alt | created |
4 | | | |2012:02:..|
Above, I expect the path to be saved, but it doesn't.
I have to admit my confusion comes mainly from my inexperience using plugins, so I am aware I might be doing something wrong regarding my models or my configuration.
Any prompt help would be appreciated greatly as I have tried to work this out on my own without any success.
A few things:
1 - In your controller you do not need to import the Uploader class. You also don't need to use the Uploader.Upload model (it's merely an example test case). All you need to do in the controller is call $this->Image->save() which will upload the file and save the path as a row into the database (if you defined the Attachment in Image).
2 - In your view, create the file input. Pay attention to the input name.
echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));
3 - In your Image model, setup the AttachmentBehavior and its options for that specific input field:
'Uploader.Attachment' => array(
'FILE_INPUT_NAME' => array(
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path'
),
'ANOTHER_INPUT' => array()
);
Be sure that the column "path" exists in your images table. And thats it.
For more information on what each option does, check out the following: http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model