Welcome, Guest
Username: Password: Remember me

TOPIC: [CLOSED] Inserting Custom Code

Inserting Custom Code 04 Nov 2012 09:09 #4973

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
HI! :)

So I am following @admin's advice in this post: www.j-cook.pro/forum/7-design-your-appli...extra-on-form-submit

So I am looking at the function prepareQuery() wondering how I am going to do this. Part of the problem is that I typically program in procedural form and not object oriented. (I am old you see.)

So, if someone could give me a clue here I would appreciate it. In the form I have built in a Bool on the admin side. If I check that box when creating a record, it means I need to run my own function on the backend after submit. Now I have looked at the Joomla 2.5 documentation. The section I need has not been written yet. Awesome.

So, here is what I would normally do:
if($_POST['my_bool'] == 'true'){
	runMyCustomFunction();
}

So here is where my problem starts. I don't see where in ./models/xxxx.php this is being processed. This is the content of the prepareQuery() function:
protected function prepareQuery(&$query, $pk)
	{

		//FROM : Main table
		$query->from('#__skm_trialkeys AS a');

		switch($this->getState('context'))
		{
			case 'trialkey.trialkey':

				//BASE FIELDS
				$query->select(	'a.id,'
							.	'a.admin_notes,'
							.	'a.asset_name,'
							.	'a.create_key,'
							.	'a.created_by,'
							.	'a.encoder,'
							.	'a.lickey,'
							.	'a.mac_address');

				//SELECT
				$query->select('_created_by_.name AS `_created_by_name`');
				$query->select('_encoder_.name AS `_encoder_name`');

				//JOIN
				$query->join('LEFT', '`#__users` AS _created_by_ ON _created_by_.id = a.created_by');
				$query->join('LEFT', '`#__skm_optionencodertemplates` AS _encoder_ ON _encoder_.id = a.encoder');

				break;
			default:
				//SELECT : raw complete query without joins
				$query->select('a.*');
				break;
		}

		//SELECT : Instance Add-ons
		foreach($this->getState('query.select', array()) as $select)
			$query->select($select);

		//JOIN : Instance Add-ons
		foreach($this->getState('query.join.left', array()) as $join)
			$query->join('LEFT', $join);

		//WHERE : Item layout (based on $pk)
		$query->where('a.id = ' . (int) $pk);		//TABLE KEY


		//WHERE : Access
		//WHERE : Publish, publish date (state field)
	}

So, I am confused. Probably because I am old and wished Fortran was back in style. Can someone clue me in here?
--
Jerry Benton
The administrator has disabled public write access.

Re: Inserting Custom Code 04 Nov 2012 10:36 #4977

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
So I have been looking at this for a couple of hours now. If someone could point me in the right direction so I can get started, I would appreciate it. I just need to understand how the logic flows. If I can get a handle on this part, the rest should be simple.

In short, within a couple of the forms on both the User and Admin side I will need to run some custom functions.

Example: Form submit:

- If this is a new (SQL insert) record, I need to be able to call a custom function.
- If information is being changed (SQL update), I need to check for ABC and call whatever function.


The functions are not a problem. I can easily write those in PHP. What I need to know:

- Where is the best place to put these functions within this structure? (What file?) Public functions will work fine.

- How are events translated? (i.e. submit, the submitted data passes rule checks, etc.)

- Where do I determine form submit parameters? I need to place calls to my functions in the right place based on events.


I have really been trying to read the (incomplete) documentation for Joomla 2.5 and I have been staring at the PHP code in my component for hours. I've also been looking for something out there that explains the code flow logic in Joomla 2.5, but everything I find is geared towards sticking custom PHP code in articles.

If someone could point me in the right direction, I would really appreciate it.
--
Jerry Benton
The administrator has disabled public write access.

Re: Inserting Custom Code 04 Nov 2012 13:57 #4988

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
Was not in ./controllers and was not in ./models

./tables/xxx.php


sigh ...
--
Jerry Benton
The administrator has disabled public write access.

Re: Inserting Custom Code 05 Nov 2012 09:11 #5000

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
@jcbenton

Have you had any luck with this yet?

I have been looking into this since I saw your post and I have to admit, I too am a little confused! :huh:

I haven't actually finished any of my COOK 2.0 components as they're quite large and as a result, I've not done any custom coding inside them. I know that in the previous version (1.5) this would have been handled in the save() function but like you, I'm having a little trouble working out how exactly one might manipulate the post data in the the prepareQuery();

@admin - please could you post a little example of this?

Kind Regards,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
The administrator has disabled public write access.

Re: Inserting Custom Code 07 Nov 2012 22:50 #5075

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
@jcbenton

You must understand better how MCV works.

READING and WRITTING are totally separated.

prepareQuery() is dealing the readings. Very simple.

Then for any other kind of operations, you hav to put them at the right place, not calling customFucnctions as you try to do.
Do not read the $POST in that way, it is too dangerous. Use the Joomla Framework.

Every operation you do means that it is doing INSERT, UPDATE, DELETE operations. Including calculations if you want ...

So.
1. You create an entry point for this operation :
Controller, new function "mytask()", in the ITEM controller (item : better to my opinion, in almost of cases) of the correct table on wich it applies.
If your function is dealing with many tables, find the better place for it. The page from wich it is coming for almost cases.
This function can also be written in the generic controller class.

2. this function will be called when the url is containing :
view=myview&task=mytask

3. When you can reach it, use this function to call the models you want, catch all the datas from post, and from db, then to exec all your independant tasks in the model.
Sometimes, the controller is only transferring ONE operation the the model.

4. To write better, write the single and simple operations in the models. (toggle value, increase, récurate calculation...)

5. At the end the controller is driving the operations. The model is DOING the operations.

Hope you can understand better the MVC.

Once an operation is finished, it must ALWAYS be redirected to a page (end of your controller function). The task is now 'display()', and the model will be called to load the redirected page (READING)
Coding is now a piece of cake
The administrator has disabled public write access.

Re: Inserting Custom Code 07 Nov 2012 23:04 #5079

  • jcbenton
  • jcbenton's Avatar
  • Offline
  • Premium Member
  • Posts: 125
  • Thank you received: 9
  • Karma: 3
Yes, this is exactly what I did in ./models and ./tables using the Joomla framework. My problem was where to get started as I started where I was recommended to start in this post:

www.j-cook.pro/forum/7-design-your-appli...extra-on-form-submit

Once I found the right place it was rather easy. Although the MCV is followed, I still have to get a handle on your coding style and thought process. (Always a learning curve when looking at code that is not your own for the first time.)

So yeah, I figured it out even though NONE of the Joomla 2.5 database abstraction is documented at docs.joomla.org. The pages are empty and they are already talking about J3.0? Ok, wow.
--
Jerry Benton
The administrator has disabled public write access.

Re: Inserting Custom Code 07 Nov 2012 23:09 #5082

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
I think you can be blocked with the toolbar to add new tasks. This is true, I am currently working on that.
Coding is now a piece of cake
The administrator has disabled public write access.
Time to create page: 0.070 seconds

Get Started