Changes between Version 5 and Version 6 of DolphinTutorialMyFirstModule


Ignore:
Timestamp:
Jan 29, 2010, 6:01:20 AM (15 years ago)
Author:
AlexT
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DolphinTutorialMyFirstModule

    v5 v6  
    263263 
    264264 
     265The simplest Dolphin module is completed. Now you can use this to start developing you first Dolphin module, or use it as a framework for your own different modules. Almost every extension can be started from such a structure. Make sure that you take a look at the bloggie/install/config.php file. This file must be heavily changed for your own module, and this file has comments on each line to help you in changing it. 
     266 
     267 
     268== 3. Adding admin panel. == 
     269  
     270We want to improve our mod and add a format for the date and allow disabling user time. We definitely need some admin settings for this purpose. 
     271 
     272Our admin panel will have the following parts: 
     273 
     274    * admin page creation 
     275    * adding settings to module install and uninstall 
     276    * print setting form on newly created admin pages 
     277    * a link in the admin modules section to the module setting page 
     278 
     279== 3.1. Admin page creation. == 
     280 
     281Add the following code to the '''bloggie/classes/MeBlggModule.php''' file as a class method: 
     282 
     283{{{ 
     284    function actionAdministration () { 
     285 
     286        if (!$GLOBALS['logged']['admin']) { 
     287            $this->_oTemplate->displayAccessDenied (); 
     288            return; 
     289        } 
     290 
     291        $this->_oTemplate->pageStart(); 
     292 
     293        echo DesignBoxAdmin (_t('_me_blgg'), 'It works!'); 
     294         
     295        $this->_oTemplate->pageCodeAdmin (_t('_me_blgg')); 
     296    } 
     297}}} 
     298 
     299The '''pageCodeAdmin''' function is similar to the  '''pageCode''' function, but it displays the admin page instead of the user page. Also, this function accepts only one parameter - page title. 
     300 
     301Also the difference in the code is that we check if admin only can access this page. The '''$GLOBALS['logged']['admin']''' variable is set to true if admin is logged in. Additionally, you can check if regular members are logged in by using the '''$GLOBALS['logged']['member']''' variable. If none of these values are true, then a guest is accessing your page. The '''displayAccessDenied''' function displays a page with an "access denied" message - in this case, we don't need to care much about that, and we can just return from the function or even exit. The '''$GLOBALS['logged']''' does not always exist, and is initialized in the '''check_logged''' function. In our case this function is called in the '''bloggie/request.php''' file. 
     302 
     303Another new function here is '''DesignBoxAdmin'''. It wraps our content in a nice box. The box title is the first parameter and box content is the second one. There is an optional 3rd parameter where you can define the box menu. Also, there is an analog of this function in the user side called '''DesignBoxContent'''. You can examine existing Dolphin code for a lot of examples. 
     304 
     305To test the page, you need to login to the admin panel and open the '''http://www.your-site.com/your-path/m/bloggie/administration''' URL. 
     306 
     307There is no artificial code, and hopefully you have made everything properly and the admin page is displayed with the  "It works!" phrase. If there is a problem, then review all the steps once again and try to find where there is a mistake. 
     308 
     309 
     310== 3.2. Adding module settings. == 
     311 
     312At this stage we need to modify the '''install.sql''' and '''uninstall.sql''' files for the first time. We need to change the installation config file and indicate that these files must be processed. To do this we need to change the following section in the '''bloggie/install/config.php''' file: 
     313 
     314 
     315{{{ 
     316    'install' => array( 
     317        'update_languages' => 1, 
     318    ), 
     319    'uninstall' => array ( 
     320        'update_languages' => 1, 
     321    ), 
     322}}} 
     323 
     324to the following: 
     325 
     326{{{ 
     327    'install' => array( 
     328        'update_languages' => 1, 
     329        'execute_sql' => 1, 
     330    ), 
     331    'uninstall' => array ( 
     332        'update_languages' => 1, 
     333        'execute_sql' => 1, 
     334    ), 
     335}}} 
     336 
     337It will execute '''install.sql''' and '''uninstall.sql''' files automatically upon modules install/uninstall. 
     338 
     339Then, place the following code into '''install.sql''': 
     340 
     341 
     342{{{ 
     343SET @iMaxOrder = (SELECT `menu_order` + 1 FROM `sys_options_cats` ORDER BY `menu_order` DESC LIMIT 1); 
     344INSERT INTO `sys_options_cats` (`name`, `menu_order`) VALUES ('My Bloggie', @iMaxOrder); 
     345SET @iCategId = (SELECT LAST_INSERT_ID()); 
     346INSERT INTO `sys_options` (`Name`, `VALUE`, `kateg`, `desc`, `Type`, `check`, `err_text`, `order_in_kateg`, `AvailableValues`) VALUES 
     347('me_blgg_date_format', 'Y-m-d H:i', @iCategId, 'Format for server date/time', 'digit', '', '', '1', ''), 
     348('me_blgg_enable_js_date', 'on', @iCategId, 'Show user time', 'checkbox', '', '', '2', ''); 
     349}}} 
     350 
     351and the following into '''uninstall.sql''': 
     352 
     353{{{ 
     354-- settings 
     355SET @iCategId = (SELECT `ID` FROM `sys_options_cats` WHERE `name` = 'My Bloggie' LIMIT 1); 
     356DELETE FROM `sys_options` WHERE `kateg` = @iCategId; 
     357DELETE FROM `sys_options_cats` WHERE `ID` = @iCategId; 
     358}}} 
     359 
     360The most important thing is that all setting options are in the '''sys_options''' database table. It is better to assign every setting option some category which is stored in the '''sys_options_cats''' database table, because it makes it easier to delete created categories in uninstall and display them. Also, it is good practice to make a prefix for all of your setting names to avoid conflicts with other mods. 
     361 
     362Now, we need to use newly created settings, so we change the '''actionHome''' function in the following way: 
     363 
     364{{{ 
     365    function actionHome () { 
     366        $this->_oTemplate->pageStart(); 
     367 
     368        $sDateFormat = getParam ('me_blgg_date_format'); 
     369        $isShowUserTime = getParam('me_blgg_enable_js_date') ? true : false; 
     370        $aVars = array ( 
     371            'server_time' => date($sDateFormat), 
     372            'bx_if:show_user_time' => array( 
     373                'condition' => $isShowUserTime, 
     374                'content' => array(), 
     375            ), 
     376        ); 
     377        echo $this->_oTemplate->parseHtmlByName('main', $aVars); 
     378        $this->_oTemplate->pageCode(_t('_me_blgg'), true); 
     379    } 
     380}}} 
     381 
     382and we change the template as shown below: 
     383 
     384{{{ 
     385<div style="text-align:center;"> 
     386    <bx_text:_me_blgg_server_time /> __server_time__ 
     387    <bx_if:show_user_time> 
     388        <br /> 
     389        <bx_text:_me_blgg_user_time /> 
     390        <script type="text/javascript"> 
     391            document.write(new Date()); 
     392        </script> 
     393    </bx_if:show_user_time> 
     394</div> 
     395}}} 
     396 
     397Lets describe a few new things: 
     398 
     399    * the '''getParam''' function is used to get a setting option with a specified name. Please note that in the case of a checkbox, it will return '''on''' as the true value or an empty string as a false value. 
     400    * '''bx_if''' template construction allows adding conditional processing to the template. The condition is in the '''condition''' field, and any variables which you need to use inside bx_if clauses in the template should be placed in the content subarray. In our case, there is no such variable and this array is empty. 
     401 
     402Almost everything is done, and it is time to test it now. Reinstall the module, since we have changed the installation config and installation files, and refresh the  '''http://www.your-site.com/your-path/m/bloggie/home''' page. If everything is done right, the result should be as follows: 
     403 
     404{{{ 
     405Server time: 2010-01-22 17:45 
     406User time: Fri Jan 22 2010 17:45:41 GMT+1100 
     407}}} 
     408 
     409Now, everything works except the fact that we cannot change any settings options we just created. So, we now proceed to creating a setting options form. 
     410 
     411 
     412== 3.3. Changing the settings form. == 
     413 
 
Below is the legacy version of the Boonex site, maintained for Dolphin.Pro 7.x support.
The new Dolphin solution is powered by UNA Community Management System.
Fork me on GitHub