2
2
3
3
namespace OroCRM \Bundle \AccountBundle \Datagrid ;
4
4
5
- use Oro \Bundle \GridBundle \Datagrid \FlexibleDatagridManager ;
6
5
use Oro \Bundle \GridBundle \Field \FieldDescriptionCollection ;
7
6
use Oro \Bundle \GridBundle \Field \FieldDescription ;
8
7
use Oro \Bundle \GridBundle \Field \FieldDescriptionInterface ;
9
8
use Oro \Bundle \GridBundle \Filter \FilterInterface ;
10
9
use Oro \Bundle \GridBundle \Sorter \SorterInterface ;
11
10
use Oro \Bundle \GridBundle \Datagrid \ParametersInterface ;
12
11
use Oro \Bundle \GridBundle \Datagrid \ProxyQueryInterface ;
12
+ use Oro \Bundle \GridBundle \Datagrid \ORM \QueryFactory \EntityQueryFactory ;
13
13
14
14
class AccountContactUpdateDatagridManager extends AccountContactDatagridManager
15
15
{
16
+ /**
17
+ * @var string
18
+ */
19
+ protected $ hasAccountExpression ;
20
+
16
21
/**
17
22
* {@inheritDoc}
18
23
*/
@@ -22,16 +27,17 @@ protected function configureFields(FieldDescriptionCollection $fieldsCollection)
22
27
$ fieldHasAccount ->setName ('has_account ' );
23
28
$ fieldHasAccount ->setOptions (
24
29
array (
25
- 'type ' => FieldDescriptionInterface::TYPE_BOOLEAN ,
26
- 'label ' => $ this ->translate ('Assigned ' ),
27
- 'field_name ' => 'hasCurrentAccount ' ,
28
- 'expression ' => 'hasCurrentAccount ' ,
29
- 'nullable ' => false ,
30
- 'editable ' => true ,
31
- 'sortable ' => true ,
32
- 'filter_type ' => FilterInterface::TYPE_BOOLEAN ,
33
- 'filterable ' => true ,
34
- 'show_filter ' => true ,
30
+ 'type ' => FieldDescriptionInterface::TYPE_BOOLEAN ,
31
+ 'label ' => $ this ->translate ('Assigned ' ),
32
+ 'field_name ' => 'hasCurrentAccount ' ,
33
+ 'expression ' => $ this ->getHasAccountExpression (),
34
+ 'nullable ' => false ,
35
+ 'editable ' => true ,
36
+ 'sortable ' => true ,
37
+ 'filter_type ' => FilterInterface::TYPE_BOOLEAN ,
38
+ 'filterable ' => true ,
39
+ 'show_filter ' => true ,
40
+ 'filter_by_where ' => true ,
35
41
)
36
42
);
37
43
$ fieldsCollection ->add ($ fieldHasAccount );
@@ -46,24 +52,34 @@ protected function prepareQuery(ProxyQueryInterface $query)
46
52
{
47
53
$ this ->applyJoinWithAddressAndCountry ($ query );
48
54
49
- $ entityAlias = $ query ->getRootAlias ();
55
+ $ query ->addSelect ($ this ->getHasAccountExpression () . ' AS hasCurrentAccount ' , true );
56
+ }
50
57
51
- if ($ this ->getAccount ()->getId ()) {
52
- $ query ->addSelect (
53
- "CASE WHEN " .
54
- "(:account MEMBER OF $ entityAlias.accounts OR $ entityAlias.id IN (:data_in)) AND " .
55
- "$ entityAlias.id NOT IN (:data_not_in) " .
56
- "THEN 1 ELSE 0 END AS hasCurrentAccount " ,
57
- true
58
- );
59
- } else {
60
- $ query ->addSelect (
61
- "CASE WHEN " .
62
- "$ entityAlias.id IN (:data_in) AND $ entityAlias.id NOT IN (:data_not_in) " .
63
- "THEN 1 ELSE 0 END AS hasCurrentAccount " ,
64
- true
65
- );
58
+ /**
59
+ * @return string
60
+ */
61
+ protected function getHasAccountExpression ()
62
+ {
63
+ if (null === $ this ->hasAccountExpression ) {
64
+ /** @var EntityQueryFactory $queryFactory */
65
+ $ queryFactory = $ this ->queryFactory ;
66
+ $ entityAlias = $ queryFactory ->getAlias ();
67
+
68
+ if ($ this ->getAccount ()->getId ()) {
69
+ $ this ->hasAccountExpression =
70
+ "CASE WHEN " .
71
+ "(:account MEMBER OF $ entityAlias.accounts OR $ entityAlias.id IN (:data_in)) AND " .
72
+ "$ entityAlias.id NOT IN (:data_not_in) " .
73
+ "THEN true ELSE false END " ;
74
+ } else {
75
+ $ this ->hasAccountExpression =
76
+ "CASE WHEN " .
77
+ "$ entityAlias.id IN (:data_in) AND $ entityAlias.id NOT IN (:data_not_in) " .
78
+ "THEN true ELSE false END " ;
79
+ }
66
80
}
81
+
82
+ return $ this ->hasAccountExpression ;
67
83
}
68
84
69
85
/**
0 commit comments