Update local_copy_object.php
[racktables-contribs] / local_copy_object.php
CommitLineData
3fcf109f 1<?php
3fcf109f 2$tab['object']['objectcopier'] = 'Object Copier ';
3fcf109f 3$tabhandler['object']['objectcopier'] = 'localfunc_ObjectCopier';
3fcf109f
DO
4$ophandler['object']['objectcopier']['copyLotOfObjects'] = 'copyLotOfObjects';
5
a0aa39e6 6function amplifyCell_object_Backend_Port (&$record, $dummy = NULL)
3fcf109f 7{
a0aa39e6 8 switch ($record['realm'])
9 {
10 case 'object':
11 $record['BackendPorts'] = getObjectBackendPortsAndLinks ($record['id']);
12 break;
13 default:
14 }
15}
3fcf109f 16
a0aa39e6 17function getObjectBackendPortsAndLinks ($object_id)
18{
19 $ret = fetchBackendPortList ("Port.object_id = ?", array ($object_id));
20 return sortPortList ($ret, TRUE);
3fcf109f 21}
0909f149 22
a0aa39e6 23function fetchBackendPortList ($sql_where_clause, $query_params = array())
0909f149 24{
a0aa39e6 25 $query = <<<END
26SELECT
27 Port.id,
28 Port.name,
29 Port.object_id,
30 Object.name AS object_name,
31 Port.l2address,
32 Port.label,
33 Port.reservation_comment,
34 Port.iif_id,
35 Port.type AS oif_id,
36 (SELECT PortInnerInterface.iif_name FROM PortInnerInterface WHERE PortInnerInterface.id = Port.iif_id) AS iif_name,
37 (SELECT Dictionary.dict_value FROM Dictionary WHERE Dictionary.dict_key = Port.type) AS oif_name,
38 IF(lba.porta, lba.cable, lbb.cable) AS cableid,
39 IF(lba.porta, pa.id, pb.id) AS remote_id,
40 IF(lba.porta, pa.name, pb.name) AS remote_name,
41 IF(lba.porta, pa.object_id, pb.object_id) AS remote_object_id,
42 IF(lba.porta, oa.name, ob.name) AS remote_object_name,
43 (SELECT COUNT(*) FROM PortLog WHERE PortLog.port_id = Port.id) AS log_count,
44 PortLog.user,
45 UNIX_TIMESTAMP(PortLog.date) as time
46FROM
47 Port
48 INNER JOIN Object ON Port.object_id = Object.id
49 LEFT JOIN LinkBackend AS lba ON lba.porta = Port.id
50 LEFT JOIN Port AS pa ON pa.id = lba.portb
51 LEFT JOIN Object AS oa ON pa.object_id = oa.id
52 LEFT JOIN LinkBackend AS lbb on lbb.portb = Port.id
53 LEFT JOIN Port AS pb ON pb.id = lbb.porta
54 LEFT JOIN Object AS ob ON pb.object_id = ob.id
55 LEFT JOIN PortLog ON PortLog.id = (SELECT id FROM PortLog WHERE PortLog.port_id = Port.id ORDER BY date DESC LIMIT 1)
56WHERE
57 $sql_where_clause
58END;
59
60 $result = usePreparedSelectBlade ($query, $query_params);
61 $ret = array();
62 while ($row = $result->fetch (PDO::FETCH_ASSOC))
0909f149 63 {
a0aa39e6 64 $row['l2address'] = l2addressFromDatabase ($row['l2address']);
65 $row['linked'] = isset ($row['remote_id']) ? 1 : 0;
66
67 // last changed log
68 $row['last_log'] = array();
69 if ($row['log_count'])
70 {
71 $row['last_log']['user'] = $row['user'];
72 $row['last_log']['time'] = $row['time'];
73 }
74 unset ($row['user']);
75 unset ($row['time']);
76
77 $ret[] = $row;
0909f149 78 }
a0aa39e6 79 return $ret;
80}
81
82
83function varDumpToString ($var)
84{
85 ob_start();
86 var_dump($var);
87 $result = ob_get_clean();
88 return $result;
89}
90
91function localfunc_ObjectCopier($object_id)
92{
93$object = spotEntity ('object', $object_id );
94amplifyCell($object);
95global $virtual_obj_types, $tagtree, $taglist, $target_given_tags;
96$typelist = readChapter (CHAP_OBJTYPE, 'o');
97$typelist[0] = 'select type...';
98$typelist = cookOptgroups ($typelist);
99$max = getConfigVar ('MASSCOUNT');
100$tabindex = 100;
101
102echo "\n";
103echo "\n<!-- printOpFormIntro ('copyLotOfObjects') -->\n";
104printOpFormIntro ('copyLotOfObjects');
105echo "\n";
106startPortlet ('Make many copies of this object');
107echo "\n" . sprintf('<table border=%s align=center>', TABLE_BORDER);
108echo "\n" . '<tr><th align=left>name or "name","label","asset_no" (no csv escaping)<br><br>';
109echo 'Example:<br> "server.example.com","server.example.com","12345"<br>www.example.com<br>testmachine<br>';
110echo '</th><th>Copy Tags</th></tr>';
111//echo "<tr><td><input type=submit name=got_very_fast_data value='Go!'></td><td></td></tr>\n";
112echo "\n" . "<tr><td valign=top ><textarea name=namelist cols=60 rows=35>\n</textarea></td>";
113echo "<td valign=top>";
114printf ("<input type=hidden name=global_type_id value='%s'>\n", $object['objtype_id']);
115//renderCopyEntityTagsPortlet ('Tag tree', $tagtree, $target_given_tags, $etype_by_pageno[$pageno]);
116renderCopyEntityTags($object);
117echo "</td></tr>";
118echo "<tr><td colspan=2><input type=submit name=got_very_fast_data value='Go!'></td></tr></table>\n";
119echo "</form>\n";
120finishPortlet();
121
0909f149
DO
122}
123
a0aa39e6 124function copyLotOfObjects()//$template_object)
3fcf109f
DO
125{
126 global $dbxlink;
127 $dbrollback = 0;
842d0f76 128 if (! $dbxlink->beginTransaction() )
3fcf109f 129 throw new RTDatabaseError ("can not start transaction");
a0aa39e6 130 // do we need this ?
3fcf109f
DO
131 $log = emptyLog();
132 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
133 assertUIntArg ('global_type_id', TRUE);
134 assertStringArg ('namelist', TRUE);
135 $global_type_id = $_REQUEST['global_type_id'];
136 $source_object_id = $_REQUEST['object_id'];
137 $source_object = spotEntity ('object', $source_object_id);
138 amplifyCell($source_object);
a0aa39e6 139 if ( function_exists ( 'amplifyCell_object_Backend_Port' ) )
842d0f76 140 {
a0aa39e6 141 amplifyCell_object_Backend_Port($source_object);
842d0f76 142 }
3fcf109f 143 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
a0aa39e6 144 {
842d0f76 145 // Log something reasonable with showError Here
a0aa39e6 146 // We do not have names to copy our object to !
842d0f76 147 // Pls check what makes $global_type_id == 0 an error
3fcf109f 148 $log = mergeLogs ($log, oneLiner (186));
a0aa39e6 149 return ;
150 }
151 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
152 $names1 = explode ("\n", $_REQUEST['namelist']);
153 $names2 = array();
154 foreach ($names1 as $line)
155 {
156 $parts = explode ('\r', $line);
157 reset ($parts);
158 if (!strlen ($parts[0]))
159 continue;
160 else
161 $names2[] = rtrim ($parts[0]);
162 }
163 foreach ($names2 as $name_or_csv)
3fcf109f 164 {
a0aa39e6 165 $label = '';
166 $asset_no = '';
167 $object_name = '';
168 $regexp='/^\"([^\"]*)\","([^\"]*)\","([^\"]*)\"/';
842d0f76 169 $object_name_or_csv = htmlspecialchars_decode($name_or_csv, ENT_QUOTES);
a0aa39e6 170 // error_log( "$regexp $object_name" );
842d0f76 171 if (preg_match($regexp, $object_name_or_csv, $matches) )
3fcf109f 172 {
a0aa39e6 173 $object_name = $matches[1];
174 $label = $matches[2];
175 $asset_no = $matches[3];
842d0f76 176 }
177 else
a0aa39e6 178 $object_name = $name_or_csv;
179 try
3fcf109f 180 {
a0aa39e6 181 $object_id = commitAddObject ($object_name, $label, $global_type_id, $asset_no, $taglist);
182 if (!$object_id)
183 throw new RTDatabaseError("could not create $object_name");
184 $info = spotEntity ('object', $object_id);
185 amplifyCell ($info);
186 foreach ($source_object['ports'] as $source_port)
3fcf109f 187 {
a0aa39e6 188 $update_port=0;
842d0f76 189 foreach ($info['ports'] as $new_port)
3fcf109f 190 {
842d0f76 191 if ($new_port['name'] == $source_port['name'] )
3fcf109f 192 {
a0aa39e6 193 commitUpdatePort ($object_id, $new_port['id'], $new_port['name'], $new_port['oif_id'], $source_port['label'], "" );
194 $update_port=1;
3fcf109f 195 }
3fcf109f 196 }
a0aa39e6 197 if ($update_port)
198 true;
842d0f76 199 else
a0aa39e6 200 commitAddPort ( $object_id, $source_port['name'], sprintf("%s-%s", $source_port['iif_id'], $source_port['oif_id']), $source_port['label'], "" );
201 }
202 // Copy Backendlinks only start if we ghave function linkmgmt_linkPorts from linkmgmt.php
203 if ( function_exists ( 'amplifyCell_object_Backend_Port' ) && function_exists ('linkmgmt_linkPorts') )
842d0f76 204 {
3fcf109f
DO
205 $info = spotEntity ('object', $object_id);
206 amplifyCell ($info);
a0aa39e6 207 amplifyCell_object_Backend_Port($info);
842d0f76 208
a0aa39e6 209 /* showError( '<div align="left"><pre>\n===== Source Object ======\n\n' .
842d0f76 210 varDumpToString ( $source_object ) .
211 '\n\n===== New Object ======\n\n' .
a0aa39e6 212 varDumpToString ( $info ) . '</pre></div>' );
842d0f76 213 */
3fcf109f 214 $name_by_id = array();
842d0f76 215 foreach ($info['BackendPorts'] as $new_be_port)
3fcf109f 216 {
a0aa39e6 217 $name_by_id[$new_be_port['name']] = $new_be_port['id'];
218 }
219
220 $linked_ports = array();
221 foreach ($source_object['BackendPorts'] as $source_be_port)
222 {
223 if ( $source_be_port['object_id'] == $source_be_port['remote_object_id'] )
3fcf109f 224 {
a0aa39e6 225 // We have a Port that has the own object as remote object we want to copy this type of Linko
842d0f76 226 // We have backend Links
a0aa39e6 227 $new_be_port_a = $name_by_id[$source_be_port['name']] ;
228 $new_be_port_b = $name_by_id[$source_be_port['remote_name']] ;
229 if ( $new_be_port_a && $new_be_port_b && ! array_key_exists($new_be_port_a, $linked_ports ) && ! array_key_exists($new_be_port_b, $linked_ports ) )
842d0f76 230 {
a0aa39e6 231 // error_log ( sprintf ('new_be_port_a %s // new_be_port_b %s // cableid %s', $new_be_port_a , $new_be_port_b, $source_be_port['cableid'] ));
232 $ret_val = linkmgmt_linkPorts( $new_be_port_a , $new_be_port_b , 'back', $source_be_port['cableid'] );
233 // error_log ( sprintf (' linkmgmt_linkPorts ret val: "%s" ', $ret_val)) ;
234 if ($ret_val)
235 {
236 throw new RTDatabaseError("could not copy Backend Links for $object_name because: $ret_val");
237 }
842d0f76 238 else
a0aa39e6 239 {
240 $linked_ports[$new_be_port_a] = True;
241 $linked_ports[$new_be_port_b] = True;
842d0f76 242 }
3fcf109f
DO
243 }
244 }
245 }
842d0f76 246 }
a0aa39e6 247 // Copy attributes
248 foreach (getAttrValues ($source_object_id) as $record)
249 {
250 $value = $record['value'];
251 switch ($record['type'])
3fcf109f 252 {
a0aa39e6 253 case 'uint':
254 case 'float':
255 case 'string':
256 $value = $record['value'];
257 break;
258 case 'dict':
259 $value = $record['key'];
260 break;
261 default:
3fcf109f 262 }
842d0f76 263
a0aa39e6 264 if (permitted (NULL, NULL, NULL, array (array ('tag' => '$attr_' . $record['id'] ))))
265 if (empty($value))
266 commitUpdateAttrValue ($object_id, $record['id'] );
267 else
268 commitUpdateAttrValue ($object_id, $record['id'], $value ) ;
269 else
270 showError ('Permission denied, "' . $record['id'] . '"can not be set');
3fcf109f 271
3fcf109f 272 }
a0aa39e6 273
842d0f76 274 //$log = mergeLogs ($log, oneLiner (5, array ('<a href="' .
a0aa39e6 275 // makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) .
276 // '">' . $info['dname'] . '</a>'))
277 //);
278 showSuccess (sprintf ("Copied Object %s ; new Object: %s", $source_object['name'] , formatPortLink($object_id, $info['dname'], 1, '', '')) );
279 }
280 catch (RTDatabaseError $e)
281 {
282 error_log("rolling back DB");
283 $dbrollback = 1;
284 $dbxlink->rollBack();
285 $log = mergeLogs ($log, oneLiner (147, array ($object_name)));
286 throw new RTDatabaseError ( $e->getMessage() . sprintf(' (%s)', $name_or_csv ));
3fcf109f
DO
287 }
288 }
289 if (! $dbrollback )
290 $dbxlink->commit();
a0aa39e6 291 // return buildWideRedirectURL ($log);
292}
293
294//MOD
295//MOD
296//MOD
297function renderCopyEntityTagsPortlet ($title, $tags, $preselect, $realm)
298{
299 startPortlet ($title);
300 echo '<a class="toggleTreeMode" style="display:none" href="#"></a>';
301 echo '<table border=0 cellspacing=0 cellpadding=1 align=center class="tagtree">';
302 printTagCheckboxTable ('taglist', $preselect, array(), $tags, $realm);
303 echo '<tr><td class=tdleft>';
304 //echo "</form></td><td class=tdright>";
305 echo '</td></tr></table>';
306 finishPortlet();
307}
308
309function renderCopyEntityTags ($entity_id)
310{
311 global $tagtree, $taglist, $target_given_tags, $pageno, $etype_by_pageno;
312 echo '<table border=0 width="10%"><tr>';
313
314 if (count ($taglist) > getConfigVar ('TAGS_QUICKLIST_THRESHOLD'))
315 {
316 $minilist = getTagChart (getConfigVar ('TAGS_QUICKLIST_SIZE'), $etype_by_pageno[$pageno], $target_given_tags);
317 // It could happen, that none of existing tags have been used in the current realm.
318 if (count ($minilist))
319 {
320 $js_code = "tag_cb.setTagShortList ({";
321 $is_first = TRUE;
322 foreach ($minilist as $tag)
323 {
324 if (! $is_first)
325 $js_code .= ",";
326 $is_first = FALSE;
327 $js_code .= "\n\t${tag['id']} : 1";
328 }
329 $js_code .= "\n});\n$(document).ready(tag_cb.compactTreeMode);";
330 addJS ('js/tag-cb.js');
331 addJS ($js_code, TRUE);
332 }
333 }
334
335 // do not do anything about empty tree, trigger function ought to work this out
336 echo '<td class=pcright>';
337 renderCopyEntityTagsPortlet ('', $tagtree, $target_given_tags, $etype_by_pageno[$pageno]);
338 echo '</td>';
339
340 echo '</tr></table>';
3fcf109f 341}
a0aa39e6 342function mergeLogs ($log1, $log2)
343{
344 $ret = emptyLog();
345 $ret['m'] = array_merge ($log1['m'], $log2['m']);
346 return $ret;
347}
348
349
3fcf109f 350
a0aa39e6 351function emptyLog ()
352{
353 return array
354 (
355 'v' => 2,
356 'm' => array()
357 );
358}
359function oneLiner ($code, $args = array())
360{
361 $ret = emptyLog();
362 $ret['m'][] = count ($args) ? array ('c' => $code, 'a' => $args) : array ('c' => $code);
363 return $ret;
364}
3fcf109f 365?>