Ajaxupload - Upload to new resource and edit
Hi,
I am trying to use Ajaxupload together with two formit snippets which are giving to the user the ability to create and edit resources from frontend.
With the code I attach bellow, I can upload photos to a new resource but I can't retrieve them when I edit the resource.
Formit call for new resource:
Formit call to edit resource:
Formit2resource:
Resource2Formit:
Any help would be great!
I am trying to use Ajaxupload together with two formit snippets which are giving to the user the ability to create and edit resources from frontend.
With the code I attach bellow, I can upload photos to a new resource but I can't retrieve them when I edit the resource.
Formit call for new resource:
[[!FormIt?
&preHooks=`resource2formit, Formit2AjaxUpload`
&hooks=`AjaxUpload2Formit, formit2resource, redirect`
&resource2formitfields=`template,resource_id,parent,Paratiriseis,Name,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,pagetitle,photo`
&redirectTo=`106`
&ajaxuploadUid=`image`
&ajaxuploadFieldname=`photo`
&ajaxuploadTarget=`images/uploads/`
&validate=`
pagetitle:required,
Sex:required,
Race:required,
Age:required,
Phone:required,
Email:required,
Pedigree:required,
Area:required`
&vTextRequired=`*Απαραίτητο πεδίο`
&submitVar=`dog_input`
]]
Formit call to edit resource:
[[!FormIt?
&preHooks=`resource2formit,Formit2AjaxUpload`
&hooks=`AjaxUpload2Formit,formit2resource,redirect`
&resource2formitfields=`template,resource_id,Active,pagetitle,Paratiriseis,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,content,photo`
&resId=`[[~[[*id]]]]`
&redirectTo=`106`
&ajaxuploadUid=`image`
&ajaxuploadFieldname=`photo`
&ajaxuploadTarget=`images/uploads/`
&validate=`
pagetitle:required,
Sex:required,
Race:required,
Age:required,
Phone:required,
Email:required,
Pedigree:required,
Area:required`
&vTextRequired=`*Απαραίτητο πεδίο`
&submitVar=`dog_input`
]]
Formit2resource:
<?php
$doc = $modx->getObject('modResource',array('id'=>$hook->getValue('resource_id')));
if (empty($doc)){
$doc = $modx->newObject('modResource');
$doc->set('createdby', $modx->user->get('id'));
}
else{
$doc->set('editedby', $modx->user->get('id'));
}
$allFormFields = $hook->getValues();
foreach ($allFormFields as $field=>$value)
{
if ($field !== 'spam' && $field !== 'resource_id'){
$doc->set($field, $value);
}
//we need to parse the title into the alias
if($field == 'pagetitle'){
//replace spaces with -
$alias = preg_replace('/[\' \']/' , '-' , $value);
//remove non alpha and a common injection string
$alias = preg_replace('/[^a-z0-9\.\x0B\-]/i' , '' , $alias);
//this is the standard revo regexp
// \0\x0B\t\n\r\f\a&=+%#<>"~:`@\?\[\]\{\}\|\^'\
}
}
// now set the alias
$doc->set('alias', $alias);
$doc->set('template', $template);
$isnew = $doc->isNew();
if ($doc->save()) {
if ($isnew) {
$doc->set('alias', $doc->get('alias' . '-' . $doc->get('id')));
$doc->save();
}
}
foreach ($allFormFields as $field=>$value)
{
if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
{
/* handles checkboxes & multiple selects elements */
if (is_array($value)) {
$featureInsert = array();
while (list($featureValue, $featureItem) = each($value)) {
$featureInsert[count($featureInsert)] = $featureItem;
}
$value = implode('||',$featureInsert);
}
$tv->setValue($doc->get('id'), $value);
$tv->save();
}
}
$url = $modx->makeUrl(84);
////////////formit2file part ////////////////////////////////////
// initialize output;
$output = true;
// valid extensions
$ext_array = array('jpg', 'jpeg', 'gif', 'png');
// create unique path for this form submission
//$uploadpath = 'assets/img-annonceurs/';
// you can create some logic to automatically
// generate some type of folder structure here.
// the path that you specify will automatically
// be created by the script if it doesn't already
// exist.
// EXAMPLE:
// this would put all file uploads into a new,
// unique folder every day.
$uploadpath = 'assets/images/users/'.date('Y-m-d').'/';
// get full path to unique folder
$target_path = $modx->config['base_path'] . $uploadpath;
// get uploaded file names:
$submittedfiles = array_keys($_FILES);
// loop through files
foreach ($submittedfiles as $sf) {
// Get Filename and make sure its good.
$filename = basename( $_FILES[$sf]['name'] );
// Get file's extension
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$ext = mb_strtolower($ext); // case insensitive
// is the file name empty (no file uploaded)
if($filename != '') {
// is this the right type of file?
if(in_array($ext, $ext_array)) {
// change name to: resource_id-input_name.ext
$filename = $doc->get('id').'-'.$sf.'.'.$ext;
// full path to new file
$myTarget = $target_path . $filename;
//URL path
$urlPath = $uploadpath . $filename;
// create directory to move file into if it doesn't exist
if(!is_dir($target_path)) {
mkdir($target_path, 0755, true);
} else {
$modx->log(modX::LOG_LEVEL_ERROR, 'Upload:Path already exists' );
}
// is the file moved to the proper folder successfully?
if(move_uploaded_file($_FILES[$sf]['tmp_name'], $myTarget)) {
// place file path to tv
if (!$doc->setTVValue($sf, $urlPath)) $modx->log(xPDO::LOG_LEVEL_ERROR, 'Upload:Saving TV error');
// set the permissions on the file
if (!chmod($myTarget, 0644)) { /*some debug function*/ }
} else {
// File not uploaded
$errorMsg = 'There was a problem uploading the file.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
} else {
// File type not allowed
$errorMsg = 'Type of file not allowed.';
$hook->addError($sf, $errorMsg);
$output = false; // generate submission error
}
// if no file, don't error, but return blank
} else {
$hook->setValue($sf, '');
}
}
$modx->cacheManager->refresh(); // suggested by gallenkamp
return $output;
Resource2Formit:
<?php
if (isset($_GET['resId'])){
if ($doc=$modx->getObject('modResource',array('id'=>$_GET['resId']))){
$docarray=$doc->toArray();
$fields = explode(',',$scriptProperties['resource2formitfields']);
$fields[] = 'id';
foreach ($fields as $field){
if ($doc->getFieldName($field) === null) {
/* if field isnt defined, look for TV value */
$tvValue = $doc->getTVValue($field);
if ($tvValue !== null) {
$hook->setValue($field,$tvValue);
}
} else {
/* otherwise get field value */
$hook->setValue($field,$docarray[$field]);
}
}
}
//$errorMsg = '<pre>'.print_r($docarray,true).'</pre>';
//$hook->addError('error_message',$errorMsg);
}
return true;
Any help would be great!