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

ContactController::createAction()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
cc 5
eloc 15
nc 4
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\ContactBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
11
use Oro\Bundle\SecurityBundle\Annotation\Acl;
12
use Oro\Bundle\SecurityBundle\Annotation\AclAncestor;
13
14
use Oro\Bundle\SoapBundle\Entity\Manager\ApiEntityManager;
15
16
use OroCRM\Bundle\ContactBundle\Entity\Contact;
17
use OroCRM\Bundle\AccountBundle\Entity\Account;
18
19
class ContactController extends Controller
20
{
21
    /**
22
     * @Route("/view/{id}", name="orocrm_contact_view", requirements={"id"="\d+"})
23
     *
24
     * @Template
25
     * @Acl(
26
     *      id="orocrm_contact_view",
27
     *      type="entity",
28
     *      permission="VIEW",
29
     *      class="OroCRMContactBundle:Contact"
30
     * )
31
     */
32
    public function viewAction(Contact $contact)
33
    {
34
        return [
35
            'entity' => $contact,
36
        ];
37
    }
38
39
    /**
40
     * @Route("/info/{id}", name="orocrm_contact_info", requirements={"id"="\d+"})
41
     *
42
     * @Template
43
     * @AclAncestor("orocrm_contact_view")
44
     */
45
    public function infoAction(Contact $contact)
46
    {
47
        if (!$this->getRequest()->get('_wid')) {
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...
48
            return $this->redirect($this->get('router')->generate('orocrm_contact_view', ['id' => $contact->getId()]));
49
        }
50
51
        return array(
52
            'entity'  => $contact
53
        );
54
    }
55
56
    /**
57
     * Create contact form
58
     * @Route("/create", name="orocrm_contact_create")
59
     * @Template("OroCRMContactBundle:Contact:update.html.twig")
60
     * @Acl(
61
     *      id="orocrm_contact_create",
62
     *      type="entity",
63
     *      permission="CREATE",
64
     *      class="OroCRMContactBundle:Contact"
65
     * )
66
     */
67
    public function createAction()
68
    {
69
        // add predefined account to contact
70
        $contact     = null;
71
        $entityClass = $this->getRequest()->get('entityClass');
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...
72
        if ($entityClass) {
73
            $entityClass = $this->get('oro_entity.routing_helper')->resolveEntityClass($entityClass);
74
            $entityId    = $this->getRequest()->get('entityId');
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...
75
            if ($entityId && $entityClass === $this->container->getParameter('orocrm_account.account.entity.class')) {
76
                $repository = $this->getDoctrine()->getRepository($entityClass);
77
                /** @var Account $account */
78
                $account = $repository->find($entityId);
79
                if ($account) {
80
                    /** @var Contact $contact */
81
                    $contact = $this->getManager()->createEntity();
82
                    $contact->addAccount($account);
83
                } else {
84
                    throw new NotFoundHttpException(sprintf('Account with ID %s is not found', $entityId));
85
                }
86
            }
87
        }
88
89
        return $this->update($contact);
90
    }
91
92
    /**
93
     * Update user form
94
     * @Route("/update/{id}", name="orocrm_contact_update", requirements={"id"="\d+"})
95
     *
96
     * @Template
97
     * @Acl(
98
     *      id="orocrm_contact_update",
99
     *      type="entity",
100
     *      permission="EDIT",
101
     *      class="OroCRMContactBundle:Contact"
102
     * )
103
     */
104
    public function updateAction(Contact $entity)
105
    {
106
        return $this->update($entity);
107
    }
108
109
    /**
110
     * @Route(
111
     *      "/{_format}",
112
     *      name="orocrm_contact_index",
113
     *      requirements={"_format"="html|json"},
114
     *      defaults={"_format" = "html"}
115
     * )
116
     *
117
     * @Template
118
     * @AclAncestor("orocrm_contact_view")
119
     */
120
    public function indexAction()
121
    {
122
        return [
123
            'entity_class' => $this->container->getParameter('orocrm_contact.entity.class')
124
        ];
125
    }
126
127
    /**
128
     * @return ApiEntityManager
129
     */
130
    protected function getManager()
131
    {
132
        return $this->get('orocrm_contact.contact.manager');
133
    }
134
135
    protected function update(Contact $entity = null)
136
    {
137
        if (!$entity) {
138
            $entity = $this->getManager()->createEntity();
139
        }
140
141
        return $this->get('oro_form.model.update_handler')->update(
142
            $entity,
143
            $this->get('orocrm_contact.form.contact'),
144
            $this->get('translator')->trans('orocrm.contact.controller.contact.saved.message'),
145
            $this->get('orocrm_contact.form.handler.contact')
146
        );
147
    }
148
149
    /**
150
     * @Route("/widget/account-contacts/{id}", name="orocrm_account_widget_contacts", requirements={"id"="\d+"})
151
     * @AclAncestor("orocrm_contact_view")
152
     * @Template()
153
     */
154
    public function accountContactsAction(Account $account)
155
    {
156
        $defaultContact = $account->getDefaultContact();
157
        $contacts = $account->getContacts();
158
        $contactsWithoutDefault = array();
159
160
        if (empty($defaultContact)) {
161
            $contactsWithoutDefault = $contacts->toArray();
162
        } else {
163
            /** @var Contact $contact */
164
            foreach ($contacts as $contact) {
165
                if ($contact->getId() == $defaultContact->getId()) {
166
                    continue;
167
                }
168
                $contactsWithoutDefault[] = $contact;
169
            }
170
        }
171
172
        /**
173
         * Compare contacts to sort them alphabetically
174
         *
175
         * @param Contact $firstContact
176
         * @param Contact $secondContact
177
         * @return int
178
         */
179
        $compareFunction = function ($firstContact, $secondContact) {
180
            $first = $firstContact->getLastName() . $firstContact->getFirstName() . $firstContact->getMiddleName();
181
            $second = $secondContact->getLastName() . $secondContact->getFirstName() . $secondContact->getMiddleName();
182
            return strnatcasecmp($first, $second);
183
        };
184
185
        usort($contactsWithoutDefault, $compareFunction);
186
187
        return array(
188
            'entity'                 => $account,
189
            'defaultContact'         => $defaultContact,
190
            'contactsWithoutDefault' => $contactsWithoutDefault
191
        );
192
    }
193
}
194