added tags for timesheets (#604)

This commit is contained in:
Mathias
2019-05-12 01:40:04 +02:00
committed by Kevin Papst
parent f31118292c
commit e29e183e84
84 changed files with 2132 additions and 158 deletions

View File

@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\DataFixtures;
use App\Entity\Tag;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Factory;
/**
* Defines the sample data to load in the database when running the unit and
* functional tests or while development.
*
* Execute this command to load the data:
* bin/console doctrine:fixtures:load
*
* @codeCoverageIgnore
*/
class TagFixtures extends Fixture
{
public const MIN_TAGS = 50;
public const MAX_TAGS = 2000;
public const BATCH_SIZE = 100;
/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
{
$faker = Factory::create();
$amount = rand(self::MIN_TAGS, self::MAX_TAGS);
$existing = [];
for ($i = 0; $i < $amount; $i++) {
$tag = new Tag();
$tagName = null;
if ($i % 2 == 9) {
$tagName = $faker->companyEmail;
} elseif ($i % 2 == 8) {
$tagName = $faker->firstName;
} elseif ($i % 2 == 7) {
$tagName = $faker->lastName;
} elseif ($i % 6 == 0) {
$tagName = $faker->iban();
} elseif ($i % 5 == 0) {
$tagName = $faker->city;
} elseif ($i % 4 == 0) {
$tagName = $faker->word;
} elseif ($i % 3 == 0) {
$tagName = $faker->streetName;
} elseif ($i % 2 == 0) {
$tagName = $faker->colorName;
} elseif ($i % 1 == 0) {
$tagName = $faker->text(rand(10, 20));
}
if (in_array($tagName, $existing)) {
continue;
}
$existing[] = $tagName;
$tag->setName($tagName);
$manager->persist($tag);
if ($i % self::BATCH_SIZE == 0) {
$manager->flush();
$manager->clear(Tag::class);
}
$manager->flush();
$manager->clear(Tag::class);
}
$manager->flush();
}
}

View File

@@ -11,6 +11,7 @@ namespace App\DataFixtures;
use App\Entity\Activity;
use App\Entity\Project;
use App\Entity\Tag;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Entity\UserPreference;
@@ -50,6 +51,7 @@ class TimesheetFixtures extends Fixture implements DependentFixtureInterface
return [
UserFixtures::class,
CustomerFixtures::class,
TagFixtures::class,
];
}
@@ -61,6 +63,7 @@ class TimesheetFixtures extends Fixture implements DependentFixtureInterface
$allUser = $this->getAllUsers($manager);
$activities = $this->getAllActivities($manager);
$projects = $this->getAllProjects($manager);
$allTags = $this->getAllTags($manager);
$faker = Factory::create();
@@ -86,13 +89,13 @@ class TimesheetFixtures extends Fixture implements DependentFixtureInterface
$user,
$activities[array_rand($activities)],
$projects[array_rand($projects)],
$description
$description,
true
);
$manager->persist($entry);
if ($i % self::BATCH_SIZE == 0) {
//echo '['.$i.'] Timesheets for User ' . $user->getId() . PHP_EOL;
$manager->flush();
$manager->clear(Timesheet::class);
}
@@ -115,6 +118,37 @@ class TimesheetFixtures extends Fixture implements DependentFixtureInterface
$manager->clear(Timesheet::class);
}
$manager->flush();
$entries = $manager->getRepository(Timesheet::class)->findAll();
foreach ($entries as $temp) {
$tagAmount = rand(0, 4);
for ($iTag = 0; $iTag < $tagAmount; $iTag++) {
$tagId = rand(1, TagFixtures::MAX_TAGS);
if (isset($allTags[$tagId])) {
$temp->addTag($allTags[$tagId]);
}
}
}
$manager->flush();
$manager->clear(Timesheet::class);
$manager->clear(Tag::class);
}
/**
* @param ObjectManager $manager
* @return Tag[]
*/
protected function getAllTags(ObjectManager $manager)
{
$all = [];
/* @var Tag[] $entries */
$entries = $manager->getRepository(Tag::class)->findAll();
foreach ($entries as $temp) {
$all[$temp->getId()] = $temp;
}
return $all;
}
/**