|
Page 1 of 4 Part 2 of the component tutorial explaines all files creating backend interface for my sample JPortfSimple component:
- admin.jportfsimple.php
- admin.jportfsimple.html.php
- toolbar.jportfsimple.php
- toolbar.jportfsimple.html.php
- install.jportfsimple.php
- uninstall.jportfsimple.php
- jportfsimple.xml
which are placed in administrator/components/com_jportfsimple folder during installation. Well, .xml file was explained earlier (see p. 4).
7. BackendSimilarly to frontend, one file is responsible for "logic" of the component (it's backend interface) - admin.jportfsimple.php, and second file is displaying the information (admin.jportfsimple.html.php).
admin.jportfsimple.php // no direct access defined('_VALID_MOS') or die('Direct Access to this location is not allowed.'); // ensure user has access to this function if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' ) | $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_jportfsimple' ))) { mosRedirect( 'index2.php', _NOT_AUTH ); } global $mosConfig_live_site, $mosConfig_absolute_path, $mosConfig_lang, $mosConfig_list_limit; // load .class.php and .html.php files for the component require_once( $mainframe->getPath( 'admin_html' ) ); require_once( $mainframe->getPath( 'class' ) ); // get task and act from request $task = trim( mosGetParam ( $_REQUEST, 'task', '' )); $act = trim( mosGetParam ( $_REQUEST, 'act', '' )); // get selected objects $id = josGetArrayInts( 'cid' ); // load language file if (file_exists( $mosConfig_absolute_path. '/components/com_jportfsimple/lang/'. $mosConfig_lang. '.php')) include_once( $mosConfig_absolute_path.'/components/com_jportfsimple/lang/'.$mosConfig_lang.'.php'); else if (file_exists( $mosConfig_absolute_path. '/components/com_jportfsimple/lang/english.php')) include_once( $mosConfig_absolute_path.'/components/com_jportfsimple/lang/english.php'); // get configuration parameters $database->setQuery('SELECT * FROM #__jportfsimple_conf' ); $conf_rows = $database -> loadObjectList(); if ($database -> getErrorNum()) { echo $database -> stderr(); return false; } if ($conf_rows) { $jpConf = $conf_rows[0]; $base_path = $jpConf->base_path; }
First part of frontend file does the following: lines 12 to 16 - acl check - does logged in user has access to the component ?
19 to 21 - loads the required class and backend html (admin.jportfsimple.html.php) files 23 to 28 - gets variables defining what to do ($act, $task) and with which object ($id)
30 to 35 - loads language file, default is english.php in
com_jportfsimple/lang folder 37 to 47 - reads data from database table containing configuration parameters. They are stored in $jpConf object, and location of images in $base_path
Main backend "logic": admin.jportfsimple.php switch( $act ) { case 'configure': switch($task) { case 'save': Conf_save( $option ); break; default: Conf_list( $option ); break; } break; case 'categories': switch ($task) { case 'save' : Cat_save( $option ); break; case 'cancel' : Cat_cancel( $option, $act); break; case 'edit' : Cat_edit( $option, $id ); break; case 'new' : $id = ''; Cat_edit( $option, $id ); break; case 'delete' : Cat_del( $option, $id ); break; case 'publish' : Cat_publish( $option, '1', $id ); break; case 'unpublish' : Cat_publish( $option, '0', $id ); break; case 'orderup': ordercat ( intval( $id[0] ), -1, $option, $act ); break; case 'orderdown': ordercat ( intval( $id[0] ), 1, $option, $act ); break; default: Cat_list($option); break; } break; case 'projects': switch ( $task ) { case 'save' : Proj_save( $option ); break; case 'cancel' : Proj_cancel( $option, $act); break; case 'edit' : Proj_edit( $option, $id ); break; case 'new' : $id = ''; Proj_edit( $option, $id ); break; case 'delete' : Proj_del( $option, $id ); break; case 'publish' : Proj_publish( $option, '1', $id ); break; case 'unpublish' : Proj_publish( $option, '0', $id ); break; case 'orderup': orderproj ( intval( $id[0] ), -1, $option, $act ); break; case 'orderdown': orderproj ( intval( $id[0] ), 1, $option, $act ); break; default: Proj_list( $option ); break; } break; case 'info': HTML_jportfsimple::Info( $option ); break; default: break; }
Variable $act get it's value from chosen menu. You remember this part of xml file: <menu>jportfsimple</menu> <submenu> <menu act="categories">Categories</menu> <menu act="projects">Projects</menu> <menu act="configure">Configuration</menu> <menu act="info">About</menu> </submenu>
So for example, if Categories was clicked, $act would be 'categories', and then variable $task will be checked (line 64). Initially $task is empty, so the default action is performed: function Cat_list in line 95. Other functions are called when you click toolbar buttons. Configuration saving function:
admin.jportfsimple.php /** * Configuration saving */ function Conf_save( $option ) { global $database, $mosConfig_absolute_path, $jfiles; // no . in path, and must end in slash if (! (strpos($_POST['base_path'], '..')=== FALSE)) { $err2=_COM_JP_ERROR; $_POST['base_path']= 'images/stories/'; } if (substr($_POST['base_path'], -1, 1)!== '/') $_POST['base_path']= $_POST['base_path']. '/'; - $row = new jportfsimpleConf($database);
// bind it to the table if (!$row -> bind($_POST)) { echo "<script> alert('". $row-> getError(). "'); window.history.go(-1); </script>\n"; } // store it in the db if (!$row -> store()) { echo "<script> alert('". $row-> getError(). "'); window.history.go(-1); </script>\n"; } mosRedirect( 'index2.php?option='.$option.'&act=configure', 'Configuration Saved'.$err2 ); }
Function called after editing configuration parameters, when "Save" is clicked. First some specific for this component checks - if base_path variable containes two dots, then it's changed to default one; if it does not end with slash, one is added. Lines 159 to 172 - whichever object in Joomla! is being saved (from those defined in .class file) similar actions are performed:
- new object is created (line 159)
- bind - copies values from $_POST to the above object - if for example you have a field in a form with a name which is not in object's class, then this part will throw an error (line 162)
- storing to database - here also an error may show up, if for example you added field to .class file, but you didn't update database table
- if no errors, then component redirects to the page depending on act variable in mosRedirect function, with nice message "Configuration Saved"
Configuration listing function:
admin.jportfsimple.php /** * Configuration listing */ function Conf_list( $option ) { HTML_jportfsimple::Conf_list( $option ); HTML_jportfsimple::bottom(); }
It just calls display function, since configuration data is already in $jpConf variable (see line 45). Function publishing a category: admin.jportfimple.php /** * Category publishing */ function Cat_publish( $option, $publish=1 ,$cid ) { $action = $publish ? 'publish' : 'unpublish'; echo "<script> alert('Select an item to $action'); window.history.go(-1);</script>\n"; } $database->setQuery( 'UPDATE #__jportfsimple_categories SET published='.$publish.' WHERE id IN ('.$cids.')');
if (!$database->query()) { echo "<script> alert('". $database-> getErrorMsg(). "'); window.history.go(-1); </script>\n"; } mosRedirect( 'index2.php?option='.$option.'&act=categories' ); }
First it checks if at least one category is checked (line 192), if not - it displays popup window with error message (this is one exeption of the rule to not use output commands in this file). Then it runs a query to update 'published' column in jos_jportfsimple_categories table (line 198), and if no errors (line 201) it redirects back to categories page. Category saving function: admin.jportfsimple.php /** * Category saving */ function Cat_save( $option ) { $row = new jportfsimpleCategories($database); // bind it to the table if (!$row -> bind($_POST)) { echo "<script> alert('". $row-> getError(). "'); window.history.go(-1); </script>\n"; } // store it in the db if (!$row -> store()) { echo "<script> alert('". $row-> getError(). "'); window.history.go(-1); </script>\n"; } $row->updateOrder(); mosRedirect( 'index2.php?option='.$option.'&act=categories', 'Saved' ); }
As you see, it's almost the same as configuration saving function earlier, just one additional function updating order of all categories (line 225).
Category deleting function: admin.jportfsimple.php /** * Category deleting */ function Cat_del( $option, $cid ) { echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n"; } { $database->setQuery('DELETE FROM #__jportfsimple_categories WHERE id IN ('.$ids.')'); } if (!$database->query()) { echo "<script> alert('". $database-> getErrorMsg(). "'); window.history.go(-1); </script>\n"; } mosRedirect( 'index2.php?option='.$option.'&act=categories' ); }
Similar to category publishing function, just different query is used (line 242).
<< Start < Prev 1 2 3 4 Next > End >> |