mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-11-28 02:20:41 +00:00
156 lines
6.5 KiB
PHP
156 lines
6.5 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 Log_file;
|
|
|
|
class AddfolderCommand extends Command
|
|
{
|
|
protected $settings;
|
|
|
|
protected $logger;
|
|
|
|
protected $translator;
|
|
|
|
public function __construct(Settings $settings, Log_file $logger, Translator $translator)
|
|
{
|
|
$this->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("<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;
|
|
}
|
|
|
|
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 folder name.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
$comment = $input->getOption('comment');
|
|
|
|
if($folderid = $input->getOption('parent')) {
|
|
$folder = $dms->getFolder((int) $folderid);
|
|
if (!is_object($folder)) {
|
|
$output->writeln("<error>Could not find specified parent folder.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
} else {
|
|
$output->writeln("<error>You need to specify a parent folder.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
if ($folder->getAccessMode($user) < M_READWRITE) {
|
|
$output->writeln("<error>Not sufficient access rights.</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;
|
|
}
|
|
$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("<error>Could not create folder</error>");
|
|
return Command::FAILURE;
|
|
} else {
|
|
// Send notification to subscribers.
|
|
if ($notifier) {
|
|
$output->writeln(sprintf('<comment>Sending notifications ...</comment>'), 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('<info>New folder with id=%d created.</info>', $subFolder->getId()), OutputInterface::VERBOSITY_QUIET);
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|
|
|
|
// vim: sw=4 ts=4 expandtab
|