1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Registry;
13
14 use ICanBoogie\ActiveRecord;
15 use ICanBoogie\Exception;
16
17 class Hooks
18 {
19 20 21
22
23 24 25 26 27 28 29 30 31
32 static public function on_editblock_alter_values(\Icybee\EditBlock\AlterValuesEvent $event, \Icybee\EditBlock $target)
33 {
34 if (!$event->key)
35 {
36 return;
37 }
38
39 $module = $event->module;
40
41 if ($module instanceof \Icybee\Modules\Nodes\Module)
42 {
43 $type = 'node';
44 }
45 else if ($module instanceof \Icybee\Modules\Users\Module)
46 {
47 $type = 'user';
48 }
49 else if ($module instanceof \Icybee\Modules\Sites\Module)
50 {
51 $type = 'site';
52 }
53 else
54 {
55 throw new Exception('Metadatas are not supported for instances of the given class: %class', [ '%class' => get_class($target) ]);
56 }
57
58 $metas = ActiveRecord\get_model('registry/' . $type)
59 ->select('name, value')
60 ->filter_by_targetid($event->key)
61 ->pairs;
62
63 $values = &$event->values;
64
65 if (isset($values['metas']))
66 {
67 if ($values['metas'] instanceof MetasHandler)
68 {
69 $values['metas'] = $values['metas']->to_aray();
70 }
71
72 $values['metas'] += $metas;
73 }
74 else
75 {
76 $values['metas'] = $metas;
77 }
78 }
79
80 81 82 83 84 85 86
87 static public function on_operation_save(\ICanBoogie\Operation\ProcessEvent $event, \ICanBoogie\SaveOperation $sender)
88 {
89 $params = $event->request->params;
90
91 if (!array_key_exists('metas', $params))
92 {
93 return;
94 }
95
96 $targetid = $event->rc['key'];
97
98 if ($sender instanceof \Icybee\Modules\Nodes\SaveOperation)
99 {
100 $type = 'node';
101 }
102 else if ($sender instanceof \Icybee\Modules\Users\SaveOperation)
103 {
104 $type = 'user';
105 }
106 else if ($sender instanceof \Icybee\Modules\Sites\SaveOperation)
107 {
108 $type = 'site';
109 }
110 else
111 {
112 throw new Exception('Metadatas are not supported for instances of the given class: %class', [ '%class' => get_class($sender) ]);
113 }
114
115 $model = ActiveRecord\get_model('registry/' . $type);
116 $driver_name = $model->connection->driver_name;
117 $delete_statement = '';
118 $update_groups = [];
119 $delete_args = [];
120
121 foreach ($params['metas'] as $name => $value)
122 {
123 if (is_array($value))
124 {
125 $value = serialize($value);
126 }
127 else if (!strlen($value))
128 {
129 $value = null;
130
131 $delete_statement .= ', ?';
132 $delete_args[] = $name;
133
134 continue;
135 }
136
137 if ($driver_name == 'sqlite')
138 {
139 $update_groups[] = [ $targetid, $name, $value ];
140 }
141 else
142 {
143 $update_groups[] = [ $targetid, $name, $value, $value ];
144 }
145 }
146
147 $model->connection->begin();
148
149 if ($delete_statement)
150 {
151 array_unshift($delete_args, $targetid);
152
153 $delete_statement = 'DELETE FROM {self} WHERE targetid = ? AND name IN (' . substr($delete_statement, 2) . ')';
154
155 $model->execute($delete_statement, $delete_args);
156 }
157
158 if ($update_groups)
159 {
160 if ($driver_name == 'sqlite')
161 {
162 $update = $model->prepare('INSERT OR REPLACE INTO {self} (targetid, name, value) VALUES(?,?,?)');
163 }
164 else
165 {
166 $update = $model->prepare('INSERT INTO {self} (targetid, name, value) VALUES(?,?,?) ON DUPLICATE KEY UPDATE value = ?');
167 }
168
169 foreach ($update_groups as $values)
170 {
171 $update->execute($values);
172 }
173 }
174
175 $model->connection->commit();
176 }
177
178 179 180 181 182 183 184 185
186 static public function on_operation_delete(\ICanBoogie\Operation\ProcessEvent $event, \ICanBoogie\DeleteOperation $operation)
187 {
188 $module = $operation->module;
189
190 if ($module instanceof \Icybee\Modules\Nodes\Module)
191 {
192 $type = 'node';
193 }
194 else if ($module instanceof \Icybee\Modules\Users\Module)
195 {
196 $type = 'user';
197 }
198 else if ($module instanceof \Icybee\Modules\Sites\Module)
199 {
200 $type = 'site';
201 }
202 else
203 {
204 throw new Exception('Metadatas are not supported for instances of the given class: %class', [ '%class' => get_class($module) ]);
205 }
206
207 ActiveRecord\get_model('registry/' . $type)
208 ->filter_by_targetid($operation->key)
209 ->delete();
210 }
211
212 213 214
215
216 217 218 219 220 221 222 223
224 static public function get_metas(\ICanBoogie\ActiveRecord $target)
225 {
226 return new MetasHandler($target);
227 }
228
229 230 231 232 233 234 235
236
237 static public function get_registry(\ICanBoogie\Core $target)
238 {
239 return $target->models['registry'];
240 }
241 }