1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\Command;
9
10use Symfony\Component\Console\Command\Command;
11use Symfony\Component\Console\Input\InputArgument;
12use Symfony\Component\Console\Input\InputInterface;
13use Symfony\Component\Console\Input\InputOption;
14use Symfony\Component\Console\Output\OutputInterface;
15
16class FilesCopyCommand extends Command
17{
18	protected function configure()
19	{
20		$this
21			->setName('files:copy')
22			->setDescription(tra('Copy files from file galleries to a regular directory on the file system'))
23			->addArgument(
24				'galleryId',
25				InputArgument::REQUIRED,
26				'Gallery to copy files from'
27			)
28			->addArgument(
29				'destinationPath',
30				InputArgument::REQUIRED,
31				'Path to copy files to'
32			)
33		;
34	}
35
36	protected function execute(InputInterface $input, OutputInterface $output)
37	{
38		global $prefs;
39
40		if ($prefs['feature_file_galleries'] != 'y') {
41			throw new \Exception(tra('Feature Galleries not set up'));
42		}
43
44		$filegallib = \TikiLib::lib('filegal');
45		$filegalcopylib = \TikiLib::lib('filegalcopy');
46
47		$galleryId = (int) $input->getArgument('galleryId');
48
49		$gal_info = $filegallib->get_file_gallery($galleryId);
50		if (! $gal_info || empty($gal_info['name'])) {
51			throw new \Exception(tr('File Copy: Gallery #%0 not found', $galleryId));
52		}
53
54		$destinationPath = $input->getArgument('destinationPath');
55		if (empty($destinationPath)) {
56			throw new \Exception(tra('File Copy: Destination path required'));
57		}
58
59		$files = $filegallib->get_files_info_from_gallery_id($galleryId);
60		if (! $files) {
61			if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
62				$output->writeln('<comment>' . tra('No files to copy') . '</comment>');
63			}
64			return;
65		}
66
67		if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
68			$output->writeln('<comment>' . tra('File Copy starting...') . '</comment>');
69		}
70
71		$feedback = $filegalcopylib->processCopy($files, $destinationPath);
72		foreach ($feedback as $message) {
73			$error = strpos($message, '<span class="text-danger">') !== false;
74			$message = strip_tags(str_replace('<br>', ' : ', $message));
75			if ($error) {
76				$message = "<error>$message</error>";
77				$output->writeln($message);
78			} elseif ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
79				$message = "<info>$message</info>";
80				$output->writeln($message);
81			}
82		}
83
84		if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
85			$output->writeln('<comment>' . tra('File Copy complete') . '</comment>');
86		}
87	}
88}
89