|
Page 2 of 8
admin.dailymessage.php
This file controls
which screen the administrator sees when configuring the component. Most of the
actual HTML is generated through the HTML_joeDailyMessage [there's something to
be said for narcissism :) ] found in the admin.dailymessage.html.php file.
<?php
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_dailymessage' ))) {
mosRedirect(
'index2.php', _NOT_AUTH );
}
This performs a
check to make sure that the user logged in is the administrator.
require_once(
$mainframe->getPath( 'admin_html' ) );
require_once(
$mainframe->getPath( 'class' ) );
These two lines
include the files admin.dailymessage.html.php and dailymessage.class.php. The
getPath() function returns the appropriate full paths and filenames.
$id =
mosGetParam( $_REQUEST, 'cid', array(0) );
if
(!is_array( $id )) {
$id
= array(0);
}
The main screen
has a form with a list of checkboxes bearing the name 'cid.' The mosGetParam()
function retrieves this array and stores it in $id. If the array is not present
(such as when the first page loads), $id is set to an empty array to prevent
errors later.
switch($act)
{
case
"configure":
switch($task)
{
case
"save":
saveConfiguration($option);
break;
default:
listConfiguration($option);
break;
}
break;
default:
switch
($task) {
case
"save" :
save($option);
break;
case
"edit" :
edit(
$option, $id );
break;
case
"new" :
$id
= '';
edit(
$option, $id);
break;
case
"delete" :
del($option,
$id);
break;
case
"publish" :
publishMessage($option,
'1', $id);
break;
case
"unpublish" :
publishMessage($option,
'0', $id);
break;
case
"listMessages" :
default:
listMessages($option);
break;
}
break;
}
Now would be a
good time to discuss the difference between $task and $act. When the
administrator clicks on a button such as "publish" or "save," the selection must
be communicated to the component. This is accomplished through the variable
$task. These buttons are used for many different forms and it is necessary for
the component to know which form is being dealt with at the moment. This is
where the variable $act comes in. On the main components menu, the selection for
Daily Message has two submenu options: Edit Messages and Configure. These
options are referred to by $act as 'all' and 'configure,' respectively.
The Daily Message
component uses nested switch statements to determine which course of action to
take. The outer switch statement is based on the $act variable, as both 'all'
and 'configure' use overlapping task names. So the value of $act is first
determined, then $task.
function
saveConfiguration($option) {
global
$database;
$row
= new joeDailyMessageConf($database);
Now the definition
of functions to handle various tasks begins. This first one will update the
database with new variables when someone clicks the 'save' button on the
configuration page. First, the $option variable is passed to the function and
should be set to 'com_dailymessage' by the Joomla environment. This will allow
the code to later forward the user back to the administration panel for this
component. Next, the $database object is pulled from outside the function. Then
we declare an object $row of type joeDailyMessageConf [defined in
dailymessage.class.php], which is an extension of mosDBTable. Objects based on
mosDBTable have many functions simplifying the process of recording information
into the database.
//
bind it to the table
if
(!$row -> bind($_POST)) {
echo
"<script> alert('"
.$row
-> getError()
."');
window.history.go(-1); </script>n";
exit();
}
This code attempts
to use the inherited mosDBTable function bind() to get the form variables from
the post and copy them into the $row object. If the attempt is unsuccessful (for
instance, the variable names do not match or are of the wrong type), a
Javascript error is displayed and the code is halted. The Javascript error is
displayed as a popup alert with a button that returns to the previous page.
//
store it in the db
if
(!$row -> store()) {
echo
"<script> alert('"
.$row
-> getError()
."');
window.history.go(-1); </script>n";
exit();
}
This piece of code
attempts to store the information in the $row object into the database. Error
handling is similar to that of the section above.
mosRedirect("index2.php?option=$option&act=configure",
"Configuration Saved");
}
Finally, if
everything is successful, the user is redirected back to the configuration page
and the message "Configuration Saved" is displayed at the top of the screen in
friendly orange lettering. The variable $option, passed to the function from
before, contains the string 'com_dailymessage' and tells Joomla to pull up the
administration panel for the Daily Message component. Notice that the URL has
the 'act' variable set to 'configure.' If this is not done, the user will be
forwarded back to the default Daily Message administration page: message
editing.
function
listConfiguration($option)
{
global
$database;
$database->setQuery("SELECT
* FROM mos_joe_dailymessage_conf" );
$rows
= $database -> loadObjectList();
This function
loads the current configuration from the database and displays it on a form
where the variables can be edited to suit the desires of the administrator. The
appropriate query is called and the result is loaded into the object $rows as a
list of objects.
if
($database -> getErrorNum()) {
echo
$database -> stderr();
return
false;
}
If there was an
error processing the query, this code will display that error and stop the
function, yet still allowing Joomla to finish loading the page without the
configuration data.
HTML_joeDailyMessage::listConfiguration($option,
$rows);
}
This calls the
function listConfiguration from the object HTML_joeDailyMessage from the
dailymessage.class.php file, passing along the component name and object with
configuration information.
function
publishMessage( $option, $publish=1 ,$cid )
{
global
$database, $my;
if
(!is_array( $cid ) || count( $cid ) < 1) {
$action
= $publish ? 'publish' : 'unpublish';
echo
"<script> alert('Select an item to $action');
window.history.go(-1);</script>n";
exit;
}
When the
administrator wants to publish a message, this function sets the 'published'
flag on that message to '1.' First, we determine whether or not the
administrator chose items to publish. If not, an error must be displayed
referring back to the previous page. The line where $action is set is a
shorthand version of an 'if' statement. The variable is set to 'publish' if it
is true that $publish equals 1, otherwise, the variable is set to 'unpublish.' A
Javascript alert is displayed, forwarding the user back.
$cids
= implode( ',', $cid );
If we have items
to publish, we set the string $ids to be a list of all the variables in the
array $id and separate the variables with commas. This will simplify insertion
into the SQL statement.
$database->setQuery(
"UPDATE mos_joe_dailymessage SET published='$publish'"
.
"nWHERE id IN ($cids)"
);
if
(!$database->query()) {
echo
"<script> alert('".$database->getErrorMsg()."'); window.history.go(-1);
</script>n";
exit();
}
A query is sent to
the database and any errors are displayed through Javascript.
mosRedirect(
"index2.php?option=$option" );
}
Finally, the
administrator is forwarded back to the main page of the Daily Message
administration interface.
function
save($option) {
global
$database;
$row
= new joeDailyMessage($database);
//
bind it to the table
if
(!$row -> bind($_POST)) {
echo
"<script> alert('"
.$row
-> getError()
."');
window.history.go(-1); </script>n";
exit();
}
//
store it in the db
if
(!$row -> store()) {
echo
"<script> alert('"
.$row
-> getError()
."');
window.history.go(-1); </script>n";
exit();
}
mosRedirect("index2.php?option=$option",
"Saved");
}
This function is
nearly identical to saveConfiguration(), only it declares row to be of type
joeDailyMessage and redirects to the default (message editing) page with the
message "Saved."
function
del($option, $cid) {
global
$database;
if
(!is_array($cid) || count($cid) < 1) {
echo
"<script> alert('Select an item to delete');
window.history.go(-1);</script>n";
exit();
}
A list of daily
messages to delete is passed to this function. If there are no items in the
array $cid [or $cid is not an array], an appropriate error message is displayed
and the administrator is forwarded back to the previous page.
if
(count($cid))
{
$ids
= implode(',', $cid);
$database->setQuery("DELETE
FROM mos_joe_dailymessage nWHERE id IN ($ids)");
}
If messages to be
deleted have been selected, set a up a query that will remove them from the
database.
if
(!$database->query()) {
echo
"<script> alert('"
.$database
-> getErrorMsg()
."');
window.history.go(-1); </script>n";
}
mosRedirect("index2.php?option=$option");
}
The deletion query
is executed and the administrator is forwarded back to the default screen.
function
edit($option, $uid) {
global
$database;
$row =
new joeDailyMessage($database);
if($uid){
$row
-> load($uid[0]);
}
When the
administrator selects a daily message to edit, this function loads up the
appropriate message from the database into the $row object. Since the
administrator can only edit one message at a time, the first id in the $uid
array [0] is chosen.
HTML_joeDailyMessage::edit($option,
$row);
}
The edit()
function is called from the HTML_joeDailyMessage class, which generates the
appropriate HTML output for the screen. The database row and the current
component name (com_dailymessage) are passed so that appropriate code can be
generated.
function
listMessages($option) {
global
$database;
$database->setQuery("SELECT
* FROM mos_joe_dailymessage ORDER BY id" );
This final
function is called to display the default screen. If no task has been chosen,
this function will list all of the daily messages so that the administrator can
edit, delete, and publish/unpublish them. The query is set to retrieve the
entire contents of the daily message table. They are ordered by id as that field
is set as an automatically incrementing field. This way, unique keys can be
generated and messages will be listed in the order of creation.
$rows
= $database -> loadObjectList();
if
($database -> getErrorNum()) {
echo
$database -> stderr();
return
false;
}
HTML_joeDailyMessage::listMessages($option,
$rows);
}
?>
The query is
executed and if there are any errors, they are simply written on the resulting
page. The listMessages() function in the HTML_joeDailyMessage class is then
called to generate the necessary HTML for the list screen.
|