<!doctype article public "-//OASIS//DTD DocBook V3.1//EN">

<article lang="en">
<!-- DocBook file was created by LyX 1.1
  See http://www.lyx.org/ for more information -->
  <artheader>
   <title>
   eGroupWare admin/config.php
  </title>
  <abstract>
  <para>
   A brief introduction to writing hooks and templates for any application to use this admin interface, by
  </para>
  <para>
   Miles Lott &lt;milosch@groupwhere.org&gt; Dec 22, 2001.
  </para>
  </abstract>
  </artheader>
  <sect1>
   <title>
   Files
  </title>
   <sect2>
    <title>
    config.tpl (required)
   </title>
   <para>
    In your application/templates/default directory, create a new template file named 'config.tpl'. This will be included by config.php and used to draw the page. This template should include a POST method form. The following template tags may be used:
   </para>
   <orderedlist>
    <listitem>
    <para>
    &lcub;action_url&rcub; - A egw-&gt;link to config.php will be inserted.
    </para>
   </listitem>
    <listitem>
    <para>
    &lcub;title&rcub; - This will be parsed to display 'Site Configuration'.
    </para>
   </listitem>
    <listitem>
    <para>
    &lcub;th_bg&rcub;,&lcub;th_text&rcub;,&lcub;row_on&rcub;,&lcub;row_off&rcub; - Replaced with the current theme colors.
    </para>
   </listitem>
   </orderedlist>
   <para>
    and the following special types:
   </para>
   <orderedlist>
    <listitem>
    <para>
    &lcub;lang_XXX&rcub; - Filled with lang('XXX').
    </para>
   </listitem>
    <listitem>
    <para>
    &lcub;value_XXX&rcub; - Filled with the current value of config item 'XXX'.
    </para>
   </listitem>
    <listitem>
    <para>
    &lcub;selected_XXX&rcub; - set to '', or ' selected' if an option value is current.
    </para>
   </listitem>
    <listitem>
    <para>
    &lcub;hook_XXX&rcub; - Calls a function named XXX (will be discussed later).
    </para>
   </listitem>
   </orderedlist>
   <para>
    Following is an example from the addressbook application:
   </para>
   <programlisting>
<![ CDATA [<form method="POST" action="{action_url}">
]]><![ CDATA [<table border="0" align="center">
]]><![ CDATA [ <tr bgcolor="{th_bg}">
]]><![ CDATA [  <td colspan="2"><font color="{th_text}">&nbsp;<b>{title}</b></font></td>
]]><![ CDATA [ </tr> <tr bgcolor="{th_err}">
]]><![ CDATA [  <td colspan="2">&nbsp;<b>{error}</b></font></td>
]]><![ CDATA [ </tr>
]]><![ CDATA [<!-- END header -->
]]><![ CDATA [<!-- BEGIN body -->
]]><![ CDATA [ <tr bgcolor="{row_on}">
]]><![ CDATA [  <td colspan="2">&nbsp;</td>
]]><![ CDATA [ </tr>
]]><![ CDATA [ <tr bgcolor="{row_off}">
]]><![ CDATA [  <td colspan="2">&nbsp;<b>{lang_Addressbook}/{lang_Contact_Settings}</b></font>
]]><![ CDATA [</td>
]]><![ CDATA [ </tr>
]]><![ CDATA [ <tr bgcolor="{row_on}">
]]><![ CDATA [  <td>{lang_Contact_application}:</td>
]]><![ CDATA [  <td><input name="newsettings[contact_application]" value="{value_contact_application}"></td>
]]><![ CDATA [ </tr>
]]><![ CDATA [...
]]>   </programlisting>
   <para>
    Note the fieldname, newsettings&lsqb;contact_application&rsqb;. This array name must be used for the form values. Next, note the value setting for this form element, &lcub;value_contact_application&rcub;. This indicates that we want the current value of the config setting, 'contact_application', to be set and displayed on the form. Lastly, look at the template element, &lcub;lang_Contact_application&rcub;. Here, the value from the lang db table will be inserted if available.
   </para>
   <para>
    Let's take a look at part of the preferences/default/config.tpl:
   </para>
   <programlisting>
<![ CDATA [ <tr bgcolor="{row_on}">
]]><![ CDATA [  <td>{lang_Country_Selection} ({lang_Text_Entry}/{lang_SelectBox}):</td>
]]><![ CDATA [  <td>
]]><![ CDATA [   <select name="newsettings[countrylist]">
]]><![ CDATA [{hook_country_set}
]]><![ CDATA [   </select>
]]><![ CDATA [  </td>
]]><![ CDATA [ </tr>
]]>   </programlisting>
   <para>
    Here, we are adding a new element, &lcub;hook_country_set&rcub;. This brings up the next file we will need to parse this value...
   </para>
   </sect2>
   <sect2>
    <title>
    hook_config.inc.php (optional)
   </title>
   <para>
    At each invocation of config.php, a call to the common class function hook_single() is made. It attempts to include a file, hook_config.inc.php as a set of code for config.php to use. In the case of the preferences example above, using hook_country_set, here is the corresponding function in preferences/inc/hook_config.inc.php:
   </para>
   <programlisting>
<![ CDATA [function country_set($config)
]]><![ CDATA [{
]]><![ CDATA [    $country = array( 'user_choice' => 'Users Choice', 'force_select' => 'Force Selectbox' );
]]><![ CDATA [    while (list ($key, $value) = each ($country))
]]><![ CDATA [    {
]]><![ CDATA [        if ($config['countrylist'] == $key)
]]><![ CDATA [        {
]]><![ CDATA [            $selected = ' selected';
]]><![ CDATA [        } 
]]><![ CDATA [        else
]]><![ CDATA [        {
]]><![ CDATA [            $selected = '';
]]><![ CDATA [        }
]]><![ CDATA [        $descr = lang($value);
]]><![ CDATA [        $out .= '<option value="' . $key . '"' . $selected . '>' . $descr . '</option>' . "\n";
]]><![ CDATA [    }
]]><![ CDATA [    return $out;
]]><![ CDATA [}
]]>   </programlisting>
   <para>
    Note again the template value we used earlier, &lcub;hook_country_set&rcub;. This causes config.php to look for a function named country_set(). Since we included the file with this function via the hook_single() call, this function is executed. It's return is a string, and the function prints nothing itself.
   </para>
   </sect2>
   <sect2>
    <title>
    hook_config_validate.inc.php (optional)
   </title>
   <para>
    Once the admin clicks the submit button to post the form, we can optionally validate their input using one or many different functions. This is done by first making another call to hook_single() in the API common class. This time, the name config_validate is used, so common tries to include 'application/inc/hook_config_validate.inc.php'.
   </para>
   <para>
    If this file exists, it sets a var to tell config.php it was found. Following then are functions named after each config we want to validate. The following example is for addressbook:
   </para>
   <programlisting>
<![ CDATA [    $GLOBALS['phpgw_info']['server']['found_validation_hook'] = True;
]]><![ CDATA [
]]><![ CDATA [    /* Check a specific setting. Name must match the setting. */
]]><![ CDATA [    function ldap_contact_context($value='')
]]><![ CDATA [    {
]]><![ CDATA [        if($value == $GLOBALS['phpgw_info']['server']['ldap_context'])
]]><![ CDATA [        {
]]><![ CDATA [            $GLOBALS['config_error'] = 'Contact context for ldap must be different from the context used for accounts';
]]><![ CDATA [        }
]]><![ CDATA [        elseif($value == $GLOBALS['phpgw_info']['server']['ldap_group_context'])
]]><![ CDATA [        {
]]><![ CDATA [            $GLOBALS['config_error'] = 'Contact context for ldap must be different from the context used for groups';
]]><![ CDATA [        }
]]><![ CDATA [        else
]]><![ CDATA [        {
]]><![ CDATA [            $GLOBALS['config_error'] = '';
]]><![ CDATA [        }
]]><![ CDATA [    }
]]>   </programlisting>
   <para>
    Here we created a function to check the entered value for the config item, ldap_contact_context. We want to make sure the admin did not set this value to one which would conflict with another config item, used for accounts or groups in eGroupWare.
   </para>
   <para>
    config.php calls this function, sending it the POSTed value. config.php continues, adding all other config items from the POSTed values.
   </para>
   <para>
    The variable &dollar;GLOBALS&lsqb;'config_error'&rsqb; is parsed through lang(), then appended to the local variable, &dollar;error. If this has any value after the POSTed variables are checked, the form then has its &lcub;error&rcub; tag filled with this result. The form is displayed again, with the error. If &dollar;error has no value, config.php redirects to admin/index.php.
   </para>
   <para>
    However, there is one more function that may be included in hook_config_validate.inc.php:
   </para>
   <programlisting>
<![ CDATA [    /* Check all settings to validate input. Name must be 'final_validation' */
]]><![ CDATA [    function final_validation($value='') 
]]><![ CDATA [    {
]]><![ CDATA [        if($value['contact_repository'] == 'ldap' && !$value['ldap_contact_dn'])
]]><![ CDATA [        {
]]><![ CDATA [            $GLOBALS['config_error'] = 'Contact dn must be set';
]]><![ CDATA [        }
]]><![ CDATA [        elseif($value['contact_repository'] == 'ldap' && !$value['ldap_contact_context'])
]]><![ CDATA [        {
]]><![ CDATA [            $GLOBALS['config_error'] = 'Contact context must be set';
]]><![ CDATA [        }
]]><![ CDATA [        else
]]><![ CDATA [        {
]]><![ CDATA [            $GLOBALS['config_error'] = '';
]]><![ CDATA [        }
]]><![ CDATA [    }
]]>   </programlisting>
   <para>
    config.php checks for the existence of the function 'final_validation()'. This function can be used to check all form values at once. It gets sent the entire &dollar;newsettings array POSTed from the form. As with the other functions in this file, final_validation() should set &dollar;GLOBALS&lsqb;'config_error'&rsqb; if there is a problem.
   </para>
   </sect2>
  </sect1>


</article>
