File: //home/timetracker.panomity.com/tm.old/Form/TaskForm.php
<?php
/*
* This file is part of the TaskManagementBundle for Kimai 2.
* All rights reserved by Kevin Papst (www.kevinpapst.de).
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace KimaiPlugin\TaskManagementBundle\Form;
use App\Entity\User;
use App\Form\FormTrait;
use App\Form\Type\DateTimePickerType;
use App\Form\Type\DurationType;
use App\Form\Type\TagsType;
use App\Form\Type\TeamType;
use App\Form\Type\UserType;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\Query\UserFormTypeQuery;
use App\Repository\UserRepository;
use KimaiPlugin\TaskManagementBundle\Entity\Task;
use KimaiPlugin\TaskManagementBundle\Form\Type\StatusType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TaskForm extends AbstractType
{
use FormTrait;
/**
* @var CustomerRepository
*/
private $customers;
/**
* @var ProjectRepository
*/
private $projects;
public function __construct(CustomerRepository $customer, ProjectRepository $project)
{
$this->customers = $customer;
$this->projects = $project;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$activity = null;
$project = null;
$customer = null;
$begin = null;
$isNew = true;
$timezone = $options['timezone'];
$entry = null;
if (isset($options['data'])) {
/** @var Task $entry */
$entry = $options['data'];
$activity = $entry->getActivity();
$project = $entry->getProject();
$customer = null === $project ? null : $project->getCustomer();
if (null !== $entry->getId()) {
$isNew = false;
}
if (null === $project && null !== $activity) {
$project = $activity->getProject();
}
if (null !== ($begin = $entry->getBegin())) {
$timezone = $begin->getTimezone()->getName();
}
}
$dateTimeOptions = [
'model_timezone' => $timezone,
'view_timezone' => $timezone,
];
$builder->add('title', TextType::class, [
'label' => 'label.title',
'required' => true,
'help' => 'task.help.title',
]);
$builder->add('todo', TextareaType::class, [
'documentation' => [
'type' => 'string',
'description' => 'The actual doings, which will be available on the detail and edit page.',
],
'label' => 'label.task_todo',
'required' => false,
]);
/*
$builder->add('begin', DateTimePickerType::class, array_merge($dateTimeOptions, [
'label' => 'label.begin',
'required' => false,
]));
*/
$builder->add('end', DateTimePickerType::class, array_merge($dateTimeOptions, [
'label' => 'label.end',
'required' => false,
'help' => 'task.help.end',
]));
$builder->add('estimation', DurationType::class, [
'documentation' => [
'type' => 'string',
'description' => 'Estimation in hh:mm format, (see duration format documentation)',
],
'label' => 'label.estimation',
'required' => false,
'help' => 'task.help.estimation',
'icon' => 'clock',
'attr' => [
'placeholder' => '00:00',
'pattern' => '[0-9]{2,3}:[0-9]{2}'
]
]);
$this->addCustomer($builder, $customer);
$this->addProject($builder, $isNew, $project, $customer);
$this->addActivity($builder, $activity, $project);
if (!$isNew && $entry !== null && $entry->getStatus() === Task::STATUS_DONE) {
$builder->add('status', StatusType::class, [
'required' => true,
]);
}
$builder->add('description', TextareaType::class, [
'label' => 'label.description',
'required' => false,
'help' => 'task.help.description',
]);
$builder->add('tags', TagsType::class, [
'required' => false,
'help' => 'task.help.tags',
]);
$hasActiveRecord = false;
// if a record is running, we cannot re-assign task
if ($entry !== null && null !== $entry->getActiveRecord(null)) {
$hasActiveRecord = true;
}
if ($options['include_user'] && !$hasActiveRecord) {
$builder->add('user', UserType::class, [
'required' => false,
'help' => 'task.help.user',
'query_builder' => function (UserRepository $repo) use ($options) {
/** @var User $user */
$user = $options['user'];
$query = new UserFormTypeQuery();
$query->setUser($user);
// users should be able to assign it to any member of their teams
// this does not make a lot of sense, but for now lets keep it as is
if ($user->hasTeamAssignment()) {
$query->setTeams($user->getTeams());
}
return $repo->getQueryBuilderForFormType($query);
}
]);
}
if ($options['include_team'] && !$hasActiveRecord) {
$builder->add('team', TeamType::class, [
'teamlead_only' => false,
'placeholder' => '',
'required' => false,
'help' => 'task.help.team',
]);
}
}
/**
* @param OptionsResolver $resolver
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Task::class,
'csrf_protection' => true,
'csrf_field_name' => '_token',
'csrf_token_id' => 'task_edit',
'include_user' => true,
'include_team' => true,
'timezone' => date_default_timezone_get(),
'method' => 'POST',
'attr' => [
'data-form-event' => 'kimai.taskUpdate',
'data-msg-success' => 'action.update.success',
'data-msg-error' => 'action.update.error',
],
]);
}
}