settings = $settings;
$this->logger = $logger;
$this->translator = $translator;
parent::__construct();
}
protected function configure()
{
$this->setName('dms:addfolder')
->setDescription('Create a new folder')
->setHelp('Create a new folder below the given parent folder')
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Owner of new folder.', '')
->addOption('parent', 'F', InputOption::VALUE_REQUIRED, 'Parent folder.', null)
->addOption('name', '', InputOption::VALUE_REQUIRED, 'Name of folder.', null)
->addOption('comment', 'c', InputOption::VALUE_REQUIRED, 'Comment of folder.', '')
->addOption('sequence', 's', InputOption::VALUE_REQUIRED, 'Sequence of folder.', 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;
}
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 folder name.");
return Command::FAILURE;
}
$comment = $input->getOption('comment');
if($folderid = $input->getOption('parent')) {
$folder = $dms->getFolder((int) $folderid);
if (!is_object($folder)) {
$output->writeln("Could not find specified parent folder.");
return Command::FAILURE;
}
} else {
$output->writeln("You need to specify a parent folder.");
return Command::FAILURE;
}
if ($folder->getAccessMode($user) < M_READWRITE) {
$output->writeln("Not sufficient access rights.");
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;
}
$minmax = $folder->getFoldersMinMax();
if($sequence == 'start') {
$seq = $minmax['min'] - 1;
} elseif ($sequence == 'end') {
$seq = $minmax['max'] + 1;
} else {
$seq = $sequence;
}
require_once('inc/inc.DBInit.php');
require_once('inc/inc.Extension.php');
require_once("inc/inc.ClassController.php");
$controller = \Controller::factory('AddSubFolder', array('dms'=>$dms, 'user'=>$user, 'extmgr'=>$extmgr, 'settings'=>$this->settings));
$controller->setParam('folder', $folder);
$controller->setParam('name', $name);
$controller->setParam('comment', $comment);
$controller->setParam('sequence', $seq);
$controller->setParam('attributes', array());
$controller->setParam('notificationgroups', array());
$controller->setParam('notificationusers', array());
if (!$subFolder = $controller->run()) {
output->writeln("Could not create folder");
return Command::FAILURE;
} else {
// Send notification to subscribers.
if ($notifier) {
$output->writeln(sprintf('Sending notifications ...'), OutputInterface::VERBOSITY_VERBOSE);
$fnl = $folder->getNotifyList();
$snl = $subFolder->getNotifyList();
$nl = array(
'users'=>array_unique(array_merge($snl['users'], $fnl['users']), SORT_REGULAR),
'groups'=>array_unique(array_merge($snl['groups'], $fnl['groups']), SORT_REGULAR)
);
$subject = "new_subfolder_email_subject";
$message = "new_subfolder_email_body";
$params = array();
$params['name'] = $subFolder->getName();
$params['folder_name'] = $folder->getName();
$params['folder_path'] = $folder->getFolderPathPlain();
$params['username'] = $user->getFullName();
$params['comment'] = $comment;
$params['url'] = $settings->getBaseUrlWithRoot()."out/out.ViewFolder.php?folderid=".$subFolder->getID();
$params['sitename'] = $settings->_siteName;
$params['http_root'] = $settings->_httpRoot;
$notifier->toList($user, $nl["users"], $subject, $message, $params);
foreach ($nl["groups"] as $grp) {
$notifier->toGroup($user, $grp, $subject, $message, $params);
}
}
}
$output->writeln(sprintf('New folder with id=%d created.', $subFolder->getId()), OutputInterface::VERBOSITY_QUIET);
return Command::SUCCESS;
}
}
// vim: sw=4 ts=4 expandtab