}
    }
    if ($foundInSets == 0) {
        $state = 0;
    } else {
        if ($foundInSets == count($files)) {
            $state = 2;
        } else {
            $state = 1;
        }
    }
    $fs->state = $state;
    $sets[] = $fs;
}
if ($_POST['task'] == 'add_to_sets') {
    $r = new \Concrete\Core\File\EditResponse();
    $r->setFiles($files);
    $r->setMessage(t('File sets saved successfully.'));
    foreach ($_POST as $key => $value) {
        if (preg_match('/fsID:/', $key)) {
            $fsIDst = explode(':', $key);
            $fsID = $fsIDst[1];
            // so the affected file set is $fsID, the state of the thing is $value
            $fs = FileSet::getByID($fsID);
            $fsp = new Permissions($fs);
            if ($fsp->canAddFile($f)) {
                switch ($value) {
                    case '0':
                        foreach ($files as $f) {
                            $fs->removeFileFromSet($f);
                        }
 public function upload_files()
 {
     $files = array();
     if ($this->token->validate('upload_files')) {
         $r = $this->entityManager->getRepository('\\PortlandLabs\\Concrete5\\MigrationTool\\Entity\\Import\\Batch');
         $batch = $r->findOneById($this->request->request('id'));
         if (is_object($batch)) {
             $cf = \Core::make('helper/file');
             $fp = \FilePermissions::getGlobal();
             if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
                 if (!$fp->canAddFileType($cf->getExtension($_FILES['file']['name']))) {
                     throw new \Exception(Importer::getErrorMessage(Importer::E_FILE_INVALID_EXTENSION));
                 } else {
                     $ih = new Importer();
                     $response = $ih->import($_FILES['file']['tmp_name'], $_FILES['file']['name']);
                     if (!$response instanceof \Concrete\Core\File\Version) {
                         throw new \Exception(Importer::getErrorMessage($response));
                     } else {
                         $file = $response->getFile();
                         $fs = Set::getByName($batch->getID());
                         if (!is_object($fs)) {
                             $fs = Set::createAndGetSet($batch->getID(), Set::TYPE_PRIVATE);
                         }
                         $fs->addFileToSet($file);
                         $files[] = $file;
                     }
                 }
             }
         }
     }
     $this->flash('success', t('File(s) uploaded successfully'));
     $r = new \Concrete\Core\File\EditResponse();
     $r->setFiles($files);
     $r->outputJSON();
 }