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
|
|
|
* CardAdmin is the card admin class for SonataAdminBundle. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class CardAdmin extends Admin |
25
|
|
|
{ |
26
|
|
|
public $supportsPreviewMode = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get template name based on the name of the page. |
30
|
|
|
* |
31
|
|
|
* @param string $name |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getTemplate($name) |
35
|
|
|
{ |
36
|
|
|
switch ($name) { |
37
|
|
|
case 'preview': |
38
|
|
|
return 'MooFlashCardAdminBundle:CRUD:preview.html.twig'; |
39
|
|
|
case 'show': |
40
|
|
|
return 'MooFlashCardAdminBundle:CRUD:show.html.twig'; |
41
|
|
|
default: |
42
|
|
|
return parent::getTemplate($name); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Configure the fields to be shown in create/edit forms |
48
|
|
|
* |
49
|
|
|
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
protected function configureFormFields(FormMapper $formMapper) |
53
|
|
|
{ |
54
|
|
|
$formMapper |
55
|
|
|
->with('General') |
56
|
|
|
->add('title', 'text', array('label' => 'Card Title')) |
57
|
|
|
->add('slug', 'text', array('required' => false)) |
58
|
|
|
->add('category', 'entity', array( |
59
|
|
|
'class' => 'Moo\FlashCardBundle\Entity\Category' |
60
|
|
|
)) |
61
|
|
|
->add('active') |
62
|
|
|
->add('content', 'textarea') |
63
|
|
|
->end() |
64
|
|
|
->with('SEO') |
65
|
|
|
->add('metaKeywords', 'text', array( |
66
|
|
|
'required' => false, |
67
|
|
|
'help' => 'Card keywords seperated by a comma.' |
68
|
|
|
)) |
69
|
|
|
->add('metaDescription', 'textarea', array('required' => false)) |
70
|
|
|
->end() |
71
|
|
|
->with('Management') |
72
|
|
|
->add('created', null, array('required' => false)) |
73
|
|
|
->add('updated', null, array('required' => false)) |
74
|
|
|
->end() |
75
|
|
|
; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Configure the fields to be shown in filter form. |
80
|
|
|
* |
81
|
|
|
* @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
protected function configureDatagridFilters(DatagridMapper $datagridMapper) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$datagridMapper |
87
|
|
|
->add('title', 'doctrine_orm_callback', array( |
88
|
|
|
'callback' => array($this, 'getSearchFilter'), |
89
|
|
|
'field_type' => 'text' |
90
|
|
|
)) |
91
|
|
|
->add('category') |
92
|
|
|
->add('active') |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Configure the columns in list page. |
98
|
|
|
* |
99
|
|
|
* @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function configureListFields(ListMapper $listMapper) |
103
|
|
|
{ |
104
|
|
|
$listMapper |
105
|
|
|
->addIdentifier('title') |
106
|
|
|
->add('category') |
107
|
|
|
->add('active', null, array('editable' => true)) |
108
|
|
|
// add custom action links |
109
|
|
|
->add('_action', 'actions', array( |
110
|
|
|
'actions' => array( |
111
|
|
|
'show' => array(), |
112
|
|
|
'edit' => array( |
113
|
|
|
'subject' => 'EDit' |
114
|
|
|
), |
115
|
|
|
'delete' => array(), |
116
|
|
|
) |
117
|
|
|
)) |
118
|
|
|
; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Callback method to the filter form to search by the card title. |
123
|
|
|
* |
124
|
|
|
* @param type $queryBuilder |
125
|
|
|
* @param string $alias |
126
|
|
|
* @param string $field |
127
|
|
|
* @param string $value |
128
|
|
|
* @return boolean |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function getSearchFilter($queryBuilder, $alias, $field, $value) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
if ($value['value']) { |
133
|
|
|
$exp = new \Doctrine\ORM\Query\Expr(); |
134
|
|
|
$queryBuilder->andWhere($exp->like($alias . '.title', $exp->literal('%' . $value['value'] . '%'))); |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function preUpdate($card) |
143
|
|
|
{ |
144
|
|
|
$card->preUpdate(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function prePersist($card) |
148
|
|
|
{ |
149
|
|
|
$card->prePersist(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
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.