mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-11-28 02:20:41 +00:00
281 lines
13 KiB
PHP
281 lines
13 KiB
PHP
<?php
|
|
namespace Seeddms\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Seeddms\Seeddms\Settings;
|
|
use Seeddms\Seeddms\Translator;
|
|
use SeedDMS_Extension_Mgr;
|
|
use Log_file;
|
|
use finfo;
|
|
|
|
class AdddocumentCommand extends Command
|
|
{
|
|
protected $settings;
|
|
|
|
protected $logger;
|
|
|
|
protected $translator;
|
|
|
|
protected $extmgr;
|
|
|
|
public function __construct(Settings $settings, Log_file $logger, Translator $translator, SeedDMS_Extension_Mgr $extmgr)
|
|
{
|
|
$this->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("<comment>Using configuration from '".$settings->_configFilePath."'.</comment>", OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
if (!is_writable($settings->_contentDir)) {
|
|
$output->writeln(sprintf("<error>The content dir '%s' is not writable by the system user running this script.</error>", $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("<error>File '%s' has zero size</error>", $filename));
|
|
return Command::FAILURE;
|
|
}
|
|
} else {
|
|
$output->writeln(sprintf("<error>File '%s' is not readable</error>", $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("<error>No such user '%s'</error>", $username));
|
|
return Command::FAILURE;
|
|
}
|
|
} else {
|
|
$user = $dms->getUser(1);
|
|
}
|
|
$dms->setUser($user);
|
|
$output->writeln("<comment>Running as user ".$user->getLogin().".</comment>", OutputInterface::VERBOSITY_VERBOSE);
|
|
|
|
$name = $input->getOption('name');
|
|
if (!$name) {
|
|
$output->writeln("<error>You need to specify a document name.</error>");
|
|
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("<error>Could not find specified parent folder.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
if ($folder->getAccessMode($user) < M_READWRITE) {
|
|
$output->writeln("<error>Not sufficient access rights.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
} elseif ($documentid = $input->getOption('document')) {
|
|
$document = $dms->getDocument((int) $documentid);
|
|
if (!is_object($document)) {
|
|
$output->writeln("<error>Could not find specified document.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
if ($document->getAccessMode($user) < M_READWRITE) {
|
|
$output->writeln("<error>Not sufficient access rights.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
} else {
|
|
$output->writeln("<error>You need to specify a parent folder or a document to be updated.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
$sequence = $input->getOption('sequence');
|
|
if (!$sequence) {
|
|
$sequence = $this->settings->_defaultFolderPosition;
|
|
}
|
|
if (!is_numeric($sequence) && $sequence != 'start' && $sequence != 'end') {
|
|
$output->writeln("<error>Sequence must be a numeric value, 'start', or 'end'</error>");
|
|
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("<error>Category '%s' not found.</error>", $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("<error>Document attribute %s not understood</error>", $thisAttribute));
|
|
return Command::FAILURE;
|
|
}
|
|
$attrdef = $dms->getAttributeDefinitionByName($attrKey);
|
|
if (!$attrdef) {
|
|
$output->writeln(sprintf("<error>Document attribute '%s' unknown</error>", $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("<error>Document attribute %s not understood</error>", $thisAttribute));
|
|
return Command::FAILURE;
|
|
}
|
|
$attrdef = $dms->getAttributeDefinitionByName($attrKey);
|
|
if (!$attrdef) {
|
|
$output->writeln(sprintf("<error>Document attribute '%s' unknown</error>", $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("<error>Could not create document</error>");
|
|
return Command::FAILURE;
|
|
} else {
|
|
// Send notification to subscribers.
|
|
if ($notifier && !$nonotifications) {
|
|
$output->writeln(sprintf('<comment>Sending notifications ...</comment>'), OutputInterface::VERBOSITY_VERBOSE);
|
|
$notifier->sendNewDocumentMail($document, $user);
|
|
}
|
|
}
|
|
$output->writeln(sprintf('<info>New document with id=%d created.</info>', $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("<error>Could not add version to document</error>");
|
|
return Command::FAILURE;
|
|
} else {
|
|
// Send notification to subscribers.
|
|
if ($notifier && !$nonotifications) {
|
|
$output->writeln(sprintf('<comment>Sending notifications ...</comment>'));
|
|
$notifier->sendNewDocumentVersionMail($document, $user);
|
|
}
|
|
}
|
|
$output->writeln(sprintf('<info>New document version %d of document with id=%d created.</info>', $content->getVersion(), $document->getId()), OutputInterface::VERBOSITY_QUIET);
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|
|
}
|
|
|
|
// vim: sw=4 ts=4 expandtab
|