Example #1
0
 /**
  * DataSet Import CSV
  * @return <XiboAPIResponse>
  */
 public function DataSetImportCsv()
 {
     // Auth
     if (!$this->user->PageAuth('dataset')) {
         return $this->Error(1, 'Access Denied');
     }
     $dataSetId = $this->GetParam('dataSetId', _INT);
     $auth = $this->user->DataSetAuth($dataSetId, true);
     if (!$auth->edit) {
         return $this->Error(1, 'Access Denied');
     }
     // Expect a file id
     $fileId = $this->GetParam('fileId', _INT);
     if (!$this->user->FileAuth($fileId)) {
         return $this->Error(1, 'Access Denied');
     }
     $file = new File();
     if (!($csvFileLocation = $file->GetPath($fileId))) {
         return $this->Error($file->GetErrorNumber(), $file->GetErrorMessage());
     }
     // Other parameters
     // Filter using HTML string because _STRING strips some of the JSON characters.
     $spreadSheetMapping = $this->GetParam('spreadSheetMapping', _HTMLSTRING);
     $overwrite = $this->GetParam('overwrite', _INT);
     $ignoreFirstRow = $this->GetParam('ignoreFirstRow', _INT);
     // Convert the spread sheet mapping into an Array
     $spreadSheetMapping = json_decode($spreadSheetMapping, true);
     // Check that the columns match the columns for this dataset
     $dataSetColumnObject = new DataSetColumn();
     // Make an array with the datasetcolumnid as the key
     $columns = array();
     foreach ($dataSetColumnObject->GetColumns($dataSetId) as $col) {
         $columns[$col['datasetcolumnid']] = true;
     }
     // Look through each column we have been provided and see if it matches
     foreach ($spreadSheetMapping as $key => $value) {
         if (!array_key_exists($value, $columns)) {
             return $this->Error(1000, __('The column mappings you have provided are invalid. Please ensure you have the correct DataSetColumnIDs.'));
         }
     }
     $dataSetObject = new DataSetData();
     if (!$dataSetObject->ImportCsv($dataSetId, $csvFileLocation, $spreadSheetMapping, $overwrite == 1, $ignoreFirstRow == 1)) {
         return $this->Error($dataSetObject->GetErrorNumber(), $dataSetObject->GetErrorMessage());
     }
     return $this->Respond($this->ReturnId('success', true));
 }