1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Nodes;
13
14 use ICanBoogie\Events;
15 use ICanBoogie\HTTP\Request;
16 use ICanBoogie\Operation;
17
18 use Brickrouge\Form;
19
20 class ImportOperation extends Operation
21 {
22 protected $keys_translations = [];
23
24 protected function get_controls()
25 {
26 return [
27
28 self::CONTROL_PERMISSION => Module::PERMISSION_ADMINISTER
29
30 ] + parent::get_controls();
31 }
32
33 protected function validate(\ICanboogie\Errors $errors)
34 {
35 return true;
36 }
37
38 protected function process()
39 {
40 $data = $this->preparse_data();
41 $data = $this->parse_data($data);
42
43 $save = Request::from([
44
45 'path' => Operation::encode("{$this->module}/save")
46
47 ], [ $_SERVER ] );
48
49
50
51
52
53 $core->events->attach(function(Operation\GetFormEvent $event, SaveOperation $operation) use($save) {
54
55 if ($event->request !== $save)
56 {
57 return;
58 }
59
60 $event->form = new Form();
61
62 });
63
64 65 66 67 68 69 70 71 72 73 74
75
76 $this->import($data, $save);
77
78 $this->response->message = "Records were successfuly imported.";
79
80 return true;
81 }
82
83 protected function preparse_data()
84 {
85 $json = file_get_contents(\ICanBoogie\DOCUMENT_ROOT . 'export.json');
86 $data = json_decode($json);
87
88 if (!$data)
89 {
90 throw new \ICanBoogie\Exception("Failed to decode JSON: !json", [ 'json' => $json ]);
91 }
92
93 return (array) $data->rc;
94 }
95
96 protected function parse_data(array $data)
97 {
98 global $core;
99
100 $site = $core->site;
101 $siteid = $site->siteid;
102 $language = $site->language;
103
104 $is_translating = true;
105
106 foreach ($data as $nid => $node)
107 {
108 $node->siteid = $siteid;
109
110 if ($is_translating)
111 {
112 $node->nativeid = $nid;
113 }
114
115 $node->language = $language;
116 }
117
118 return $data;
119 }
120
121 protected function import(array $data, Request $request)
122 {
123 foreach ($data as $nid => $node)
124 {
125 $request->params = (array) $node;
126
127 $response = $request->post();
128
129 $this->keys_translations[$nid] = $response->rc['key'];
130 }
131 }
132 }