Parse Contents of Uploaded CSV
Before reading the contents of the CSV, check that the file is not empty.
1. if (!isset(@@csvFiles) or empty(@@csvFiles)) {
2. $mesg = "CSV File is empty. Please upload again.";
3. goto uploadAgain;
4. }
If it is empty, use a goto structure (there are several error checks, so using a goto structure makes it possible to reuse) to notify the user and redirect back to the upload screen.
1. uploadAgain: $g = new G();
2. $g - > SendMessageText($mesg, 'ERROR'); //change for the unique ID of your DynaForm:
3. PMFRedirectToStep(@@APPLICATION, @%INDEX, 'DYNAFORM', '4305736055af0d0c656d1d0019182436');
When the file is not empty, we store the name of the file and the case id to be used in a query to obtain the UID of the uploaded file.
1. $file = @@csvFiles[0]['name'];
2. $caseId = @@APPLICATION; //find the assigned UID for the uploaded file:
3. $query = "SELECT C.CON_ID
4. FROM CONTENT C, APP_DOCUMENT AD WHERE AD.APP_UID = '$caseId'
5. AND AD.APP_DOC_UID = C.CON_ID AND C.CON_VALUE = '$file'";
6. $result = executeQuery($query);
If the query is unable to find the file in the APP_DOCUMENT table, use the same goto structure to redirect back to the upload screen.
1. if (!is_array($result) || count($result) == 0) {
2. $mesg = "Error: Unable to find the uploaded file '$file' in the APP_DOCUMENT table.";
3. goto uploadAgain;
4. }
When the file isn’t empty and can be found within the APP_DOCUMENT table, a unique file path is built to store it on the server, where $result[1][‘CON_ID’] is the UID of the uploaded file (from the query above). Then the file() PHP function is used to convert the file contents into an array of arrays containing each line of the .csv file.
$g = new G();
$caseIdPath = method_exists(G, "getPathFromUID") ? $g - > getPathFromUID($caseId) : $caseId;
$filePath = PATH_DOCUMENT.$caseIdPath.PATH_SEP.$result[1]['CON_ID'].
'_1.'.pathinfo($file, PATHINFO_EXTENSION);
$aRecords = file($filePath); //Open the CSV file as an array for each line
Now that the data is stored in an array, loop through each line of the array and parse into its individual fields using the explode() PHP function. This function will take a string of a delimiter separated values (comma, semi-colon, tab, etc…) and splits the string into an array containing the individual values. This array is then used to populate each part of your grid.
1. //Initialize the grid variable.
2. @=weatherGrid = array(); //Loop through the array and for each record, break the line into its individual fields.
3. for ($i = 1; $i < count($aRecords); $i++) {
4. $aFields = explode(‘,’ , $aRecords[$i]); //change the fields names to match your grid:
5. @=weatherGrid[$i] = array(
6. 'dateGrid' => trim($aFields[0], "\t\n\r\" "), //datetime
7. 'minTempGrid' => trim($aFields[1], "\t\n\r\" "), //float
8. 'maxTempGrid' => trim($aFields[2], "\t\n\r\" "), //float
9. 'mistGrid' => (trim($aFields[3], "\t\n\r\" ") == 'Yes' ? true : false), //checkbox
10. 'rainGrid' => trim($aFields[4], "\t\n\r\" "), //textbox
11. 'snowGrid' => (trim($aFields[5], "\t\n\r\" ") == 'Yes' ? true : false) //checkbox
12. );
13. } //end loop
14. goto endScript; //where the endScript goto structure is endScript: and is located at the last line of the trigger.
(this will skip the redirect code and go straight to the end of the trigger).
A full example trigger can be found under “Importing an Uploaded CSV File into a Grid” in ProcessMaker’s documentation ( ProcessMaker 2.0 Files ).
1. $inpDocQuery = "SELECT APP_DOC_UID, DOC_VERSION
2. FROM APP_DOCUMENT WHERE APP_UID = '$caseId'
3. //The input doc UID (@@csvFileId) obtained from a separate trigger
4. AND DOC_UID = '".@@csvFileId."'
5. AND APP_DOC_STATUS = 'ACTIVE'";
6.
7. $g = new G();
8. $caseIdPath = method_exists(G, "getPathFromUID") ? $g - > getPathFromUID($caseId) : $caseId;
9. $filePath = PATH_DOCUMENT.$caseIdPath.PATH_SEP.$result[1][‘APP_DOC_UID’].
10. '_1.'.pathinfo($file, PATHINFO_EXTENSION);
Copyright© 2024 IFS AB. Copying prohibited. All rights reserved.