1 <?php
3 /*! \brief This class allows to manage backend config items and packages.
4 */
5 class newConfigManagement extends plugin
6 {
7 var $initTime;
8 var $plHeadline = "Config management";
9 var $plDescription = "Config management";
11 var $selectedContainer;
12 var $selectedItem;
14 var $dataModel = NULL;
15 var $listing = NULL;
18 /*! \brief Initialize the plugin and finally update the data model.
19 */
20 function __construct($config, $dn)
21 {
22 $this->config = &$config;
23 $this->listing = new ConfigManagementListing($this->config, get_userinfo(), $this);
25 // Request an update of the data model
26 $this->updateDataModel();
27 }
30 /*! \brief Updates all distributions, releases, packages and items in the dataModel
31 * Load information from the backend.
32 */
33 function updateDataModel()
34 {
35 // Recreate the data model, to have a clean and fresh instance.
36 $this->dataModel = new ConfigManagementDataModel();
38 // Load distributions
39 $rpc = $this->config->getRpcHandle();
40 $res = $rpc->getDistributions();
41 if(!$rpc->success()){
42 msg_dialog::display(_("Error"), sprintf(_("Failed to load distributions, error was '%s'!"), $rpc->get_error()),ERROR_DIALOG);
43 return(NULL);
44 }
45 foreach($res as $dist){
46 $this->dataModel->addItem('Distribution','/root', $dist['name'], $dist);
47 foreach($dist['releases'] as $release){
48 $distPath = "/root/{$dist['name']}";
49 $this->dataModel->addItem('Release',$distPath, $release['name'], $release);
51 foreach($dist['components'] as $component){
52 $comPath = "{$distPath}/{$release['name']}";
53 $this->dataModel->addItem('Component',$comPath, $component['name'], $component);
54 }
55 }
56 }
57 }
60 /*! \brief Keep track of posted values and populate those
61 * which are interesting for us.
62 * Inspects the _POST and _GET values.
63 */
64 function save_object()
65 {
66 // Update the listing class, this is necessary to get post
67 // actions from it.
68 $this->listing->save_object();
70 // Get the selected distribution and release from the listing widget.
71 $cont = $this->listing->getSelectedContainer();
72 $item = $this->listing->getSelectedItem();
73 $this->setCurrentContainer($cont, $item);
74 }
77 /*! \brief Load extended sub-objecte like 'config items' or 'packages'
78 * for the given release path.
79 * @param String The release path to load sub-objects for.
80 * @return NULL
81 */
82 function updateItemList($path)
83 {
84 // Fist get Item and check if it is an release
85 if($this->dataModel->itemExistsByPath($path)){
86 $data = $this->dataModel->getItemByPath($path);
88 // Only releases can contain config-items.
89 if($data['type'] == 'Release' && $data['status'] != "fetched"){
91 $rpc = $this->config->getRpcHandle();
92 $res = $rpc->listConfigItems($data['name']);
93 if(!$rpc->success()){
94 msg_dialog::display(_("Error"),
95 sprintf(_("Failed to load distributions, error was '%s'!"),
96 $rpc->get_error()),ERROR_DIALOG);
97 }else{
98 $rpc = $this->config->getRpcHandle();
99 $res = $rpc->getPackages($data['name']);
101 return;
102 foreach($res as $itemPath => $type){
104 // Make names dataModel conform
105 $itemPath = $path.'/'.preg_replace("/^\//","/root", $itemPath);
106 $name = preg_replace("/^.*\//","",$itemPath);
107 $itemPath = preg_replace("/\/[^\/]*$/","", $itemPath);
108 $this->dataModel->addItem($type, $itemPath, $name);
109 }
110 $this->dataModel->setItemStatus($path, 'fetched');
111 }
112 }
113 }
114 }
117 /*! \brief Sets the currently selected container and item path.
118 * @param String The path of the container to set.
119 * @param String The path of the item to set.
120 * @return
121 */
122 function setCurrentContainer($cont, $item)
123 {
124 $this->selectedContainer = $cont;
125 $this->selectedItem = $item;
127 // Get a list of all available container and items.
128 $distList = $this->getContainerList();
129 $releaseList = $this->getItemList();
131 // Ensure that we've valid values selected.
132 if(!isset($releaseList[$this->selectedItem])){
133 if(count($releaseList)){
134 echo "Fallback";
135 $this->selectedItem = key($releaseList);
136 }else{
137 echo "Fallback 2";
138 $this->selectedItem = "";
139 }
140 }
142 // Update list of items within the selected container.
143 $this->updateItemList($this->selectedContainer);
145 // Transfer checked values back to the listing class.
146 $this->listing->setContainer($this->selectedContainer);
147 $this->listing->setItem($this->selectedItem);
148 $this->listing->setContainers($this->getContainerList());
149 $this->listing->setItems($this->getItemList());
150 }
153 /*! \brief Generate the HTML content for this plugin.
154 * Actually renders the listing widget..
155 */
156 function execute()
157 {
158 // Get the selected release and store it in a session variable
159 // to allow the configFilter to access it and display the
160 // packages and items.
161 $res = $this->listing->execute();
162 $this->listing->setListingTypes($this->getListingTypes());
164 return($res);
165 }
168 /*! \brief Returns a list of items which will then be displayed
169 * in the management-list.
170 * (The management class calls this method from its execute())
171 * @return Array A list of items/objects for the listing.
172 */
173 function getItemsToBeDisplayed()
174 {
176 $path = $this->selectedContainer.$this->selectedItem;
177 $item = $this->dataModel->getItemByPath($path);
178 return($item);
179 }
182 /*! \brief Returns a simply list of all releases of the CURRENT distribution.
183 * This list will then be used to generate the entries of the
184 * ItemSelectors in the listing class.
185 */
186 function getItemList()
187 {
188 $data = $this->dataModel->getItemByPath($this->selectedContainer);
189 $res = array();
190 $res[""] = array("name" => "/", "desc" => "");
191 $res = array_merge($res,$this->__recurseItem($data, array('Component')));
192 return($res);
195 if($data['type'] != 'Release'){
196 }else{
197 $res = array_merge($res,$this->__recurseItem($data, array('Component'),$this->selectedContainer));
198 }
199 return($res);
200 }
203 /*! \brief Returns a simply list of all distributions.
204 * This list will then be used to generate the entries of the
205 * ItemSelectors in the listing class.
206 */
207 function getContainerList()
208 {
209 $data = $this->dataModel->getItemByPath('/root');
210 $res = array();
211 $res["/root"] = array("name" => "/", "desc" => "");
212 $res = array_merge($res,$this->__recurseItem($data, array('Distribution','Release')));
214 print_a($res);
215 return($res);
216 }
219 /*! \brief Recursivly walks through an item and collects all path and name info.
220 * The reult can then be used to fill the ItemSelector.
221 * @param Array The Item to recurse.
222 * @param Array The type of of objects to collect.
223 * @param String The parent path prefix which should be removed.
224 * @return Array An array containing Array[path] = name
225 */
226 function __recurseItem($item, $types, $parent = "")
227 {
228 $res = array();
229 if(in_array($item['type'], $types)){
230 $path = preg_replace("/".preg_quote($parent,'/')."/","",$item['path']);
231 $res[$path] = array('name' => $item['name'],'desc'=>$item['type']);
232 }
233 if(count($item['children'])){
234 foreach($item['children'] as $child){
235 $res = array_merge($res, $this->__recurseItem($child, $types, $parent));
236 }
237 }
238 return($res);
239 }
242 /*! \brief Intializes this plugin
243 * All available installation methods will be loaded
244 */
245 function loadInstallationMethods()
246 {
247 // Reset erros
248 $this->rpcError = $this->initFailed = FALSE;
250 // Load configuration via rpc.
251 $rpc = $this->config->getRpcHandle();
253 // Populate install methods on success.
254 $res = $rpc->getSupportedInstallMethods();
255 if(!$rpc->success()){
256 $this->rpcError = TRUE;
257 $this->errorMessage = $rpc->get_error();;
258 return;
259 }
260 $this->installationMethods = $res;
261 if(!count($this->installationMethods)){
262 $this->errorMessage = _("No selectable install methods returned!");
263 msg_dialog::display(_("Setup"), $this->errorMessage , ERROR_DIALOG);
264 $this->initFailed = TRUE;
265 return;
266 }
267 }
270 /*! \brief Returns a info list about all items we can manage,
271 * this used to fill the listings <objectType> settings.
272 * @return Array An array with item info.
273 */
274 function getListingTypes()
275 {
276 $types= array();
277 $types['Distribution']['objectClass'] = 'Distribution';
278 $types['Distribution']['label'] = _('Distribution');
279 $types['Distribution']['image'] = 'images/lists/edit.png';
280 $types['Distribution']['category'] = 'Device';
281 $types['Distribution']['class'] = 'Device';
283 $types['Release']['objectClass'] = 'Release';
284 $types['Release']['label'] = _('Release');
285 $types['Release']['image'] = 'images/lists/delete.png';
286 $types['Release']['category'] = 'Device';
287 $types['Release']['class'] = 'Device';
289 $types['Component']['objectClass'] = 'Component';
290 $types['Component']['label'] = _('Component');
291 $types['Component']['image'] = 'plugins/users/images/select_user.png';
292 $types['Component']['category'] = 'Device';
293 $types['Component']['class'] = 'Device';
294 return($types);
295 }
298 /*! \brief Acts on edit requests and opens an edit dialog for the received item-id.
299 * (This action is received from the ConfigManagementListing class.)
300 * @param Array The items ids. (May contain multiple ids)
301 * @return
302 */
303 function editEntry($ids)
304 {
305 foreach($ids as $id){
306 echo "<br>Edit {$id}";
307 }
308 }
311 /*! \brief Acts on remove requests.
312 * (This action is received from the ConfigManagementListing class.)
313 * @param Array The items ids. (May contain multiple ids)
314 * @return
315 */
316 function removeEntry($ids)
317 {
318 foreach($ids as $id){
319 echo "<br>Remove {$id}";
320 }
321 }
324 /*! \brief Acts on open requests.
325 * (This action is received from the ConfigManagementListing class.)
326 * @param Array The items ids. (May contain multiple ids)
327 * @return
328 */
329 function openEntry($ids)
330 {
331 $id = $ids[0];
332 $item = $this->dataModel->getItemById($id);
333 if($item){
334 if(in_array($item['type'], array('Distribution', 'Release'))){
335 $this->setCurrentContainer($item['path'], $this->selectedItem);
336 }elseif(in_array($item['type'], array('Component'))){
337 $next = preg_replace("/".preg_quote($this->selectedContainer,'/')."/","", $item['path']);
338 $this->setCurrentContainer($this->selectedContainer, $next);
339 }else{
340 $this->editEntry(array($id));
341 }
342 }
343 }
346 /*! \brief The plugins ACL and plugin-property definition.
347 * @return
348 */
349 public static function plInfo()
350 {
351 return (array(
352 "plShortName" => _("Config management"),
353 "plDescription" => _("Config management"),
354 "plSelfModify" => FALSE,
355 "plDepends" => array(),
356 "plPriority" => 0,
357 "plSection" => array("administration"),
358 "plCategory" => array(
359 "newConfigManagement" => array("description" => _("Config management"),
360 "objectClass" => "FAKE_OC_newConfigManagement")),
361 "plProvidedAcls"=> array()
362 ));
363 }
366 /*! \brief
367 * @param
368 * @return
369 */
370 function remove_lock()
371 {
372 }
375 }
376 ?>