Questions about adding settings in Admin Section

Discussion in 'Developers' Forum' started by dclardy, May 2, 2013.

  1. dclardy

    dclardy Member

    I know the thread title is a little broad, but I have a couple of questions about adding a section to the Admin section for a theme.

    I would like to add some settings for my theme inside of the admin section of ISPConfig3. I was unable to get it to add dynamically so I had to hard code it for now. How can I get this to be added dynamically? Does the logic to check for admin.module.conf work in the theme folders as well?

    It appears that it does, and I want to be sure that I am correct in that assumption.

    The next question that I have is this, How do you query records from the database? This is the code that I am trying to use.

    Code:
    $logo = $app->db->queryOneRecord('SELECT logo_url FROM tpl_ispc_clean');
    
    $form["tabs"]['basic'] = array (
    	'title' 	=> "Basic Settings",
    	'width' 	=> 80,
    	'template' 	=> "templates/tpl_ispc-clean_basic.htm",
    	'fields' 	=> array (
    	##################################
    	# Beginn Datenbankfelder
    	##################################
    		'logo_url' => array (
    			'datatype'	=> 'VARCHAR',
    			'formtype'	=> 'TEXT',
    			'validators'=> '',
    			'default'	=> '',
    			'value'		=> $logo,
    			'separator'	=> '',
    			'width'		=> '40',
    			'maxlength'	=> '255'
    		),
            'sidebar_state' => array (
    			'datatype'	=> 'INTEGER',
    			'formtype'	=> 'TEXT',
    			'validators'=> '',
    			'default'	=> '',
    			'value'		=> $sidebar_state,
    			'separator'	=> '',
    			'width'		=> '40'
    		),
    	##################################
    	# ENDE Datenbankfelder
    	##################################
    	)
    );
    On my template file, I am calling that field, but it is not working.

    Code:
    <h2><tmpl_var name="tpl_ispc-clean_head_txt"></h2>
    <p><tmpl_var name="tpl_ispc-clean_desc_txt"></p>
    
    <div class="panel panel_system_config">
        
        <div class="pnl_formsarea">
            <fieldset class="inlineLabels"><legend>Basic Settings</legend>
                <div class="ctrlHolder">
                    <label for="logo_url">{tmpl_var name='logo_url_txt'}Logo URL</label>
                    <input name="logo_url" id="logo_url" value="{tmpl_var name='logo_url'}" size="50" maxlength="255" type="text" class="textInput" /> 152x46px
                </div>
                <div class="ctrlHolder">
                    <label for="sidebar_state">{tmpl_var name='sidebar_state_txt'}Sidebar</label>
                    <select class="selectInput" id="sidebar_state" name="sidebar_state">
                        <option value="0">Off</option>
                        <option value="1">On (Static)</option>
                        <option value="2">On (Retractable)</option>
                    </select>
                </div>
            </fieldset>
            <input type="hidden" name="id" value="{tmpl_var name='id'}">
            <div class="buttonHolder buttons">
                <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onclick="submitForm('pageForm','admin/tpl_ispc-clean.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
                <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onclick="loadContent('admin/server_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
            </div>
        </div>
        
    </div>
    Right now, I am placing all of this inside of the admin module of ISPConfig3, but it appears that I can load this all from the theme. Is that correct? I would love to keep it there, if possible. I do not like making people have to copy files install ISPConfig3 directories.

    Any help would be appreciated. I am just trying to make the theme that I created a little better than it currently is.
     
  2. dclardy

    dclardy Member

    After more investigation, I can't load it from the theme file. I guess that you can, but you have to make some changes to the requires stuff that it is kind of silly. ../../../../ It gets to be a lot after a few of those.

    The menu is created from the admin->lib->menu.d-> directory. This can not be included in your theme.

    I was unable to figure out how to write that query. If someone could help with that, I would be very glad. I am learning PHP so it is taking me a lot longer than it probably should to figure this out.
     
  3. till

    till Super Moderator Staff Member ISPConfig Developer

    No and thats intended as themes may not contain own form files or meny entries.

    No. Themes may not contain php files. Themes may only contain css files, images and form files.

    There is a strict separation between code files which contain programming logic, they have to be placed inside the module folders and themes which contain only style and template files.
     
  4. dclardy

    dclardy Member

    Till,

    Thanks for the information. I will include all the files in the admin section.

    Can you help me with the query listed above? I am trying to pull a single value from a table I created. I don't see any examples of that inside of the code, and am kind of a beginner at PHP.
     
  5. till

    till Super Moderator Staff Member ISPConfig Developer

    Replace the line:

    'value' => $logo,

    with:

    'value' => $logo['logo_url'],
     
  6. dclardy

    dclardy Member

    Till,

    Thanks for that information. How do I get this to become a variable in the template? I am trying to load the value inside of the .htm file.

    Once I get this one done, I am sure that I will be able to figure the rest of them out.

    Are there any places to go for templating help?
     
  7. dclardy

    dclardy Member

    After looking through the files some, I thought that I had found the way to load the value. I put this code in my file.

    Code:
    class page_action extends tform_actions {
    
    	//* Customisations for the page actions will be defined here
    	function onShow() {
    		global $app, $conf;
    		
    		$logo_sql = $app->db->queryOneRecord("SELECT * FROM `tpl_ispc_clean` where id = 1 ");
    		$logo = $logo_sql['logo_url'];
    
    		$app->tpl->setVar('logo_url', $logo);
    		
    		parent::onShow();
    	}
    
    }
    This did not work for the admin pages.

    I can get the information to show on the login page. I added a template variable inside of the index.php for that module.

    Code:
    $logo_sql = $app->db->queryOneRecord("SELECT * FROM `tpl_ispc_clean` where id = 1 ");
    		$logo = $logo_sql['logo_url'];
    
    		$app->tpl->setVar('error', $error);
            $app->tpl->setVar('pw_lost_txt', $app->lng('pw_lost_txt'));
    		$app->tpl->setVar('username_txt', $app->lng('username_txt'));
    		$app->tpl->setVar('password_txt', $app->lng('password_txt'));
    		$app->tpl->setVar('login_button_txt', $app->lng('login_button_txt'));
    		$app->tpl->setVar('logo_url', $logo);
     
  8. dclardy

    dclardy Member

    Does anyone know what template tools ISPConfig uses? Is it using a readily available project for this or is it doing it on its own? I thought that it was using Smarty, but I don't think that it is. I could be completely wrong.
     
  9. dclardy

    dclardy Member

    After even more finding, I can get it to save to the the database table now. The only issue is that it always inserts a new record instead of updating the data? How can I change that functionality? I need it to update here. Is there a parameter than I need to send that I am not.

    tpl_ispc-clean_edit.php

    Code:
    <?php
    
    /******************************************
    * Begin Form configuration
    ******************************************/
    
    $tform_def_file = "form/tpl_ispc-clean.tform.php";
    
    /******************************************
    * End Form configuration
    ******************************************/
    
    require_once('../../lib/config.inc.php');
    require_once('../../lib/app.inc.php');
    
    //* Check permissions for module
    $app->auth->check_module_permissions('admin');
    
    // Loading classes
    $app->uses('tpl,tform,tform_actions');
    $app->load('tform_actions');
    
    class page_action extends tform_actions {
    
    }
    
    $page = new page_action;
    $page->onLoad();
    
    ?>
    tpl_ispc-clean.tform.php
    Code:
    <?php
    
    /*
    	Form Definition
    
    	Tabellendefinition
    
    	Datentypen:
    	- INTEGER (Wandelt Ausdrücke in Int um)
    	- DOUBLE
    	- CURRENCY (Formatiert Zahlen nach Währungsnotation)
    	- VARCHAR (kein weiterer Format Check)
    	- TEXT (kein weiterer Format Check)
    	- DATE (Datumsformat, Timestamp Umwandlung)
    
    	Formtype:
    	- TEXT (normales Textfeld)
    	- TEXTAREA (normales Textfeld)
    	- PASSWORD (Feldinhalt wird nicht angezeigt)
    	- SELECT (Gibt Werte als option Feld aus)
    	- RADIO
    	- CHECKBOX
    	- FILE
    
    	VALUE:
    	- Wert oder Array
    
    	Hinweis:
    	Das ID-Feld ist nicht bei den Table Values einzufügen.
    
    
    */
    
    $form["title"] 		= "ISPC-Clean Theme Settings";
    $form["description"]= "Basic Settings for the ISPC-Clean Theme";
    $form["name"] 		= "tpl_ispc-clean_settings";
    $form["action"]		= "tpl_ispc-clean_edit.php";
    $form["db_table"]	= "tpl_ispc_clean";
    $form["db_table_idx"]	= "theme_settings_id";
    $form["db_history"]	= "yes";
    $form["tab_default"]= "basic";
    $form["list_default"]	= "server_list.php";
    $form["auth"]		= 'yes';
    
    $form["auth_preset"]["userid"]  = 0; // 0 = id of the user, > 0 id must match with id of current user
    $form["auth_preset"]["groupid"] = 0; // 0 = default groupid of the user, > 0 id must match with groupid of current user
    $form["auth_preset"]["perm_user"] = 'riud'; //r = read, i = insert, u = update, d = delete
    $form["auth_preset"]["perm_group"] = 'riud'; //r = read, i = insert, u = update, d = delete
    $form["auth_preset"]["perm_other"] = ''; //r = read, i = insert, u = update, d = delete
    
    $theme_sql = $app->db->queryOneRecord("SELECT * FROM `tpl_ispc_clean` where theme_settings_id = 1 ");
    
    $form["tabs"]['basic'] = array (
    	'title' 	=> "Basic Settings",
    	'width' 	=> 80,
    	'template' 	=> "templates/tpl_ispc-clean_edit.htm",
    	'fields' 	=> array (
    	##################################
    	# Beginn Datenbankfelder
    	##################################
    		'logo_url' => array (
    			'datatype'	=> 'VARCHAR',
    			'formtype'	=> 'TEXT',
    			'default'	=> $theme_sql['logo_url'],
    			'value'		=> '',
    			'separator'	=> '',
    			'width'		=> '40',
    			'maxlength'	=> '255'
    		),
            'sidebar_state' => array (
    			'datatype'	=> 'INTEGER',
    			'formtype'	=> 'SELECT',
    			'default'	=> $theme_sql['sidebar_state'],
    			'value'		=> array(0 => 'Off', 1 => 'On'),
    		),
    	##################################
    	# ENDE Datenbankfelder
    	##################################
    	)
    );
    
    ?>
    tpl_ispc-clean_edit.htm
    Code:
    <h2><tmpl_var name="tpl_ispc-clean_head_txt"></h2>
    <p><tmpl_var name="tpl_ispc-clean_desc_txt"></p>
    
    <div class="panel panel_system_config">
        
        <div class="pnl_formsarea">
            <fieldset class="inlineLabels"><legend>Basic Settings</legend>
                <div class="ctrlHolder">
                    <label for="logo_url"><tmpl_var name='logo_url_txt'></label>
                    <input name="logo_url" id="logo_url" value="{tmpl_var name='logo_url'}" size="50" maxlength="255" type="text" class="textInput" />&nbsp; Size:&nbsp;152x46px
                </div>
                <div class="ctrlHolder">
                    <label for="sidebar_state">{tmpl_var name='sidebar_state_txt'}</label>
                    <select class="selectInput" id="sidebar_state" name="sidebar_state">
                        {tmpl_var name='sidebar_state'}
                    </select>
                </div>
            </fieldset>
            {tmpl_var name='sidebar_state'}
            <input type="hidden" name="id" value="{tmpl_var name='id'}">
    
            <div class="buttonHolder buttons">
                <button class="positive iconstxt icoPositive" type="button" value="{tmpl_var name='btn_save_txt'}" onclick="submitForm('pageForm','admin/tpl_ispc-clean_edit.php');"><span>{tmpl_var name='btn_save_txt'}</span></button>
                <button class="negative iconstxt icoNegative" type="button" value="{tmpl_var name='btn_cancel_txt'}" onclick="loadContent('admin/server_list.php');"><span>{tmpl_var name='btn_cancel_txt'}</span></button>
            </div>
        </div>
    </div>
    Please any help would be appreciated.
     
  10. till

    till Super Moderator Staff Member ISPConfig Developer

    You have to add the ?id=1 to the URL when you want to open up the first record in your table
     
  11. dclardy

    dclardy Member

    Till,

    I went ahead and added that ?id=1 to the end of the link open up the section. It did not place the value.

    How do I set the template variable {tmpl_var name='id'}. Do I need to set this inside of my form configuration file? I am looking at the Interface config section because it is the most similar to what I am trying to do for this section.

    I see that it sets the template variable id, but how is it getting that information? How can I do the same thing? I just need the ID set, and I think that I will have it all done. I can set it manually, but I don't think that is the right thing to do here.
     
  12. till

    till Super Moderator Staff Member ISPConfig Developer

    The ID is set automatically based in the definition of the primary ID field in the form file.
     
  13. dclardy

    dclardy Member

    It appears that ISPConfig did not like the field name that I used.

    I had theme_settings_id. I changed the field to themesettings_id, and it magically works.

    Till,

    Thanks for the help. I appreciate it. I am not sure how mane people even use my theme, but this will make it better for those that do!
     
  14. dclardy

    dclardy Member

    Till,

    I have everything almost completed in the backend, but this has led to more questions.

    1. When you are using a datasource in a SELECT type field for the form, how can you have it save the value_field into the database. Right now, on save it is saving the value of the select as 0 or 1 or whatever. Can you set the value equal to the value that it displays in the select dropdown?

    2. How do we do right template loops in ISPConfig3? Is it better to try to do it PHP and output the code in the template that way?
     
  15. till

    till Super Moderator Staff Member ISPConfig Developer

    1) Sure. In the form file you can define which column is used for the value field, this can be the same column that you use for the displayed value.

    2) Use the tmpl_loop statement in the template, no need for any manual programming in php.
     
  16. dclardy

    dclardy Member

    Till,

    Thanks. I have it saving the correct information to the database now! Now, I just have to figure out this front page.

    I am trying to add a sidebar to the front page that output data from the database.

    This is how I envision it working.

    Category 1
    ---Stuff from First Item in Category 1
    ---Stuff Form Second Item in Category 1
    Category 2
    ---Stuff from First Item in Category 2
    ---Stuff Form Second Item in Category 2

    This is my current index.htm.

    Code:
    <aside id="sidebar" class="login-sidebar">
        {tmpl_loop name='sidebar_records'}
    </aside>
    
    <div id="login-form">
        <div class="box-inner">
            <img title="<tmpl_var name="app_title">" id="logo" alt="<tmpl_var name="app_title">" src="<tmpl_var name='app_logo'>">
            <table>
                <tbody>
                    <tr>
                        <td class="title">
                            <label for="username">{tmpl_var name='username_txt'}</label>
                        </td>
                        <td class="input">
                            <input name="username" id="username" value="" size="30" maxlength="255" type="text" class="textInput"  onkeypress="if (event.keyCode && event.keyCode == 13) {submitLoginForm('pageForm'); return false;};" />
                        </td>
                    </tr>
                    <tr>
                        <td class="title">
                            <label for="passwort">{tmpl_var name='password_txt'}</label>
                        </td>
                        <td class="input">
                            <input name="passwort" id="passwort" value="" size="30" maxlength="255" type="password" class="textInput"  onkeypress="if (event.keyCode && event.keyCode == 13) {submitLoginForm('pageForm'); return false;};" />
                        </td>
                    </tr>
                </tbody>
            </table>
            <input type="hidden" name="s_mod" value="login" />
            <input type="hidden" name="s_pg" value="index" />
            <p id="loginbutton">
                <button type="button" class="button" value="{tmpl_var name="add_new_record_txt"}" onclick="submitLoginForm('pageForm');"><span>{tmpl_var name='login_button_txt'}</span></button>
            </p>
            <p class="powered">Powered by <a href="<tmpl_var name="app_link">" target="_blank"><tmpl_var name="app_title"></a></p>
        </div>
        <div class="box-bottom">
            <div id="message">
                <tmpl_if name="msg">
                    <div id="OKMsg"><p><tmpl_var name="msg"></p></div>
                </tmpl_if>
                <tmpl_if name="error">
                    <div id="errorMsg">
                        <div class="warning"><tmpl_var name="error"></div>
                        <br /><a href="#" onclick="loadContent('login/password_reset.php');">{tmpl_var name='pw_lost_txt'}</a>
                    </div>
                </tmpl_if>
            </div>
        </div>
    </div>
    I have added this information to the login module.

    Code:
    $sidebar_records = array();
    		$sidebar_query = $app->db->queryAllRecords("SELECT category,title,tpl_ispc_clean_app.desc,image_url,link,target FROM tpl_ispc_clean_app order by sorting ");
    		if(is_array($sidebar_query)) {
    			foreach($sidebar_query as $side_qry) {
    				$sidebar_records[$side_qry['category']] = $side_qry['category'];
    				$sidebar_records[$side_qry['title']] = $side_qry['title'];
    				$sidebar_records[$side_qry['desc']] = $side_qry['desc'];
    				$sidebar_records[$side_qry['image_url']] = $side_qry['image_url'];
    				$sidebar_records[$side_qry['link']] = $side_qry['link'];
    				$sidebar_records[$side_qry['target']] = $side_qry['target'];
    			}
    		}
    
    $app->tpl->setVar('sidebar', print_r($sidebar_records));
    $app->tpl->setLoop('sidebar_records', $sidebar_records);
    I guess that I don't understand loops in the templates because nothing is happening. It is throwing out the error Invalid Loop Structure. Any help would be great!
     
  17. dclardy

    dclardy Member

    Any ideas on this, Till?

    I am pretty sure that you and Falko can help me here.
     
  18. till

    till Super Moderator Staff Member ISPConfig Developer

    The way sou build the array for th loop looks wrong please check out the ispconfig files that build the menus for some working code. And the login module is just doing the user authentication, it is not writing ny menus or templates. So adding a menu thre oes not mke much sense for me.
     
  19. dclardy

    dclardy Member

    Till,

    I'll look at the code for the submenus. I guess the dashboard would be a good module to look at for that.

    I am using the login module because I am adding a list of helpful links for customers on the login page. When I finish it, it will make perfect sense!
     
  20. dclardy

    dclardy Member

    Till.

    I just wanted to say thanks for all the help. I have completed my little project, and I wanted to show it to you. I will be pushing out all of these changes to my theme later in the week.

    The entire sidebar area is dynamically created. You can add as many links and categories as you want form the System Admin section.

    [​IMG]
     

Share This Page