improved form handling

improved profile editing
added activity editing
This commit is contained in:
Kevin Papst
2016-11-12 23:22:47 +01:00
parent 8401797728
commit cc269d3142
17 changed files with 425 additions and 34 deletions

View File

@@ -9,6 +9,14 @@
<source>browser.title</source> <source>browser.title</source>
<target>Kimai - Time Tracking</target> <target>Kimai - Time Tracking</target>
</trans-unit> </trans-unit>
<trans-unit id="yes">
<source>yes</source>
<target>Ja</target>
</trans-unit>
<trans-unit id="no">
<source>no</source>
<target>Nein</target>
</trans-unit>
<!-- <!--
Login / Security Login / Security
@@ -221,6 +229,10 @@
<source>action.save</source> <source>action.save</source>
<target>Speichern</target> <target>Speichern</target>
</trans-unit> </trans-unit>
<trans-unit id="action.back">
<source>action.back</source>
<target>Zurück</target>
</trans-unit>
<trans-unit id="action.updated_successfully"> <trans-unit id="action.updated_successfully">
<source>action.updated_successfully</source> <source>action.updated_successfully</source>
<target>Änderungen erfolgreich gespeichert</target> <target>Änderungen erfolgreich gespeichert</target>

View File

@@ -5,9 +5,5 @@
{% block main %} {% block main %}
{{ include('default/_flash_messages.html.twig') }} {{ include('default/_flash_messages.html.twig') }}
{{ include('default/_form.html.twig', {'title': user.username, 'form': form}) }}
{{ include('admin/user/_form.html.twig', {
user: user,
form: form,
}, with_context = false) }}
{% endblock %} {% endblock %}

View File

@@ -1,6 +1,6 @@
<div class="box box-primary"> <div class="box box-primary">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">{{ user.username }}</h3> <h3 class="box-title">{{ title }}</h3>
</div> </div>
{{ form_start(form) }} {{ form_start(form) }}
<div class="box-body"> <div class="box-body">
@@ -8,6 +8,9 @@
</div> </div>
<div class="box-footer"> <div class="box-footer">
<input type="submit" value="{{ 'action.save'|trans }}" class="btn btn-primary" /> <input type="submit" value="{{ 'action.save'|trans }}" class="btn btn-primary" />
{% if back %}
<a href="{{ back }}" class="btn btn-warning">{{ 'action.back'|trans }}</a>
{% endif %}
</div> </div>
{{ form_end(form) }} {{ form_end(form) }}
</div> </div>

View File

@@ -79,6 +79,19 @@ class ProfileController extends Controller
); );
} }
protected function getRoles()
{
$roles = array();
foreach ($this->getParameter('security.role_hierarchy.roles') as $key => $value) {
$roles[] = $key;
foreach ($value as $value2) {
$roles[] = $value2;
}
}
$roles = array_unique($roles);
return $roles;
}
/** /**
* @Route("/{username}/edit", name="user_profile_edit") * @Route("/{username}/edit", name="user_profile_edit")
* @Method({"GET", "POST"}) * @Method({"GET", "POST"})

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Custom form field type to select the language.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class LanguageType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'choices' => array(
Intl::getLocaleBundle()->getLocaleName('de', 'de') => 'de'
)
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return ChoiceType::class;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Custom form field type to select between Yes and No.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class YesNoType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'choices' => ['yes' => true, 'no' => false],
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return ChoiceType::class;
}
}

View File

@@ -12,8 +12,9 @@
namespace AppBundle\Form; namespace AppBundle\Form;
use AppBundle\Entity\User; use AppBundle\Entity\User;
use AppBundle\Form\Type\LanguageType;
use AppBundle\Form\Type\YesNoType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -47,8 +48,13 @@ class UserEditType extends AbstractType
// string - length 5 // string - length 5
->add('language', LanguageType::class, [ ->add('language', LanguageType::class, [
'label' => 'label.language', 'label' => 'label.language',
'choices' => array('Deutsch' => 'de') // FIXME translation
]) ])
// boolean
->add('active', YesNoType::class, [
'label' => 'label.active',
])
// TODO avatar
// TODO roles - see ProfileController::getRoles()
; ;
} }

View File

@@ -88,7 +88,7 @@ class Extensions extends \Twig_Extension
$second = $seconds % 60; $second = $seconds % 60;
$second = $second > 9 ? $second : '0' . $second; $second = $second > 9 ? $second : '0' . $second;
return $hour . ':' . $minute . ':' . $second; return $hour . ':' . $minute . ':' . $second . 'h';
} }
/** /**

View File

@@ -11,13 +11,16 @@
namespace TimesheetBundle\Controller\Admin; namespace TimesheetBundle\Controller\Admin;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use TimesheetBundle\Entity\Activity; use TimesheetBundle\Entity\Activity;
use TimesheetBundle\Entity\Timesheet;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use TimesheetBundle\Form\ActivityEditForm;
use TimesheetBundle\Repository\ActivityRepository;
/** /**
* Controller used to manage activities in the admin part of the site. * Controller used to manage activities in the admin part of the site.
@@ -42,4 +45,67 @@ class ActivityController extends Controller
return $this->render('TimesheetBundle:admin:activity.html.twig', ['entries' => $entries]); return $this->render('TimesheetBundle:admin:activity.html.twig', ['entries' => $entries]);
} }
/**
* @Route("/{id}/edit", name="admin_activity_edit")
* @Method({"GET", "POST"})
*/
public function editAction($id, Request $request)
{
$activity = $this->getById($id);
$editForm = $this->createEditForm($activity);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($activity);
$entityManager->flush();
$this->addFlash('success', 'action.updated_successfully');
return $this->redirectToRoute(
'admin_activity', ['id' => $activity->getId()]
);
}
return $this->render(
'TimesheetBundle:admin:activity_edit.html.twig',
[
'activity' => $activity,
'form' => $editForm->createView()
]
);
}
/**
* @param $id
* @return null|Activity
*/
protected function getById($id)
{
/* @var $repo ActivityRepository */
$repo = $this->getDoctrine()->getRepository(Activity::class);
$activity = $repo->getById($id);
if (null === $activity) {
throw new NotFoundHttpException('Activity "'.$id.'" does not exist');
}
return $activity;
}
/**
* @param Activity $activity
* @return \Symfony\Component\Form\Form
*/
private function createEditForm(Activity $activity)
{
return $this->createForm(
ActivityEditForm::class,
$activity,
[
'action' => $this->generateUrl('admin_activity_edit', ['id' => $activity->getId()]),
'method' => 'POST'
]
);
}
} }

View File

@@ -13,12 +13,10 @@ namespace TimesheetBundle\DataFixtures\ORM;
use AppBundle\Entity\User; use AppBundle\Entity\User;
use TimesheetBundle\Entity\Activity; use TimesheetBundle\Entity\Activity;
use TimesheetBundle\Entity\Customer;
use TimesheetBundle\Entity\Project; use TimesheetBundle\Entity\Project;
use TimesheetBundle\Entity\Timesheet; use TimesheetBundle\Entity\Timesheet;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use AppBundle\DataFixtures\ORM\LoadFixtures as AppBundleLoadFixtures; use AppBundle\DataFixtures\ORM\LoadFixtures as AppBundleLoadFixtures;
/** /**
@@ -43,6 +41,7 @@ class LoadFixtures extends AppBundleLoadFixtures
*/ */
public function load(ObjectManager $manager) public function load(ObjectManager $manager)
{ {
$this->loadCustomers($manager);
$this->loadProjects($manager); $this->loadProjects($manager);
$this->loadActivities($manager); $this->loadActivities($manager);
$this->loadTimesheet($manager); $this->loadTimesheet($manager);
@@ -63,6 +62,21 @@ class LoadFixtures extends AppBundleLoadFixtures
return $all; return $all;
} }
/**
* @param ObjectManager $manager
* @return Customer[]
*/
protected function getAllCustomers(ObjectManager $manager)
{
$all = [];
/* @var Customer[] $entries */
$entries = $manager->getRepository(Customer::class)->findAll();
foreach ($entries as $temp) {
$all[$temp->getId()] = $temp;
}
return $all;
}
/** /**
* @param ObjectManager $manager * @param ObjectManager $manager
* @return Project[] * @return Project[]
@@ -131,15 +145,37 @@ class LoadFixtures extends AppBundleLoadFixtures
$manager->flush(); $manager->flush();
} }
private function loadCustomers(ObjectManager $manager)
{
$allTimezones = \DateTimeZone::listIdentifiers();
$amountTimezone = count($allTimezones);
for ($i = 0; $i <= self::AMOUNT_CUSTOMER; $i++) {
$entry = new Customer();
$entry->setName($this->getRandomCustomer());
$entry->setCity($this->getRandomLocation());
$entry->setComment($this->getRandomPhrase());
$entry->setVisible($i % 3 != 0);
$entry->setTimezone($allTimezones[rand(1, $amountTimezone)]);
$manager->persist($entry);
}
$manager->flush();
}
private function loadProjects(ObjectManager $manager) private function loadProjects(ObjectManager $manager)
{ {
$allCustomer = $this->getAllCustomers($manager);
$amountCustomer = count($allCustomer);
for ($i = 0; $i <= self::AMOUNT_PROJECTS; $i++) { for ($i = 0; $i <= self::AMOUNT_PROJECTS; $i++) {
$entry = new Project(); $entry = new Project();
$entry->setName($this->getRandomProject()); $entry->setName($this->getRandomProject());
$entry->setBudget(rand(1000, 100000)); $entry->setBudget(rand(1000, 100000));
$entry->setComment($this->getRandomPhrase()); $entry->setComment($this->getRandomPhrase());
$entry->setCustomerId(rand(1, self::AMOUNT_CUSTOMER)); // TODO should be a user object $entry->setCustomer($allCustomer[rand(1, $amountCustomer)]);
$entry->setVisible($i % 3 != 0); $entry->setVisible($i % 3 != 0);
$manager->persist($entry); $manager->persist($entry);
@@ -227,4 +263,26 @@ class LoadFixtures extends AppBundleLoadFixtures
$all = $this->getLocations(); $all = $this->getLocations();
return $all[array_rand($all)]; return $all[array_rand($all)];
} }
private function getCustomers()
{
return [
'Acme University',
'Snake Oil',
'Apple',
'Microsoft',
'Google',
'Oracle',
'Yahoo',
'Twitter',
'Zend',
'SensioLabs',
];
}
private function getRandomCustomer()
{
$all = $this->getCustomers();
return $all[array_rand($all)];
}
} }

View File

@@ -47,6 +47,13 @@ class Customer
*/ */
private $comment; private $comment;
/**
* @var Project[]
*
* @ORM\OneToMany(targetEntity="TimesheetBundle\Entity\Project", mappedBy="customer")
*/
private $projects;
/** /**
* @var boolean * @var boolean
* *

View File

@@ -16,7 +16,7 @@ use Doctrine\ORM\Mapping as ORM;
/** /**
* Project * Project
* *
* @ORM\Table(name="projects", indexes={@ORM\Index(name="customerID", columns={"customerID"})}) * @ORM\Table(name="projects")
* @ORM\Entity(repositoryClass="TimesheetBundle\Repository\ProjectRepository") * @ORM\Entity(repositoryClass="TimesheetBundle\Repository\ProjectRepository")
* *
* @author Kevin Papst <kevin@kevinpapst.de> * @author Kevin Papst <kevin@kevinpapst.de>
@@ -34,12 +34,11 @@ class Project
private $id; private $id;
/** /**
* FIXME manytoone * @var Customer
* @var integer
* *
* @ORM\Column(name="customerID", type="integer", nullable=false) * @ORM\ManyToOne(targetEntity="TimesheetBundle\Entity\Customer", inversedBy="projects")
*/ */
private $customerid; private $customer;
/** /**
* @var string * @var string
@@ -87,27 +86,21 @@ class Project
} }
/** /**
* Set customerid * @return Customer
*
* @param integer $customerid
*
* @return Project
*/ */
public function setCustomerId($customerid) public function getCustomer()
{ {
$this->customerid = $customerid; return $this->customer;
return $this;
} }
/** /**
* Get customerid * @param $customer
* * @return $this
* @return integer
*/ */
public function getCustomerId() public function setCustomer($customer)
{ {
return $this->customerid; $this->customer = $customer;
return $this;
} }
/** /**

View File

@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the Kimai package.
*
* (c) Kevin Papst <kevin@kevinpapst.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TimesheetBundle\Form;
use AppBundle\Form\Type\YesNoType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use TimesheetBundle\Entity\Activity;
use TimesheetBundle\Form\Type\ProjectType;
/**
* Defines the form used to manipulate Activities.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class ActivityEditForm extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// string - length 255
->add('name', null, [
'label' => 'label.name',
])
// text
->add('comment', TextareaType::class, [
'label' => 'label.comment',
])
// boolean
->add('visible', YesNoType::class, [
'label' => 'label.visible',
])
->add('project', ProjectType::class, [
'label' => 'label.project',
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Activity::class,
'csrf_protection' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'admin_activity_edit',
]);
}
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of the Kimai package.
*
* (c) Kevin Papst <kevin@kevinpapst.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace TimesheetBundle\Form\Type;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Custom form field type to select a project.
*
* @author Kevin Papst <kevin@kevinpapst.de>
*/
class ProjectType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => 'TimesheetBundle:Project',
'choice_label' => function ($project) {
/* @var $project Project */
return
//'[' . $project->getId() . '] ' .
$project->getName() .
' (' .
$project->getCustomer()->getName() .
')';
},
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return EntityType::class;
}
}

View File

@@ -12,6 +12,7 @@
namespace TimesheetBundle\Repository; namespace TimesheetBundle\Repository;
use AppBundle\Entity\User; use AppBundle\Entity\User;
use TimesheetBundle\Entity\Activity;
use TimesheetBundle\Entity\Timesheet; use TimesheetBundle\Entity\Timesheet;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
@@ -27,6 +28,15 @@ use TimesheetBundle\Model\ActivityStatistic;
class ActivityRepository extends EntityRepository class ActivityRepository extends EntityRepository
{ {
/**
* @param $id
* @return null|Activity
*/
public function getById($id)
{
return $this->find($id);
}
/** /**
* Return statistic data for all user. * Return statistic data for all user.
* *

View File

@@ -6,6 +6,8 @@
{% block page_subtitle %}{{ 'admin_activity.subtitle'|trans }}{% endblock %} {% block page_subtitle %}{{ 'admin_activity.subtitle'|trans }}{% endblock %}
{% block main %} {% block main %}
{{ include('default/_flash_messages.html.twig') }}
{% if entries.count > 0 %} {% if entries.count > 0 %}
{{ tables.data_table_header({ {{ tables.data_table_header({
'label.id': '', 'label.id': '',
@@ -24,7 +26,10 @@
<td>{{ entry.comment }}</td> <td>{{ entry.comment }}</td>
<td>{{ widgets.label_visible(entry.visible) }}</td> <td>{{ widgets.label_visible(entry.visible) }}</td>
<td> <td>
{{ widgets.button_group({'edit': '#', 'trash': '#'}) }} {{ widgets.button_group({
'edit': path('admin_activity_edit', {'id': entry.id}),
'trash': '#'}
) }}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}
{% import "macros/widgets.html.twig" as widgets %}
{% import "macros/datatables.html.twig" as tables %}
{% block page_title %}{{ 'admin_activity.title'|trans }}{% endblock %}
{% block page_subtitle %}{{ 'admin_activity.subtitle'|trans }}{% endblock %}
{% block main %}
{{ include('default/_flash_messages.html.twig') }}
{{ include('default/_form.html.twig', {
'title': activity.name,
'form': form,
'back': path('admin_activity')
}) }}
{% endblock %}