Cron task

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

What I am trying to do is quite simple.
I have a profile field pf_tankdim that the user can modify and another one pf_autovol that he cannot.
When he clicks on Submit in the profile screen I would like to calculate pf_autovol as a function of pf_tankdim and store it in the DB.
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 532
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: Cron task

Post by danieltj »

You will have to store this functionality in an extension. You can’t be editing the phpBB core with custom changes anymore so the first step is create an extension.

The second step is to provide some examples of the data. So say I edit field one, what does that do to field two? How does the data change and what does it look like?
Awesome Payments extension now available!
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

Field1 is editable by user, Field2 not.
Field1 contains the dimensions of an aquarium, examples
123 x 456 x 789
123 456 789
123, 456, 789
...
the function keeps only the 3 numbers, multiply them by each other and stores the result in field2.
Here is the full code, there is no need to write an extension.
$UserID contains the user id of the current cession :

Code: Select all

$dims = mysqli_query($db,"SELECT pf_tankdim FROM phpbb_profile_fields_data WHERE user_id=".$UserID);
$dim = mysqli_fetch_assoc($dims);
$TankDim = $dim['pf_tankdim'];
$TankDim = preg_replace("/\D+/", ':',$TankDim);	// Remplace les non-digits consécutifs par :
$Dim3 = array_pad(explode(":",$TankDim),3,"0");
$Vol = $Dim3[0] * $Dim3[1] * $Dim[2] / 1000;
$upd = mysqli_query($db,"UPDATE phpbb_profile_fields_data SET `pf_autovol`='".$Vol."' WHERE user_id=".$UserID);
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 532
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: Cron task

Post by danieltj »

I don't know why you're so adverse to putting this in an extension 😂

Use the core.ucp_profile_info_modify_sql_ary event which is run just before the profile information is saved to the database. You can update the relevant profile field during that event before it's saved to the database.

Referenced in ucp_profile.php on line 400.
Awesome Payments extension now available!
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

Yes thanks, I guess I should modify the data before it is saved to the DB, here is the code you point to.
My problem is I don't know the structure of these arrays : how do I find the row of the 2 variables I need (input to and output from my function) ?
I only know the custom profiles rows names in the DB : pf_tankdim and pf_tankvol.

Code: Select all

						/**
						* Modify profile data in UCP before submitting to the database
						*
						* @event core.ucp_profile_info_modify_sql_ary
						* @var	array	cp_data		Array with the user custom profile fields data
						* @var	array	data		Array with user profile data
						* @var  array	sql_ary		user options data we update
						* @since 3.1.4-RC1
						*/
						$vars = array('cp_data', 'data', 'sql_ary');
						extract($phpbb_dispatcher->trigger_event('core.ucp_profile_info_modify_sql_ary', compact($vars)));
User avatar
danieltj
Infrastructure Team Member
Infrastructure Team Member
Posts: 532
Joined: Thu May 03, 2018 9:32 pm
Location: United Kingdom
Name: Daniel James

Re: Cron task

Post by danieltj »

It’s documented above in the file. You should be able to read the code before the event and figure out what you need to do. To make it easier use var_dump to see the contents of the variables.
Awesome Payments extension now available!
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6337
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Cron task

Post by thecoalman »

Add an exit; after dumping the array. e.g.

Code: Select all

var_dump($cp_data);
// Your code
var_dump($cp_data);
exit;
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

Thanks a lot,
var_dump($cp_data); gives for instance
["pf_tankdim"]=> string(9) "150x90x65"
["pf_autovol"]=> string(10) "878 litres"

So I've juste added $cp_data["pf_autovol"] = $Vol;
$Vol being the computed volume
it works :D
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

This is the full added code, comments welcome
in /includes/ucp/ucp_profile.php after $vars = array('cp_data', 'data', 'sql_ary'); add

Code: Select all

$TankDim = preg_replace("/\D+/", ':',$cp_data['pf_tankdim']);	// Replace consecutive non-digits by ":" 
$Dim = array_pad(explode(":",$TankDim),3,"0");
$TankVol = round((int)$Dim[0]*(int)$Dim[1]*(int)$Dim[2]/1000);
($TankVol == 0) ? $TankVol = "?" : $TankVol = $TankVol." litres";	// (Condition) ? true : false
$cp_data["pf_autovol"] = $TankVol;
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

One thing I don't understand : after $vars = array('cp_data', 'data', 'sql_ary');
why do I acces to the array $cp_data[] ?
I dont find this array with $ sign defined anywhere !
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3902
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay

Re: Cron task

Post by Kailey »

The code for manipulating that array is in /phpbb/profilefields/manager.php. Check out the build_insert_sql_array() function in that file (lines 535 - 568).
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules
If you have any questions about the rules/customs of this website, feel free to send me a PM.

My little corner of the world | Administrator @ phpBB Modders
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

Image
User avatar
thecoalman
Community Team Member
Community Team Member
Posts: 6337
Joined: Wed Dec 22, 2004 3:52 am
Location: Pennsylvania, U.S.A.

Re: Cron task

Post by thecoalman »

If you are not using a decent editor get one like Notepad++. One feature is you can search all the files in folders/subfolders, $cp_data returns 16 hits in 6 files including the lines mentioned by Kailey,
“Results! Why, man, I have gotten a lot of results! I have found several thousand things that won’t work.”

Attributed - Thomas Edison
Hervé
Registered User
Posts: 571
Joined: Tue Jun 04, 2019 7:51 am
Location: Belgium
Name: Rudy

Re: Cron task

Post by Hervé »

I use Notepad++.
My question was because danieltj recommend to place the modification near line 400 in ucp_profile.php (code below)
and I don't find $cp_data there, only cp_data.
It's all right now, it works and I understand why, thanks again.

Code: Select all

						/**
						* Modify profile data in UCP before submitting to the database
						*
						* @event core.ucp_profile_info_modify_sql_ary
						* @var	array	cp_data		Array with the user custom profile fields data
						* @var	array	data		Array with user profile data
						* @var  array	sql_ary		user options data we update
						* @since 3.1.4-RC1
						*/
						$vars = array('cp_data', 'data', 'sql_ary');
						extract($phpbb_dispatcher->trigger_event('core.ucp_profile_info_modify_sql_ary', compact($vars)));
User avatar
ssl
Registered User
Posts: 1984
Joined: Sat Feb 08, 2020 2:15 pm
Location: Le Lude, Pays de la Loire - France
Name: Fred Rimbert

Re: Cron task

Post by ssl »

Hervé wrote: Thu Jul 18, 2024 8:41 am My question was because danieltj recommend to place
No not danieltj: viewtopic.php?p=16021867#p16021867
Sorry for my English ... I do my best! :anger_right:

:point_right_tone3: phpBB: 3.3.13 | PHP: 8.3.9
:point_right_tone4: [Kill spam on phpBB] - [Some French translation of extensions]
"Mistress, Mistress someone is bothering me in pm"

Return to “phpBB Custom Coding”