|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ClientBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Sonata\AdminBundle\Controller\CRUDController; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SonataAdminClientController |
|
11
|
|
|
*/ |
|
12
|
|
|
class SonataAdminClientController extends CRUDController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param Request $request |
|
16
|
|
|
* @param mixed $object |
|
17
|
|
|
* @return null|Response |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function preCreate(Request $request, $object) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::preCreate($request, $object); |
|
22
|
|
|
|
|
23
|
|
|
return $this->updateContacts($request, $object); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Request $request |
|
28
|
|
|
* @param mixed $object |
|
29
|
|
|
* @return null|Response |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function preEdit(Request $request, $object) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::preEdit($request, $object); |
|
34
|
|
|
|
|
35
|
|
|
return $this->updateContacts($request, $object); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function updateContacts(Request $request, $object) |
|
39
|
|
|
{ |
|
40
|
|
|
$arr1 = []; |
|
41
|
|
|
//шматок гавнокода |
|
42
|
|
|
$postData = $request->request->all(); |
|
43
|
|
|
foreach ($postData as $arr) { |
|
44
|
|
|
$arr1 = $arr; |
|
45
|
|
|
break; |
|
46
|
|
|
} |
|
47
|
|
|
$tmpTest = ''; |
|
48
|
|
|
|
|
49
|
|
|
if ($postData) { |
|
|
|
|
|
|
50
|
|
|
$oldContacts = null; |
|
|
|
|
|
|
51
|
|
|
$newContacts = null; |
|
|
|
|
|
|
52
|
|
|
$oldContacts = $object->getContacts(); |
|
53
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
54
|
|
|
if (array_key_exists('contacts', $arr1)) { |
|
55
|
|
|
$newContacts = $arr1['contacts']; |
|
56
|
|
|
|
|
57
|
|
|
// Викидуем з массива нових контактів, які прийшли з POST, ті що вже були |
|
58
|
|
|
// і видаляем з бази контакти які видалені у клієнта |
|
59
|
|
|
foreach ($oldContacts as $contact) { |
|
60
|
|
|
$k = array_search($contact->getId(), $newContacts); |
|
61
|
|
|
if (false === $k) { |
|
62
|
|
|
$object->removeContact($contact); |
|
63
|
|
|
$em->remove($contact); |
|
64
|
|
|
} else { |
|
65
|
|
|
unset($newContacts[$k]); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
foreach ($newContacts as $contactId) { |
|
70
|
|
|
$contact = $em->getRepository('ClientBundle:Contact')->find($contactId); |
|
71
|
|
|
if (!$contact) { |
|
72
|
|
|
throw $this->createNotFoundException(sprintf('не знайдений об\'єкт з id : %s', $contactId)); |
|
73
|
|
|
} else { |
|
74
|
|
|
$object->addContact($contact); |
|
75
|
|
|
$contact->setClient($object); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} else { |
|
79
|
|
|
// Post прийшов без контактів - видаляем всі контакти клієнта |
|
80
|
|
|
foreach ($oldContacts as $contact) { |
|
81
|
|
|
$object->removeContact($contact); |
|
82
|
|
|
$em->remove($contact); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if ('' === $tmpTest) { |
|
88
|
|
|
return null; |
|
89
|
|
|
} else { |
|
90
|
|
|
return new Response($tmpTest); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.