Code

79758264a1f7b03292804bb979104a89125900e2
[gosa.git] / gosa-plugins / goto / admin / systems / goto / Device / class_DevicePartition.inc
1 <?php
3 class DevicePartition 
4 {
5     public $object =NULL;
6     public $partitionString =NULL;
8     public $addDialog = NULL;
9     public $initialized = FALSE;
11     function __construct($config, $partitionString)
12     {
13         $this->config = &$config;
14         $this->partitionString = $partitionString;
16         // Prepare lists
17         $this->entryList = new sortableListing();
18         $this->entryList->setDeleteable(false);
19         $this->entryList->setEditable(false);
20         $this->entryList->setWidth("100%");
21         $this->entryList->setHeight("400px");
22         $this->entryList->setHeader(array(_("Type"),_("Target"),_("Size"),_("Uses device")." / "._("Used by"),
23                     _("Filesystem"), _("Options"), _("Details"), "-"));
24         $this->entryList->setColspecs(array('*','*','*', '*'));
25         $this->entryList->setAcl('rwcdm');
26         $this->entryList->setReorderable(FALSE);
27         $this->entryList->sortingEnabled(FALSE);
28     }
30     function execute()
31     {
32         /*****
33          * Handle add-partition dialog
34          *****/
36         if(isset($_POST['create_partition'])){
37             $this->addDialog = new AddPartitionDialog($this->config, $this->object); 
38         }
39         if(isset($_POST['cancel_partition_add'])){
40             $this->addDialog = NULL;
41         }
42         if(isset($_POST['save_partition_add'])){
43             $this->addDialog->save_object();
44             if($this->addDialog->save()){
45                 $this->addDialog = NULL;
46             }
47         }
48         if($this->addDialog){
49             $this->addDialog->save_object();
50             return($this->addDialog->execute());
51         }
54         /*****
55          * Open partition object on demand
56          *****/
58         if(!$this->initialized){
59             $rpc = $this->config->getRpcHandle();
60             $this->object = $rpc->openObject('libinst.preseed.diskdefinition', $this->partitionString);
61             if(!$rpc->success()){
62                 $message = _("An error occured while loading partition information.");
63                 $message = sprintf(_("An error occured while loading partition information, error was: '%s'."), $rpc->get_error());
64                 $smarty = get_smarty();
65                 $smarty->assign('error', TRUE);
66                 $smarty->assign('errorMsg', $message);
67                 return($smarty->fetch(get_template_path('goto/Device/DevicePartition.tpl', TRUE)));
68             }else{
69                 $this->initialized = TRUE;
70             }
71         }
74         /*****
75          * Fill listing
76          *****/
78         // Receive list informations
79         $map = array();
80         $map['disks'] = 'getDisks';
81         $map['partitions'] = 'getPartitions';
82         $map['raids'] = 'getRaidDevices';
83         $map['volumeGroups'] = 'getVolumeGroups';
84         $map['volumes'] = 'getVolumes';
85         $map['deviceUsage'] = 'getDeviceUsage';
86         foreach($map as $target => $func){
87             $$target = @$this->object->$func();
88             if(!$this->object->success()){
89                 $message = sprintf(_("An error occured while loading partition information, error was: '%s'."), $this->object->getError());
90                 $smarty = get_smarty();
91                 $smarty->assign('error', TRUE);
92                 $smarty->assign('errorMsg', $message);
93                 return($smarty->fetch(get_template_path('goto/Device/DevicePartition.tpl', TRUE)));
94             }
95         }
97         // Create a mapping that show which partitions was used for which raid.
98         $map_partToRaid = array();
99         $raidTargets = array();
100         foreach($raids as $raid){
101             $raidTargets[] = $raid['target'];
102             foreach($raid['devices'] as $devName){
103                 $map_partToRaid[$devName] = $raid['name'];
104             }
105         }
106         
107         // Create a mapping that shows which partition was used in which volumeGroup.
108         $map_partToVolumeGroup = array();
109         foreach($volumeGroups as $vol){
110             foreach($vol['partitions'] as $part){
111                 $map_partToVolumeGroup[$part] = $vol['name'];
112             }
113         }
115         // Create partition name map        
116         $partNames = array();
118         // Create separators to create a visible indentation 
119         $seps = array();
120         $seps[0] = "";
121         $seps[1] = str_pad("", 18, "&nbsp;");
122         $seps[2] = str_pad("", 36, "&nbsp;");
124         // Prepare images
125         $partitionImg = image('plugins/goto/images/partition.png','',_("Partition"));
126         $diskImg = image('plugins/goto/images/disk.png','',_("Disk"));
127         $raidImg = image('plugins/goto/images/raid.png', '', _('Raid'));
128         $volumeGroupImg = image('plugins/goto/images/volumeGroups.png', '', _('Volume group'));
129         $volumeImg = image('plugins/goto/images/volume.png','',_('Volume'));
131         // Add raid devices
132         $lData = array();
133         if(count($raids)){
134         
135             // Add raid header
136             $lData[] = array("data" => array("<b>"._("Raid devices")."</b>"));
137     
138             // Add raids
139             foreach($raids as $id =>  $raid){
140                 $str = "{$seps[1]}{$raidImg}&nbsp;{$raid['name']} ({$raid['level']})";
141                 $delImg = image('images/lists/trash.png', "delete_raid_{$id}", _("Remove raid device"));
143                 $devsStr = "";
144                 foreach($raid['devices'] as $devName){
145                     $devsStr .= $partitionImg."&nbsp;".$devName.", ";
146                 }
147                 $devsStr = rtrim($devsStr, ', ');
149                 $target = $raid['target'];
150                 if(isset($map_partToVolumeGroup[$target])){
151                     $target = $volumeGroupImg."&nbsp;".$target;
152                 }
154                 $fsType = (isset($raid['fsType'])) ? $raid['fsType'] :"";
155                 $fsOptions = (isset($raid['fsOptions'])) ? $raid['fsOptions'] :"";
157                 $size = "";
158                 if(isset($deviceUsage['raid'][$raid['target']])){
159                     $size = "<i>".$deviceUsage['raid'][$raid['target']]['size']."</i>";
160                 }
162                 $lData[] = array("data" => array($str, 
163                     $target,
164                     $size,
165                     $devsStr,
166                     $fsType,
167                     $fsOptions,
168                     '',
169                     $delImg
170                     ));
171             }
172         }
174         // Add volume groups
175         if(count($volumeGroups)){
176         
177             // Add LVM volume groups header
178             $lData[] = array("data" => array("<b>"._("LVM Volume Groups")."</b>"));
179     
180             // Add volume groups
181             foreach($volumeGroups as $id =>  $vg){
182                 $str = "{$seps[1]}{$volumeGroupImg}&nbsp;{$vg['name']}";
183                 $delImg = image('images/lists/trash.png', "delete_vg_{$id}", _("Remove volume group"));
185                 // Build up a list of all used partitions
186                 $partStr = "";
187                 foreach($vg['partitions'] as $partName){
188                     if(in_array($partName, $raidTargets)){
189                         $img = $raidImg;
190                     }else{
191                         $img = $partitionImg;
192                     }
193                     $partStr .= "{$img}&nbsp;{$partName}, ";
194                 }
195                 $partStr = rtrim($partStr, ", ");
197                 $size = "";
198                 if(isset($deviceUsage['vg'][$vg['name']])){
199                     $size = "<b>".$deviceUsage['vg'][$vg['name']]['size']."</b>";
200                 }
202                 // Add entry to the list.
203                 $lData[] = array("data" => array($str, '', $size, $partStr, '', '','',$delImg));
205                 // Add volumes 
206                 foreach($volumes as $vid => $volume){
207                     $delImg = image('images/lists/trash.png', "delete_v_{$vid}", _("Remove volume"));
208                     if($volume['volGroup'] == $vg['name']){
209                         $str = "{$seps[2]}{$volumeImg}&nbsp;{$volume['name']}";
210                         $lData[] = array("data" => array(
211                                     $str, 
212                                     $volume['target'],
213                                     $this->__convertPartSize($volume['size']),
214                                     '',
215                                     $volume['fsType'],
216                                     $volume['fsOptions'],
217                                     '',
218                                     $delImg
219                                     ));
221                         
223                     }
224                 }
225             }
226         }
228         // Add physical disks to the listing.
229         if(count($disks)){
230         
231             // Add disks
232             $lData[] = array("data" => array("<b>"._("Disks")."</b>"));
233             foreach($disks as $id =>  $disk){
234                 $str = "{$seps[1]}{$diskImg}&nbsp;{$disk['device']}";
236                 $size = "";
237                 if(isset($deviceUsage['disk'][$disk['device']])){
238                     $size = "<b>".$deviceUsage['disk'][$disk['device']]['size']."</b>";
239                 }
241                 // Create remove icon
242                 $delImg = image('images/lists/trash.png', "delete_disk_{$id}", _("Remove disk"));
243                 $lData[] = array("data" => array($str,"",$size,"","","","", $delImg));
245                 // Add partitions
246                 foreach($partitions as $pid => $part){
248                     if($part['onDisk'] == $disk['device']){
249                     
250                         // Prepare columns
251                         $str = "{$seps[2]}{$partitionImg}&nbsp;{$part['target']}";
252                         $device = "";
253                         $fsType = $part['fsType'];
254                         $target = "";
255                         if(preg_match("/^(swap|\/)/", $part['target'])){
256                             $target = $part['target'];
257                         }
259                         // Do we have a raid here? Then update the device column 
260                         //  to point to the raid name
261                         if(isset($map_partToRaid[$part['target']])){
262                             $device = sprintf(_("Used by: %s"), $raidImg."&nbsp;".$map_partToRaid[$part['target']]);
263                             $fsType.= " "._("Software raid");
264                             $fsType = trim($fsType); 
265                         }elseif(isset($map_partToVolumeGroup[$part['target']])){
266                             $device = sprintf(_("Used by: %s"), $volumeGroupImg."&nbsp;".$map_partToVolumeGroup[$part['target']]);
267                             $fsType.= " "._("Physical volume LVM");
268                             $fsType = trim($fsType); 
269                         }
271                         // Create property icons
272                         $emptyImage = image("images/empty.png");
273                         $formatImg = ($part['format']) ? image('plugins/goto/images/formatDisk.png','',_("Format partition")) : $emptyImage;
274                         $primaryImg = ($part['primary']) ? image('plugins/goto/images/primary.png','',_("Primary partition")) : $emptyImage;
275                         $encryptImg = ($part['encrypted']) ? image('images/lists/locked.png','',_("Encrypted")) : $emptyImage;
277                         // Create remove icon
278                         $delImg = image('images/lists/trash.png', "delete_part_{$pid}", _("Remove partition"));
280                         // Add entry to the listing
281                         $lData[] = array("data" => array(
282                                     $str, 
283                                     $target, 
284                                     $this->__convertPartSize($part['size']),
285                                     $device,
286                                     $fsType,
287                                     $part['fsOptions'],
288                                     $primaryImg.$formatImg.$encryptImg, 
289                                     $delImg));
290                     }
291                 }
292             }
293         }
295         // Updated columns length for all entries to avoid render errors.
296         $length = 8;
297         foreach($lData as $id => $entry){
298             while(count($lData[$id]['data']) < $length){
299                 $lData[$id]['data'][] = '';
300             }
301         }
302         $this->entryList->setListData($lData, $lData);
303         $this->entryList->update();
305         $smarty = get_smarty();
306         $smarty->assign('error', FALSE);
307         $smarty->assign('list', $this->entryList->render());
308         return($smarty->fetch(get_template_path('goto/Device/DevicePartition.tpl', TRUE)));
309     }
311     function save_object()
312     {
313         // Create a map whcih points to the correct remove method for each device.
314         $map = array();
315         $map['part'] = array('func' => 'delPartition', 'name' => _("Partition"));
316         $map['raid'] = array('func' => 'delRaidDevice', 'name' => _("Raid device"));
317         $map['disk'] = array('func' => 'delDisk', 'name' => _("Disk"));
318         $map['vg'] = array('func' => 'delVolumeGroup', 'name' => _("Volume group"));
319         $map['v'] = array('func' => 'delVolume', 'name' => _("Volume"));
321         // Walk through posts and search for remove requests.
322         foreach($_POST as $name => $value){
323             foreach($map as $type => $data){
325                 // Remove paritions
326                 if(preg_match("/^delete_{$type}_[0-9]*$/", $name)){
327                     $id = preg_replace("/^delete_{$type}_/i", "", $name) + 0;
328                     $func = $data['func'];
329                     @$this->object->$func($id);
330                     if(!$this->object->success()){
331                         $msg = sprintf(_("Failed to remove '%s': %s"), $data['name'], $this->object->getError());
332                         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
333                     }
334                 }
335             }
336         }
337     }
339     function save()
340     {
341         return($this->object->dump());
342     }
344     function __convertPartSize($size)
345     {
346         return($size." MB");
347     }
350 ?>