-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a command that allows users to remove timesheets older than a specified timeframe. Defaults to 1 year.
- Loading branch information
0 parents
commit db671e3
Showing
11 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
.disabled | ||
composer.lock | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai ArchiveTimesheetsCommandBundle. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KimaiPlugin\ArchiveTimesheetsCommandBundle; | ||
|
||
use App\Plugin\PluginInterface; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class ArchiveTimesheetsCommandBundle extends Bundle implements PluginInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Changelog | ||
|
||
## 1.0.0 | ||
|
||
### Added | ||
|
||
- New command to archive timesheets older than a specified timeframe. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai ArchiveTimesheetsCommandBundle. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KimaiPlugin\ArchiveTimesheetsCommandBundle\Command; | ||
|
||
use App\Repository\Query\TimesheetQuery; | ||
use App\Repository\TimesheetRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Pagerfanta\Exception\OutOfRangeCurrentPageException; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
class ArchiveTimesheetsCommand extends Command | ||
{ | ||
protected static $defaultName = 'kimai:archive:timesheets'; | ||
|
||
/** | ||
* @var TimesheetRepository | ||
*/ | ||
protected $timesheetRepository; | ||
|
||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
protected $entityManager; | ||
|
||
public function __construct(TimesheetRepository $timesheetRepository, EntityManagerInterface $entityManager) | ||
{ | ||
parent::__construct(); | ||
$this->timesheetRepository = $timesheetRepository; | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setDescription( | ||
'Archive (remove) timesheets older than the given preserve ' | ||
. 'period.' | ||
) | ||
->addOption( | ||
'preserve-period', | ||
'p', | ||
InputOption::VALUE_OPTIONAL, | ||
'The period for which to preserve the timesheets, ' | ||
. 'in the PHP DateInterval format ' | ||
. '(http://php.net/manual/en/dateinterval.construct.php' | ||
. '#refsect1-dateinterval.construct-parameters). ' | ||
. 'Defaults to 1 year.', | ||
'P1Y' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$style = new SymfonyStyle($input, $output); | ||
try { | ||
$preservePeriod = $input->getOption('preserve-period'); | ||
$period = new \DateInterval($preservePeriod); | ||
} catch (\Exception $exception) { | ||
$style->error(sprintf( | ||
'%s is not a valid period format. See ' | ||
. 'http://php.net/manual/en/dateinterval.construct.php' | ||
. '#refsect1-dateinterval.construct-parameters for more information.', | ||
$preservePeriod | ||
)); | ||
|
||
// Return an exit code higher than 0. | ||
return 1; | ||
} | ||
|
||
// The TimesheetQuery sets the time component of the end date to | ||
// 23:59:59, so we substract another day. | ||
$end = (new \DateTime())->sub($period)->sub(new \DateInterval('P1D')); | ||
$query = new TimesheetQuery(); | ||
$query->setEnd($end); | ||
// Do it in batch, so we don't have to load all timesheets into memory | ||
// at once. | ||
$query->setPageSize(50); | ||
$pagedTimesheets = $this->timesheetRepository->getPagerfantaForQuery($query); | ||
$progressBar = new ProgressBar($output, $pagedTimesheets->count()); | ||
|
||
$this->entityManager->beginTransaction(); | ||
while (true) { | ||
try { | ||
$timesheets = $pagedTimesheets->getCurrentPageResults(); | ||
foreach ($timesheets as $timesheet) { | ||
// Metafields aren't cascaded, remove them manually. | ||
foreach ($timesheet->getMetaFields() as $meta) { | ||
$this->entityManager->remove($meta); | ||
} | ||
$this->entityManager->remove($timesheet); | ||
$progressBar->advance(); | ||
} | ||
$pagedTimesheets->setCurrentPage($pagedTimesheets->getCurrentPage() + 1); | ||
} | ||
catch (OutOfRangeCurrentPageException $ex) { | ||
break; | ||
} | ||
catch (\Exception $ex) { | ||
$this->entityManager->rollback(); | ||
throw $ex; | ||
} | ||
|
||
} | ||
|
||
$this->entityManager->flush(); | ||
$this->entityManager->commit(); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Kimai ArchiveTimesheetsCommandBundle. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace KimaiPlugin\ArchiveTimesheetsCommandBundle\DependencyInjection; | ||
|
||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; | ||
use Symfony\Component\DependencyInjection\Loader; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
|
||
class ArchiveTimesheetsCommandExtension extends Extension implements PrependExtensionInterface | ||
{ | ||
/** | ||
* @param array $configs | ||
* @param ContainerBuilder $container | ||
* @throws \Exception | ||
*/ | ||
public function load(array $configs, ContainerBuilder $container) | ||
{ | ||
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); | ||
$loader->load('services.yaml'); | ||
} | ||
|
||
public function prepend(ContainerBuilder $container) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 District09 - https://district09.gent | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# ArchiveTimesheetsCommandBundle | ||
|
||
A Kimai 2 plugin, which allows you to archive timesheets older than a specified | ||
timeframe, using a command. | ||
|
||
## Installation | ||
|
||
First clone it to your Kimai installation `plugins` directory: | ||
``` | ||
cd /kimai/var/plugins/ | ||
git clone https://github.com/digipolisgent/kimai_plugin_archive-timesheets-commmand.git ArchiveTimesheetsCommandBundle | ||
``` | ||
|
||
And then rebuild the cache: | ||
``` | ||
cd /kimai/ | ||
bin/console cache:clear | ||
bin/console cache:warmup | ||
``` | ||
|
||
You could also [download it as zip](https://github.com/digipolisgent/kimai_plugin_archive-timesheets-commmand/archive/master.zip) and upload the directory via FTP: | ||
|
||
``` | ||
/kimai/var/plugins/ | ||
├── ArchiveTimesheetsCommandBundle | ||
│ ├── ArchiveTimesheetsCommandBundle.php | ||
| └ ... more files and directories follow here ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
public: false | ||
|
||
KimaiPlugin\ArchiveTimesheetsCommandBundle\: | ||
resource: '../../*' | ||
exclude: '../../{Resources}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
<file source-language="en" target-language="en" datatype="plaintext" original="none"> | ||
<body> | ||
</body> | ||
</file> | ||
</xliff> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2"> | ||
<file source-language="en" target-language="en" datatype="plaintext" original="none"> | ||
<body> | ||
</body> | ||
</file> | ||
</xliff> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "digipolisgent/archive-timesheets-command-plugin", | ||
"description": "A Kimai 2 plugin, which allows you to archive timesheets older than a specified timeframe, using a command.", | ||
"homepage": "https://github.com/digipolisgent/kimai_plugin_personal-calendar-pref", | ||
"type": "kimai-plugin", | ||
"keywords": [ | ||
"kimai", | ||
"kimai-plugin" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jelle Sebreghts", | ||
"email": "Jelle.Sebreghts@district09.gent" | ||
} | ||
], | ||
"extra": { | ||
"kimai": { | ||
"require": "1.15", | ||
"version": "1.0.1", | ||
"name": "ArchiveTimesheetsCommand" | ||
}, | ||
"installer-name": "ArchiveTimesheetsCommandBundle" | ||
} | ||
} |