Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excel Import & Export Feature #49

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6f91c3c
Initial commit for export command
mustafaaloko May 30, 2016
67e082f
Merge remote-tracking branch 'upstream/master' into excel-integration
mustafaaloko May 30, 2016
4bd8cac
Add config file. Refactoring ExportCommand class
mustafaaloko May 30, 2016
683e498
Fix Arabic/Persian UTF8 issues in CSV. Some refactoring.
mustafaaloko May 31, 2016
c8a830f
Add only and exclude options. Some Refactoring.
mustafaaloko Jun 1, 2016
c3fe068
Moving functions to right place
mustafaaloko Jun 1, 2016
172721a
Add import feature
mustafaaloko Jul 29, 2016
839eb14
Change from CSV to Excel
mustafaaloko Jul 29, 2016
5428139
Add PHPExcel
mustafaaloko Aug 8, 2016
b359a9c
Complete CSV to Excel export conversion
mustafaaloko Aug 8, 2016
cc32d68
Change from CSV to Excel
mustafaaloko Aug 11, 2016
c110dc0
Temp commit
mustafaaloko Aug 11, 2016
9576921
Complete import feature
mustafaaloko Aug 11, 2016
3979715
Merge with langman upstream
mustafaaloko Aug 11, 2016
947ec6e
Add comments
mustafaaloko Aug 11, 2016
2b92097
Change help for commands
mustafaaloko Aug 11, 2016
09ebed4
Pop header row from file contents
mustafaaloko Aug 11, 2016
d85df9b
Fix CS.
mustafaaloko Aug 11, 2016
fabc62c
Fix CS
mustafaaloko Aug 11, 2016
9d9edb8
Fix CS
mustafaaloko Aug 11, 2016
7cf6923
Remove test temp files
mustafaaloko Aug 11, 2016
237b465
Fix CS
mustafaaloko Aug 11, 2016
2ecc038
Fix CS
mustafaaloko Aug 11, 2016
d420bea
Fix CS
mustafaaloko Aug 11, 2016
17656eb
Fix CS
mustafaaloko Aug 11, 2016
2632628
Merge branch 'excel-import-export' of github.com:mustafaaloko/laravel…
mustafaaloko Aug 11, 2016
d6bdaa5
Add test temp folder
mustafaaloko Aug 11, 2016
25a944f
Fix CS
mustafaaloko Aug 11, 2016
bdc0f16
Merge with upstream
mustafaaloko Sep 9, 2016
7442266
Remove comment from test
mustafaaloko Sep 9, 2016
aaa40d8
Write initial tests for export command
mustafaaloko Sep 9, 2016
223665c
Test cleanup
mustafaaloko Sep 9, 2016
9b5c191
Refactor tests
mustafaaloko Sep 9, 2016
554b929
Add .gitignore back to temp folder
mustafaaloko Sep 9, 2016
4dfb816
Checkout .gitignore to old version
mustafaaloko Sep 9, 2016
0219786
Add tests for only option of export command
mustafaaloko Sep 10, 2016
63392d8
Complete tests for export command
mustafaaloko Sep 10, 2016
b514f1a
Test for combining exclude and only option of export command
mustafaaloko Sep 10, 2016
e4640b2
Initial import command tests
mustafaaloko Sep 10, 2016
ffdacfe
Complete import command tests
mustafaaloko Sep 10, 2016
b237efe
Fix CS
mustafaaloko Sep 10, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"php": "^5.5.9 || ^7.0",
"illuminate/support": "5.1.* || 5.2.* || 5.3.*",
"illuminate/console": "5.1.* || 5.2.* || 5.3.*",
"illuminate/filesystem": "5.1.* || 5.2.* || 5.3.*"
"illuminate/filesystem": "5.1.* || 5.2.* || 5.3.*",
"phpoffice/phpexcel": "^1.8"
},
"require-dev": {
"phpunit/phpunit" : "^4.8 || ^5.0",
Expand Down
12 changes: 12 additions & 0 deletions config/langman.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@
*/

'path' => realpath(base_path('resources/lang')),

/*
* --------------------------------------------------------------------------
* Path to the directory where Excel files should be exported
* --------------------------------------------------------------------------
*
* This option determines where to put the exported Excel files. This directory
* must be writable by server. By default storage/langman directory
* will be used.
*/

'exports_path' => storage_path('langman-exports'),
];
211 changes: 211 additions & 0 deletions src/Commands/ExportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php

namespace Themsaid\Langman\Commands;

use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Themsaid\Langman\Manager;

class ExportCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'langman:export
{--P|path= : The location where the exported file should be saved (Default: storage/langman-exports).}
{--only= : Specify the file(s) you want to export to Excel}
{--exclude= : Specify the file(s) you do NOT want to export to Excel}';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $description = 'Generates a Excel file from your language files';

/**
* The Languages manager instance.
*
* @var \Themsaid\LangMan\Manager
*/
private $manager;

/**
* Array of files grouped by filename.
*
* @var array
*/
protected $files;

/**
* ListCommand constructor.
*
* @param \Themsaid\LangMan\Manager $manager
* @param \Illuminate\Contracts\Filesystem\Filesystem
* @return void
*/
public function __construct(Manager $manager)
{
parent::__construct();

$this->manager = $manager;
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$path = $this->generateExcelFile($this->option('path'));

$this->info('Excel file successfully generated in ' . $path .'.');
}

/**
* Generates an Excel file from translations files and putting it in
* the given path.
*
* @param string|null $path
* @return string
*/
protected function generateExcelFile($path = null)
{
$filePath = $this->getFilePath($path);

$userSelectedFiles = $this->filterFilesForExport();

$this->writeContentsToFile($this->getHeaderContent(), $this->getBodyContent($userSelectedFiles), $filePath);

return $filePath;
}

/**
* Filter files based on user options
*
* @return array|string
*/
protected function filterFilesForExport()
{
if (! is_null($this->option('only')) && ! is_null($this->option('exclude'))) {
$this->error('You cannot combine --only and --exclude options. Please use one of them.');
return;
}

$onlyFiles = [];

if (! is_null($this->option('only'))) {
$onlyFiles = array_keys($this->manager->files(explode(',', $this->option('only'))));
}

if (! is_null($this->option('exclude'))) {
$excludeFiles = explode(',', $this->option('exclude'));
$onlyFiles = array_diff(array_keys($this->manager->files()), $excludeFiles);
}

return $onlyFiles;
}

/**
* Creating a Excel file from the given content.
*
* @param $header
* @param $content
* @param $filepath
* @return void
*/
protected function writeContentsToFile($header, $content, $filepath)
{
array_unshift($content, $header);

$excelObj = new \PHPExcel();
$excelObj->getProperties()
->setTitle('Laravel Langman Exported Language File')
->setSubject('Laravel Langman Exported Language File')
->setCreator('Laravel Langman');

$rowNumber = 1;
foreach ($content as $record) {
$excelObj->getActiveSheet()->fromArray($record, '', 'A'. $rowNumber);
$rowNumber++;
}

$writer = \PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');
$writer->save($filepath);
}

/**
* Get the file path for the CSV file.
*
* @param $path
* @return string
*/
protected function getFilePath($path)
{
$exportDir = is_null($path) ? config('langman.exports_path') : base_path($path);

if (! file_exists($exportDir)) {
mkdir($exportDir, 0755);
}

return $exportDir . '/' . $this->getDatePrefix() . '_langman.xlsx';
}

/*
* Get the date prefix for the Excel file.
*
* @return string
*/
protected function getDatePrefix()
{
return date('Y_m_d_His');
}

/**
* Get the Excel header content.
*
* @return array
*/
protected function getHeaderContent()
{
return array_merge(['Language File', 'Key'], $this->manager->languages());
}

/**
* Get the Excel body content from language files.
*
* @return array
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function getBodyContent($files)
{
$langFiles = $this->manager->getFilesContentGroupedByFilenameAndKey($files);
$content = [];

foreach ($langFiles as $langFileName => $langProps) {
foreach ($langProps as $key => $translations) {
$row = [$langFileName, $key];

foreach ($this->manager->languages() as $language) {
// If an UndefinedIndex Exception was thrown, it means that $key
// does not have translation in the $language, so we will
// handle it by just assigning it to an empty string
try {
// If a translation is just an array (empty), it means that it doesn't have
// any translation so we will skip it by assigning it an empty string.
$row[] = is_array($translations[$language]) ? '' : $translations[$language];
} catch (\ErrorException $ex) {
$row[] = '';
}
}

$content[] = $row;
}
}

return $content;
}
}
Loading