-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
133 lines (106 loc) · 3.63 KB
/
Plugin.php
File metadata and controls
133 lines (106 loc) · 3.63 KB
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
<?php
namespace Kobo;
use MapasCulturais\App;
use MapasCulturais\i;
require_once __DIR__ . "/JobTypes/KoboSyncJob.php";
class Plugin extends \MapasCulturais\Plugin
{
protected static $instance;
function __construct(array $config = [])
{
self::$instance = $this;
$config += [
'api_url' => 'https://kf.kobotoolbox.org/api/v2',
'api_token' => '',
'integrations' => []
];
parent::__construct($config);
}
/**
* @return void
*/
function register() {
$this->registerKoboMetadata();
}
function _init()
{
$app = App::i();
$self = $this;
$app->registerJobType(new JobTypes\KoboSyncJob(JobTypes\KoboSyncJob::SLUG));
$self->scheduleSyncJobs();
}
protected function registerKoboMetadata()
{
$integrations = $this->config['integrations'];
if (!isset($integrations) || !is_array($integrations)) {
return;
}
$entities = [];
foreach ($integrations as $integration) {
$target_entity = $integration['target_entity'] ?? null;
if ($target_entity && !in_array($target_entity, $entities)) {
$entities[] = $target_entity;
}
}
foreach ($entities as $entity_class) {
// Registra o metadado kobo_submission_uuid
$this->registerMetadata($entity_class, 'kobo_submission_uuid', [
'label' => i::__('UUID da Submissão Kobo'),
'type' => 'string',
'private' => true,
]);
// Registra o metadado kobo_last_modified
$this->registerMetadata($entity_class, 'kobo_last_modified', [
'label' => i::__('Data da última sincronização Kobo'),
'type' => 'datetime',
'private' => true,
]);
}
}
public function scheduleSyncJobs()
{
$app = App::i();
$integrations = $this->config['integrations'];
if (!isset($integrations) || !is_array($integrations)) {
return;
}
foreach ($integrations as $integration_id => $integration) {
if (!isset($integration['enabled']) || !$integration['enabled']) {
continue;
}
$job_data = [
'integration_id' => $integration_id,
'kobo_form_id' => $integration['kobo_form_id'],
'target_entity' => $integration['target_entity'],
'field_mapping' => $integration['field_mapping'] ?? []
];
$interval_string = $integration['periodicity'] ?? '+1 day';
// Agendar para meia-noite de hoje
$midnight_today = date('Y-m-d 00:00:00');
// Verificar se o job já está enfileirado
$job_type = $app->getRegisteredJobType(JobTypes\KoboSyncJob::SLUG);
$job_id = $job_type->generateId($job_data, $midnight_today, $interval_string, 10 * 365);
if ($app->repo('Job')->findOneBy(['id' => $job_id])) {
continue;
}
$app->enqueueJob(
JobTypes\KoboSyncJob::SLUG,
$job_data,
$midnight_today,
$interval_string,
10 * 365,
);
}
}
static function getInstance()
{
return self::$instance;
}
public function getApiConfig()
{
return [
'api_url' => $this->config['api_url'],
'api_token' => $this->config['api_token'],
];
}
}