Exemplo n.º 1
0
 function onBeforeSaveField(&$field, &$post, &$file, &$item)
 {
     if (!in_array($field->field_type, self::$field_types)) {
         return;
     }
     $use_ingroup = $field->parameters->get('use_ingroup', 0);
     if (!is_array($post) && !strlen($post) && !$use_ingroup) {
         return;
     }
     // Make sure posted data is an array
     $post = !is_array($post) ? array($post) : $post;
     //echo "<pre>"; print_r($post);
     // Get configuration
     $is_importcsv = JRequest::getVar('task') == 'importcsv';
     $import_media_folder = JRequest::getVar('import_media_folder');
     $image_source = $field->parameters->get('image_source', 0);
     if ($image_source > 1) {
         global $fc_folder_mode_err;
         if (empty($fc_folder_mode_err[$field->id])) {
             echo __FUNCTION__ . "(): folder-mode: " . $image_source . " not implemented please change image-source mode in image/gallery field with id: " . $field->id;
             $fc_folder_mode_err[$field->id] = 1;
             $image_source = 1;
         }
     }
     $unique_tmp_itemid = JRequest::getVar('unique_tmp_itemid', '');
     // Set a warning message for overriden/changed files: form.php (frontend) or default.php (backend)
     if (!$is_importcsv && empty($unique_tmp_itemid)) {
         $app = JFactory::getApplication();
         $app->enqueueMessage('WARNING, field: ' . $field->label . ' requires variable -unique_tmp_itemid- please update your ' . ($app->isSite() ? 'form.php' : 'default.php'), 'warning');
     }
     // Execute once
     static $initialized = null;
     static $srcpath_original = '';
     if (!$initialized) {
         $initialized = 1;
         jimport('joomla.filesystem.folder');
         jimport('joomla.filesystem.path');
         if ($is_importcsv) {
             $srcpath_original = JPath::clean(JPATH_SITE . DS . $import_media_folder . DS);
         }
     }
     // ***********************************************
     // Special steps for image field in folder-mode(s)
     // ***********************************************
     if ($image_source > 0) {
         $dir = $field->parameters->get('dir');
         $unique_tmp_itemid = JRequest::getVar('unique_tmp_itemid', '');
         $destpath = JPath::clean(JPATH_SITE . DS . $dir . DS . 'item_' . $item->id . '_field_' . $field->id . DS);
         if ($image_source > 1) {
         }
         // TODO
         // Create original images folder if doing CSV import and folder does not exist
         if ($is_importcsv) {
             $destpath_original = $destpath . 'original' . DS;
             if (!JFolder::exists($destpath_original) && !JFolder::create($destpath_original)) {
                 JError::raiseWarning(100, $field->label . ': Error. Unable to create folder: ' . $destpath_original);
                 return false;
                 // Cancel item creation
             }
         } else {
             if ($unique_tmp_itemid && $item->id != $unique_tmp_itemid) {
                 $temppath = JPath::clean(JPATH_SITE . DS . $dir . DS . 'item_' . $unique_tmp_itemid . '_field_' . $field->id . DS);
                 JFolder::move($temppath, $destpath);
             }
         }
     }
     // **************************************************************************
     // Rearrange file array so that file properties are groupped per image number
     // **************************************************************************
     //echo "<pre>"; print_r($file); echo "</pre>";
     $files = array();
     if ($file) {
         foreach ($file as $key => $all) {
             foreach ($all as $i => $val) {
                 $files[$i][$key] = $val;
             }
         }
     }
     //echo "<pre>"; print_r($files); echo "</pre>";
     // *****************************************************************************************
     // Reformat the posted data & handle uploading / removing / deleting / replacing image files
     // *****************************************************************************************
     $new_filenames = array();
     $newpost = array();
     $new = 0;
     foreach ($post as $n => $v) {
         if (empty($v)) {
             // skip empty value, but allow empty (null) placeholder value if in fieldgroup
             if ($use_ingroup) {
                 $newpost[$new++] = null;
             }
             continue;
         }
         // support for basic CSV import / export
         if ($is_importcsv && !is_array($v)) {
             if (@unserialize($v) !== false || $v === 'b:0;') {
                 // support for exported serialized data)
                 $v = unserialize($v);
             } else {
                 $v = array('originalname' => $v);
             }
         }
         // Add system message if upload error
         $err_code = isset($files[$n]['error']) ? $files[$n]['error'] : false;
         if ($err_code && $err_code != UPLOAD_ERR_NO_FILE) {
             $err_msg = array(UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', UPLOAD_ERR_NO_FILE => 'No file was uploaded', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload');
             JFactory::getApplication()->enqueueMessage("FILE FIELD: " . $err_msg[$err_code], 'warning');
             continue;
         }
         // Handle uploading a new original file
         $new_file = $err_code === 0;
         $new_file_uploaded = null;
         if ($new_file) {
             $new_file_uploaded = $this->uploadOriginalFile($field, $v, $files[$n]);
         } else {
             if ($is_importcsv && $import_media_folder) {
                 $filename = basename($v['originalname']);
                 $sub_folder = dirname($v['originalname']);
                 $sub_folder = $sub_folder && $sub_folder != '.' ? DS . $sub_folder : '';
                 if ($image_source) {
                     $srcfilepath = JPath::clean($srcpath_original . $v['originalname']);
                     $destfilepath = JPath::clean($destpath_original . $filename);
                     if (JFile::exists($srcfilepath)) {
                         $result = JFile::copy($srcfilepath, $destfilepath);
                         if ($result && JPath::canChmod($destfilepath)) {
                             chmod($destfilepath, 0644);
                         }
                     }
                     $v['originalname'] = $filename;
                     // make sure filename is without subfolder
                 } else {
                     $fman = new FlexicontentControllerFilemanager();
                     JRequest::setVar('return-url', null, 'post');
                     JRequest::setVar('file-dir-path', DS . $import_media_folder . $sub_folder, 'post');
                     JRequest::setVar('file-filter-re', preg_quote($filename), 'post');
                     JRequest::setVar('secure', 1, 'post');
                     JRequest::setVar('keep', 1, 'post');
                     $file_ids = $fman->addlocal();
                     reset($file_ids);
                     // Reset array to point to first element
                     $v['originalname'] = key($file_ids);
                     // The (first) key of file_ids array is the cleaned up filename
                 }
             }
         }
         // Defaut values for unset required properties of values
         $v['originalname'] = isset($v['originalname']) ? $v['originalname'] : '';
         $v['existingname'] = isset($v['existingname']) ? $v['existingname'] : '';
         $v['remove'] = isset($v['remove']) ? $v['remove'] : false;
         if ($v['originalname'] || $v['existingname']) {
             //echo $v['originalname'] ." ". $v['existingname'] ."<br>";
             // (c) Handle replacing image with a new existing image
             if ($v['existingname']) {
                 $v['originalname'] = $v['existingname'];
                 $v['existingname'] = '';
             } else {
                 if ($v['remove']) {
                     // Deleting or unloading current value: Skip current image row, but allow empty (null) placeholder value if in fieldgroup
                     $v = $use_ingroup ? null : false;
                     //$use_ingroup ? array('originalname'=>'') : null;
                 }
             }
         } else {
             // No new file posted and no existing selected: Skip current image row, but allow empty (null) placeholder value if in fieldgroup
             $v = $use_ingroup ? null : false;
             //$use_ingroup ? array('originalname'=>'') : null;
         }
         // Add image entry to a new array skipping empty image entries
         if ($v !== false) {
             if ($v) {
                 $new_filenames[$v['originalname']] = 1;
             }
             $newpost[$new] = $v;
             $new++;
         }
     }
     $post = $newpost;
     // Remove no longer used files, if limiting existing image list to current field, or if existing image list is hidden/disabled
     if ($image_source && ($field->parameters->get('auto_delete_unused', 1) || !$field->parameters->get('list_all_media_files', 0) || !$field->parameters->get('existing_imgs', 1))) {
         // Get existing field values,
         if (!isset($item->fieldvalues)) {
             $_fieldvalues = FlexicontentFields::getFieldValsById(null, array($item->id));
             $item->fieldvalues = isset($_fieldvalues[$item->id]) ? $_fieldvalues[$item->id] : array();
         }
         $db_values = !empty($item->fieldvalues[$field->id]) ? $item->fieldvalues[$field->id] : array();
         //echo "<pre>"; print_r($new_filenames); print_r($db_values);
         // Remove unused files
         foreach ($db_values as $i => $v) {
             $fdata = unserialize($v);
             $filename = isset($fdata['originalname']) ? $fdata['originalname'] : '';
             //echo $filename."\n";
             if ($filename && !isset($new_filenames[$filename])) {
                 $canDeleteImage = $this->canDeleteImage($field, $filename, $item);
                 // check if value is in use
                 if ($canDeleteImage) {
                     $this->removeOriginalFile($field, $filename);
                     //JFactory::getApplication()->enqueueMessage($field->label . ' ['.$n.'] : ' . 'Deleted image file: '.$filename.' from server storage');
                 }
             }
         }
     }
     // Serialize multi-property data before storing them into the DB,
     // null indicates to increment valueorder without adding a value
     foreach ($post as $i => $v) {
         if ($v !== null) {
             $post[$i] = serialize($v);
         }
     }
 }