-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugin.php
221 lines (176 loc) · 6.34 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace FinancialValidador;
use MapasCulturais\App;
use MapasCulturais\Entities\Registration;
require_once __DIR__ . "/../AbstractValidator/AbstractValidator.php";
class Plugin extends \AbstractValidator\AbstractValidator
{
protected static $instanceBySlug = null;
function __construct(array $config = [])
{
$slug = $config['slug'] ?? null;
$config += [
// se true, só exporta as inscrições pendentes que já tenham alguma avaliação
'exportador_requer_homologacao' => true,
//Retorna o nome que sera exibido na interface
'name' => "",
// Campos que devem existir na planilha
'fields' => [],
// se true, só exporta as inscrições
'exportador_requer_validacao' => [],
// Trata os campos que aparecem na exportação
'fields_tratament' => function ($registration, $field) {
if(!$this->config['fields']){return;}
$result = [
'CPF' => function () use ($registration, $field) {
$field = $this->prefixFieldId($field);
return preg_replace('/[^0-9]/i', '', $registration->$field);
},
'NOME_COMPLETO' => function () use ($registration, $field) {
$field = $this->prefixFieldId($field);
return $registration->$field;
},
];
$callable = $result[$field] ?? null;
return $callable ? $callable() : null;
}
];
$config['forcar_resultado'] = true;
$this->_config = $config;
parent::__construct($config);
self::$instanceBySlug[$config["slug"]] = $this;
}
function _init()
{
$app = App::i();
$plugin = $this;
//botao de export csv
$app->hook('template(opportunity.<<single|edit>>.sidebar-right):end', function () use($plugin, $app){
$opportunity = $this->controller->requestedEntity; //Tive que chamar o controller para poder requisitar a entity
$isOpportunityManager = $plugin->config['is_opportunity_managed_handler'];
if ($opportunity->id == $isOpportunityManager($opportunity) && $opportunity->canUser('@control')) {
/** @var \MapasCulturais\Theme $this */
$this->part('financial-validador/validador-uploads', ['entity' => $opportunity, 'plugin' => $plugin]);
}
});
// atualiza os metadados legados para o novo formato requerido
if (!$app->repo('DbUpdate')->findBy(['name' => 'update registration_meta financeiro'])) {
$conn = $app->em->getConnection();
$conn->beginTransaction();
$slug = $this->getSlug();
$conn->executeQuery("
UPDATE
registration_meta
SET
value = CONCAT('[\"',value,'\"]')
WHERE
key = '{$slug}_filename'");
$conn->executeQuery("
UPDATE
registration_meta
SET
value = CONCAT('[',value,']')
WHERE
key = '{$slug}_raw'");
$app->disableAccessControl();
$db_update = new \MapasCulturais\Entities\DbUpdate;
$db_update->name = 'update registration_meta financeiro';
$db_update->save(true);
$app->enableAccessControl();
$conn->commit();
}
parent::_init();
}
function register()
{
$app = App::i();
$slug = $this->getSlug();
$this->registerOpportunityMetadata($this->prefix('processed_files'), [
'label' => 'Arquivos do Validador Financeiro Processados',
'type' => 'json',
'private' => true,
'default_value' => '{}'
]);
$this->registerRegistrationMetadata($this->prefix('filename'), [
'label' => 'Nome do arquivo de retorno do validador financeiro',
'type' => 'json',
'private' => true,
'default_value' => '[]'
]);
$this->registerRegistrationMetadata($this->prefix('raw'), [
'label' => 'Validador Financeiro raw data (csv row)',
'type' => 'json',
'private' => true,
'default_value' => '[]'
]);
$file_group_definition = new \MapasCulturais\Definitions\FileGroup($slug, ['^text/csv$', 'aplication/vnd.ms-excel'], 'O arquivo enviado não é um csv.',false,null,true);
$app->registerFileGroup('opportunity', $file_group_definition);
parent::register();
// $app->controller($slug)->plugin = $this;
}
/**
* Retorna o nome configurado
*
* @return string
*/
function getName(): string
{
return $this->config['name'];
}
/**
* Retorna o slug configurado
*
* @return string
*/
function getSlug(): string
{
return $this->config['slug'];
}
/**
* Retorna o nome da classe
*
* @return string
*/
function getControllerClassname(): string
{
return Controller::class;
}
/**
* Retorna o usuário autenticado
*
*/
function getUser()
{
$app = App::i();
return $app->repo('User')->findOneBy(['authUid' => $this->getAuthUid()]);
}
/**
* Retorna uma instancia do plugin com base no slug
*
* @param string $slug
* @return Plugin
*/
public static function getInstanceBySlug(string $slug)
{
return self::$instanceBySlug[$slug];
}
/**
* Retorna o valor com o slug fixado como prefixo
*
* @param string $value
* @return string
*/
public function prefix(string $value)
{
return $this->getSlug()."_".$value;
}
function isRegistrationEligible(Registration $registration): bool
{
return true;
}
public function prefixFieldId($value)
{
$fields_id = $this->config['fields'];
return $fields_id[$value] ? "field_".$fields_id[$value] : null;
}
}