[gosa.git] / gosa-plugins / goto / admin / systems / services / repository / class_servRepository.inc
1 <?php
3 class servrepository extends goService
4 {
5 /* attribute list for save action */
6 // var $ignore_account = TRUE;
7 var $attributes = array("FAIrepository");
8 var $objectclasses = array("FAIrepositoryServer");
10 /* Repositories */
11 var $repositories = array();
12 var $FAIrepository = array();
13 var $conflicts = array("FAIrepositoryServer");
14 var $DisplayName = "";
15 var $StatusFlag = "";
17 var $view_logged = FALSE;
18 var $fai_activated = FALSE;
20 var $divlist = NULL;
22 function servrepository (&$config, $dn= NULL, $parent= NULL)
23 {
24 plugin::plugin ($config, $dn, $parent);
26 $this->DisplayName = _("Repository service");
28 /* Skip this if fai is deactivated */
29 $tmp= $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
30 if(!empty($tmp)){
31 $this->fai_activated = TRUE;
32 }else{
33 return;
34 }
36 $this->repositories = array();
37 if(isset($this->attrs['FAIrepository'])){
38 for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
39 $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
40 $tmp2 = array();
41 $tmp3 = array();
43 if(isset($tmp[1])){
44 $tmp2['ParentServer'] = $tmp[1];
45 if(empty($tmp[1])){
46 $tmp2['ParentServer'] = "none";
47 }
48 }else{
49 $tmp2['ParentServer'] = "none";
50 }
52 if(isset($tmp[0])){
53 $tmp2['Url'] = $tmp[0];
54 }else{
55 $tmp2['Url'] = "";
56 }
58 if(isset($tmp[2])){
59 $tmp2['Release'] = $tmp[2];
60 }else{
61 $tmp2['Release'] = "";
62 }
64 if(isset($tmp[3])){
65 $tmp3 = split(",",$tmp[3]);
66 foreach($tmp3 as $sec){
67 $tmp2['Sections'][$sec]=$sec;
68 }
69 }else{
70 $tmp['Section']=array();
71 }
73 $this->repositories[$tmp[2]]=$tmp2;
74 }
75 }
78 /* Create divlist */
79 $this->divlist = new divListRepository($this->config,$this);
80 }
82 function execute()
83 {
84 /* Call parent execute */
85 plugin::execute();
87 if($this->is_account && !$this->view_logged){
88 $this->view_logged = TRUE;
89 new log("view","server/".get_class($this),$this->dn);
90 }
92 if(!$this->fai_activated){
93 $str = "<h2>"._("You can't use this plugin until FAI is activated.")."</h2>";
94 return $str;
95 }
97 /* Fill templating stuff */
98 $smarty= get_smarty();
99 $smarty->assign("is_createable",$this->acl_is_createable());
100 $display= "";
102 /* Show tab dialog headers */
103 /*
104 ADD / EDIT Repository
105 Dialog Handling
106 */
107 $once = false;
108 if(isset($_POST['servRepository'])){
109 foreach($_POST as $name => $value){
111 if(preg_match("/AddRepository/",$name) && $this->acl_is_createable()){
112 $once = true;
113 $this->dialog = new servRepositorySetup($this->config,$this->dn);
114 $this->dialog->parent = $this;
115 }
117 if((preg_match("/^delete_/",$name)) && (!$once) && $this->acl_is_removeable()){
118 $once = true;
119 $value = preg_replace("/delete_/","",$name);
120 $value = base64_decode(preg_replace("/_.*$/","",$value));
122 $url = $this->repositories[$value]['Url'];
123 $release = $this->repositories[$value]['Release'];
125 $ldap = $this->config->get_ldap_link();
126 $ldap->cd ($this->config->current['BASE']);
128 $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
129 if ($ldap->count() != 0){
130 $obj= array();
131 $found= false;
132 while($attrs = $ldap->fetch()){
133 foreach($attrs['FAIclass'] as $class){
134 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
135 $obj[$ldap->getDN()]= $attrs['cn'][0];
136 $found= true;
137 }
138 }
139 }
141 if ($found){
142 msg_dialog::display(_("Error"), msgPool::stillInUse(_("FAI release"), msgPool::buildList($obj)), ERROR_DIALOG);
143 }
144 }else{
145 if(isset($this->repositories[$value])){
146 unset($this->repositories[$value]);
147 }
148 }
149 }
151 if((preg_match("/^edit_/",$name))&&(!$once)){
152 $value = preg_replace("/edit_/","",$name);
153 $value = base64_decode(preg_replace("/_.$/","",$value));
155 if(isset($this->repositories[$value])){
157 $ldap = $this->config->get_ldap_link();
158 $ldap->cd ($this->config->current['BASE']);
160 $url = $this->repositories[$value]['Url'];
161 $release = $this->repositories[$value]['Release'];
163 $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
164 if ($ldap->count() != 0){
165 $found = false;
166 $obj= array();
167 while($attrs = $ldap->fetch()){
168 foreach($attrs['FAIclass'] as $class){
169 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
170 $found = true;
171 $obj[$ldap->getDN()]= $attrs['cn'][0];
172 }
173 }
174 }
176 if($found){
177 msg_dialog::display(_("Warning"), msgPool::stillInUse(_("FAI release"), msgPool::buildList($obj)), WARNING_DIALOG);
178 }
179 }
181 if(isset($this->repositories[$value])){
182 $once = true;
183 $obj = $this->repositories[$value];
185 /* to be able to detect if this was renamed */
186 $obj['initialy_was'] = $obj['Release'];
187 $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
188 $this->dialog->parent = $this;
189 }
190 }
191 }
192 }
193 }
194 if((isset($_GET['act']))&&($_GET['act']=="open_repository")&&(isset($_GET['id']))){
195 $obj = $this->repositories[base64_decode($_GET['id'])];
196 $obj['initialy_was'] = $obj['Release'];
197 $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
198 $this->dialog->parent = $this;
199 }
201 if(isset($_POST['repository_setup_save']) && is_object($this->dialog)){
202 $this->dialog->save_object();
203 if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
204 msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
205 }else
207 if(count($this->dialog->check())!=0){
208 foreach($this->dialog->check() as $msg){
209 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
210 }
211 }else{
212 $obj = $this->dialog->save();
213 if($this->dialog->is_new_name()){
214 $oldname = $this->dialog->initialy_was;
215 $this->repositories[$obj['Release']]=$obj;
216 unset($this->repositories[$oldname]);
217 }else{
218 $this->repositories[$obj['Release']]=$obj;
219 }
220 $this->dialog = FALSE;
221 $this->is_dialog= false;
222 }
223 }
225 if(isset($_POST['repository_setup_cancel'])){
226 $this->dialog=FALSE;
227 $this->is_dialog = false;
228 }
230 if(is_object($this->dialog)){
231 $this->dialog->save_object();
232 $this->is_dialog = true;
233 return($this->dialog->execute());
234 }
236 /*
237 Repository setup dialog handling /END
238 */
241 $link = "<a href='?plug=".$_GET['plug']."&act=open_repository&id=%s'>%s</a>";
242 $edit = "<input type='image' value='%s' name='edit_%s' src='images/lists/edit.png'> ";
244 /* Hide delete icon, if delete is not allowed */
245 if($this->acl_is_removeable()){
246 $delete = "<input type='image' value='%s' name='delete_%s' src='images/lists/trash.png'>";
247 }else{
248 $delete = "<img src='images/empty.png' alt=' '>";
249 }
251 $this->divlist->execute();
252 $this->divlist->setEntries($this->repositories);
253 $smarty->assign("Repositories",$this->divlist->Draw());
254 $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE,dirname(__FILE__)));
255 return($display);
256 }
259 /* Save data to object */
260 function save_object()
261 {
262 plugin::save_object();
263 if(is_object($this->divlist)){
264 $this->divlist->save_object();
265 }
266 }
269 /* Check supplied data */
270 function check()
271 {
272 /* Call common method to give check the hook */
273 $message= plugin::check();
274 return ($message);
275 }
278 /* Save to LDAP */
279 function save()
280 {
281 if(!$this->fai_activated) return;
283 plugin::save();
285 $arr = array();
286 foreach($this->repositories as $servername => $conf){
287 $str = "";
288 foreach($conf['Sections'] as $sec){
289 $str.=$sec.",";
290 }
291 $str=preg_replace("/,$/","",$str);
293 if($conf['ParentServer']=="none"){
294 $conf['ParentServer'] ="";
295 }
297 $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
298 }
299 $this->attrs['FAIrepository'] = $arr;
301 $ldap= $this->config->get_ldap_link();
302 $ldap->cd ($this->config->current['BASE']);
304 $ldap->cat($this->dn, array('dn'));
306 if($ldap->count()){
307 $ldap->cd($this->dn);
308 $this->cleanup();
309 $ldap->modify ($this->attrs);
310 $this->handle_post_events("modify");
311 if(count($this->attrs)){
312 $this->trigger_si_fai_server_reload();
313 }
314 }else{
315 $ldap->cd ($this->config->current['BASE']);
316 $ldap->create_missing_trees($this->dn);
317 $ldap->cd($this->dn);
318 $ldap->add($this->attrs);
319 $this->handle_post_events("add");
320 if(count($this->attrs)){
321 $this->trigger_si_fai_server_reload();
322 }
323 }
325 if($this->initially_was_account){
326 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
327 }else{
328 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
329 }
330 }
333 function getListEntry()
334 {
335 $fields = goService::getListEntry();
336 $fields['Message'] = _("Repository service");
337 $fields['AllowEdit'] = true;
338 $fields['AllowStart'] = $fields['AllowStop'] = $fields['AllowRestart'] = false;
339 return($fields);
340 }
343 function trigger_si_fai_server_reload()
344 {
345 /* Reload GOsa si FAI DB/cache
346 */
347 if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
348 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
349 if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_server_db'])){
350 $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_server_db'];
351 $tmp = new $evt['CLASS_NAME']($this->config);
352 $tmp->set_type(TRIGGERED_EVENT);
353 $tmp->add_targets(array("GOsa"));
354 $o_queue = new gosaSupportDaemon();
355 if(!$o_queue->append($tmp)){
356 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
357 }
358 }
359 }
360 }
363 function remove_from_parent()
364 {
365 goService::remove_from_parent();
366 $this->trigger_si_fai_server_reload();
367 }
369 /* Return plugin informations for acl handling */
370 static function plInfo()
371 {
372 return (array(
373 "plShortName" => _("Repository"),
374 "plDescription" => _("Repository service")." ("._("Services").")",
375 "plSelfModify" => FALSE,
376 "plDepends" => array(),
377 "plPriority" => 84,
378 "plSection" => array("administration"),
379 "plCategory" => array("server"),
381 "plProvidedAcls"=> array(
382 "cn" => _("Name"),
383 "start" => _("Start"),
384 "stop" => _("Stop"),
385 "restart" => _("Restart"),
386 "Release" => _("Releases"),
387 "Section" => _("Sections"),
388 "ParentServer" => _("Parent server"),
389 "Url" => _("Url"))
390 ));
391 }
392 }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>