I have been using this documentation (the only one I can find on the web) to build a component: http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Introduction
I can understand it to a certain extent, however it does really lack any definition. The component I have created works to a degree, though I am having some wier problems.
Basically all I need the component to do is simply load a settings area to set a few values, and through it be able to change those values. Here is a breakdown of what I have:
A view for a form, loading in form data from database. toolbars setup for save/apply and cancel.
This loads with no errors, and according to all docs on joomla I have found, by initializing a JControllerForm instance with a JTable connected in the model, simple saving of forms should work automatically. However even though there is absolutely no reference anywhere in the code to a view with an s at the end (main view is tireapi, the forms always redirects to tireapis).
This brings up a 500 error, as there is no place set that has that view. The documentation does include a list for a view, however I have only one row I need to edit, so a list is pointless. I know parameters can be set to components rather then making a database field, however I am unable to find any documentation relating to it.
What I am looking for is direction on how to stop the component from redirecting to a none existent view, and properly save the data. Links to documentation that not only shows example code, but describes functions and how they work would be beneficial.
Here is some of the code, feel free to point out anything I might be completely overlooking (I am newer to creating components):
tireapi.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import joomla controller library
jimport('joomla.application.component.controller');
// Get an instance of the controller prefixed by TireAPI
$controller = JController::getInstance('TireAPI');
// Get the task
$jinput = JFactory::getApplication()->input;
$task = $jinput->get('task', "", 'STR' );
// Perform the Request task
$controller->execute($task);
// Redirect if set by the controller
$controller->redirect();
?>
controller.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
class TireAPIController extends JController{
function display($cachable = false){
// set default view if not set
$input = JFactory::getApplication()->input;
$input->set('view', $input->getCmd('view', 'TireAPI'));
// call parent behavior
parent::display($cachable);
}
}
?>
controllers/tireapi.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
class TireAPIControllerTireAPI extends JControllerForm{}
?>
models/tireapi.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modeladmin');
class TireAPIModelTireAPI extends JModelAdmin{
protected $settings; //define settings
public function getTable($type = 'TireAPI', $prefix = 'TireAPITable', $config = array()){
return JTable::getInstance($type, $prefix, $config);
}
public function getSettings(){ //grab settings from database
if(!isset($this->settings)){
$table = $this->getTable();
$table->load(1);
$this->settings = $table;
}
return $this->settings;
}
public function getForm($data = array(), $loadData = true){
// Get the form.
$form = $this->loadForm('com_tireapi.tireapi', 'tireapi',
array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)){
return false;
}
return $form;
}
protected function loadFormData(){
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_tireapi.edit.tireapi.data', array());
if (empty($data)){
$data = $this->getSettings();
}
return $data;
}
}
?>
tables/tireapi.php:
<?php
// No direct access
defined('_JEXEC') or die('Restricted access');
// import Joomla table library
jimport('joomla.database.table');
class TireAPITableTireAPI extends JTable
{
function __construct( &$db ) {
parent::__construct('#__tireapi', 'id', $db);
}
}
?>
views/tireapi/view.html.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
class TireAPIViewTireAPI extends JView{
function display($tpl = null){
$form = $this->get('Form');
$item = $this->get('Settings');
// Check for errors.
if(count($errors = $this->get('Errors'))){
JError::raiseError(500, implode('<br />', $errors));
return false;
}
// Assign data to the view
$this->item = $item;
$this->form = $form;
$this->addToolBar();
// Display the template
parent::display($tpl);
}
protected function addToolBar() {
$input = JFactory::getApplication()->input;
JToolBarHelper::title(JText::_('COM_TIREAPI_MANAGER_TIREAPIS'));
JToolBarHelper::apply('tireapi.apply');
JToolBarHelper::save('tireapi.save');
JToolBarHelper::cancel('tireapi.cancel');
}
}
?>
views/tireapi/tmpl/default.php:
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
// load tooltip behavior
JHtml::_('behavior.tooltip');
?>
<form action="<?php echo JRoute::_('index.php?option=com_tireapi&layout=edit&id='.(int) $this->item->id); ?>"
method="post" name="adminForm" id="tireapi-form">
<fieldset class="adminform">
<legend><?php echo JText::_( 'COM_TIREAPI_DETAILS' ); ?></legend>
<ul class="adminformlist">
<?php foreach($this->form->getFieldset() as $field): ?>
<li><?php echo $field->label;echo $field->input;?></li>
<?php endforeach; ?>
</ul>
</fieldset>
<div>
<input type="hidden" name="task" value="tireapi.edit" />
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
These are all the files I can think of that may matter, let me know if I should include anymore.
UPDATE: Now I can get the redirect problem to stop, however it will not save data. I get this error: You are not permitted to use that link to directly access that page (#1).
This is the last hurdle to get this extremely basic admin feature to work. Any ideas? To clarify, I set the forms though an xml file and that loads properly, even filling them with the proper data from database. However when I click "apply" it just directs me back to the form with the error listed above, without saving.
The main question that you left open is where do you want it to redirect? Joomla by default redirects to the list view (by adding an 's' to the view name unless you specify a list view directly).
You can override this in a couple of ways:
In your controller (controllers/tireapi.php), set your own list view. I think you can even make this the same view:
function __construct() {
$this->view_list = 'tireapi';
parent::__construct();
}
Override the save function to change the redirect that happens after save (again in the controller). This works by changing the redirect that happens naturally to something else:
public function save($key = null, $urlVar = null) {
$return = parent::save($key, $urlVar);
$this->setRedirect(JRoute::_('index.php?option=com_tireapi&view=tireapi'));
return $return;
}
One of those should do the trick for you.
** UPDATE
To get the item to checkout initially you will want to change how your component handles no view being set. Right now you just set the view, instead, let's redirect! Updated controller.php is below.
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
class TireAPIController extends JController{
function display($cachable = false){
// set default view if not set
$input = JFactory::getApplication()->input;
$view = $input->get('view');
if (!$view) {
JFactory::getApplication()->redirect('index.php?option=com_tireapi&task=tireapi.edit&id=1');
exit();
}
// call parent behavior
parent::display($cachable);
}
}
?>
NOTE: This will work really poorly if more than one person needs to edit this, since the system checks it out when you open the component if you also have it redirect back to this page after saving. Because then it will always be checked out to the last person that edited it so the next person won't be able to open it. If only one person edits it, it will be fine.
SECOND NOTE: If you don't want to hack the checkout system you can also ignore it during the save process, which is basically the same level of hacking.
Below is a copy of the save function from controllerform.php in libraries/joomla/application/component/
. This is what is running on save normally (because of where you inherit from. I have removed the check if the item is checked out. So if you put this in your tireapi.php controller where the parent::save...
bit is, it will run instead and you don't have to bother with checking the item out (i.e. ignore all the comments...). (Honestly, in your case, you can probably delete a lot more of this, but this is what is happening on save, btw!)
public function save($key = null, $urlVar = null)
{
// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
// Initialise variables.
$app = JFactory::getApplication();
$lang = JFactory::getLanguage();
$model = $this->getModel();
$table = $model->getTable();
$data = JRequest::getVar('jform', array(), 'post', 'array');
$checkin = property_exists($table, 'checked_out');
$context = "$this->option.edit.$this->context";
$task = $this->getTask();
// Determine the name of the primary key for the data.
if (empty($key))
{
$key = $table->getKeyName();
}
// To avoid data collisions the urlVar may be different from the primary key.
if (empty($urlVar))
{
$urlVar = $key;
}
$recordId = JRequest::getInt($urlVar);
// Populate the row id from the session.
$data[$key] = $recordId;
// The save2copy task needs to be handled slightly differently.
if ($task == 'save2copy')
{
// Check-in the original row.
if ($checkin && $model->checkin($data[$key]) === false)
{
// Check-in failed. Go back to the item and display a notice.
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
return false;
}
// Reset the ID and then treat the request as for Apply.
$data[$key] = 0;
$task = 'apply';
}
// Access check.
if (!$this->allowSave($data, $key))
{
$this->setError(JText::_('JLIB_APPLICATION_ERROR_SAVE_NOT_PERMITTED'));
$this->setMessage($this->getError(), 'error');
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_list
. $this->getRedirectToListAppend(), false
)
);
return false;
}
// Validate the posted data.
// Sometimes the form needs some posted data, such as for plugins and modules.
$form = $model->getForm($data, false);
if (!$form)
{
$app->enqueueMessage($model->getError(), 'error');
return false;
}
// Test whether the data is valid.
$validData = $model->validate($form, $data);
// Check for validation errors.
if ($validData === false)
{
// Get the validation messages.
$errors = $model->getErrors();
// Push up to three validation messages out to the user.
for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++)
{
if ($errors[$i] instanceof Exception)
{
$app->enqueueMessage($errors[$i]->getMessage(), 'warning');
}
else
{
$app->enqueueMessage($errors[$i], 'warning');
}
}
// Save the data in the session.
$app->setUserState($context . '.data', $data);
// Redirect back to the edit screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
return false;
}
// Attempt to save the data.
if (!$model->save($validData))
{
// Save the data in the session.
$app->setUserState($context . '.data', $validData);
// Redirect back to the edit screen.
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
return false;
}
// Save succeeded, so check-in the record.
if ($checkin && $model->checkin($validData[$key]) === false)
{
// Save the data in the session.
$app->setUserState($context . '.data', $validData);
// Check-in failed, so go back to the record and display a notice.
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
return false;
}
$this->setMessage(
JText::_(
($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
? $this->text_prefix
: 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
)
);
// Redirect the user and adjust session state based on the chosen task.
switch ($task)
{
case 'apply':
// Set the record data in the session.
$recordId = $model->getState($this->context . '.id');
$this->holdEditId($context, $recordId);
$app->setUserState($context . '.data', null);
$model->checkout($recordId);
// Redirect back to the edit screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend($recordId, $urlVar), false
)
);
break;
case 'save2new':
// Clear the record id and data from the session.
$this->releaseEditId($context, $recordId);
$app->setUserState($context . '.data', null);
// Redirect back to the edit screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_item
. $this->getRedirectToItemAppend(null, $urlVar), false
)
);
break;
default:
// Clear the record id and data from the session.
$this->releaseEditId($context, $recordId);
$app->setUserState($context . '.data', null);
// Redirect to the list screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_list
. $this->getRedirectToListAppend(), false
)
);
break;
}
// Invoke the postSave method to allow for the child class to access the model.
$this->postSaveHook($model, $validData);
return true;
}