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\Output\OutputInterface; 14 15class TrackerImportCommand extends Command 16{ 17 protected function configure() 18 { 19 $this 20 ->setName('tracker:import') 21 ->setDescription('Import a CSV file into a tracker using a tracker tabular format') 22 ->addArgument( 23 'tabularId', 24 InputArgument::REQUIRED, 25 'ID of tracker tabular format to use' 26 ) 27 ->addArgument( 28 'filename', 29 InputArgument::REQUIRED, 30 'Location of CSV file to import' 31 ); 32 } 33 34 protected function execute(InputInterface $input, OutputInterface $output) 35 { 36 37 $output->writeln('Importing tracker...'); 38 39 $lib = \TikiLib::lib('tabular'); 40 $info = $lib->getInfo($input->getArgument('tabularId')); 41 42 $perms = \Perms::get('tabular', $info['tabularId']); 43 if (! $info || ! $perms->tabular_import) { 44 throw new \Exception('Tracker Import: Tabular Format not found'); 45 } 46 47 $fileName = $input->getArgument('filename'); 48 if (! file_exists($fileName)) { 49 throw new \Exception('Tracker Import: File not found'); 50 } 51 52 // from \Services_Tracker_TabularController::getSchema TODO refactor? 53 $tracker = \Tracker_Definition::get($info['trackerId']); 54 55 if (! $tracker) { 56 throw new \Exception('Tracker Import: Tracker not found'); 57 } 58 59 $schema = new \Tracker\Tabular\Schema($tracker); 60 $schema->loadFormatDescriptor($info['format_descriptor']); 61 $schema->loadFilterDescriptor($info['filter_descriptor']); 62 $schema->loadConfig($info['config']); 63 64 $schema->validate(); 65 66 if (! $schema->getPrimaryKey()) { 67 throw new \Exception(tr('Primary Key required')); 68 } 69 70 // this will throw exceptions and not return if there's a problem 71 $source = new \Tracker\Tabular\Source\CsvSource($schema, $fileName); 72 $writer = new \Tracker\Tabular\Writer\TrackerWriter; 73 $writer->write($source); 74 75 \Feedback::printToConsole($output); 76 77 $output->writeln('Import done'); 78 79 return(0); 80 } 81} 82