Elxis CMS Forum

Extensions => Components => Topic started by: jmous on March 20, 2012, 14:06:00

Title: Connecting component back-end toolbar buttons with respective tasks
Post by: jmous on March 20, 2012, 14:06:00
Hello all,
After reading the  Elxis Component Development Guide i still need help on how the tasks in the administrative part actually work.

I guess i need a little more on that (page 23) :
"When you click a button a JavaScript function is being executed that fills-in our form’s “task” input text
with the button’s task and submits the form. The tasks are “save” for the Save button and “cancel” for the Cancel button."

Suppose i need two forms and both forms load the same toolbar (cancel and save)
the first form is loaded when task=config
the second form is loaded when task=new
When the html part posts the form following a config\save or a new\save, what's the idea of understanding which save was pressed and continue with the appropriate function execution in the component's main class?

Thank you in advance
 
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: datahell on March 20, 2012, 19:36:54
Why you need 2 forms? This is crazy!
All edit pages consist by 1 form. The form should contain a hidden field named "task".
<input type="hidden" name="task" value="" />

When you click on a button having task "test", Elxis fills in the hidden field with this value and then submits the form.
<input type="hidden" name="task" value="test" />
In your main component file (admin.xxx.php) you should add a routing for this task:
if ($task == 'test') {
   doSomething();
}
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: jmous on March 20, 2012, 23:55:31
Thanks, maybe i wasn't clear enough
In my example the control panel has two options (links).
One link to the component's configuration page(link to page task=config)
One link to the component's add new item page (link to page task=new)
Both these pages (and not forms as i mistook before) load a save-cancel toolbar
If i understood well, when i press save in the configuration page the task executed in admin.xxx.php is
if($task=='save'){
 save configuration options to file
}
Now i am back in the control panel and move to the add new item page. Fill in the form and press save.
Do i need to override the value of the save button to something like  "save_item" so that in the admin.xxx.php
if($task=='save_item'){
 do something else
}

Or should i add an other hidden field (name='option') noting which page sent me the 'save' task
and in admin.xxx.php :
if($task=='save'){
     if($option=='config'){ do smthing}
     if($option=='new'){ do smthing else}
}
If what i am asking doesn't make sense maybe i am missing something more basic. 
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: apkoutsou on March 21, 2012, 12:42:27
There is a lot yet to do. The toolbar is controlled by zzz.toolbar.php and zzz.toolbar.html.php files. Study those two files. When you add a new toolbar item you set the task variable that will fill the hidden task input when form submitted. So you do not have to create a new input.

So you have to code:

if($task=='save'){
  // do something
} elseif($task=='save'){
   // do something
}elseif($option=='new'){
  // do something
}

Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: jmous on March 21, 2012, 13:23:14
Thanx a lot!
I just checked the toolbar files in some elxis core components and I guess what i need partly lies there.
I will study those and I am confident that I will figure out the rest.

PS: there is something i don't get in your code ( ???checking the condition $task='save' twice) - Not important as you gave me the right direction.
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: datahell on March 21, 2012, 19:08:03
Use different task names for different edit pages.

Example:
mosMenuBar::save(); //no task specified, the default "save" task will be applied
mosMenuBar::save('savearticle'); //task = savearticle
mosMenuBar::save('savecategory'); //task = savecategory
mosMenuBar::save('save_the_planet'); //task = save_the_planet

Here is a fully custom save button:
mosMenuBar::customX('saveitem', 'save.png', 'save_f2.png', $adminLanguage->A_SAVE, true ); //task = saveitem

Catch the current task and proceed to the requested action (file admin.xxx.php) :

Code: [Select]
<?php 
$task 
mosGetParam($_REQUEST, &#39;task&#39;, &#39;&#39;);
switch ($task) {
   case &
#39;save&#39;: doSave(); break;
   
case &#39;savearticle&#39;: doSaveArticle(); break;
   
case &#39;savecategory&#39;: doSaveCategory(); break;
   
case &#39;save_the_planet&#39;: doSaveThePlanet(); break;
   
case &#39;saveitem&#39;: saveItemPlease(); break;
   
default: pageNotFound(); break;
}
?>

Informational note for Elxis 4.0 Nautilus:
Elxis 4.0 does not uses option and task variables. It does not even use any POST/GET variables!
Elxis Nautilus routes the user request by analysing the URI segments.

I paste a part of the routing procedure of component Search

Code: [Select]
<?php 
private function makeRoute() {
$c count($this->segments);
if ($c == 0) { return; }
if ($c 1) {
exitPage::make(&#39;404&#39;, &#39;CSEA-0003&#39;);
}

if ($this->segments[0] == &#39;osdescription.xml&#39;) {
$this->controller = &#39;open&#39;;
$this->task = &#39;osDescription&#39;;
return;
}

$eng strtolower(str_ireplace(&#39;.html&#39;, &#39;&#39;, $this->segments[0]));
$eng filter_var($engFILTER_SANITIZE_STRINGFILTER_FLAG_STRIP_LOW FILTER_FLAG_STRIP_HIGH);
if ($eng != &#39;&#39;) { $this->engine = $eng; }
}

?>

And here is a sample on how you create buttons in Elxis Nautilus (no special file is required - buttons are generated on runtime)
Code: [Select]
<?php 
$toolbar 
$elxis->obj(&#39;toolbar&#39;);
$toolbar->add($eLang->get(&#39;SAVE&#39;), &#39;save&#39;, false, &#39;&#39;, &#39;elxSubmit(\&#39;save\&#39;);&#39;);
$toolbar->add($eLang->get(&#39;APPLY&#39;), &#39;saveedit&#39;, false, &#39;&#39;, &#39;elxSubmit(\&#39;apply\&#39;);&#39;);
$toolbar->add($eLang->get(&#39;CANCEL&#39;), &#39;cancel&#39;, false, $elxis->makeAURL(&#39;content:categories/&#39;));
?>
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: apkoutsou on March 21, 2012, 20:17:58
mosMenuBar::save('save_the_planet'); //task = save_the_planet

ROLF!!!!!

Anyway, it is always nice to have a sneak peek to Nautilus' functionallity... As everybody else here, I'm looking forward to the new version..!!
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: datahell on March 21, 2012, 22:24:43
For those interested I publish my work on Elxis 4.0 on my Twitter (https://twitter.com/#!/IsOpenSource).
Title: Re: Connecting component back-end toolbar buttons with respective tasks
Post by: jmous on March 22, 2012, 10:40:52
Thanks! Exactly what i needed to save (my) world!