Code

More speed optimizations
[gosa.git] / plugins / admin / systems / class_servRepositorySetup.inc
1 <?php
3 class servRepositorySetup  extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary        = "Manage FAI repositories";
7   var $cli_description    = "Some longer text\nfor help";
8   var $cli_parameters     = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* attribute list for save action */
11   var $ignore_account   = TRUE;
12   var $attributes       = array("Release","ParentServer","Url","cn");
13   var $objectclasses    = array("whatever");
15   /* Attributes */
16   var $Release          = "";
17   var $ParentServer     = "";
18   var $Url              = "";   
19   var $Sections         = array();
20   var $ParentServers    = "";
21   var $initialy_was     = false;
22   var $cn               = "";
24   function servRepositorySetup ($config, $dn= NULL,$data = false)
25   {
26     plugin::plugin ($config, $dn);
27     if($data != false){
28       foreach(array("Sections","Release","Url","ParentServer","initialy_was") as $atr){
29         if(isset($data[$atr])){
30           $this->$atr = $data[$atr];
31         }
32       }
33     }
34   }
36   function GetName()
37   {
38     return($this->Release);
39   }
40   
41   function is_new_name()
42   {
43     if(!$this->initialy_was){
44       return(true);
45     }else{
46       if($this->Release != $this->initialy_was){
47         return(true);
48       }
49     }
50     return(false);
51   }
53   
55   function execute()
56   {
57     /* Call parent execute */
58     plugin::execute();
60     /* Fill templating stuff */
61     $smarty= get_smarty();
63     if((isset($_POST['AddSection']))&&(isset($_POST['SectionName']))&&(!empty($_POST['SectionName']))){
64      
65       /* Replace multiple spaces with a single, and cut of white spaces (trim)*/ 
66       $val = preg_replace("/\ \ * /" , " ", trim($_POST['SectionName']));
67     
68       /* check if there are more than one entry given ( "section1 section2 )*/
69       if(preg_match("/ /",$val)){
71         /* Generate list of new section names */
72         $vals = split(" ",$val);
73  
74         /* Add new entries */ 
75         foreach($vals as $entry){
76           $entry = trim($entry);
77           $this->Sections[$entry]=$entry;
78         }
79       }else{
80         $this->Sections[$val]=$val;
81       }
82     }
83     
84     foreach($_POST as $name => $value){
85       if(preg_match("/^delete_/",$name)){
87         $val = preg_replace("/^delete_/","",$name);
88         $val = base64_decode(preg_replace("/_.*$/","",$val));
90         if(isset($this->Sections[$val])){
91           unset($this->Sections[$val]);
92         }
93       }
94     }
96     $divlist = new divSelectBox("servRepositorySetup");
97     $divlist->setHeight("220");
99     $dellink = "<input type='image' src='images/edittrash.png' title='delete' alt='delete' name='delete_%s' value='%s'>";
101     foreach($this->Sections as $sec){
102       $divlist->AddEntry(array(
103                               array("string"=>$sec),
104                               array("string"=>sprintf($dellink,base64_encode($sec),$sec),"attach"=>"style='border-right:0px;width:20px;'")
105                               ));
106     }
107   
108     $smarty->assign("Sections",$divlist->DrawList());
110     foreach($this->attributes as $attr){
111       $smarty->assign($attr       ,$this->$attr);
112       $smarty->assign($attr."ACL" ,chkacl($this->acl,$attr));
113     }
114   
115     $tmp = $this->getParentServers();
116     $smarty->assign("ParentServers"   ,$tmp);
117     $smarty->assign("ParentServerKeys",array_flip($tmp));
119     return($smarty->fetch(get_template_path('servRepositorySetup.tpl', TRUE)));
120   }
122   /* Save data to object */
123   function save_object()
124   {
125     plugin::save_object();
126   }
129   /* Check supplied data */
130   function check()
131   {
132     /* Call common method to give check the hook */
133     $message= plugin::check();
135     if(empty($this->Release)){
136       $message[]=_("Please enter a value for 'release'.");
137     }
139     if(empty($this->Url)){
140       $message[] = _("Please specify a valid value for 'url'.");
141     }
143     return ($message);
144   }
147   /* Save to LDAP */
148   function save()
149   {
150     $tmp = array();
151     $tmp['ParentServer']  = $this->ParentServer;
152     $tmp['Url']           = $this->Url;
153     $tmp['Release']       = $this->Release;
154     $tmp['Sections']      = $this->Sections;
155     return($tmp);
156   }
158   function getParentServers()
159   {
160     $ret = array();
161     $ldap = $this->config->get_ldap_link();
162     $ldap->cd($this->config->current['BASE']);
163     $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
164     while($attr = $ldap->fetch()){
165       if($attr['cn'][0] == $this->cn) continue;
166       $ret[$attr['cn'][0]]= $attr['cn'][0];   
167     }
169     $ret = array_merge($ret,$this->GetHookElements());
170     
171     $ret['none']= "&nbsp;";
172     asort($ret);
173     return($ret);
174   }
176   /* this funtions calls a defined hook 
177       and parses all additional serverdata 
178    */
179   function GetHookElements()
180   {
181     $ret = array();
182     $cmd= search_config($this->config->data['TABS'], "servrepository", "REPOSITORY_HOOK");
183     if(!empty($cmd)){
184       $res = shell_exec($cmd);
185       $res2 = trim($res);
186       if(!$res){
187         print_red(sprintf(_("Can't execute specified REPOSITORY_HOOK '%s'. Please check your gosa.conf."),$cmd));
188       }elseif(empty($res2)){
189         print_red(sprintf(_("The specified REPOSITORY_HOOK '%s', specified in your gosa.conf, returns an empty string."),$cmd));
190       }else{  
191         $tmp = split("\n",$res);
192         foreach($tmp as $hook){
193           /* skip empty */
194           if(empty($hook)) continue;
196           if(preg_match("/;/",$hook)){ 
197             $hookinfo = split(";",$hook);
198             $ret[$hookinfo[0]] = $hookinfo[0];
199           }else{
200             $ret[$hook] = $hook;
201           }
202         }
203       }
204     }
205     return($ret);
206   }
210 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
211 ?>