Skip to content

Excel Import & Export Feature #49

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

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add only and exclude options. Some Refactoring.
  • Loading branch information
mustafaaloko committed Jun 1, 2016
commit c8a830faa925fdf00bb1cc1e9c38959bd1920ac0
47 changes: 39 additions & 8 deletions src/Commands/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use League\Csv\Writer;
use League\Csv\Reader;
use Themsaid\Langman\Manager;
use Illuminate\Support\Str;

class ExportCommand extends Command
{
Expand All @@ -18,7 +14,9 @@ class ExportCommand extends Command
* @var string
*/
protected $signature = 'langman:export
{--P|path= : The location where the CSV file should be exported.}';
{--P|path= : The location where the CSV file should be exported.}
{--only= : Specify the file(s) you want to export to CSV}
{--exclude= : File(s) you do not want to export to CSV}';

/**
* The name and signature of the console command.
Expand Down Expand Up @@ -75,6 +73,37 @@ public function handle()
$this->info('CSV file successfully generated in ' . $path .'.');
}

/**
* Filter files based on user options
*
* @return array|string
*/
protected function filterFilesForCsvExport()
{
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.');
exit();
}

$availableFiles = $this->manager->files();

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

// We will only return files for those keys which a file exist for.
return array_intersect_key($this->manager->files(), array_combine($onlyFiles, $onlyFiles));
}

if (! is_null($this->option('exclude'))) {
$excludeFiles = explode(',', $this->option('exclude'));

// We will only return files for those keys which a file exist for.
return array_diff_key($this->manager->files(), array_combine($excludeFiles, $excludeFiles));
}

return null;
}

/**
* Generates a CSV file from translations files and putting it in
* the given path.
Expand All @@ -86,7 +115,9 @@ private function generateCsvFile($path = null)
{
$csvPath = $this->getCsvPath($path);

$this->writeContentToCsvFile($this->getHeaderContent(), $this->getBodyContent(), $csvPath);
$userSelectedFiles = $this->filterFilesForCsvExport();

$this->writeContentToCsvFile($this->getHeaderContent(), $this->getBodyContent($userSelectedFiles), $csvPath);

return $csvPath;
}
Expand Down Expand Up @@ -170,9 +201,9 @@ protected function getHeaderContent()
* @return array
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function getBodyContent()
protected function getBodyContent($files)
{
$langFiles = $this->manager->getFilesContentGroupedByFilenameAndKey();
$langFiles = $this->manager->getFilesContentGroupedByFilenameAndKey($files);
$content = [];

foreach ($langFiles as $langFileName => $langProps) {
Expand Down
5 changes: 3 additions & 2 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,12 +373,13 @@ public function getKeysExistingInALanguageButNotTheOther($values)
* @return array
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function getFilesContentGroupedByFilenameAndKey()
public function getFilesContentGroupedByFilenameAndKey(array $selectedFiles = null)
{
$files = is_null($selectedFiles) ? $this->files() : $selectedFiles;
$allLangs = [];
$filesContent = [];

foreach ($this->files() as $langFileName => $langFilePath) {
foreach ($files as $langFileName => $langFilePath) {
foreach ($langFilePath as $languageKey => $file) {
foreach ($filesContent[$languageKey] = Arr::dot($this->getFileContent($file)) as $key => $value) {
$allLangs[$langFileName][$key]['key'] = $key;
Expand Down