示例#1
0
 /**
  * Process media files
  *
  * @copyright
  * @author 		RolandD
  * @todo
  * @see
  * @access 		private
  * @param
  * @return
  * @since 		4.0
  */
 private function _processMedia()
 {
     $jinput = JFactory::getApplication()->input;
     $template = $jinput->get('template', null, null);
     $csvilog = $jinput->get('csvilog', null, null);
     $generate_image = $template->get('auto_generate_image_name', 'image', false);
     // Check if any image handling needs to be done
     if (!is_null($this->file_url) || $generate_image) {
         // Check if we have any images
         if (is_null($this->file_url) && $generate_image) {
             $this->_createImageName();
         }
         // Create an array of images to process
         $images = explode('|', $this->file_url);
         $thumbs = explode('|', $this->file_url_thumb);
         $titles = explode('|', $this->file_title);
         $descriptions = explode('|', $this->file_description);
         $metas = explode('|', $this->file_meta);
         $order = explode('|', $this->file_ordering);
         $ordering = 1;
         $max_width = $template->get('resize_max_width', 'image', 1024);
         $max_height = $template->get('resize_max_height', 'image', 768);
         // Image handling
         $imagehelper = new ImageHelper();
         // Delete existing image links
         if ($template->get('delete_product_images', 'image', false)) {
             $db = JFactory::getDbo();
             $query = $db->getQuery(true)->delete($db->qn('#__virtuemart_product_medias'))->where($db->qn('virtuemart_product_id') . '=' . $this->virtuemart_product_id);
             $db->setQuery($query);
             $db->query();
             $csvilog->addDebug('Delete images', true);
         }
         foreach ($images as $key => $image) {
             $image = trim($image);
             // Create image name if needed
             if (count($images) == 1) {
                 $img_counter = 0;
             } else {
                 $img_counter = $key + 1;
             }
             if ($generate_image) {
                 $name = null;
                 $name = $this->_createImageName($img_counter);
                 if (!empty($name)) {
                     $image = $name;
                 }
             }
             if (!empty($image)) {
                 // Get the image path
                 $imgpath = $template->get('file_location_product_files', 'path');
                 // Verify the original image
                 if ($imagehelper->isRemote($image)) {
                     $original = $image;
                     $remote = true;
                     $full_path = $imgpath;
                 } else {
                     // Check if the image contains the image path
                     $dirname = dirname($image);
                     if (strpos($imgpath, $dirname) !== false) {
                         $image = basename($image);
                     }
                     $original = $imgpath . $image;
                     $remote = false;
                     // Get subfolders
                     $path_parts = pathinfo($original);
                     $full_path = $path_parts['dirname'] . '/';
                 }
                 // Generate image names
                 if ($template->get('process_image', 'image', false)) {
                     if ($generate_image) {
                         $file_details = $imagehelper->ProcessImage($original, $full_path, $this->product_full_image_output);
                     } else {
                         $file_details = $imagehelper->ProcessImage($original, $full_path);
                     }
                 } else {
                     $file_details['exists'] = true;
                     $file_details['isimage'] = $imagehelper->isImage(JPATH_SITE . '/' . $image);
                     $file_details['name'] = $image;
                     $file_details['output_name'] = basename($image);
                     if (file_exists(JPATH_SITE . '/' . $image)) {
                         $file_details['mime_type'] = $imagehelper->findMimeType($image);
                     } else {
                         $file_details['mime_type'] = '';
                     }
                     $file_details['output_path'] = $full_path;
                 }
                 // Process the file details
                 if ($file_details['exists']) {
                     // Check if the image is an external image
                     if (substr($file_details['name'], 0, 4) == 'http') {
                         $csvilog->AddStats('incorrect', 'COM_CSVI_VM_NOSUPPORT_URL');
                     } else {
                         $title = isset($titles[$key]) ? $titles[$key] : $file_details['output_name'];
                         $description = isset($descriptions[$key]) ? $descriptions[$key] : '';
                         $meta = isset($metas[$key]) ? $metas[$key] : '';
                         $media = array();
                         $media['virtuemart_vendor_id'] = $this->virtuemart_vendor_id;
                         if ($template->get('autofill', 'image')) {
                             $media['file_title'] = $file_details['output_name'];
                             $media['file_description'] = $file_details['output_name'];
                             $media['file_meta'] = $file_details['output_name'];
                         } else {
                             $media['file_title'] = $title;
                             $media['file_description'] = $description;
                             $media['file_meta'] = $meta;
                         }
                         $media['file_mimetype'] = $file_details['mime_type'];
                         $media['file_type'] = 'product';
                         $media['file_is_product_image'] = 1;
                         $media['file_is_downloadable'] = $file_details['isimage'] ? 0 : 1;
                         $media['file_is_forSale'] = 0;
                         $media['file_url'] = empty($file_details['output_path']) ? $file_details['output_name'] : $file_details['output_path'] . $file_details['output_name'];
                         $media['published'] = $this->published;
                         // Create the thumbnail
                         if ($file_details['isimage']) {
                             $thumb = isset($thumbs[$key]) ? $thumbs[$key] : null;
                             if ($template->get('thumb_create', 'image')) {
                                 $thumb_sizes = getimagesize(JPATH_SITE . '/' . $media['file_url']);
                                 if (empty($thumb) || $generate_image) {
                                     // Get the subfolder structure
                                     $thumb_path = str_ireplace($imgpath, '', $full_path);
                                     if (empty($this->file_url_thumb)) {
                                         $thumb = 'resized/' . $thumb_path . basename($media['file_url']);
                                     }
                                 } else {
                                     // Check if we are not overwriting any large images
                                     $thumb_path_parts = pathinfo($thumb);
                                     if ($thumb_path_parts['dirname'] == '.') {
                                         $csvilog->AddStats('incorrect', 'COM_CSVI_THUMB_OVERWRITE_FULL');
                                         $thumb = false;
                                     }
                                 }
                                 if ($thumb && ($thumb_sizes[0] < $max_width || $thumb_sizes[1] < $max_height)) {
                                     $media['file_url_thumb'] = $imagehelper->createThumbnail($media['file_url'], $imgpath, $thumb);
                                 } else {
                                     $media['file_url_thumb'] = '';
                                 }
                             } else {
                                 $media['file_url_thumb'] = empty($thumb) ? $media['file_url'] : $file_details['output_path'] . $thumb;
                                 if (substr($media['file_url_thumb'], 0, 4) == 'http') {
                                     $csvilog->addDebug(JText::sprintf('COM_CSVI_RESET_THUMB_NOHTTP', $media['file_url_thumb']));
                                     $media['file_url_thumb'] = '';
                                 }
                             }
                         } else {
                             $media['file_is_product_image'] = 0;
                             $media['file_url_thumb'] = '';
                         }
                         // Bind the media data
                         $this->_medias->bind($media);
                         // Check if the media image already exists
                         $this->_medias->check();
                         // Store the media data
                         if ($this->_medias->store()) {
                             if ($this->queryResult() == 'UPDATE') {
                                 $csvilog->AddStats('updated', JText::_('COM_CSVI_UPDATE_MEDIA'));
                             } else {
                                 $csvilog->AddStats('added', JText::_('COM_CSVI_ADD_MEDIA'));
                             }
                             // Store the debug message
                             $csvilog->addDebug('COM_CSVI_MEDIA_QUERY', true);
                             // Watermark the image
                             if ($template->get('full_watermark', 'image') && $file_details['isimage']) {
                                 $imagehelper->addWatermark(JPATH_SITE . '/' . $media['file_url']);
                             }
                             // Store the product image relation
                             $data = array();
                             $data['virtuemart_product_id'] = $this->virtuemart_product_id;
                             $data['virtuemart_media_id'] = $this->_medias->virtuemart_media_id;
                             $data['ordering'] = isset($order[$key]) && !empty($order[$key]) ? $order[$key] : $ordering;
                             $this->_product_medias->bind($data);
                             if (!$this->_product_medias->check()) {
                                 if ($this->_product_medias->store()) {
                                     $csvilog->addDebug('COM_CSVI_STORE_PRODUCT_IMAGE_RELATION', true);
                                     $ordering++;
                                 }
                             } else {
                                 $csvilog->addDebug('COM_CSVI_PRODUCT_IMAGE_RELATION_EXISTS');
                             }
                         } else {
                             $csvilog->AddStats('incorrect', JText::sprintf('COM_CSVI_MEDIA_NOT_ADDED', $this->_medias->getError()));
                             return false;
                         }
                         // Reset the product media table
                         $this->_medias->reset();
                         $this->_product_medias->reset();
                     }
                     // else
                 }
                 // if
             }
             // if
         }
         // foreach
     }
     // if
 }