Jump to content
  • 0

How To Call A User Balance In The Template?not Ajax


ty0716

Question

14 answers to this question

Recommended Posts

  • 0

Blesta uses this to get the credit:

<div class="amounts"></div>

<?php
		$this->Javascript->setInline('
			$(document).ready(function() {
				fetchCurrencyAmounts("' . $this->Html->ifSet($contact->settings['default_currency']) . '");
				$(".col-md-3 .amounts").on("click", "a", function() {
					fetchCurrencyAmounts($(this).attr("id"));
					return false;
				});
			});
			
			function fetchCurrencyAmounts(currency) {
				$(this).blestaRequest("GET", "' . $this->Html->safe($this->base_uri . "main/getcurrencyamounts/") . '" + currency, null, function(data) {
					if (data)
						$(".amounts").html(data);
				},
				null,
				{dataType:"json"});
			}
		');
		?>
Link to comment
Share on other sites

  • 0

Performing database queries from a template negates the purpose of the MVC design pattern. You're better off making an AJAX request as Licensecart mentioned to fetch currency balances from existing controller methods. Alternatively, you can update the controller to fetch the balances and set them as variables to the template you want to display them in.

Link to comment
Share on other sites

  • 0

i was forcing the same issue already when i was working in adminLTE template .

i think blesta should think about a way that let the developpers/designer to add thier own function or request in the template, as now ,we can just use what blesta offer .

there a re a old thread about this subject .

Link to comment
Share on other sites

  • 0

Performing database queries from a template negates the purpose of the MVC design pattern. You're better off making an AJAX request as Licensecart mentioned to fetch currency balances from existing controller methods. Alternatively, you can update the controller to fetch the balances and set them as variables to the template you want to display them in.

And how to use database query on controller?

Link to comment
Share on other sites

  • 0

Performing database queries from a template negates the purpose of the MVC design pattern. You're better off making an AJAX request as Licensecart mentioned to fetch currency balances from existing controller methods. Alternatively, you can update the controller to fetch the balances and set them as variables to the template you want to display them in.

Now i can use database query in the controller.

How to deliver the query results to template?

Link to comment
Share on other sites

  • 0

naja7host said

i was forcing the same issue already when i was working in adminLTE template .

i think blesta should think about a way that let the developpers/designer to add thier own function or request in the template, as now ,we can just use what blesta offer .

there a re a old thread about this subject . 

I can not agree more with naja7host

 

Impose MVC for all is very rigid and it's more easy for most people to simply do light customization only from template, VS follow full MVC way

Link to comment
Share on other sites

  • 0

Now i can use database query in the controller.

How to deliver the query results to template?

 

Sample:

class MyController extends AppController {
    ...
    public function method() {
        // Load the model to use to query the database
        $this->uses(array("Clients"));
        // Fetch client ID 1
        $client = $this->Clients->get(1);
        // Make the client available to the template in the variable "$client1"
        $this->set("client1", $client);
    }
    ...
}
Link to comment
Share on other sites

  • 0

naja7host said

i was forcing the same issue already when i was working in adminLTE template .

i think blesta should think about a way that let the developpers/designer to add thier own function or request in the template, as now ,we can just use what blesta offer .

there a re a old thread about this subject . 
I can not agree more with naja7host

Impose MVC for all is very rigid and it's more easy for most people to simply do light customization only from template, VS follow full MVC way

i respect your opinion , but i challange you with this .

create your own controller in a plugin , make it send some vars to a template file like client_main.pdt . 100$ bounty from me if you do it .

in reality what i demand is not changin the way how the MVC work , but improuve or find a reialable solution to do some task to make blesta really different from other and easy to use it .

Some concrete exemple, i design a new template and i want to add some extra info that is not provided by the controller , so here we are facing a limitation of adding some extra info that is not available by default .

the only solution is to do a direct call to database .

http://www.blesta.com/forums/index.php?/topic/3422-possible-to-pass-in-external-constant-file-to-templates/

http://www.blesta.com/forums/index.php?/topic/2992-call-function-from-template-file/

the more open doors to developpers/dsigners the more profit we can obtain from them .

Finnaly, i'm not against blesta but i push some request because i beleive some of them is like a revolution .

Link to comment
Share on other sites

  • 0

Sample:

class MyController extends AppController {
    ...
    public function method() {
        // Load the model to use to query the database
        $this->uses(array("Clients"));
        // Fetch client ID 1
        $client = $this->Clients->get(1);
        // Make the client available to the template in the variable "$client1"
        $this->set("client1", $client);
    }
    ...
}

Now i can use database query in the controller.

How to deliver the query results to template?

if you want to deliver the result to a template file in the theme isnot possible; use blestaRequest in ajax mode to fetch the content and add them to your pdt file .

Link to comment
Share on other sites

  • 0

@naja7host

------------------

thxs, naja7host

 

by the way my english have mabye some limitation ;-)  and i was saying I fully agree with you.

 

we need flexible way using the blesta method/function to make querry in the database, and using the blesta db connection directlly from blesta template

 

I was also faced to do custom database connexion/querry in my template, and even if I know it's no the best, it's still more light work than creating plugin/module..

---------------

 

But for ty0716 , as I think the amount in customer account do not come from a field in the database & table (I have not found it)  but maybe more from a calculation and how?

 

That why I have no code example to give to him,  that follow custom database connexion & with an apropriate querry

Link to comment
Share on other sites

  • 0

 

Sample:

class MyController extends AppController {
    ...
    public function method() {
        // Load the model to use to query the database
        $this->uses(array("Clients"));
        // Fetch client ID 1
        $client = $this->Clients->get(1);
        // Make the client available to the template in the variable "$client1"
        $this->set("client1", $client);
    }
    ...
}

When I want to use database table of "support_ticketspro",shoud i how to use the function uses()?

 

this table "support_ticketspro" is a third-party plugin.

Link to comment
Share on other sites

  • 0

no, is a custom query to the ytransaction table, you can check the function in the transations.php model .

and the code to get the balance is :

 

	public function getCurrencyAmounts() {
		// Ensure a valid client was given
		if (!$this->isAjax()) {
			header($this->server_protocol . " 401 Unauthorized");
			exit();
		}
		
		$this->requirePermission("_credits");
		
		$this->uses(array("Currencies", "Transactions"));
		
		$currency_code = $this->client->settings['default_currency'];
		if (isset($this->get[0]) && ($currency = $this->Currencies->get($this->get[0], $this->company_id)))
			$currency_code = $currency->code;
		
		// Fetch the amounts
		$amounts = array(
			'total_credit' => array(
				'lang' => Language::_("ClientMain.getcurrencyamounts.text_total_credits", true),
				'amount' => $this->CurrencyFormat->format($this->Transactions->getTotalCredit($this->client->id, $currency_code), $currency_code)
			)
		);
		
		// Build the vars
		$vars = array(
			'selected_currency' => $currency_code,
			'currencies' => array_unique(array_merge($this->Clients->usedCurrencies($this->client->id), array($this->client->settings['default_currency']))),
			'amounts' => $amounts
		);
		
		// Set the partial for currency amounts
		$response = $this->partial("client_main_getcurrencyamounts", $vars);
		
		// JSON encode the AJAX response
		$this->outputAsJson($response);
		return false;
	}
	


note you should remove the ajax condition and remove

		// Ensure a valid client was given
		if (!$this->isAjax()) {
			header($this->server_protocol . " 401 Unauthorized");
			exit();
		}
and replce the ajax return with a simple set var

// JSON encode the AJAX response
$this->outputAsJson($response);
return false;
to

 

$this->set("balance", $response);
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...