JToolBarHelper::apply

359 views
Skip to first unread message

HGraca

unread,
Jul 27, 2011, 3:45:24 PM7/27/11
to Joomla! General Development
Hi,

I'm making the back-office for my component and I'm trying to code the
buttons.

I have the toolbar in place, with the title and a button.
I'm doing this in the view, with the following code:

JToolBarHelper::title(JText::_('COM_INSCRICAOEXAMES_ADMINISTRATION'),
'icon');
JToolBarHelper::apply("grupos.setGroups", "salvar");

Under the button the word is "salvar", according to the previous code,
however, I cant figure out where to put the code that it should do. I
read that I should make a task called "setGroups" in a controller
called "grupos".

So I made it in: administrator/components/com_inscricaoexames/
controllers/grupos.php
with the class name: inscricaoexamesControllerGrupos
and a function called: public function setGroups(){...}

I have tried several other things and it just doesn’t work. The rest
of the code is fine coz I've tested it in other ways.

Tkx in advance for your help.

Herb

Swapnil Shah

unread,
Jul 27, 2011, 4:47:08 PM7/27/11
to joomla-de...@googlegroups.com
Is your sub controller extending Jcontroller? Or another?

Did you look at the wiki about adding "backend actions"?

Sent from my iPhone

> --
> You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
> To post to this group, send an email to joomla-de...@googlegroups.com.
> To unsubscribe from this group, send email to joomla-dev-gene...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/joomla-dev-general?hl=en-GB.
>

Sam Moffatt

unread,
Jul 27, 2011, 5:40:27 PM7/27/11
to joomla-de...@googlegroups.com
What version of Joomla! are you developing on?

Cheers,

Sam Moffatt
http://pasamio.id.au

HGraca

unread,
Jul 28, 2011, 5:33:54 AM7/28/11
to joomla-de...@googlegroups.com
Hi,

It's extending JControllerAdmin, coz that's what I saw in the tutorial

http://docs.joomla.org/Developing_a_Model-View-Controller_%28MVC%29_Component_for_Joomla!1.6_-_Part_09
(is this the wiki you were referring to?)

In that tut, they make 2 controllers, and each of them extend JControllerAdmin and JControllerForm but they don’t explain why nor the difference...

@Samuel Moffatt, I'm using version 1.6.

tkx for your reply.

HGraca

unread,
Jul 28, 2011, 5:47:20 AM7/28/11
to joomla-de...@googlegroups.com
OK,
I was missing the last lines in the layout form:

    <input type="hidden" name="task" value="" />
    <input type="hidden" name="boxchecked" value="0" />

however,
  1. I dont understand what is the line <input type="hidden" name="boxchecked" value="0" />, why is it needed?
  2. Now, I my form is executing the main admin controller, however, how can I make it go to a secondary controller, i.e., a controller in the controllers folder ?
Tkx
Message has been deleted

HGraca

unread,
Jul 28, 2011, 6:36:00 AM7/28/11
to joomla-de...@googlegroups.com
I found the problem, but I dont have the solution.

In my admin entry file (admin.inscricaoexames.php) I have

// Require specific controller if requested
    dump(JRequest::getVar('controller'),'$controller');
    dump(JPATH_COMPONENT_ADMINISTRATOR,'JPATH_COMPONENT_ADMINISTRATOR');
if($controller = JRequest::getVar('controller')) {
    $path = JPATH_COMPONENT_ADMINISTRATOR.DS.'controllers'.DS.$controller.'.php';
    dump($path,'path');
    if (file_exists($path)) {
        require_once $path;
    }
    else {
        $controller = '';
    }
}

So this makes it go get the specific controller, BUT when I make the dump(JRequest::getVar('controller'),'$controller'); , it says its "", even though in the view I have JToolBarHelper::apply("grupos.setGroups", "salvar");

My question is, how do I make the form send the controller name? In the wiki it doesnt say anything more than what I have done already... (or at least that’s what I think...)

I'm sending an atached zip of the admin side.

Tkx.
admin.zip

Yurii Korotia

unread,
Jul 28, 2011, 8:09:26 AM7/28/11
to Joomla! General Development
my guess, you can use

<input type="hidden" name="task" value="{controller.task}" />

or
<input type="hidden" name="controller" value="{controller}" />
<input type="hidden" name="task" value="{task}" />

or

?bla-blabla&task={task}&controller={controller}

if I got it right. It still is html

HGraca

unread,
Jul 28, 2011, 8:33:32 AM7/28/11
to joomla-de...@googlegroups.com
Tkx for your reply.

Yes, I understand that, but that's not the way it's supposed to work... I mean, I shouldn’t have to do that.
Anyway, it would work if I have only one button, but if I have 2 buttons and each of them have different controllers, your workaround wont work any more coz it will send always to the same controller...

If I put this: JToolBarHelper::apply("grupos.setGroups", "salvar"); 
in my view, then it should create a button in the toolbar (which is creating), that would send the data to controller grupos, task setGroups.
And its not... :(

El KuKu

unread,
Jul 28, 2011, 9:34:48 AM7/28/11
to joomla-de...@googlegroups.com
Yes you are right, this is the way it is supposed to work.
I can not see anything wrong with your code.. are you sure the code in the controller method does not get executed ?
What i noted is that you don't like the curly braces {} - that makes the code quite vulnerable for errors - especially on foreach ;)

Why not put a simple
jexit('tudo de bom');
on top of your method and see if it works ;)

BTW: The reason why you wont see a 'controller' item in your request is that it is passed via the 'task' and later separated in controller and task ;)

Hope that helps a bit..

Saludos,
Nikolai

HGraca

unread,
Jul 28, 2011, 4:25:28 PM7/28/11
to joomla-de...@googlegroups.com
tkx for your reply.

yes, I was sure it was not beeing executed.
Ho, I like curly braces, I know what you are talking about, but sometimes I get lazy... :P

I didnt get that the controller.task was being sent in the task. Tkx for that info! This helped a lot.

In my admin.inscricaoexames.php I added this:

// separate the controler from the task, if necessary
$vars = explode(".",JRequest::getVar('task'));
If (count($vars)>1){
    JRequest::setVar('controller',$vars[0]);
    JRequest::setVar('task',$vars[1]);
}

This way if the task is of type controler.task they will be separated, if not it will not change a thing.

It bugs me that this is not in the documentation, at least, not in the wiki and none that I have found.

Anyway, in case someone is interested, my admin entry point is now this:
admin.inscricaoexames.php
// no direct access
defined('_JEXEC') or die('Restricted access');

// Require the base controller
require_once (JPATH_COMPONENT.DS.'controller.php');

$document = JFactory::getDocument();
$cssFile = JURI::base(true).'/components/com_inscricaoexames/css/inscricaoexames.css';
$document->addStyleSheet($cssFile, 'text/css', null, array());

// separate the controler from the task, if necessary
$vars = explode(".",JRequest::getVar('task'));
If (count($vars)>1){
    JRequest::setVar('controller',$vars[0]);
    JRequest::setVar('task',$vars[1]);

}

// Require specific controller if requested   
if($controller = JRequest::getVar('controller')) {
    $path = JPATH_COMPONENT_ADMINISTRATOR.DS.'controllers'.DS.$controller.'.php';
    if (file_exists($path)) {
        require_once $path;
    }
    else {
        $controller = '';
    }
}

// Create the controller
$classname    = 'inscricaoexamesController'.$controller;
$controller = new $classname( );

// Perform the Request task
$controller->execute( JRequest::getVar('task'));

// Redirect if set by the controller
$controller->redirect();

again, thank you all for your tips.

herb.

El KuKu

unread,
Jul 28, 2011, 5:07:50 PM7/28/11
to joomla-de...@googlegroups.com
This should not be necessary.. Using JController::getInstance('...') you should get the correct (sub)controller without exploding it yourself ;)

Sam Moffatt

unread,
Jul 28, 2011, 5:08:19 PM7/28/11
to joomla-de...@googlegroups.com
If you're running 1.6 then this should work:

// no direct access
defined('_JEXEC') or die('Restricted access');

$document = JFactory::getDocument();


$cssFile = JURI::base(true).'/components/com_inscricaoexames/css/inscricaoexames.css';
$document->addStyleSheet($cssFile, 'text/css', null, array());

// Get the Controller
$controller = JController::getInstance('inscricaoexames');

// Perform the Request task
$controller->execute( JRequest::getVar('task'));

// Redirect if set by the controller
$controller->redirect();

Instead of manually creating an instance of the controller, you should
be able to defer to JController::getInstance.

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.

> To view this discussion on the web, visit
> https://groups.google.com/d/msg/joomla-dev-general/-/K9Fum3rYDqsJ.

HGraca

unread,
Jul 28, 2011, 5:37:18 PM7/28/11
to Joomla! General Development
That's it!

just missing the

// Include dependencies
jimport('joomla.application.component.controller');

that's the right way to do it!

tkx a lot! :)

On 28 Jul, 22:08, Sam Moffatt <pasa...@gmail.com> wrote:
> If you're running 1.6 then this should work:
>
> // no direct access
> defined('_JEXEC') or die('Restricted access');
>
> $document = JFactory::getDocument();
> $cssFile = JURI::base(true).'/components/com_inscricaoexames/css/inscricaoexames.css';
> $document->addStyleSheet($cssFile, 'text/css', null, array());
>
> // Get the Controller
> $controller = JController::getInstance('inscricaoexames');
>
> // Perform the Request task
> $controller->execute( JRequest::getVar('task'));
>
> // Redirect if set by the controller
> $controller->redirect();
>
> Instead of manually creating an instance of the controller, you should
> be able to defer to JController::getInstance.
>
> Cheers,
>
> Sam Moffatthttp://pasamio.id.au

Nikolai Plath

unread,
Jul 28, 2011, 7:50:47 PM7/28/11
to joomla-de...@googlegroups.com
Am 28.07.2011 16:08, schrieb Sam Moffatt:
> $controller->execute( JRequest::getVar('task'));

RING RING - alarmbells all over the place - Sorry Sam but this was the
source of a bunch of hacks in J 1.5...

Please use only:

$controller->execute(JRequest::getCmd('task'));


Sam Moffatt

unread,
Jul 28, 2011, 8:12:06 PM7/28/11
to joomla-de...@googlegroups.com
Yeah good catch. Didn't notice that when I copied the code and put in
the JController call.

Cheers,

Sam Moffatt
http://pasamio.id.au

> --
> You received this message because you are subscribed to the Google Groups
> "Joomla! General Development" group.

HGraca

unread,
Jul 29, 2011, 4:45:25 AM7/29/11
to Joomla! General Development
cool! something more I've learned!

however, where do you guys learn about that? I haven’t found any
documentation explaining those kind of things... :(

On 29 Jul, 01:12, Sam Moffatt <pasa...@gmail.com> wrote:
> Yeah good catch. Didn't notice that when I copied the code and put in
> the JController call.
>
> Cheers,
>
> Sam Moffatthttp://pasamio.id.au
Reply all
Reply to author
Forward
0 new messages