1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Pages;
13
14 use ICanBoogie\Event;
15 use ICanBoogie\Routing\Pattern;
16
17 class SaveOperation extends \Icybee\Modules\Nodes\SaveOperation
18 {
19 20 21 22 23
24 protected function lazy_get_properties()
25 {
26 global $core;
27
28 $properties = parent::lazy_get_properties() + array
29 (
30 Page::PARENTID => 0
31 );
32
33 if (!$this->key)
34 {
35
36
37 $site = $core->site;
38 $siteid = $site->siteid;
39 $properties[Page::SITEID] = $siteid;
40 $properties[Page::LANGUAGE] = $site->language;
41
42 if (empty($properties[Page::WEIGHT]))
43 {
44 $model = $this->module->model;
45
46 if ($model->count())
47 {
48 $weight = $model
49 ->where('siteid = ? AND parentid = ?', $siteid, $properties[Page::PARENTID])
50 ->maximum('weight');
51
52 $properties[Page::WEIGHT] = ($weight === null) ? 0 : $weight + 1;
53 }
54 else
55 {
56 $properties[Page::WEIGHT] = 0;
57 }
58 }
59 }
60
61 return $properties;
62 }
63
64 65 66
67 protected function validate(\ICanBoogie\Errors $errors)
68 {
69 $contents = $this->request['contents'];
70 $editors = $this->request['editors'];
71
72 if ($contents)
73 {
74 foreach (array_keys($contents) as $name)
75 {
76 if (!array_key_exists($name, $editors))
77 {
78 $errors['content'][] = $errors->format('The editor is missing for the content %name.', array('name' => $name));
79 }
80 }
81 }
82
83 return parent::validate($errors);
84 }
85
86 protected function process()
87 {
88 global $core;
89
90 $record = null;
91 $oldurl = null;
92
93 if ($this->record)
94 {
95 $record = $this->record;
96 $pattern = $record->url_pattern;
97
98 if (!Pattern::is_pattern($pattern))
99 {
100 $oldurl = $pattern;
101 }
102 }
103
104 $rc = parent::process();
105 $nid = $rc['key'];
106
107
108
109
110
111
112
113 $preserve = array();
114 $contents_model = $this->module->model('contents');
115
116 $contents = $this->request['contents'];
117 $editor_ids = $this->request['editors'];
118
119 if ($contents && $editor_ids)
120 {
121 foreach ($contents as $content_id => $unserialized_content)
122 {
123 if (!$unserialized_content)
124 {
125 continue;
126 }
127
128 $editor_id = $editor_ids[$content_id];
129 $editor = $core->editors[$editor_id];
130 $content = $editor->serialize($unserialized_content);
131
132 if (!$content)
133 {
134 continue;
135 }
136
137 $preserve[$content_id] = $content_id;
138
139 $values = array
140 (
141 'content' => $content,
142 'editor' => $editor_id
143 );
144
145 $contents_model->insert
146 (
147 array
148 (
149 'pageid' => $nid,
150 'contentid' => $content_id
151 )
152
153 + $values,
154
155 array
156 (
157 'on duplicate' => $values
158 )
159 );
160 }
161 }
162
163
164
165
166
167 $arr = $contents_model->filter_by_pageid($nid);
168
169 if ($preserve)
170 {
171 $arr->where(array('!contentid' => $preserve));
172 }
173
174 $arr->delete();
175
176 if ($record && $oldurl)
177 {
178 $record = $this->module->model[$nid];
179 $newurl = $record->url;
180
181 if ($newurl && $newurl != $oldurl)
182 {
183 new Page\MoveEvent($record, $oldurl, $newurl);
184 }
185 }
186
187 return $rc;
188 }
189 }
190
191 namespace Icybee\Modules\Pages\Page;
192
193 194 195
196 class MoveEvent extends \ICanBoogie\Event
197 {
198 199 200 201 202
203 public $from;
204
205 206 207 208 209
210 public $to;
211
212 213 214 215 216 217 218
219 public function __construct(\Icybee\Modules\Pages\Page $target, $from, $to)
220 {
221 $this->from = $from;
222 $this->to = $to;
223
224 parent::__construct($target, 'move');
225 }
226 }