Completed
Push — master ( 4284f6...258d83 )
by
unknown
09:47
created

TaskController::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
1
<?php
2
3
namespace OroCRM\Bundle\TaskBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
10
use Oro\Bundle\SecurityBundle\Annotation\Acl;
11
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
12
13
use Oro\Bundle\UserBundle\Entity\User;
14
use OroCRM\Bundle\TaskBundle\Entity\Task;
15
use OroCRM\Bundle\TaskBundle\Form\Type\TaskType;
16
use OroCRM\Bundle\TaskBundle\Entity\Repository\TaskRepository;
17
18
/**
19
 * @Route("/task")
20
 */
21
class TaskController extends Controller
22
{
23
    /**
24
     * @Route(
25
     *      ".{_format}",
26
     *      name="orocrm_task_index",
27
     *      requirements={"_format"="html|json"},
28
     *      defaults={"_format" = "html"}
29
     * )
30
     * @Acl(
31
     *      id="orocrm_task_view",
32
     *      type="entity",
33
     *      class="OroCRMTaskBundle:Task",
34
     *      permission="VIEW"
35
     * )
36
     * @Template
37
     */
38
    public function indexAction()
39
    {
40
        return [
41
            'entity_class' => $this->container->getParameter('orocrm_task.entity.class')
42
        ];
43
    }
44
45
    /**
46
     * @Route("/widget/sidebar-tasks/{perPage}", name="orocrm_task_widget_sidebar_tasks", defaults={"perPage" = 10})
47
     * @AclAncestor("orocrm_task_view")
48
     * @Template("OroCRMTaskBundle:Task/widget:tasksWidget.html.twig")
49
     */
50
    public function tasksWidgetAction($perPage)
51
    {
52
        /** @var TaskRepository $repository */
53
        $repository = $this->getRepository('OroCRM\Bundle\TaskBundle\Entity\Task');
54
        $id = $this->getUser()->getId();
55
        $perPage = (int)$perPage;
56
        $tasks = $repository->getTasksAssignedTo($id, $perPage);
57
58
        return array('tasks' => $tasks);
59
    }
60
61
    /**
62
     * @Route("/create", name="orocrm_task_create")
63
     * @Acl(
64
     *      id="orocrm_task_create",
65
     *      type="entity",
66
     *      class="OroCRMTaskBundle:Task",
67
     *      permission="CREATE"
68
     * )
69
     * @Template("OroCRMTaskBundle:Task:update.html.twig")
70
     */
71
    public function createAction()
72
    {
73
        $task = new Task();
74
75
        $defaultPriority = $this->getRepository('OroCRMTaskBundle:TaskPriority')->find('normal');
76
        if ($defaultPriority) {
77
            $task->setTaskPriority($defaultPriority);
78
        }
79
80
        $formAction = $this->get('oro_entity.routing_helper')
81
            ->generateUrlByRequest('orocrm_task_create', $this->getRequest());
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
82
83
        return $this->update($task, $formAction);
84
    }
85
86
    /**
87
     * @return User
88
     */
89
    protected function getCurrentUser()
90
    {
91
        $token = $this->container->get('security.context')->getToken();
92
93
        return $token ? $token->getUser() : null;
94
    }
95
96
    /**
97
     * @Route("/view/{id}", name="orocrm_task_view", requirements={"id"="\d+"})
98
     * @AclAncestor("orocrm_task_view")
99
     * @Template
100
     */
101
    public function viewAction(Task $task)
102
    {
103
        return array('entity' => $task);
104
    }
105
106
    /**
107
     * This action is used to render the list of tasks associated with the given entity
108
     * on the view page of this entity
109
     *
110
     * @Route(
111
     *      "/activity/view/{entityClass}/{entityId}",
112
     *      name="orocrm_task_activity_view"
113
     * )
114
     *
115
     * @AclAncestor("orocrm_task_view")
116
     * @Template
117
     */
118
    public function activityAction($entityClass, $entityId)
119
    {
120
        return array(
121
            'entity' => $this->get('oro_entity.routing_helper')->getEntity($entityClass, $entityId)
122
        );
123
    }
124
125
    /**
126
     * @Route("/update/{id}", name="orocrm_task_update", requirements={"id"="\d+"})
127
     * @Template
128
     * @Acl(
129
     *      id="orocrm_task_update",
130
     *      type="entity",
131
     *      class="OroCRMTaskBundle:Task",
132
     *      permission="EDIT"
133
     * )
134
     */
135
    public function updateAction(Task $task)
136
    {
137
        $formAction = $this->get('router')->generate('orocrm_task_update', ['id' => $task->getId()]);
138
139
        return $this->update($task, $formAction);
140
    }
141
142
    /**
143
     * @Route("/widget/info/{id}", name="orocrm_task_widget_info", requirements={"id"="\d+"})
144
     * @Template
145
     * @AclAncestor("orocrm_task_view")
146
     */
147
    public function infoAction(Task $entity)
148
    {
149
        return [
150
            'entity'         => $entity,
151
            'target'         => $this->getTargetEntity(),
152
            'renderContexts' => true
153
        ];
154
    }
155
156
    /**
157
     * @Route("/user/{userId}", name="orocrm_task_user_tasks", requirements={"userId"="\d+"})
158
     * @AclAncestor("orocrm_task_view")
159
     * @Template
160
     */
161
    public function userTasksAction($userId)
162
    {
163
        return ['userId' => $userId];
164
    }
165
166
    /**
167
     * @Route("/my", name="orocrm_task_my_tasks")
168
     * @AclAncestor("orocrm_task_view")
169
     * @Template
170
     */
171
    public function myTasksAction()
172
    {
173
        return [];
174
    }
175
176
    /**
177
     * @param Task $task
178
     * @param string $formAction
179
     * @return array
180
     */
181 View Code Duplication
    protected function update(Task $task, $formAction)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183
        $saved = false;
184
        if ($this->get('orocrm_task.form.handler.task')->process($task)) {
185
            if (!$this->getRequest()->get('_widgetContainer')) {
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
186
                $this->get('session')->getFlashBag()->add(
187
                    'success',
188
                    $this->get('translator')->trans('orocrm.task.saved_message')
189
                );
190
191
                return $this->get('oro_ui.router')->redirect($task);
192
            }
193
            $saved = true;
194
        }
195
196
        return array(
197
            'entity'     => $task,
198
            'saved'      => $saved,
199
            'form'       => $this->get('orocrm_task.form.handler.task')->getForm()->createView(),
200
            'formAction' => $formAction,
201
        );
202
    }
203
204
    /**
205
     * @return TaskType
206
     */
207
    protected function getFormType()
208
    {
209
        return $this->get('orocrm_task.form.handler.task')->getForm();
210
    }
211
212
    /**
213
     * @param string $entityName
214
     * @return \Doctrine\Common\Persistence\ObjectRepository
215
     */
216
    protected function getRepository($entityName)
217
    {
218
        return $this->getDoctrine()->getRepository($entityName);
219
    }
220
221
    /**
222
     * Get target entity
223
     *
224
     * @return object|null
225
     */
226 View Code Duplication
    protected function getTargetEntity()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
    {
228
        $entityRoutingHelper = $this->get('oro_entity.routing_helper');
229
        $targetEntityClass   = $entityRoutingHelper->getEntityClassName($this->getRequest(), 'targetActivityClass');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
230
        $targetEntityId      = $entityRoutingHelper->getEntityId($this->getRequest(), 'targetActivityId');
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
231
        if (!$targetEntityClass || !$targetEntityId) {
232
            return null;
233
        }
234
235
        return $entityRoutingHelper->getEntity($targetEntityClass, $targetEntityId);
236
    }
237
}
238