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;
13 var $dataModel = NULL;
14 var $listing = NULL;
17 /*! \brief Initialize the plugin and finally update the data model.
18 */
19 function __construct($config, $dn)
20 {
21 $this->config = &$config;
22 $this->listing = new ConfigManagementListing($this->config, get_userinfo(), $this);
24 // Request an update of the data model
25 $this->loadInstallationMethods();
26 $this->updateDataModel();
27 $this->listing->setListingTypes($this->getListingTypes());
28 }
31 /*! \brief Updates all distributions, releases, packages and items in the dataModel
32 * Load information from the backend.
33 */
34 function updateDataModel()
35 {
36 // Recreate the data model, to have a clean and fresh instance.
37 $this->dataModel = new ConfigManagementDataModel();
39 // Load distributions
40 $rpc = $this->config->getRpcHandle();
41 $res = $rpc->getDistributions();
43 if(!$rpc->success()){
44 msg_dialog::display(_("Error"), sprintf(_("Failed to load distributions: %s"), $rpc->get_error()),ERROR_DIALOG);
45 return(NULL);
46 }else{
47 foreach($res as $dist){
48 $this->dataModel->addItem('Distribution','/root', $dist['name'], $dist);
49 foreach($dist['releases'] as $release){
50 $distPath = "/root/{$dist['name']}";
51 $this->dataModel->addItem('Release',$distPath, $release['name'], $release);
53 # foreach($dist['components'] as $component){
54 # $comPath = "{$distPath}/{$release['name']}";
55 # $this->dataModel->addItem('Component',$comPath, $component['name'], $component);
56 # }
57 }
58 }
59 }
60 }
63 /*! \brief Keep track of posted values and populate those
64 * which are interesting for us.
65 * Inspects the _POST and _GET values.
66 */
67 function save_object()
68 {
69 // Update the listing class, this is necessary to get post
70 // actions from it.
71 $this->listing->save_object();
73 // Get the selected distribution and release from the listing widget.
74 $cont = $this->listing->getSelectedContainer();
75 if(isset($_POST['ROOT'])){
76 $this->setCurrentContainer('/root');
77 }elseif(isset($_POST['BACK'])){
78 $this->setCurrentContainer('/root');
79 }else{
80 $this->setCurrentContainer($cont);
81 }
82 }
85 /*! \brief Load extended sub-objecte like 'config items' or 'packages'
86 * for the given release path.
87 * @param String The release path to load sub-objects for.
88 * @return NULL
89 */
90 function updateItemList($path)
91 {
92 // Fist get Item and check if it is an release
93 if($this->dataModel->itemExistsByPath($path)){
94 $data = $this->dataModel->getItemByPath($path);
96 // Only releases can contain config-items.
97 if($data['type'] == 'Release' && $data['status'] != "fetched"){
99 $rpc = $this->config->getRpcHandle();
100 $res = $rpc->listConfigItems($data['name']);
101 if(!$rpc->success()){
102 msg_dialog::display(_("Error"),
103 sprintf(_("Failed to load distributions: %s"),
104 $rpc->get_error()),ERROR_DIALOG);
105 }else{
107 // Sort entries by path length
108 $sLen = array();
109 foreach($res as $itemPath => $type){
110 $sLen[strlen($itemPath)."_".$itemPath] = $itemPath;
111 }
112 uksort($sLen, "strnatcasecmp");
114 // Walk through each entry and then try to add it to the model
115 foreach($sLen as $unused => $itemPath){
117 $type = $res[$itemPath];
119 // Root installation objects do not have a name, so we use 'root' here.
120 $itemPath = trim($path."/root/".$itemPath);
122 // Remove trailing and duplicated slashes
123 $itemPath = rtrim($itemPath, '/');
124 $itemPath = preg_replace("/\/\/*/","/", $itemPath);
126 // Extract the items name
127 $name = preg_replace("/^.*\//","", $itemPath);
129 // Cleanup the path and then add the item.
130 $itemPath = preg_replace("/[^\/]*$/","", $itemPath);
131 $itemPath = rtrim($itemPath,'/');
132 $this->dataModel->addItem($type, $itemPath, $name);
133 }
134 $this->dataModel->setItemStatus($path, 'fetched');
135 }
136 }
137 }
138 }
141 /*! \brief Sets the currently selected container and item path.
142 * @param String The path of the container to set.
143 * @param String The path of the item to set.
144 * @return
145 */
146 function setCurrentContainer($cont)
147 {
148 echo $cont."<br>";
150 $this->selectedContainer = $cont;
152 // Update list of items within the selected container.
153 $this->updateItemList($this->selectedContainer);
155 // Transfer checked values back to the listing class.
156 $this->listing->setContainers($this->getContainerList());
157 $this->listing->setContainer($cont);
158 }
161 /*! \brief Generate the HTML content for this plugin.
162 * Actually renders the listing widget..
163 */
164 function execute()
165 {
166 // Get the selected release and store it in a session variable
167 // to allow the configFilter to access it and display the
168 // packages and items.
169 $res = $this->listing->execute();
170 $this->listing->setListingTypes($this->getListingTypes());
172 return($res);
173 }
176 /*! \brief Returns a list of items which will then be displayed
177 * in the management-list.
178 * (The management class calls this method from its execute())
179 * @return Array A list of items/objects for the listing.
180 */
181 function getItemsToBeDisplayed()
182 {
184 $path = $this->selectedContainer;
185 $item = $this->dataModel->getItemByPath($path);
186 return($item);
187 }
190 /*! \brief Returns a simply list of all distributions.
191 * This list will then be used to generate the entries of the
192 * ItemSelectors in the listing class.
193 */
194 function getContainerList()
195 {
196 $data = $this->dataModel->getItemByPath('/root');
197 $res = array();
198 $res["/root"] = array("name" => "/", "desc" => "");
199 $res = array_merge($res,$this->__recurseItem($data, array('Distribution','Release')));
200 return($res);
201 }
204 /*! \brief Recursivly walks through an item and collects all path and name info.
205 * The reult can then be used to fill the ItemSelector.
206 * @param Array The Item to recurse.
207 * @param Array The type of of objects to collect.
208 * @param String The parent path prefix which should be removed.
209 * @return Array An array containing Array[path] = name
210 */
211 function __recurseItem($item, $types, $parent = "")
212 {
213 $res = array();
214 if(1 || in_array($item['type'], $types)){
215 $path = preg_replace("/".preg_quote($parent,'/')."/","",$item['path']);
216 $res[$path] = array('name' => $item['name'],'desc'=>$item['type']);
217 }
218 if(count($item['children'])){
219 foreach($item['children'] as $child){
220 $res = array_merge($res, $this->__recurseItem($child, $types, $parent));
221 }
222 }
223 return($res);
224 }
227 /*! \brief Intializes this plugin
228 * All available installation methods will be loaded
229 */
230 function loadInstallationMethods()
231 {
232 // Reset erros
233 $this->rpcError = $this->initFailed = FALSE;
235 // Load configuration via rpc.
236 $rpc = $this->config->getRpcHandle();
238 // Populate install methods on success.
239 $res = $rpc->getSupportedInstallMethods();
240 if(!$rpc->success()){
241 $this->rpcError = TRUE;
242 $this->errorMessage = $rpc->get_error();;
243 return;
244 }
245 $this->installationMethods = $res;
246 if(!count($this->installationMethods)){
247 $this->errorMessage = _("No selectable install methods returned!");
248 msg_dialog::display(_("Setup"), $this->errorMessage , ERROR_DIALOG);
249 $this->initFailed = TRUE;
250 return;
251 }
252 }
255 /*! \brief Returns a info list about all items we can manage,
256 * this used to fill the listings <objectType> settings.
257 * @return Array An array with item info.
258 */
259 function getListingTypes()
260 {
261 $types= array();
262 $types['Distribution']['objectClass'] = 'Distribution';
263 $types['Distribution']['label'] = _('Distribution');
264 $types['Distribution']['image'] = 'images/lists/edit.png';
265 $types['Distribution']['category'] = 'Device';
266 $types['Distribution']['class'] = 'Device';
268 $types['Release']['objectClass'] = 'Release';
269 $types['Release']['label'] = _('Release');
270 $types['Release']['image'] = 'images/lists/delete.png';
271 $types['Release']['category'] = 'Device';
272 $types['Release']['class'] = 'Device';
274 $types['Component']['objectClass'] = 'Component';
275 $types['Component']['label'] = _('Component');
276 $types['Component']['image'] = 'plugins/users/images/select_user.png';
277 $types['Component']['category'] = 'Device';
278 $types['Component']['class'] = 'Device';
280 foreach($this->installationMethods as $method => $items){
281 foreach($items['items'] as $itemName => $item){
282 $types[$itemName]['objectClass'] = $itemName;
283 $types[$itemName]['label'] = $item['name'];
284 $types[$itemName]['image'] = 'plugins/fai/images/fai_script.png';
285 $types[$itemName]['category'] = 'Device';
286 $types[$itemName]['class'] = 'Device';
287 }
288 }
290 return($types);
291 }
294 /*! \brief Acts on edit requests and opens an edit dialog for the received item-id.
295 * (This action is received from the ConfigManagementListing class.)
296 * @param Array The items ids. (May contain multiple ids)
297 * @return
298 */
299 function editEntry($ids)
300 {
301 foreach($ids as $id){
302 echo "<br>Edit {$id}";
303 }
304 }
307 /*! \brief Acts on remove requests.
308 * (This action is received from the ConfigManagementListing class.)
309 * @param Array The items ids. (May contain multiple ids)
310 * @return
311 */
312 function removeEntry($ids)
313 {
314 foreach($ids as $id){
315 echo "<br>Remove {$id}";
316 }
317 }
320 /*! \brief Acts on open requests.
321 * (This action is received from the ConfigManagementListing class.)
322 * @param Array The items ids. (May contain multiple ids)
323 * @return
324 */
325 function openEntry($ids)
326 {
327 $id = $ids[0];
328 $item = $this->dataModel->getItemById($id);
329 $this->setCurrentContainer($item['path']);
330 return;
331 }
334 /*! \brief The plugins ACL and plugin-property definition.
335 * @return
336 */
337 public static function plInfo()
338 {
339 return (array(
340 "plShortName" => _("Config management"),
341 "plDescription" => _("Config management"),
342 "plSelfModify" => FALSE,
343 "plDepends" => array(),
344 "plPriority" => 0,
345 "plSection" => array("administration"),
346 "plCategory" => array(
347 "newConfigManagement" => array("description" => _("Config management"),
348 "objectClass" => "FAKE_OC_newConfigManagement")),
349 "plProvidedAcls"=> array()
350 ));
351 }
354 /*! \brief
355 * @param
356 * @return
357 */
358 function remove_lock()
359 {
360 }
363 }
364 ?>