Welcome, Guest
Username: Password: Remember me

TOPIC: Passing a variable in the save controller

Passing a variable in the save controller 05 Oct 2015 11:58 #13570

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
So I have a button
<button onclick="return Joomla.submitform('bid.apply');" class="btn btn-success">Start Bid</button>

which fires the controller function in public function save
case 'createbid.apply':
                $this->applyRedirection($result, array(
                    'stay',
                    'com_mycomponent.bid.createbid'
                ), array(
                    'cid[]' => $model->getState('bid.id')
                ));
                break;

What I want to do is pass information BACK via this save function.. i.e step=2
<button onclick="return Joomla.submitform('bid.apply', 'step-2');" class="btn btn-success">Start Bid</button>

So something like

case 'createbid.apply':
$this->applyRedirection($result, array(
'stay',
'com_bidslate.bid.createbid'
), array(
'cid[]' => $model->getState('bid.id'),
'step' => $step
));
break;

I suppose I could just create a field in the database for temporary use called step, but I was wondering if there was another way of doing it beyond, for example, using a localStorage variable?
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Passing a variable in the save controller 07 Oct 2015 08:34 #13579

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
You can use json for this, I will create an example.
It has some non-forkable code, because you need a controller.json.php, which is not generated by Cook but has to be in controllers folder in order to work.

I will assume some names in the code, you will probably have to change it and there will also be some typos ;)

First: create a bids.json.php file in the controllers folder (site) of your component (I will assume com_bids)
Then paste this code
<?php

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

class BidsCkControllerBids extends BidsClassControllerList
{
	public function createbid() {
        try
        {   
        	//you can get inputs from the calling code:
					//$var1 = JFactory::getApplication()->input->get('var1','','string'));
					//$var2 = (int)JFactory::getApplication()->input->get('var2,0,'int'));
                    
					$data = array();
					$table = JTable::getInstance('bid', 'BidsTable');
					//set your properties
					$table->created_by = (int)JFactory::getUser()->get('id');
					$table->creation_date = XplogHelperDates::getSqlDate(date("Y-m-d H:i:s"), array('Y-m-d H:i:s'), true);
					//etc.....
					
					if ($table->check()) //you can create a custom check if you need it
					{
						if ($table->store())
						{
							$inserted_id = $table->getDBO()->insertid();
						}
					}
					$step = 2; //or get some next step 
					$data["step"] = $step;
					$data["bidID"] = $inserted_id;
					
					echo new JResponseJson($data);
					
        }
        catch(Exception $e)
        {
            echo new JResponseJson($e);
        }
	}
}

// Load the fork
BidsHelper::loadFork(__FILE__);

// Fallback if no fork has been found
if (!class_exists('BidsControllerBids')){ class BidsControllerBids extends BidsCkControllerBids{} }
The code above will return {"success":true,"message":null,"messages":null,"data":{"step":2,"bidID":42}}
See also: docs.joomla.org/JSON_Responses_with_JResponseJson

Next, fork the tpml file (you probably already did that and add javascript:
<script type="text/javascript">
function CreateBid() {
	jQuery.ajax({
		type:"POST",
		url: "index.php?option=com_bids&task=bids.createbid&format=json",
		//data: if you need it, can be something like 'var1='+encodeURIComponent(jQuery("#sometextfield").val())+"&var2="+somevar,
		//you can use that in the conroller!
		success: function(r){
			//debug, check the result
			console.log(r);  
			if (r.data) { 
				//you will have data as an object.
				//you can use r.data.step and r.data.bidID
			}
		}
	});
}
</script>
Make your button
<button onclick="return CreateBid();" class="btn btn-success">Start Bid</button>

When clicking the Start Bid button, you will call the CreateBid() function.
The will do an ajax post to the controller and return data. You can then use that data for further coding
Maybe call a function GotoStep(r.data.step) or someting like that.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, MorganL

Passing a variable in the save controller 07 Oct 2015 11:11 #13580

  • MorganL
  • MorganL's Avatar
  • Offline
  • Platinum Member
  • Posts: 438
  • Thank you received: 53
  • Karma: 16
I will have to wrap my head around that, but looks good.. and if it just goes back to the record but I can control the steps.. brilliant!

The bid form has 7 steps and I want the NEXT / PREVIOUS buttons to fire the save result everytime they are pressed so the record is constantly updating between steps, and using this method I should be able to achieve that.

As you probably worked out, all the Start Bid button does is save the initial record and then as $this->item->id now exists it unlocks the next step (using a simple test for the variable being defined)
Morgan Leecy MCSE

Novell / Linux
PHP. MYSQL, Apache, node.js
Coldfusion, JQuery, HTML5
Joomla
The administrator has disabled public write access.

Passing a variable in the save controller 07 Oct 2015 11:17 #13581

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 984
  • Karma: 140
Thanks Romkabouter, K++

I love Ajax, and chance that Joomla finally introduce the JSON Response object, but to my opinion, the transaction object structure is far from beeing complete.
I am right now working on this part, and in the next cook release (pure and cleaned), I will introduce the JS layer to simplify the AJAX calls in the pages.

To be continued...
Coding is now a piece of cake
The administrator has disabled public write access.
The following user(s) said Thank You: Romkabouter

Passing a variable in the save controller 07 Oct 2015 11:30 #13583

  • Romkabouter
  • Romkabouter's Avatar
  • Offline
  • Elite Member
  • Posts: 310
  • Thank you received: 131
  • Karma: 48
morganleecy wrote:
IThe bid form has 7 steps and I want the NEXT / PREVIOUS buttons to fire the save result everytime they are pressed so the record is constantly updating between steps, and using this method I should be able to achieve that.
Hence my comment: $step = 2; //or get some next step

You can get and post the next and current step.
I can image havng a workflow table, with step and nextstep following some business rules.
Then, you post the current step via ajax and return the next step, which has been calculated in the controller (actually you should calculate this in the workflowmodel, following mvc stucture)
like => $next = $workflow->calculateNext($currentstep);
The administrator has disabled public write access.
Time to create page: 0.122 seconds

Get Started