I asked the question on stackoverflow. Nobody replied. Please help me find the solution.
My question is: I want to store user name when I save a record in administration back end.
http://stackoverflow.com/questions/1715 ... -component
I have a component being developed for joomla back end administration.
I have an edit view and contoller, model associated with it.
Whenever I edit or create a record, I want to get user name and store it to my table which has a field for current user which makes the update or create the record.
I know how to get user name as:
Code: Select all
$user = JFactory::getUser();
$uname = $user->name;
Code: Select all
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
$option = JRequest::getCmd('option');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<form action="<?php echo JRoute::_('index.php?option=com_reservation&view=tabletypes'); ?>" method="post" name="adminForm" id="tabletype-admin-form" class="form-validate">
<input type="hidden" name="option" value="<?=$option?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="id" value="<?=$this->item->id?>" />
<input type="hidden" name="upd_user" value="Test-ere" />
<?php echo JHtml::_('form.token'); ?>
<fieldset class="adminform">
<legend>DETAILS</legend>
<ul class="adminformlist">
<li><?php echo $this->form->getLabel('name'); ?>
<?php echo $this->form->getInput('name'); ?></li>
</ul>
</fieldset>
</form>
Code: Select all
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controlleradmin library
jimport('joomla.application.component.controllerform');
class <ComponentName>Controller<ViewName> extends JControllerForm
{
public function __construct($config = array())
{
$this->view_list = '<viewName>';
parent::__construct($config);
}
}
Code: Select all
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modeladmin' );
class <ComponentName>Model<ViewName> extends JModelAdmin
{
public function getForm($data = array(), $loadData = true)
{
// Get the form
$form = $this->loadForm('com_<componentname>.<viewname>', '<viewname>',
array('control' => 'jform', 'load_data' => $loadData));
if (!$form) {
return false;
}else{
return $form;
}
}
public function getTable($type = '<tableName>', $prefix = '<Prefix>', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
public function loadFormData()
{
// Load form data
$data = JFactory::getApplication()->getUserState('com_<componentname>.edit.<viewname>.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
}
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<form>
<fieldset>
<field
name="id"
type="hidden"
/>
<field
name="name"
type="text"
...
/>
<field
name="published"
type="combo"
...
</field>
<field
name="upd_user" (this is the field where i wanna store current user)
type="user"
/>
<field
name="upd_date"
type="hidden"
/>
</fieldset>
</form>
Code: Select all
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
jimport('joomla.database.table');
/**
* Table class
*/
class [ComponentName]Table<ViewName> extends JTable
{
/**
* Constructor
*
* @param object Database connector object
*/
function __construct(&$db)
{
parent::__construct('#__[db_table_name]', 'id', $db);
}
}