1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Pages;
13
14 use ICanBoogie\HTTP\Request;
15
16 class ImportOperation extends \Icybee\Modules\Nodes\ImportOperation
17 {
18 private $parentid = array();
19 private $locationid = array();
20
21 protected function parse_data(array $data)
22 {
23 foreach ($data as $nid => $obj)
24 {
25 if ($obj->parentid)
26 {
27 $this->parentid[$nid] = $obj->parentid;
28 }
29
30 unset($obj->parentid);
31
32 if ($obj->locationid)
33 {
34 $this->locationid[$nid] = $obj->locationid;
35 }
36
37 unset($obj->locationid);
38
39 if (empty($obj->contents))
40 {
41 \ICanBoogie\log("page $nid has no content");
42 }
43 else
44 {
45 $contents = (array) $obj->contents;
46 $editors = (array) $obj->editors;
47
48 foreach ($contents as $contentid => &$content)
49 {
50 if (($content{0} == '{' || $content{0} == '[') && $content{1} == '"')
51 {
52 $content = json_decode($content, true);
53 }
54 }
55
56 foreach ($editors as $contentid => $editor_name)
57 {
58 if ($editor_name != 'widgets' || empty($contents[$contentid]))
59 {
60 continue;
61 }
62
63 $content = &$contents[$contentid];
64 $content = array_combine($content, array_fill(0, count($content), 'on'));
65 }
66
67 $obj->contents = $contents;
68 $obj->editors = $editors;
69 }
70 }
71
72 return parent::parse_data($data);
73 }
74
75 protected function import(array $data, Request $save)
76 {
77 global $core;
78
79 parent::import($data, $save);
80
81
82
83 $update = $core->db->prepare('UPDATE {prefix}pages SET parentid = ?, locationid = ? WHERE nid = ?');
84
85 $original_nodes_with_parentid = $this->parentid;
86 $original_nodes_with_locationid = $this->locationid;
87
88 foreach (array_keys($data) as $nid)
89 {
90 $parentid = 0;
91
92 if (isset($original_nodes_with_parentid[$nid]))
93 {
94 $parentid = $this->keys_translations[$original_nodes_with_parentid[$nid]];
95 }
96
97 $locationid = 0;
98
99 if (isset($original_nodes_with_locationid[$nid]))
100 {
101 $locationid = $this->keys_translations[$original_nodes_with_locationid[$nid]];
102 }
103
104 if ($parentid || $locationid)
105 {
106 $update->execute(array($parentid, $locationid, $this->keys_translations[$nid]));
107 }
108 }
109 }
110 }