Ajaxupload edit - Queue problem

I use a Formit call with some snippets (hooks and prehooks), to give users the ability to upload and edit resources.

Everything working properly except the files upload part. There I use AjaxUpload. The problem is that Ajaxupload when the user logs out, flashes the queue. So, when the user logout and login and try to edit resource, can't see the thumbnails. Data in the field is there but queue is blank.

[[!FormIt? 
    &hooks=`AjaxUpload2Formit, formit2resource, redirect`
    &preHooks=`resource2formit,Formit2AjaxUpload`
 &resource2formitfields=`template,resource_id,parent,Paratiriseis,pagetitle,Sex,Race,Age,Weight,Pedigree,Microchip,Passport,Area,Phone,Email,refnum,IMAGE`
    &redirectTo=`106`
    &ajaxuploadFieldname=`IMAGE`
    &ajaxuploadTarget=`images/uploads/`
    &ajaxuploadUid=`image_[[unique]]`
    &submitVar=`dog_input`
]]

<form class="form" action="[[~[[*id]]]]" method="post" name="dog_input" enctype='multipart/form-data' >  [[!AjaxUpload?
    &uid=`image_[[unique]]`
    &allowedExtensions=`jpg,jpeg,png,gif`
    &maxFilesizeMb=`1`
    &maxFiles=`3`
    &thumbX=`75`
    &thumbY=`75`
]]
 <input type="submit" id="button_5" name="dog_input" value="Καταχώρηση"/>
</form>


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;
Poko Loko
17 февраля 2016, 15:08
modx.pro
1 451
0

Комментарии: 0

Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
0