Esempio n. 1
0
 function uploadDocument($doc_name, $ws_id, $path)
 {
     if (str_starts_with($doc_name, "~")) {
         return;
     }
     try {
         DB::beginWork();
         $project = Projects::findById($ws_id);
         //$file = ProjectFiles::findOne(array("conditions" => "`filename` = '$doc_name'"));
         $files = ProjectFiles::getAllByFilename($doc_name, $ws_id);
         if (is_array($files) && count($files) > 0) {
             $file = $files[0];
         } else {
             $file = null;
         }
         if (!$file instanceof ProjectFile) {
             $file = new ProjectFile();
             $file->setFilename($doc_name);
             $file->setIsVisible(true);
             $file->setIsPrivate(false);
             $file->setIsImportant(false);
             $file->setCommentsEnabled(true);
             $file->setAnonymousCommentsEnabled(false);
             //$file->setCreatedOn(new DateTimeValue(time()) );
         }
         $sourcePath = $path . $doc_name;
         $handle = fopen($sourcePath, "r");
         $size = filesize($sourcePath);
         $file_content = fread($handle, $size);
         fclose($handle);
         $file_dt['name'] = $file->getFilename();
         $file_dt['size'] = strlen($file_content);
         $file_dt['tmp_name'] = $sourcePath;
         //TEMP_PATH . DIRECTORY_SEPARATOR . rand() ;
         $extension = trim(get_file_extension($sourcePath));
         $file_dt['type'] = Mime_Types::instance()->get_type($extension);
         if (!trim($file_dt['type'])) {
             $file_dt['type'] = 'text/html';
         }
         $file->save();
         $file->removeFromAllWorkspaces();
         $file->addToWorkspace($project);
         $old_revs = $file->getRevisions();
         foreach ($old_revs as $rev) {
             $rev->delete();
         }
         $revision = $file->handleUploadedFile($file_dt, true, '');
         $file_date = new DateTimeValue(filemtime($sourcePath));
         $revision->setCreatedOn($file_date);
         $revision->setUpdatedOn($file_date);
         $revision->save();
         $file->setCreatedOn($file_date);
         $file->setUpdatedOn($file_date);
         $file->save();
         $ws = $file->getWorkspaces();
         ApplicationLogs::createLog($file, $ws, ApplicationLogs::ACTION_ADD);
         ImportLogger::instance()->log("   File: {$doc_name} [{$ws_id}]");
         print "   File: {$doc_name} [{$ws_id}]\r\n";
         DB::commit();
     } catch (Exception $e) {
         DB::rollback();
         ImportLogger::instance()->logError("{$e}\r\n**************************************************");
         print "\r\n\r\nERROR: {$e}\r\n";
     }
 }
    die('Missing Parameter: file name or directory');
}
if (isset($argv[2])) {
    $parentWorkSpace = $argv[2];
} else {
    ImportLogger::instance()->logError('Missing Parameter: parent workspace id');
    die('Missing Parameter: parent workspace id');
}
if (isset($argv[3])) {
    $user_id = $argv[3];
} else {
    ImportLogger::instance()->logError('Missing Parameter: user id');
    die('Missing Parameter: user id');
}
/* ***************** */
ImportLogger::instance()->log("Init Import ------------------------------------------------------------ \r\n");
$imp = new ZipImport($parentWorkSpace);
$is_zip = str_ends_with($zip_path, ".zip");
if ($is_zip) {
    $imp->extractToTmpDir($zip_path);
} else {
    $imp->setDirectory($zip_path);
}
$imp->initUser($user_id);
$imp->makeWorkSpaces($is_zip ? null : $zip_path);
if ($is_zip) {
    $imp->deleteTmpDir();
}
print "Complete\r\n";
ImportLogger::instance()->log("\r\nEnd Import -------------------------------------------------------------");