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