Code

Fixed PHP warning messagesFixed PHP warning messages
[gosa.git] / gosa-core / plugins / addons / propertyEditor / class_propertyEditor.inc
1 <?php
4 class propertyEditor extends management
5 {
6     var $plHeadline= "Preferences";
7     var $plDescription= "Configure global and special GOsa settings like hooks and plugin parameters";
8     var $plIcon  = "plugins/propertyEditor/images/plugin.png";
10     var $toBeMigrated = array();
12     var $warningAccepted = FALSE;
14     function __construct($config,$ui)
15     {
16         $this->config = $config;
17         $this->ui = $ui;
19         // Build filter
20         if (session::global_is_set(get_class($this)."_filter")){
21             $filter= session::global_get(get_class($this)."_filter");
22         } else {
23             $filter = new filter(get_template_path("property-filter.xml", true));
24             $filter->setObjectStorage($this->storagePoints);
25         }
26         $this->setFilter($filter);
28         // Build headpage
29         $headpage = new listing(get_template_path("property-list.xml", true));
30         $headpage->registerElementFilter("propertyName", "propertyEditor::propertyName");
31         $headpage->registerElementFilter("propertyGroup", "propertyEditor::propertyGroup");
32         $headpage->registerElementFilter("propertyClass", "propertyEditor::propertyClass");
33         $headpage->registerElementFilter("propertyValue", "propertyEditor::propertyValue");
34         $headpage->setFilter($filter);
35         parent::__construct($config, $ui, "property", $headpage);
37         $this->registerAction("saveProperties","saveProperties");
38         $this->registerAction("cancelProperties","cancelProperties");
39     }
42     function execute()
43     {
44         // Walk trough all properties and check if there posts for us.
45         $all = $this->config->configRegistry->getAllProperties();
46         foreach($all as $prop){
47             $post = "{$prop->getClass()}:{$prop->getName()}";
48             if(isset($_POST[$post]) && $prop->getStatus() != 'removed'){
49                 $prop->setValue(get_post($post));
50             }
52             // Open the command verify dialog
53             if(isset($_POST["testCommand_{$post}"])){
54                 $this->dialogObject = new commandVerifier($this->config,$prop);
55             }
56         }
57         if(isset($_POST['commandVerifier_save'])){
58             $this->dialogObject->save();
59             $this->closeDialogs();
60         }
61         if(isset($_POST['commandVerifier_cancel'])){
62             $this->closeDialogs();
63         }
65         // Once accepted hide the warning message        
66         if(isset($_POST['warningAccepted'])){
67             $this->warningAccepted = TRUE;
68         }
70         // Execute registered management event listeners.
71         $this->handleActions($this->detectPostActions());
73         // Handle properties that have to be migrated 
74         if(isset($_POST['propertyMigrate_cancel']) && count($this->toBeMigrated)){
75             unset($this->toBeMigrated[0]);
76             $this->toBeMigrated = array_values($this->toBeMigrated);
77         }
78         if(isset($_POST['propertyMigrate_save']) && count($this->toBeMigrated)){
79             $first = $this->toBeMigrated[0]->getMigrationClass();
80             $first->save_object();
81             $msgs = $first->check();
82             if(!count($msgs)){
83                 $this->toBeMigrated[0]->save();
84                 unset($this->toBeMigrated[0]);
85                 $this->toBeMigrated = array_values($this->toBeMigrated);
87                 // Nothing to migrate and everything is fine, reload the list now.
88                 if(!count($this->toBeMigrated)){
89                     $this->config->configRegistry->reload($force=TRUE);
90                 }
91             }
92         }
93         if(count($this->toBeMigrated)){
94             $first = $this->toBeMigrated[0]->getMigrationClass();
95             $first->save_object();
96     
97             // We've no problems with this property anymore.
98             while($first instanceOf propertyMigration && !$first->checkForIssues()){
99                 $this->toBeMigrated[0]->save();
100                 unset($this->toBeMigrated[0]);
101                 $this->toBeMigrated = array_values($this->toBeMigrated);
102                 if(count($this->toBeMigrated)){
103                     $first = $this->toBeMigrated[0]->getMigrationClass();
104                 }else{
105                     $first = NULL;
106                     
107                     // Nothing to migrate and everything is fine, reload the list now.
108                     if(!count($this->toBeMigrated)){
109                         $this->config->configRegistry->reload($force=TRUE);
110                     }
111                 }
112             }
114             if($first){
115                 $content =  $first->execute();
116                 $smarty = get_smarty();
117                 $smarty->assign('content', $content);
118                 $smarty->assign('leftSteps', count($this->toBeMigrated));
119                 return($smarty->fetch(get_template_path('migrate.tpl',TRUE)));
120             }
121         }
123         $smarty = get_smarty();
124         $smarty->assign("warningAccepted", $this->warningAccepted);
125         $smarty->assign("ignoreLdapProperties", $this->config->configRegistry->ignoreLdapProperties);
126         return(management::execute());
127     }
129     function renderList()
130     {
131         // Walk trough all properties and check if we have modified something
132         $all = $this->config->configRegistry->getAllProperties();
133         foreach($all as $prop){
134             $modified = in_array($prop->getStatus(),array('modified','removed'));
135             if($modified) break;
136         }
137        
138         $smarty = get_smarty();
139         $smarty->assign('is_modified', $modified);
140         return(management::renderList());
141     }
144     function cancelProperties()
145     {
146         $this->config->configRegistry->reload($force=TRUE);
147     }
149     function saveProperties()
150     {
151         // Check if we've misconfigured properties and skip saving in this case.
152         $all = $this->config->configRegistry->getAllProperties();
153         $valid = TRUE;
154         foreach($all as $prop){
155             $valid &= $prop->check();
156         }
158         // Now save the properties.
159         if($valid){
160             $this->toBeMigrated = $this->config->configRegistry->saveChanges();
162             // Nothing to migrate and everything is fine, reload the list now.
163             if(!count($this->toBeMigrated)){
164                 $this->config->configRegistry->reload($force=TRUE);
165             }
166         }
167     }
169     function detectPostActions()
170     {
171         $action = management::detectPostActions();
172         if(isset($_POST['saveProperties']))  $action['action'] = 'saveProperties';
173         if(isset($_POST['cancelProperties']))  $action['action'] = 'cancelProperties';
174         return($action);
175     }
177     protected function removeEntryRequested($action="",$target=array(),$all=array())
178     {
179         foreach($target as $dn){
180             list($class,$name) = preg_split("/:/", $dn);
181             if($this->config->configRegistry->propertyExists($class,$name)){
182                 $prop = $this->config->configRegistry->getProperty($class,$name);
183                 $prop->restoreDefault();
184             }
185         }
186     } 
188     static function propertyGroup($group, $description = array())
189     {
190         return($group[0]);
191     }
192     static function propertyClass($class, $description = array())
193     {
194         global $config;
195         if(isset($config->configRegistry->classToName[$class[0]])){
196             $class = $config->configRegistry->classToName[$class[0]];
197         }else{
198             $class = $class[0];
199         }
200         return($class);
201     }
202     static function propertyName($class,$cn, $description,$mandatory)
203     {
204         $id = "{$class[0]}_{$cn[0]}";
206         $title = _("No description");
207         if(isset($description[0])) $title = htmlentities($description[0],ENT_COMPAT, 'UTF-8');
208         $title = preg_replace("/\n/", "<br>", $title);
209         $tooltip = "<div id='tooltip_{$id}' class='tooltip' style='display:none'>".$title."</div>";
211         $must = ($mandatory[0]) ? "<span class='required'>*</span>" : "";
213         return($tooltip."<span title='tooltip_{$id}'>{$cn[0]}{$must}</span>");
214     }
215     static function propertyValue($class,$cn,$value,$type,$default,$defaults,$check,$mandatory)
216     {
217         $ssize  = "208px";
218         $isize  = "200px";
219         $name  = "{$class[0]}:{$cn[0]}";
220         $value = htmlentities($value[0],ENT_QUOTES      ,'UTF-8');
222         // Add slashes to keep escaped values escaped after passing them to smarty.
223         $value = addslashes($value);
225         switch($type[0]){
226             case 'bool':
227                 $res = "<select size=1 name=\"{$name}\" style='width:{$ssize}'>";
228                 $false = (preg_match('/true/i', $value)) ? '' : "selected";
229                 $true  = (preg_match('/true/i', $value)) ? "selected" : '';
230                 $res.= "<option {$false} value='false'>"._("FALSE")."</option>";
231                 $res.= "<option {$true} value='true'>"._("TRUE")."</option>";
232                 $res.= "</select>";
233             case 'switch':
234                 if(!empty($defaults[0])){
235                     $data = call_user_func(preg_split("/::/", $defaults[0]), $class[0],$cn[0],$value, $type[0]);
236                     if(is_array($data)){
237                         $res = "<select size=1 name=\"{$name}\" style='width:{$ssize}'>";
238                         foreach($data as $oValue => $oDesc){
239                             if($oValue == $value){
240                                 $res.="<option selected value=\"{$oValue}\">{$oDesc}</option>\n";
241                             }else{
242                                 $res.="<option value=\"{$oValue}\">{$oDesc}</option>\n";
243                             }
244                         }
245                         $res.= "</select>";
246                     }
247                 }
248                 break;
249             case 'command':
250                 $res = "<input style='width:{$isize}' type='text' value=\"{$value}\" name=\"{$name}\">";
251                 $res.= image('images/lists/edit.png', "testCommand_{$name}", _("Test the given command."));
252                 break;
253             case 'dn':
254             case 'rdn':
255             case 'uri':
256             case 'path':
257             case 'file':
258             case 'string':
259             case 'integer':
260                 $res = "<input style='width:{$isize}' type='text' value=\"{$value}\" name=\"{$name}\">";
261                 break;
262             default: echo $type[0].$name."  ";$res = ""; 
263         }
264        
265         // Check if it is a required value. 
266         if($mandatory[0] && empty($value)){
267            $res.= "<rowClass:entry-error/>";
268         }
270         // Color row in red if the check methods returns false.
271         if(!empty($check[0]) && !empty($value)){
272             $check = call_user_func(preg_split("/::/", $check[0]),$displayMessage=FALSE, $class[0], $cn[0], $value, $type[0]);
273             if(!$check){
274                 $res.= "<rowClass:entry-error/>";
275             }
276         }
278         return($res);
279     }
281 ?>