fix hasbackend() (Thx Max)
[racktables-contribs] / local_copy_object.php
CommitLineData
3fcf109f
DO
1<?php
2
0909f149
DO
3
4// I need this in local.php
5// define ('TABLE_BORDER', 0);
6// require_once "local_copy_object.php";
7// (c)2011 Manon Goo <manon@dg-i.net>
3fcf109f
DO
8
9$tab['object']['objectcopier'] = 'Object Copier ';
3fcf109f 10$tabhandler['object']['objectcopier'] = 'localfunc_ObjectCopier';
0909f149 11// $ophandler['object']['objectcopier']['updateobjectcopier'] = 'updateconfig_ObjectCopier';
3fcf109f
DO
12$ophandler['object']['objectcopier']['copyLotOfObjects'] = 'copyLotOfObjects';
13
3fcf109f
DO
14function localfunc_ObjectCopier($object_id)
15{
16 $object = spotEntity ('object', $object_id );
17 amplifyCell($object);
0909f149 18 global $virtual_obj_types, $tagtree, $taglist, $target_given_tags;
3fcf109f
DO
19 $typelist = readChapter (CHAP_OBJTYPE, 'o');
20 $typelist[0] = 'select type...';
21 $typelist = cookOptgroups ($typelist);
22 $max = getConfigVar ('MASSCOUNT');
23 $tabindex = 100;
24
3fcf109f
DO
25 echo "\n";
26 echo "\n<!-- printOpFormIntro ('copyLotOfObjects') -->\n";
27 printOpFormIntro ('copyLotOfObjects');
28 echo "\n";
0909f149
DO
29 startPortlet ('Make many copies of this object');
30 echo "\n" . sprintf('<table border=%s align=center>', TABLE_BORDER);
31 echo "\n" . '<tr><th align=left>name or "name","label","asset_no" (no csv escaping)<br><br>';
32 echo 'Example:<br> "server.example.com","server.example.com","12345"<br>www.example.com<br>testmachine<br>';
33 echo '</th><th>Tags</th></tr>';
3fcf109f 34 echo "<tr><td><input type=submit name=got_very_fast_data value='Go!'></td><td></td></tr>\n";
0909f149 35 echo "\n" . "<tr><td valign=top ><textarea name=namelist cols=60 rows=40>\n</textarea></td>";
3fcf109f
DO
36 echo "<td valign=top>";
37 printf ("<input type=hidden name=global_type_id value='%s'>\n", $object['objtype_id']);
0909f149 38 renderNewPreseclectedEntityTags ('Tag Tree', $tagtree, $target_given_tags, 'object');
3fcf109f
DO
39 echo "</td></tr>";
40 echo "<tr><td colspan=2><input type=submit name=got_very_fast_data value='Go!'></td></tr></table>\n";
41 echo "</form>\n";
42 finishPortlet();
3fcf109f
DO
43
44}
0909f149
DO
45
46function renderNewPreseclectedEntityTags ($title, $tags, $preselect = array (), $for_realm = '' )
47{
48 global $taglist;
49 if (!count ($taglist))
50 {
51 echo "No tags defined";
52 return;
53 }
54 printf ('<table border=%s align=center cellspacing=0 class="tagtree">', TABLE_BORDER);
55 foreach ($tags as $taginfo)
56 renderTagCheckbox ('taglist', $preselect , $taginfo, $for_realm);
57 echo '</table>';
58}
59
3fcf109f
DO
60function copyLotOfObjects($template_object)
61{
62 global $dbxlink;
63 $dbrollback = 0;
64 if (! $dbxlink->beginTransaction() )
65 throw new RTDatabaseError ("can not start transaction");
66 $log = emptyLog();
67 $taglist = isset ($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
68 assertUIntArg ('global_type_id', TRUE);
69 assertStringArg ('namelist', TRUE);
70 $global_type_id = $_REQUEST['global_type_id'];
71 $source_object_id = $_REQUEST['object_id'];
72 $source_object = spotEntity ('object', $source_object_id);
73 amplifyCell($source_object);
74 if ($global_type_id == 0 or !strlen ($_REQUEST['namelist']))
75 $log = mergeLogs ($log, oneLiner (186));
76 else
77 {
78 // The name extractor below was stolen from ophandlers.php:addMultiPorts()
79 $names1 = explode ("\n", $_REQUEST['namelist']);
80 $names2 = array();
81 foreach ($names1 as $line)
82 {
83 $parts = explode ('\r', $line);
84 reset ($parts);
85 if (!strlen ($parts[0]))
86 continue;
87 else
88 $names2[] = rtrim ($parts[0]);
89 }
0909f149 90 foreach ($names2 as $name_or_csv)
3fcf109f
DO
91 {
92 $label = '';
93 $asset_no = '';
94 $object_name = '';
95 $regexp='/^\"([^\"]*)\","([^\"]*)\","([^\"]*)\"/';
0909f149
DO
96 $object_name_or_csv = htmlspecialchars_decode($name_or_csv, ENT_QUOTES);
97 // error_log( "$regexp $object_name" );
98 if (preg_match($regexp, $object_name_or_csv, $matches) )
3fcf109f
DO
99 {
100 $object_name = $matches[1];
101 $label = $matches[2];
102 $asset_no = $matches[3];
103 }
104 else
0909f149 105 $object_name = $name_or_csv;
3fcf109f
DO
106 try
107 {
108 $object_id = commitAddObject ($object_name, $label, $global_type_id, $asset_no, $taglist);
109 if (!$object_id)
110 throw new RTDatabaseError("could not create $object_name");
111 $info = spotEntity ('object', $object_id);
112 amplifyCell ($info);
113 $name_by_id = array();
114 foreach ($source_object['ports'] as $source_port)
115 {
116 $update_port=0;
117 foreach ($info['ports'] as $existing_port)
118 {
119 if ($existing_port['name'] == $source_port['name'] )
120 {
121 commitUpdatePort ($object_id, $existing_port['id'], $existing_port['name'], $existing_port['oif_id'], $source_port['label'], "" );
122 $update_port=1;
123 }
124 }
125 if ($update_port)
126 true;
127 else
128 commitAddPort ( $object_id, $source_port['name'], sprintf("%s-%s", $source_port['iif_id'], $source_port['oif_id']), $source_port['label'], "" );
129 }
130 // Copy links
131 $info = spotEntity ('object', $object_id);
132 amplifyCell ($info);
133 $name_by_id = array();
134 foreach ($source_object['ports'] as $source_port)
135 {
136 foreach ($info['ports'] as $existing_port)
137 {
0909f149 138 foreach ( array( BACKEND_PROT_ID, 0 ) as $link_type)
3fcf109f
DO
139 {
140 $name_by_id[$existing_port['name']] = $existing_port['id'];
0909f149
DO
141 if ( $source_port[$link_type]['remote_object_id'] == $source_object_id &&
142 $existing_port['name'] == $source_port[$link_type]['remote_name'] )
143 linkPorts($existing_port['id'],
144 $name_by_id[$source_port['name']],
145 $source_port[$link_type][cabelid],
146 $link_type
147 );
3fcf109f
DO
148 }
149 }
150 }
151 // Copy attributes
152 foreach (getAttrValues ($source_object_id) as $record)
153 {
154 $value = $record['value'];
155 switch ($record['type'])
156 {
157 case 'uint':
158 case 'float':
159 case 'string':
160 $value = $record['value'];
161 break;
162 case 'dict':
163 $value = $record['key'];
164 break;
165 default:
166 }
167
168 if (permitted (NULL, NULL, NULL, array (array ('tag' => '$attr_' . $record['id'] ))))
3fcf109f 169 if (empty($value))
3fcf109f 170 commitUpdateAttrValue ($object_id, $record['id'] );
3fcf109f 171 else
3fcf109f 172 commitUpdateAttrValue ($object_id, $record['id'], $value ) ;
3fcf109f 173 else
3fcf109f 174 showError ('Permission denied, "' . $record['id'] . '"can not be set');
3fcf109f
DO
175
176 }
177
0909f149
DO
178 $log = mergeLogs ($log, oneLiner (5, array ('<a href="' .
179 makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) .
180 '">' . $info['dname'] . '</a>'))
181 );
3fcf109f
DO
182 }
183 catch (RTDatabaseError $e)
184 {
185 error_log("rolling back DB");
186 $dbrollback = 1;
187 $dbxlink->rollBack();
188 $log = mergeLogs ($log, oneLiner (147, array ($object_name)));
0909f149 189 throw new RTDatabaseError ( $e->getMessage() . sprintf(' (%s)', $name_or_csv ));
3fcf109f
DO
190 }
191 }
192 }
193 if (! $dbrollback )
194 $dbxlink->commit();
195 return buildWideRedirectURL ($log);
196}
197
3fcf109f 198?>