Bug fix: Generating admin setting forms
[estigi.git] / core / system / system.admin
blob77503916dd0a33374ba3dde6200f4cb0cc8bfc60
1 <?php
3 function system_main_links(){
5         global $admin_links;
7         if(user_access('admin site')){
8                 $admin_links['System']['Settings'][] = array('href' => 'system/admin/settings', 'text' => 'System settings', 'help' => 'Main system settings');
9                 $admin_links['System']['Settings'][] = array('href' => 'system/admin/php', 'text' => 'PHP Info');
10                 $admin_links['System']['Modules'][] = array('href' => 'system/admin/rebuild/modules', 'text' => 'Rebuild Module Settings');
11                 $admin_links['System']['Modules'][] = array('href' => 'system/admin/modules', 'text' => 'Handle Modules');
12         }
15 function system_links_main(&$links){
17         if($links == ''){
18                 return NULL;
19         }
21         foreach($links as $cat => $details){
22                 $final_links .= '<h2> '.$cat.' </h2><hr/>';
24                 foreach($details as $d => $data){
25                         foreach($data as $content => $c){
26                                 $final_links .= path_link($c['href'], $c['text'], $c['help']) .'<br />';
27                         }
28                 }
30                 $final_links .= '<br/>';
31         }
33         return $final_links;
36 /**
37  * Add tabs to the system admin pages
38  */
39 function system_admin_tabs(){
41         $skin_tabs = array('admin' => 'Main Page',
42                                                          'block/admin/new' => 'Add Block'
43                                                 );
45         skin_tabs($skin_tabs);
48 function system_admin_main(){
50         global $admin_links, $skin, $admin_notices;
52         //Call hook for admin main page
53         system_main_links();
54         hooks_invoke('admin_page');
56         $skin['page_title'] = 'Adiminstrative Center';
59         $content = implode("<br />", $admin_notices) . "<br /><br /><hr/><br />";
61         $content .= system_links_main($admin_links);
63         return $content;
67 function system_settings_form(){
69         global $settings;
71         $form['system_admin']['site_name'] = array(
72                 'type'  => 'text',
73                 'text'  => 'Site Name',
74                 'value' => 'esTigi',
75                 'size' => 50
76                 );
78         $form['system_admin']['site_slogan'] = array(
79                 'type'  => 'text',
80                 'text' =>'Slogan',
81                 'value' => '',
82                 'size' => 50
83                 );
85         $form['system_admin']['site_mail'] = array(
86                 'type'  => 'text',
87                 'text' =>'Default email',
88                 'value' => '',
89                 'size' => 50
90                 );
92         $form['system_admin']['anon_name'] = array(
93                 'type'  => 'text',
94                 'text' =>'Anonymous name',
95                 'value' => 'Anonymous',
96                 'size' => 50
97                 );
99         $form['system_admin']['site_frontpage'] = array(
100                 'type'  => 'text',
101                 'text' => 'Front page ' . $settings['base_root'].$settings['base_url'] . $settings['base_path'] . '?x=',
102                 'help' => 'Leave blank for node',
103                 'value' => '',
104                 'size' => 50
105                 );
107         $form['system_admin']['debug_info'] = array(
108                 'type'  => 'select',
109                 'text' => 'Display Debug info',
110                 'help' => 'Leave blank for node',
111                 'value' => 'No',
112                 'options' => array('No', 'Yes'),
113                 'help' => 'This will be displayed for all acounts'
114                 );
116         $form['system_admin']['db_errors'] = array(
117                 'type'  => 'select',
118                 'text' => 'Display Database errors',
119                 'value' => 'Screen Only',
120                 'options' => array('Screen Only', 'Screen and Database', 'Database Only', 'Do not Display'),
121                 'help' => 'This will be displayed for all acounts'
122                 );
124         return $form;
128  * Set values for admin form
130  * @param &$form 
131  *  The form with the values
132  * @param $module
133  *  Working module
134  */
135 function system_convert_form_admin(&$form, $module){
137         //modify default values if they have been set already
138         foreach($form[$module.'_admin'] as $item => $value){
140                 //Fieldsets
141                 if($value['type'] == 'fieldset'){
142                         foreach($value as $field => $field_value){
143                                 if(is_array($field_value)){
144                                         $form[$module.'_admin'][$item][$field]['value'] = core_variable_get($field, $field_value['value'], $module);
145                                 }
146                         } 
147                 }
148                 else{
149                         $form[$module.'_admin'][$item]['value'] = core_variable_get($item, $value['value'], $module);
150                 }
152         }
157  * Registers modules variables for the modules settings
159  * @param &$form
160  *  The form with the information
161  * @param $module
162  *  The working module
163  * @todo
164  *  Not very convinced in the fieldset handling method
165  */
166 function system_process_form_admin(&$form, $module){
168         global $x;
170         //Remove all variables
171         core_variable_rm(FALSE, $module, TRUE);
173         foreach($form[$module.'_admin'] as $item => $value){
175                 if($value['type'] == 'fieldset'){
176                         foreach($value as $field => $field_value){
177                                 if(is_array($field_value)){
178                                         core_variable_set($field, $field_value['value'], $module);
179                                 }
180                         } 
181                 }
182                 if($value['type'] != 'submit' && $value['type'] != 'fieldset'){
183                         core_variable_set($item, $value['value'], $module);
184                 }
185         }
187         system_warnings("Changes succesfully made");
188         path_redirect($x);
193  * Gets settigs for an administrative form, any module can/should use this.
195  * @param $func
196  *  Function to call in order to get the appropriate form
197  * @param $module
198  *  The module that wants to get/set settings
199  * @param $perms
200  *  An array with a list of access permissions in order to access this form
201  * @param $title
202  *  The page title, will use 'Foo Settings Page' by default
203  * @param $file
204  *  The file extension in which your function is located, default = admin (will load foo.admin)
205  * 
206  */
207 function system_settings_page($func, $module, $perms, $title = NULL, $file = 'admin'){
209         global $form_status, $skin;
211         //No access by default
212         $access = FALSE;
214         foreach($perms as $perm){
215                 if(user_access($perm)){
216                         $access = TRUE;
217                 }
218         }
220         if(!$access){
221                 return PATH_NO_ACCESS;
222         }
224         $skin['page_title'] = (!$title ? $module.' Settings Page' : $title);
226         core_load($module, $file);
228         $form = call_user_func($func);
230         //Convert accordingly
231         if(!isset($_POST['form_id'])){
232                 system_convert_form_admin($form, $module);
233         }
235         //Add a few buttons
236         $form[$module . '_admin']['submit'] = array(
237                 'type' => 'submit',
238                 'value' => 'Submit'
239         );
241         $content = form_form($form, 'post', NULL, (isset($_POST['form_id']) ? FALSE : TRUE));
243         if($form_status == 'verified'){
244                 //Process the form
245                 system_process_form_admin($content, $module);
246         }
248         return $content;
252 //FORMS
254 function system_settings(){
256         global $skin;
258         $skin['page_title'] = 'System main settings';
260         return system_settings_page('system_settings_form', 'system');
265  This page will manually rebuild the modules in the database
267 function system_rebuild_modules(){
269         global $skin;
271         include_once('./includes/modules.admin.inc');
273         $skin['page_title'] = "Module rebuild system";
275         $modules = modules_rebuild_module_settings();
277         $content = "The following modules where proccesed: <br /><br />" ;
279         foreach($modules as $module => $status){
280                 $content .= $module . " ........... " . $status . "<br />";
281         }
283         return $content;
287 function system_modules_overview(){
289         global $skin;
291         include_once('./includes/modules.admin.inc');
293         $skin['page_title'] = "Module administration page";
294         system_warnings("I am not doing any dependency verifications, if you install something without its dependencies you may crash your system.");
296         //Full available module list
297         $modules = modules_get_modules_list();
299         //Required modules
300         $core_modules = modules_required_modules();
302         //Installed modules
303         $module_list = modules_list(-1, TRUE);
305         //Details of all available modules
306         $details = modules_get_module_settings($modules);
308         $content .='<table id="system_module_list">
309                                                 <thead><tr>
310                                                         <th>Status</th>
311                                                         <th>Description</th>
312                                                 </tr>
313                                                 </thead>';
315         foreach($details as $module => $module_details){
317                 if(isset($module_list[$module])){
318                         if($module_list[$module]['status'] == 1){
319                                 $module_status = 'Installed';
320                         }
321                         elseif($module_list[$module]['status'] == 0){
322                                 $module_status = 'Disabled';
323                         }
324                 }
325                 else{
326                         $module_status = 'Not Installed';
327                 }
329                 $content .= '<tr>
330                                                         <td>'.$module_status.'</td>
331                                                         <td>'. module_parse_details($module_details). '</td>
332                                                 </tr>';
334         }
336         $content .= '</table>';
338         return $content;
341 function system_modules_admin($module){
343         global $skin;
344         system_admin_tabs();
346         include_once('./includes/modules.admin.inc');
347         $skin['page_title'] = "Module Administration Page";
348         system_warnings("I am not doing any dependency verifications, if you install something without its dependencies you may crash your system.");
350         $module_list = modules_list(-1, TRUE);
352         //I have no idea if the module is installed or not at this point. Let's find out where it is
353         if(is_file('./modules/'.$module.'/'.$module.'.install')){
354                 include_once('./modules/'.$module.'/'.$module.'.install');
355         }
356         elseif(is_file('./core/'.$module.'/'.$module.'.install')){
357                 include_once('./core/'.$module.'/'.$module.'.install');
358         }
360         //Operations
361         if(isset($_GET['t'])){
362                 if($_GET['t'] == 'enable' && user_access('admin modules')){
363                         module_admin_status(1, $module);
364                 }
365                 else if($_GET['t'] == 'install' && user_access('admin modules')){
366                         if(function_exists($module.'_install')){
367                                 call_user_func($module.'_install'); //Install the module, or at least tell it to run its own installation procedures
368                                 echo module_admin_status(1, $module);
369                         }
370                 }
371         }
373         if(!isset($module_list[$module])){
374                 $module_details = array($module => 'modules/'.$module.'/'.$module.'.module', 'status' => -1);
375         }
376         else{
377                 $module_details = $module_list[$module];
378         }
380         $module_ini = modules_get_module_settings(array($module => $module_details[$module]));
382         if($module_details['status'] == 1){
383                 $module_status = 'Installed';
384         }
385         elseif($module_details['status'] == 0){
386                 $module_status = 'Disabled';
387         }
388         else{
389                 $module_status = 'Not Installed';
390         }
392         $content = '<h2>Module Details</h2';
393         $content .= '<div>'.module_parse_details($module_ini[$module]).'</div><br />';
394         $content .= 'Module Status: ' . $module_status . '<br />';
395         $content .= 'Operations: ';
397         if($module_details['status'] == 1){
398                 $content .= path_link('system/admin/modules/admin/'.$module.'&t=uninstall', 'Uninstall');
399                 $content .= ' ' . path_link('system/admin/modules/admin/'.$module.'&t=disable', 'Disable');
400         }
401         elseif($module_details['status'] == -1){
402                 $content .= path_link('system/admin/modules/admin/'.$module.'&t=install', 'Install');
403         }
404         else{
405                 $content .= path_link('system/admin/modules/admin/'.$module.'&t=enable', 'Enable');
406         }
408         //Perms
409         if(function_exists($module.'_perms') && $module_details['status'] == 1){
410                 $content .= system_module_perms($module);
411         }
413         return $content;
417  * Permisions?
419  * 
421 function system_module_perms($module){
423         $roles = user_roles();
424         core_load('form', 'api');
425         //foreach()
426         $content = form_set_head('roles_perms');
427         $content .= '<table id="roles_perms">
428                                                 <thead><tr>
429                                                         <th>Owner</th>
430                                                         <th>(Id)Title</th>
431                                                         <th>Pos</th>
432                                                 </tr>
433                                                 </thead>';
435         $content .= '<tr>
436                                                 <td>'.$block['owner'].'</td>
437                                                 <td> ('. $block['rid']. ')' .$block['title']. '</td>
438                                                 <td>'.$pos.'</td>
439                                         </tr>';
441         $content .= '</table></form>';
443         return $content;
448  *      Page to display php information
450 function system_show_php_info(){
452         ob_start () ;
453         phpinfo () ;
454         $pinfo = ob_get_contents () ;
455         ob_end_clean () ;
457         // the name attribute "module_Zend Optimizer" of an anker-tag is not xhtml valide, so replace it with "module_Zend_Optimizer"
458         $content = ( str_replace ( "module_Zend Optimizer", "module_Zend_Optimizer", preg_replace ( '%^.*<body>(.*)</body>.*$%ms', '$1', $pinfo ) ) ) ;
459         return $pinfo;