SaveOrder doesn't work in backend - why?

184 views
Skip to first unread message

Grzesiek

unread,
May 16, 2012, 4:54:08 AM5/16/12
to Joomla! General Development
controllers/categories.php

<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.controlleradmin');
class gpLSControllerCategories extends JControllerAdmin
{
public function getModel($name = 'Category', $prefix = 'gpLS',
$config = array('ignore_request' => true))
{
$model = parent::getModel($name, $prefix, $config);
return $model;
}

}

controllers/category.php:
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.controllerform');
class gpLSControllerCategory extends JControllerForm
{
public function batch($model = null)
{
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

// Set the model
$model = $this->getModel('Category', '', array());

// Preset the redirect
$this->setRedirect(JRoute::_('index.php?
option=com_gpls&view=categories' . $this->getRedirectToListAppend(),
false));

return parent::batch($model);
}
}


models/categories.php:
<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import the Joomla modellist library
jimport('joomla.application.component.modellist');


class gpLSModelCategories extends JModelList {

public function __construct($config = array()) {
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
'id', 'c.id',
'categoryName', 'c.categoryName',
'shortName', 'c.shortName',
'state', 'c.published',
'ordering', 'c.ordering',
);
}

parent::__construct($config);
}

protected function populateState($ordering = null, $direction =
null) {
// Initialise variables.
$app = JFactory::getApplication('administrator');

// Load the filter state.
$search = $this->getUserStateFromRequest($this->context .
'.filter.search', 'filter_search');
$this->setState('filter.search', $search);

$state = $this->getUserStateFromRequest($this->context .
'.filter.state', 'filter_state', '', 'string');
$this->setState('filter.state', $state);

parent::populateState('c.categoryName', 'desc');
}

protected function getStoreId($id = '') {
// Compile the store id.
$id .= ':' . $this->getState('filter.search');
$id .= ':' . $this->getState('filter.state');

return parent::getStoreId($id);
}

protected function getListQuery() {
// Create a new query object.
$db = $this->getDbo();
$query = $db->getQuery(true);
$user = JFactory::getUser();

// Select the required fields from the table.
$query->select(
$this->getState(
'list.select', 'c.id, categoryName,
c.shortName, c.logo, c.published, c.ordering'
)
);
$query->from('#__gpls_categories AS c');

// Join over the users for message owner.
// Filter by published state.
$state = $this->getState('filter.state');
if (is_numeric($state)) {
$query->where('c.published = ' . (int) $state);
} elseif ($state === '') {
$query->where('(c.published IN (0, 1))');
}

// Filter by search in subject or message.
$search = $this->getState('filter.search');

if (!empty($search)) {
$search = $db->Quote('%' . $db->escape($search, true) .
'%', false);
$query->where('c.categoryName LIKE ' . $search . ' OR
c.shortName LIKE ' . $search);
}

// Add the list ordering clause.
// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering',
'c.categoryName');
$orderDirn = $this->state->get('list.direction', 'asc');
if ($orderCol == 'c.ordering' || $orderCol == 'ordering') {
$orderCol = 'c.categoryName ' . $orderDirn . ',
c.ordering';
}
//sqlsrv change
if ($orderCol == 'shortName')
$orderCol = 'c.shortName';
if ($orderCol == 'published')
$orderCol = 'c.published';
$query->order($db->escape($orderCol . ' ' . $orderDirn));
//echo nl2br(str_replace('#__','jos_',$query));
return $query;
}

protected $text_prefix = 'com_gpls';

public function getTable($type = 'Category', $prefix =
'gpLSTable', $config = array()) {
return JTable::getInstance($type, $prefix, $config);
}

public function getForm($data = array(), $loadData = true) {

}

}


models/category.php:
<?php
/**
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All
rights reserved.
* @license GNU General Public License version 2 or later; see
LICENSE.txt
*/

// No direct access.
defined('_JEXEC') or die;

jimport('joomla.application.component.modeladmin');

class gpLSModelCategory extends JModelAdmin
{
/**
* @var string The prefix to use with controller messages.
* @since 1.6
*/
protected $text_prefix = 'COM_GPLS';


/**
* Returns a reference to the a Table object, always creating it.
*
* @param type The table type to instantiate
* @param string A prefix for the table class name. Optional.
* @param array Configuration array for model. Optional.
* @return JTable A database object
* @since 1.6
*/
public function getTable($type = 'Category', $prefix = 'gpLSTable',
$config = array())
{
return JTable::getInstance($type, $prefix, $config);
}

/**
* Method to get the record form.
*
* @param array $data An optional array of data for the form to
interogate.
* @param boolean $loadData True if the form is to load its own data
(default case), false if not.
* @return JForm A JForm object on success, false on failure
* @since 1.6
*/
public function getForm($data = array(), $loadData = true)
{
// Initialise variables.
$app = JFactory::getApplication();

// Get the form.
$form = $this->loadForm('com_gpls.category', 'category',
array('control' => 'jform', 'load_data' => $loadData));
if (empty($form)) {
return false;
}

// Determine correct permissions to check.
if ($this->getState('category.id')) {
// Existing record. Can only edit in selected categories.
$form->setFieldAttribute('id', 'action', 'core.edit');
} else {
// New record. Can only create in selected categories.
$form->setFieldAttribute('id', 'action', 'core.create');
}

// Modify the form based on access controls.
if (!$this->canEditState((object) $data)) {
// Disable fields for display.
$form->setFieldAttribute('ordering', 'disabled', 'true');
$form->setFieldAttribute('state', 'disabled', 'true');
$form->setFieldAttribute('publish_up', 'disabled', 'true');
$form->setFieldAttribute('publish_down', 'disabled', 'true');

// Disable fields while saving.
// The controller has already verified this is a record you can
edit.
$form->setFieldAttribute('ordering', 'filter', 'unset');
$form->setFieldAttribute('state', 'filter', 'unset');
$form->setFieldAttribute('publish_up', 'filter', 'unset');
$form->setFieldAttribute('publish_down', 'filter', 'unset');
}

return $form;
}



/**
* Method to get a single record.
*
* @param integer The id of the primary key.
*
* @return mixed Object on success, false on failure.
* @since 1.6
*/
public function getItem($pk = null)
{
if ($item = parent::getItem($pk)) {
}

return $item;
}

/**
* Prepare and sanitise the table prior to saving.
*
* @since 1.6
*/
protected function prepareTable(&$table)
{
$date = JFactory::getDate();
$user = JFactory::getUser();

$table->categoryName = htmlspecialchars_decode($table-
>categoryName, ENT_QUOTES);
$table->shortName = JApplication::stringURLSafe($table->shortName);

if (empty($table->shortName)) {
$table->shortName = JApplication::stringURLSafe($table-
>categoryName);
}

if (empty($table->id)) {
// Set the values

// Set ordering to the last item if not set
if (empty($table->ordering)) {
$db = JFactory::getDbo();
$db->setQuery('SELECT MAX(ordering) FROM #__gpls_categories');
$max = $db->loadResult();

$table->ordering = $max+1;
}
}
else {
// Set the values
}
}

/**
* A protected method to get a set of ordering conditions.
*
* @param object A record object.
* @return array An array of conditions to add to add to ordering
queries.
* @since 1.6
*/
protected function getReorderConditions($table)
{
$condition = array();
$condition[] = 'id = '.(int) $table->id;
return $condition;
}
}

tables/category.php

<?php
defined('_JEXEC') or die;

class gpLSTableCategory extends JTable
{
public function __construct(&$db)
{
parent::__construct('#__gpls_categories', 'id', $db);
}

public function bind($array, $ignore = '')
{
return parent::bind($array, $ignore);
}

public function store($updateNulls = false)
{
// Attempt to store the user data.
return parent::store($updateNulls);
}


public function check()
{
return true;
}

}


view.html.php:
<?php
defined('_JEXEC') or die;

jimport('joomla.application.component.view');

class gpLiveStreamViewCategories extends JView
{
protected $items;
protected $pagination;
protected $state;

/**
* Display the view
*/
public function display($tpl = null)
{
$this->state = $this->get('State');
$this->items = $this->get('Items');
$this->pagination = $this->get('Pagination');

// Check for errors.
if (count($errors = $this->get('Errors'))) {
JError::raiseError(500, implode("\n", $errors));
return false;
}

parent::display($tpl);
}


}

default.php - place with save button:
<th width="10%">
<?php echo JHtml::_('grid.sort',
'JGRID_HEADING_ORDERING', 'c.ordering', $listDirn, $listOrder); ?>
<?php echo JHtml::_('grid.order', $this->items,
'filesave.png', 'categories.saveorder'); ?>
</th>

Main file controller.php:

<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class gpLSController extends JController
{
function display($cachable = false)
{
// set default view if not set
JRequest::setVar('view', JRequest::getCmd('view', 'cpanel'));
// call parent behavior
parent::display($cachable);
}


}

gpLS.php:
<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
$controller = JController::getInstance('gpLS');
require_once('../administrator/components/com_gpls/helpers/
common.php');
$controller->execute(JRequest::getCmd('task'));
$controller->redirect();


After press saveOrder button I'm getting an error:
Invalid controller name='categories', format=''

What I'm doing wrong?
I followed by tutorials on docs and looked at weblinks component and I
have no idea what is the problem.

Thanks.

Grzesiek

unread,
May 16, 2012, 7:19:32 AM5/16/12
to Joomla! General Development
Or maybe I have to write my own saveOrder, orderUp/Down functions,
because there is no way to use these functions in my custom component
(only in J's?)?
Reply all
Reply to author
Forward
0 new messages