settings = $settings; $this->logger = $logger; $this->translator = $translator; $this->extmgr = $extmgr; parent::__construct(); } protected function configure() { $this->setName('dms:adddocument') ->setDescription('Create a new document') ->setHelp('Create a new document below the given parent folder') ->addArgument('filename', InputArgument::REQUIRED, 'Name of file to be uploaded.') ->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Owner of new document.', '') ->addOption('no-notifications', '', InputOption::VALUE_NONE, 'Do not send notifications.') ->addOption('folder', 'F', InputOption::VALUE_REQUIRED, 'Parent folder.', null) ->addOption('document', 'D', InputOption::VALUE_REQUIRED, 'Document to be updated.', null) ->addOption('name', '', InputOption::VALUE_REQUIRED, 'Name of document.', null) ->addOption('comment', 'c', InputOption::VALUE_REQUIRED, 'Comment of document.', '') ->addOption('mimetype', 't', InputOption::VALUE_REQUIRED, 'Mime type of document.', '') ->addOption('sequence', 's', InputOption::VALUE_REQUIRED, 'Sequence of document.', null) ->addOption('keyword', 'k', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Keyword.', null) ->addOption('category', 'K', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Category.', null) ->addOption('attribute', 'a', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Document attribute.', null) ->addOption('version_attribute', 'A', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Document version attribute.', null) ; } protected function execute(InputInterface $input, OutputInterface $output) : int { $settings = $this->settings; $logger = $this->logger; $translator = $this->translator; $output->writeln("Using configuration from '".$settings->_configFilePath."'.", OutputInterface::VERBOSITY_VERBOSE); if (!is_writable($settings->_contentDir)) { $output->writeln(sprintf("The content dir '%s' is not writable by the system user running this script.", $settings->_contentDir)); return Command::FAILURE; } $filename = $input->getArgument('filename'); $mymimetype = $input->getOption('mimetype'); /* Check if file is readable {{{ */ if (is_readable($filename)) { if (filesize($filename)) { $finfo = new finfo(FILEINFO_MIME_TYPE); if (!$mymimetype) { $mymimetype = $finfo->file($filename); } $filetype = "." . pathinfo($filename, PATHINFO_EXTENSION); } else { $output->writeln(sprintf("File '%s' has zero size", $filename)); return Command::FAILURE; } } else { $output->writeln(sprintf("File '%s' is not readable", $filename)); return Command::FAILURE; } /* }}} */ $nonotifications = $input->getOption('no-notifications'); require_once('inc/inc.DBInit.php'); $username = $input->getOption('user'); if ($username) { if (!($user = $dms->getUserByLogin($username))) { $output->writeln(sprintf("No such user '%s'", $username)); return Command::FAILURE; } } else { $user = $dms->getUser(1); } $dms->setUser($user); $output->writeln("Running as user ".$user->getLogin().".", OutputInterface::VERBOSITY_VERBOSE); $name = $input->getOption('name'); if (!$name) { $output->writeln("You need to specify a document name."); return Command::FAILURE; } $comment = $input->getOption('comment'); $folder = $document = null; if ($folderid = $input->getOption('folder')) { $folder = $dms->getFolder((int) $folderid); if (!is_object($folder)) { $output->writeln("Could not find specified parent folder."); return Command::FAILURE; } if ($folder->getAccessMode($user) < M_READWRITE) { $output->writeln("Not sufficient access rights."); return Command::FAILURE; } } elseif ($documentid = $input->getOption('document')) { $document = $dms->getDocument((int) $documentid); if (!is_object($document)) { $output->writeln("Could not find specified document."); return Command::FAILURE; } if ($document->getAccessMode($user) < M_READWRITE) { $output->writeln("Not sufficient access rights."); return Command::FAILURE; } } else { $output->writeln("You need to specify a parent folder or a document to be updated."); return Command::FAILURE; } $sequence = $input->getOption('sequence'); if (!$sequence) { $sequence = $this->settings->_defaultFolderPosition; } if (!is_numeric($sequence) && $sequence != 'start' && $sequence != 'end') { $output->writeln("Sequence must be a numeric value, 'start', or 'end'"); return Command::FAILURE; } if ($folder) { $minmax = $folder->getDocumentsMinMax(); if ($sequence == 'start') { $seq = $minmax['min'] - 1; } elseif ($sequence == 'end') { $seq = $minmax['max'] + 1; } else { $seq = $sequence; } } /* Parse categories {{{ */ $cats = $input->getOption('category'); if ($cats) { foreach($cats as $categoryname) { $cat = $dms->getDocumentCategoryByName($categoryname); if ($cat) { $categories[] = $cat; } else { $output->writeln(sprintf("Category '%s' not found.", $categoryname)); return Command::FAILURE; } } } /* }}} */ /* Parse document attributes. {{{ */ $document_attributes = array(); $attributes = $input->getOption('attribute'); if ($attributes) { foreach ($attributes as $thisAttribute) { $attrKey = strstr($thisAttribute, '=', true); $attrVal = substr(strstr($thisAttribute, '='), 1); if (empty($attrKey) || empty($attrVal)) { $output->writeln(sprintf("Document attribute %s not understood", $thisAttribute)); return Command::FAILURE; } $attrdef = $dms->getAttributeDefinitionByName($attrKey); if (!$attrdef) { $output->writeln(sprintf("Document attribute '%s' unknown", $attrKey)); return Command::FAILURE; } $document_attributes[$attrdef->getID()] = $attrVal; } } /* }}} */ /* Parse version attributes. {{{ */ $version_attributes = array(); $attributes = $input->getOption('version_attribute'); if ($attributes) { foreach ($attributes as $thisAttribute) { $attrKey = strstr($thisAttribute, '=', true); $attrVal = substr(strstr($thisAttribute, '='), 1); if (empty($attrKey) || empty($attrVal)) { $output->writeln(sprintf("Document attribute %s not understood", $thisAttribute)); return Command::FAILURE; } $attrdef = $dms->getAttributeDefinitionByName($attrKey); if (!$attrdef) { $output->writeln(sprintf("Document attribute '%s' unknown", $attrKey)); return Command::FAILURE; } $version_attributes[$attrdef->getID()] = $attrVal; } } /* }}} */ // return Command::FAILURE; $keywords = implode(' ', $input->getOption('keyword')); $expires = false; $reviewers = array(); $approvers = array(); require_once("inc/inc.ClassController.php"); if ($folder) { $controller = \Controller::factory('AddDocument', array('dms'=>$dms, 'user'=>$user, 'extmgr'=>$this->extmgr, 'settings'=>$this->settings)); $controller->setParam('documentsource', 'script'); $controller->setParam('folder', $folder); $controller->setParam('fulltextservice', $fulltextservice); $controller->setParam('name', $name); $controller->setParam('comment', $comment); $controller->setParam('expires', $expires); $controller->setParam('keywords', $keywords); $controller->setParam('categories', $categories); $controller->setParam('owner', $user); $controller->setParam('sequence', $seq); $controller->setParam('userfiletmp', $filename); $controller->setParam('userfilename', basename($filename)); $controller->setParam('filetype', $filetype); $controller->setParam('userfiletype', $mymimetype); $controller->setParam('reviewers', $reviewers); $controller->setParam('approvers', $approvers); $controller->setParam('attributes', array()); $controller->setParam('notificationgroups', array()); $controller->setParam('notificationusers', array()); if (!$document = $controller->run()) { output->writeln("Could not create document"); return Command::FAILURE; } else { // Send notification to subscribers. if ($notifier && !$nonotifications) { $output->writeln(sprintf('Sending notifications ...'), OutputInterface::VERBOSITY_VERBOSE); $notifier->sendNewDocumentMail($document, $user); } } $output->writeln(sprintf('New document with id=%d created.', $document->getId()), OutputInterface::VERBOSITY_QUIET); return Command::SUCCESS; } elseif ($document) { $controller = \Controller::factory('UpdateDocument', array('dms'=>$dms, 'user'=>$user)); $controller->setParam('folder', $document->getFolder()); $controller->setParam('document', $document); $controller->setParam('fulltextservice', $fulltextservice); $controller->setParam('comment', $comment); $controller->setParam('userfiletmp', $filename); $controller->setParam('userfilename', basename($filename)); $controller->setParam('filetype', $filetype); $controller->setParam('userfiletype', $mymimetype); $controller->setParam('reviewers', $reviewers); $controller->setParam('approvers', $approvers); $controller->setParam('attributes', $version_attributes); $controller->setParam('workflow', null); if (!$content = $controller->run()) { output->writeln("Could not add version to document"); return Command::FAILURE; } else { // Send notification to subscribers. if ($notifier && !$nonotifications) { $output->writeln(sprintf('Sending notifications ...')); $notifier->sendNewDocumentVersionMail($document, $user); } } $output->writeln(sprintf('New document version %d of document with id=%d created.', $content->getVersion(), $document->getId()), OutputInterface::VERBOSITY_QUIET); return Command::SUCCESS; } } } // vim: sw=4 ts=4 expandtab