1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Moo\FlashCardAdminBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Moo\FlashCardAdminBundle\Admin; |
13
|
|
|
|
14
|
|
|
use Sonata\AdminBundle\Admin\Admin; |
15
|
|
|
use Sonata\AdminBundle\Datagrid\ListMapper; |
16
|
|
|
use Sonata\AdminBundle\Datagrid\DatagridMapper; |
17
|
|
|
use Sonata\AdminBundle\Form\FormMapper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* CategoryAdmin is the card category admin class for SonataAdminBundle. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class CategoryAdmin extends Admin |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Configure the fields to be shown in create/edit forms |
29
|
|
|
* |
30
|
|
|
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
protected function configureFormFields(FormMapper $formMapper) |
34
|
|
|
{ |
35
|
|
|
$formMapper |
36
|
|
|
->with('General') |
37
|
|
|
->add('title', 'text', array('label' => 'Category Title')) |
38
|
|
|
->add('parent', 'entity', array('class' => 'Moo\FlashCardBundle\Entity\Category', 'required' => false)) |
39
|
|
|
->add('active') |
40
|
|
|
->add('description', 'textarea', array('label' => 'Description')) |
41
|
|
|
->end() |
42
|
|
|
->with('Management') |
43
|
|
|
->add('created', null, array('required' => false)) |
44
|
|
|
->add('updated', null, array('required' => false)) |
45
|
|
|
->end() |
46
|
|
|
; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Configure the fields to be shown in filter form. |
51
|
|
|
* |
52
|
|
|
* @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$datagridMapper |
58
|
|
|
->add('title', 'doctrine_orm_callback', array( |
59
|
|
|
'callback' => array($this, 'getSearchFilter'), |
60
|
|
|
'field_type' => 'text' |
61
|
|
|
)) |
62
|
|
|
->add('parent') |
63
|
|
|
->add('active') |
64
|
|
|
; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Configure the columns in list page. |
69
|
|
|
* |
70
|
|
|
* @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function configureListFields(ListMapper $listMapper) |
74
|
|
|
{ |
75
|
|
|
$listMapper |
76
|
|
|
->addIdentifier('title') |
77
|
|
|
->add('parent') |
78
|
|
|
->add('active', null, array('editable' => true)) |
79
|
|
|
// add custom action links |
80
|
|
|
->add('_action', 'actions', array( |
81
|
|
|
'actions' => array( |
82
|
|
|
'edit' => array(), |
83
|
|
|
'delete' => array(), |
84
|
|
|
) |
85
|
|
|
)) |
86
|
|
|
; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Callback method to the filter form to search by the card title. |
91
|
|
|
* |
92
|
|
|
* @param type $queryBuilder |
93
|
|
|
* @param string $alias |
94
|
|
|
* @param string $field |
95
|
|
|
* @param string $value |
96
|
|
|
* @return boolean |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function getSearchFilter($queryBuilder, $alias, $field, $value) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if ($value['value']) { |
101
|
|
|
$exp = new \Doctrine\ORM\Query\Expr(); |
102
|
|
|
$queryBuilder->andWhere($exp->like($alias . '.title', $exp->literal('%' . $value['value'] . '%'))); |
103
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function preUpdate($category) |
111
|
|
|
{ |
112
|
|
|
$category->preUpdate(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function prePersist($category) |
116
|
|
|
{ |
117
|
|
|
$category->prePersist(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
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.