From 57b626cae9e3fa22a30935a9a90819ced2aca991 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adri=C3=A1n=20Pardini?= <adrian@camba.coop>
Date: Tue, 25 Jul 2017 19:19:17 -0300
Subject: [PATCH 1/1] Remote Control: process Survey files on add_response()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the response data has fields for file attachments, move those files
to the final upload folder.

Signed-off-by: Adrián Pardini <adrian@camba.coop>
---
 .../helpers/remotecontrol/remotecontrol_handle.php | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/application/helpers/remotecontrol/remotecontrol_handle.php b/application/helpers/remotecontrol/remotecontrol_handle.php
index 53e97e7..f4ffe7d 100644
--- a/application/helpers/remotecontrol/remotecontrol_handle.php
+++ b/application/helpers/remotecontrol/remotecontrol_handle.php
@@ -2491,7 +2491,29 @@ class remotecontrol_handle
             $result_id = $survey_dynamic->insertRecords($aResponseData);
 
             if ($result_id)
+            {
+                $oResponse = Response::model($iSurveyID)->findByAttributes(array('token' => $sToken, 'id' => $result_id));
+                foreach ($oResponse->getFiles() as $aFile)
+                {
+                    $sUploadPath = Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files/";
+                    $sFileRealName = Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files/" . $aFile['filename'];
+                    $sFileTempName = Yii::app()->getConfig('tempdir') . "/upload/" . $aFile['filename'];
+
+                    if (!file_exists($sFileRealName))
+                    {
+                        if (!is_dir($sUploadPath))
+                        {
+                            mkdir($sUploadPath, 0777, true);
+                        }
+                        if (!rename($sFileTempName, $sFileRealName))
+                        {
+                            return array('status' => 'Unable to move files ' . $sFileTmpName . ' ' . $sFileRealName);
+                        }
+                    }
+
+                }
                 return $result_id;
+            }
             else
                 return array('status' => 'Unable to add response');
         }
-- 
2.7.4

