CRM-19294: Unable to upload files via a contribution page
authordeb.monish <monish.deb@webaccessglobal.com>
Fri, 30 Sep 2016 11:18:47 +0000 (16:48 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Mon, 3 Oct 2016 07:40:27 +0000 (13:10 +0530)
CRM/Contribute/Form/ContributionBase.php
CRM/Core/BAO/CustomField.php
CRM/Core/Page/File.php
CRM/Utils/File.php
templates/CRM/UF/Form/Block.tpl

index 45f13c35ad34ed496e82b3552a4129454d92d1cd..e408c5ff1994cf7cf8f891dbb29054dd0bec13b8 100644 (file)
@@ -629,13 +629,19 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
         CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
         $addCaptcha = FALSE;
+        // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page
+        $viewOnlyFileValues = array();
         foreach ($fields as $key => $field) {
           if ($viewOnly &&
             isset($field['data_type']) &&
             $field['data_type'] == 'File' || ($viewOnly && $field['name'] == 'image_URL')
           ) {
-            // ignore file upload fields
-            continue;
+            $viewOnlyFileValues[$key] = '';
+            if (!empty($this->_params[$key])) {
+              $path = CRM_Utils_Array::value('name', $this->_params[$key]);
+              $fileType = CRM_Utils_Array::value('type', $this->_params[$key]);
+              $viewOnlyFileValues[$key] = CRM_Utils_File::getFileURL($path, $fileType);
+            }
           }
 
           if ($profileContactType) {
@@ -687,6 +693,10 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
         $this->assign($name, $fields);
 
+        if (count($viewOnlyFileValues)) {
+          $this->assign('viewOnlyFileValues', $viewOnlyFileValues);
+        }
+
         if ($addCaptcha && !$viewOnly) {
           $captcha = CRM_Utils_ReCAPTCHA::singleton();
           $captcha->add($this);
index 4bdcacc0fa8027d1ffff466f007872bd4f783a68..a71e50310904e660e48740e0a80b1772489bb371 100644 (file)
@@ -1438,18 +1438,13 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
             'id'
           );
           list($path) = CRM_Core_BAO_File::path($fileID, $entityId, NULL, NULL);
-          list($imageWidth, $imageHeight) = getimagesize($path);
-          list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
           $url = CRM_Utils_System::url('civicrm/file',
             "reset=1&id=$fileID&eid=$contactID",
             $absolute, NULL, TRUE, TRUE
           );
-          $result['file_url'] = "
-          <a href=\"$url\" class='crm-image-popup'>
-          <img src=\"$url\" width=$imageThumbWidth height=$imageThumbHeight/>
-          </a>";
-          // for non image files
+          $result['file_url'] = CRM_Utils_File::getFileURL($path, $fileType, $url);
         }
+        // for non image files
         else {
           $uri = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_File',
             $fileID,
@@ -1459,7 +1454,7 @@ class CRM_Core_BAO_CustomField extends CRM_Core_DAO_CustomField {
             "reset=1&id=$fileID&eid=$contactID",
             $absolute, NULL, TRUE, TRUE
           );
-          $result['file_url'] = "<a href=\"$url\">{$uri}</a>";
+          $result['file_url'] = CRM_Utils_File::getFileURL($uri, $fileType, $url);
         }
       }
       return $result;
index b0632cf9beb61a6087d1f44ee249406389a63ce9..dc02ce801734d169d02dad357862917ec1389c22 100644 (file)
 class CRM_Core_Page_File extends CRM_Core_Page {
 
   public function run() {
-    $eid = CRM_Utils_Request::retrieve('eid', 'Positive', $this, TRUE);
-    $fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE);
-    $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
-    $quest = CRM_Utils_Request::retrieve('quest', 'String', $this);
+    $fileName = CRM_Utils_Request::retrieve('filename', 'String', $this);
+    $path = CRM_Core_Config::singleton()->customFileUploadDir . $fileName;
+    $mimeType = CRM_Utils_Request::retrieve('mime-type', 'String', $this);
     $action = CRM_Utils_Request::retrieve('action', 'String', $this);
 
-    list($path, $mimeType) = CRM_Core_BAO_File::path($id, $eid, NULL, $quest);
+    // if we are not providing essential parameter needed for file preview then
+    if (empty($fileName) && empty($mimeType)) {
+      $eid = CRM_Utils_Request::retrieve('eid', 'Positive', $this, TRUE);
+      $fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE);
+      $id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
+      $quest = CRM_Utils_Request::retrieve('quest', 'String', $this);
+
+      list($path, $mimeType) = CRM_Core_BAO_File::path($id, $eid, NULL, $quest);
+    }
+
     if (!$path) {
       CRM_Core_Error::statusBounce('Could not retrieve the file');
     }
index 284b71a5ea31d9b53598fb3d3779830e26779141..a99077ef677f90a981b581eaca427991231561a7 100644 (file)
@@ -786,4 +786,44 @@ HTACCESS;
     $param[$fileName] = $fileParams;
   }
 
+  /**
+   * Return formatted file URL, like for image file return image url with image icon
+   *
+   * @param string $path
+   *   Absoulte file path
+   * @param string $fileType
+   * @param string $url
+   *   File preview link e.g. https://example.com/civicrm/file?reset=1&filename=image.png&mime-type=image/png
+   *
+   * @return string $url
+   */
+  public static function getFileURL($path, $fileType, $url = NULL) {
+    if (empty($path) || empty($fileType)) {
+      return '';
+    }
+    elseif (empty($url)) {
+      $fileName = basename($path);
+      $url = CRM_Utils_System::url('civicrm/file', "reset=1&filename={$fileName}&mime-type={$fileType}");
+    }
+    switch ($fileType) {
+      case 'image/jpeg':
+      case 'image/pjpeg':
+      case 'image/gif':
+      case 'image/x-png':
+      case 'image/png':
+        list($imageWidth, $imageHeight) = getimagesize($path);
+        list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
+        $url = "<a href=\"$url\" class='crm-image-popup'>
+          <img src=\"$url\" width=$imageThumbWidth height=$imageThumbHeight/>
+          </a>";
+        break;
+
+      default:
+        $url = sprintf('<a href="%s">%s</a>', $url, basename($path));
+        break;
+    }
+
+    return $url;
+  }
+
 }
index adc17e86a0125a02ffe1b25609247a46cacb38fb..c59098b604f627e53653f8c173ef74f1b82777f7 100644 (file)
                     {$form.onbehalfof_id.html}
                   {/if}
                   {$form.$prefix.$n.html}
+    {elseif $field.html_type eq 'File' && $viewOnlyFileValues}
+      {$viewOnlyFileValues.$n}
                {else}
                  {$form.$n.html}
                {/if}