forked from mapasculturais/plugin-MapasNetwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeBootstrapJobType.php
More file actions
94 lines (87 loc) · 3.57 KB
/
NodeBootstrapJobType.php
File metadata and controls
94 lines (87 loc) · 3.57 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
<?php
namespace MapasNetwork;
use MapasCulturais\ApiQuery;
use MapasCulturais\App;
use MapasCulturais\Entities\Agent;
use MapasCulturais\Entities\Job;
use MapasCulturais\Entities\Space;
class NodeBootstrapJobType extends \MapasCulturais\Definitions\JobType
{
protected $plugin;
function __construct(string $slug, Plugin $plugin)
{
$this->plugin = $plugin;
parent::__construct($slug);
return;
}
protected function _execute(Job $job)
{
$app = App::i();
$node = $job->node;
$user = $node->user;
$file_groups = [
"agent" => array_keys($app->getRegisteredFileGroupsByEntity(Agent::class)),
"space" => array_keys($app->getRegisteredFileGroupsByEntity(Space::class)),
];
$metalist_groups = [
"agent" => array_keys($app->getRegisteredMetaListGroupsByEntity(Agent::class)),
"space" => array_keys($app->getRegisteredMetaListGroupsByEntity(Space::class)),
];
$map_ids = function ($entity) { return $entity->id; };
$map_serialize = function ($entity) use ($file_groups, $metalist_groups) {
if (Plugin::ensureNetworkID($entity)) {
$this->plugin->skip($entity, [Plugin::SKIP_BEFORE, Plugin::SKIP_AFTER]);
Plugin::saveMetadata($entity, ["network__id"]);
}
$serialised = $this->plugin->serializeEntity($entity);
$serialised = $this->plugin->serializeAttachments($entity, "files", $file_groups[$entity->controllerId], $serialised);
$serialised = $this->plugin->serializeAttachments($entity, "metalists", $metalist_groups[$entity->controllerId], $serialised);
return $serialised;
};
// agents
$agents_args = Plugin::parseFilters($node->getFilters('agent'));
$agent_ids = array_map($map_ids, $user->getEnabledAgents());
if ($agent_ids) { // it HAS been the case that this was reached with only a draft agent, thus the need to check
$agents_args["id"] = "IN(" . implode(",", $agent_ids) . ")";
$agents_query = new ApiQuery(Agent::class, $agents_args);
$agent_ids = $agents_query->findIds();
if(!in_array($user->profile->id, $agent_ids)) {
$agent_ids[] = $user->profile->id;
}
$agents = $app->repo("Agent")->findBy(["id" => $agent_ids]);
} else {
$agents = [];
}
// spaces
$spaces_args = Plugin::parseFilters($node->getFilters('space'));
$space_ids = array_map($map_ids, $user->getEnabledSpaces());
if ($space_ids) {
$spaces_args["id"] = "IN(" . implode(",", $space_ids) . ")";
$spaces_query = new ApiQuery(Space::class, $spaces_args);
$space_ids = $spaces_query->findIds();
$spaces = $app->repo("Space")->findBy(["id" => $space_ids]);
} else {
$spaces = [];
}
// POST data
$data = [
"nodeSlug" => $this->plugin->nodeSlug,
"agents" => array_map($map_serialize, $agents),
"spaces" => array_map($map_serialize, $spaces),
];
$node->api->apiPost("network-node/bootstrapSync", $data);
return true;
}
/**
*
* @param mixed $data
* @param string $start_string
* @param string $interval_string
* @param int $iterations
* @return string
*/
protected function _generateId(array $data, string $start_string, string $interval_string, int $iterations)
{
return ("{$data["node"]}/bootstrapSync");
}
}