Keith: Thanks that also looks fairly simple and maybe what I'm after.
Not sure I understand this 100%:
"You can put your functions in the model and call them from your view
but be
sure to block unauthorized access by filtering users else you will be
opening yourself up to people downloading your data without
authorization."
I may need somebody to walk me through some of this, just sort of
really got my head around MVC and I still get confused easily!
This is my model:
<?php
//Makes sure the code is being executed within Joomla only
defined( '_JEXEC' ) or die( 'Restricted access' );
//Pulls in JModel from the joomla framework
jimport('joomla.application.component.model');
//HonoursModelAll is declared as an extension of JModel
class HonoursModelStudentmanage extends JModel
{
var $_data = null; //Used to cache the results from query
var $_pagination = null;
var $_total = null;
var $_search = null;
var $_query = null;
function getData()
{
$pagination =& $this->getPagination();
if (empty($this->data)) {
$query = $this->buildSearch();
$this->_data = $this->_getList($query, $pagination->limitstart,
$pagination->limit);
}
return $this->_data;
}
function buildSearch()
{
if (!$this->_query) {
$search = $this->getSearch();
$status = $this->getStatus();
//Query Joins data from across 5 tables to produce
results shown in the student managment page
$this->_query = "SELECT s.user_id, s.matric_no, s.course_id,
c.title,
u1.name student_name, u1.email student_email, a.supervisor
supervisor_id,
u2.name
supervisor_name, a.project_id, a.status, d.title course_title
FROM jos_main_students s
LEFT
JOIN jos_users u1 ON
u1.id = s.user_id
LEFT
JOIN jos_main_allocations a ON a.student_id =
u1.id
LEFT
JOIN jos_main_course c ON
c.id = s.course_id
LEFT
JOIN jos_users u2 ON
u2.id = a.supervisor
LEFT
JOIN jos_main_projects d ON
d.id = a.project_id";
if ($search != '') {
$search = $this->_db->getEscaped( $search, true );
$this->_query .= " WHERE (
u1.name LIKE '%{$search}%') OR
(u1.email LIKE '%{$search}%') OR (
u2.name LIKE '%{$search}%') OR
(c.title LIKE '%{$search}%') ";
}
if ($status == 2) {
$this->_query .= " WHERE a.status='Not Allocated' OR a.status IS
NULL";
}
else if ($status == 3) {
$this->_query .= " WHERE a.status='Allocated'";
}
}
return $this->_query;
}
function getTotal()
{
if (!$this->_total) {
$query = $this->buildSearch();
$this->_total = $this->_getListCount($query);
}
return $this->_total;
}
function &getPagination() {
if(!$this->_pagination) {
jimport('joomla.html.pagination');
global $mainframe;
$this->_pagination = new JPagination($this->getTotal(),
JRequest::getVar('limitstart', 0), JRequest::getVar('limit',
$mainframe->getCfg('list_limit')));
}
return $this->_pagination;
}
function getSearch()
{
if (!$this->_search) {
global $mainframe, $option;
$search = $mainframe->getUserStateFromRequest( "$option.search",
'search', '', 'string' );
$this->_search = JString::strtolower($search);
}
return $this->_search;
}
function getStatus()
{
if (!$this->_status) {
global $mainframe, $option;
$status = $mainframe-
>getUserStateFromRequest( "$option.get_status", 'get_status', '',
'string' );
$this->_status = JString::strtolower($status);
}
return $this->_status;
}
}
The view:
<?php
//Makes sure the code is being executed within Joomla only
defined( '_JEXEC' ) or die( 'Restricted access' );
//Pulls in JView from the joomla framework
jimport( 'joomla.application.component.view');
//HonoursViewAll is declared as an extension of the JView class
class HonoursViewStudentmanage extends JView
{
function display($tpl = null)
{
//get() looks for the model that shares the same name as the this
view "all"
$rows =& $this->get('data'); //results are returned
$pagination =& $this->get('pagination');//get pagination info
$search = $this->get('search');//gets serch term
$status = $this->get('status');//gets status term in order to set
select box value to last selected option.
$this->assignRef('rows', $rows);// results assigned to the view
$this->assignRef('pagination', $pagination); //pagination info
assigned to view
$this->assign('search', $search);//search term assinged to view in
order to set search box to term last searched for
//create status select box array
$select_status = array(
array('value' => '1', 'text' => 'All'),
array('value' => '2', 'text' => 'Not Allocated'),
array('value' => '3', 'text' => 'Allocated')
);
//assign status array to generic select box
$select = JHTML::_('select.genericList', $select_status,
'get_status', 'class="inputbox" '. '', 'value', 'text', $status );
//assign status select box to the view
$this->assign('select', $select);
parent::display($tpl);//display() calls the output template
}
}
And the template:
<?php
//Makes sure the code is being executed within Joomla only
defined( '_JEXEC' ) or die( 'Restricted access' );
//Sets up toolbars and title in backend administration
JToolBarHelper::title( JText::_( 'Student Management' ),
'generic.png' );
JToolBarHelper::editList();
dump($this->rows);
//this is the backend form for displaying the list of avaible projects
?>
<form action="index.php" method="post" name="adminForm">
<table>
<tr>
<td>
Select Status:
<?php echo $this->select ?>
</td>
<td align="right">
Search:
<input type="text" name="search" value="<?php echo $this->search ?>"
id="search" />
<button type="submit">Go</button>
</td>
<td style="color:#F00; font-style:italic;">
<< Search by Name, Email, Course or Supervisor!
</td>
</tr>
</table>
<table class="adminlist">
<thead>
<tr>
<th width="3%">
</th>
<th width="5%">User_id</th>
<th width="10%">Name</th>
<th width="10%">Matric_no</th>
<th width="10%">Email</th>
<th width="20%">Course</th>
<th width="10%">Supervisor</th>
<th width="20%">Project_id</th>
<th width="7%">Status</th>
</tr>
</thead>
<?php
//Loop through the rows and display the results (includes a link to
each row via id)
jimport('joomla.filter.output');//used to filter ampersands
$k = 0;
for ($i=0, $n=count( $this->rows ); $i < $n; $i++)
{
$row = &$this->rows[$i];
$checked = JHTML::_('
grid.id', $i, $row->user_id );
$published = JHTML::_('grid.published', $row, $i );
//$link = 'index.php?option=com_honours&id=' . $row->user_id .
'&view=allocate';
$link = JFilterOutput::ampReplace( 'index.php?option=' . $option .
'&controller=studentmanage&task=edit&cid[]='. $row->user_id );
?>
<tr class="<?php echo "row$k"; ?>">
<td>
<?php echo $checked; ?>
</td>
<td align="center">
<?php echo '<a href="' . $link . '">'. $row->user_id . '</a>'; ?>
</td>
<td>
<?php echo '<a href="' . $link . '">'. $row->student_name. '</
a>'; ?>
</td>
<td align="center">
<?php echo $row->matric_no; ?>
</td>
<td align="left">
<?php echo $row->student_email; ?>
</td>
<td align="left" >
<?php echo $row->title; ?>
</td>
<td >
<?php
if($row->supervisor_name == null)
{
echo 'N/A';
}
else
{
echo $row->supervisor_name;
}
?>
</td>
<td >
<?php
if($row->course_title == null)
{
echo 'N/A';
}
else
{
echo $row->course_title;
}
?>
</td>
<td align="center" >
<?php
$status = $row->status;
if ($status ==null)
{
$status = 'Not Allocated';
}
echo $status;
?>
</td>
</tr>
<?php
$k = 1 - $k;
}
?><tfoot>
<tr>
<td colspan="9"><?php echo $this->pagination->getListFooter(); ?></td>
</tr>
</tfoot>
</table>
<?php echo JHTML::_( 'form.token' );//send token to ensure legit
request ?>
<input type="hidden" name="option" value="<?php echo $option;?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="studentmanage" />
<input type="hidden" name="boxchecked" value="0" />
</form>
I need to be able to generate an csv or xls file of whatever is
displayed currently by this view.