CRM-19294: Unable to upload files via a contribution page (backport 4.6 PR)
authordeb.monish <monish.deb@webaccessglobal.com>
Mon, 3 Oct 2016 08:11:17 +0000 (13:41 +0530)
committerdeb.monish <monish.deb@webaccessglobal.com>
Mon, 3 Oct 2016 08:11:17 +0000 (13:41 +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 5661011c3f1f39807a7280a58bf3a2faeba71f1a..68a3c2ae6954212d4d698bd96fd9ae678c49a986 100644 (file)
@@ -715,13 +715,29 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
         CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID);
         $addCaptcha = FALSE;
+        $viewOnlyFileValues = empty($profileContactType) ? array() : array($profileContactType => array());
+        // fetch file preview when not submitted yet, like in online contribution Confirm and ThankYou page
         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;
+            //retrieve file value from submitted values on basis of $profileContactType
+            $fileValue = empty($profileContactType) ? CRM_Utils_Array::value($key, $this->_params) : CRM_Utils_Array::value(sprintf('%s[%s]', $profileContactType, $key), $this->_params);
+
+            if ($fileValue) {
+              $path = CRM_Utils_Array::value('name', $fileValue);
+              $fileType = CRM_Utils_Array::value('type', $fileValue);
+              $fileValue = CRM_Utils_File::getFileURL($path, $fileType);
+            }
+
+            // format custom file value fetched from submitted value
+            if ($profileContactType) {
+              $viewOnlyFileValues[$profileContactType][$key] = $fileValue;
+            }
+            else {
+              $viewOnlyFileValues[$key] = $fileValue;
+            }
           }
 
           if ($profileContactType) {
@@ -771,6 +787,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 717459a10e0e6cc234927d312a2ed1a1f4d6c8ed..9c6beae7a5a1bb5d2db472639f2ead421e1819c0 100644 (file)
@@ -1508,18 +1508,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,
@@ -1529,7 +1524,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 d8e6900fb01a6318286c10201244df59f4acdbcd..39e4c2146d8c581b8c155c1fac2162e4685d7d95 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 3eeddf4d36cccaf3ae863c018c27dc275e49f251..48607588d15571f25c9b66047ba8b558128241e5 100644 (file)
@@ -749,4 +749,44 @@ HTACCESS;
     return TRUE;
   }
 
+  /**
+   * 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 627c34923fa07b88c3972abbb7a7a234dede7ab0..80c24bdf58fca88013be579808a77cb8e908880d 100644 (file)
         {else}
           <div class="crm-section editrow_{$n}-section form-item" id="editrow-{$n}">
             <div class="label">
-              {if $prefix}{$form.$prefix.$n.label}{else}{$form.$n.label}{/if}
+              {if $prefix}
+                {$form.$prefix.$n.label}
+              {else}
+                {$form.$n.label}
+              {/if}
             </div>
             <div class="content">
               {if $n|substr:0:3 eq 'im-'}
                 {if $form.$phone_ext_field.html}
                   &nbsp;{$form.$phone_ext_field.html}
                 {/if}
+              {elseif $field.html_type eq 'File' && ($viewOnlyFileValues.$n OR $viewOnlyFileValues.$prefix)}
+                {if $prefix}
+                  {$viewOnlyFileValues.$prefix.$n}
+                {else}
+                  {$viewOnlyFileValues.$n}
+                {/if}
               {else}
-                {if $prefix}{$form.$prefix.$n.html}{else}{$form.$n.html}{/if}
+                {if $prefix}
+                  {$form.$prefix.$n.html}
+                {else}
+                  {$form.$n.html}
+                {/if}
               {/if}
 
             {*CRM-4564*}