Skip to Content Skip to Menu

Need Help with PHP Code Action in Auto Actions

  • bascherz
  • bascherz
  • OFFLINE
  • Posts: 228
  • Thanks: 29
  • Karma: 11
3 years 11 months ago #321710 by bascherz
I have an Auto Action that runs a small snippet of PHP code triggered by onAfterUserUpdate or onAfterUpdateUser. The condition is if the [cb_lat] field is empty, then go lookup the user's address and get the latitude and longitude from the Google Maps Geocode API. The function for that works as I use it elsewhere. The desire is to then stuff those new values into the respective profile fields.

If I use these triggers, do I have to submit an UPDATE query myself?
If I use the onBefore... triggers, can I just assign the new value to $user->cb_lat and will it get updated by CB?

I wish the documentation for Auto Actions was better. I use it a lot, it is VERY feature-rich, but it's often guesswork to figure things out.

Thanks in advance,
Bruce

Bruce S - Vienna, VA

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48547
  • Thanks: 8290
  • Karma: 1445
3 years 11 months ago #321740 by krileon

If I use these triggers, do I have to submit an UPDATE query myself?

Depends on what trigger you use.

If I use the onBefore... triggers, can I just assign the new value to $user->cb_lat and will it get updated by CB?

Yup, you can directly modify the user object for that trigger using the following.

Trigger: onBeforeUserUpdate
Code:
Code:
$variables['var1']->FIELD_NAME = 'FIELD_VALUE';
Reference Variables: Variable 1

It's important to set the reference variables parameter so it knows what trigger variable is a reference it can modify otherwise you just get a copy of the variable.

I wish the documentation for Auto Actions was better. I use it a lot, it is VERY feature-rich, but it's often guesswork to figure things out.

What you're asking for isn't something we can really document. It's effectively asking us "can I use PHP to do PHP things?" of which the answer is always "yes, but.." since it depends entirely on your knowledge of PHP. The non-code based auto actions of CB Auto Actions are significantly more straight forward with System Actions providing several usage examples.

With that said we do want to improve CB Auto Actions documentation specifically regarding triggers and their available variables, which we'll be improving when we redo our trigger API as we want to create official phpdoc for triggers that we can parse out into documentation.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • bascherz
  • bascherz
  • OFFLINE
  • Posts: 228
  • Thanks: 29
  • Karma: 11
3 years 10 months ago #322717 by bascherz
Replied by bascherz on topic Need Help with PHP Code Action in Auto Actions
How about on the onAfterUserUpdate event? What is available in that case and how do I alter what actually gets saved? I am implementing a momentary button, so I need it to return to its other state after being pressed.

Bruce S - Vienna, VA

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48547
  • Thanks: 8290
  • Karma: 1445
3 years 10 months ago #322725 by krileon
See the below tutorial for what variables are available to a trigger. It has been recently updated to include all core triggers.

www.joomlapolis.com/documentation/279-community-builder/tutorials/18358-using-cb-triggers

You won't be able to modify the user object before storage with onAfterUserUpdate as by time that trigger is fired the user has already been stored. You'll need to do a second user store for your changes to take place which a Field action can already handle.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in or Create an account to join the conversation.

  • bascherz
  • bascherz
  • OFFLINE
  • Posts: 228
  • Thanks: 29
  • Karma: 11
3 years 9 months ago #322858 by bascherz
Replied by bascherz on topic Need Help with PHP Code Action in Auto Actions
I hate to belabor this topic, but I've been noticing some things not working as I thought they should when it comes to events triggering auto actions. I'd like to get the event naming convention clarified and also some other things.

1. Many events have the form "onBeforeUserUpdate" and "onBeforeUpdateUser". It's always been my understanding that the former ("UserUpdate") is when the profile is edited in the front end (typically by the user, but could be by a moderator or administrator) and the latter ("UpdateUser") is when it is edited in the back end. Right?

2. If the site is setup to only require confirmation and not approval after registration, does the "onAfterUserApproval" event even trigger?

3. What is the difference between "onAfterUserConfirm" and "onAfterUserConfirmation"? Seems to me the former may be when the user clicks the confirmation link. So what is the latter? When the user's status is changed to "Confirmed" in the back end? Would they ever both trigger at the same time?

Thanks in advance,
Bruce

Bruce S - Vienna, VA

Please Log in or Create an account to join the conversation.

  • krileon
  • krileon
  • ONLINE
  • Posts: 48547
  • Thanks: 8290
  • Karma: 1445
3 years 9 months ago #322866 by krileon

I'd like to get the event naming convention clarified and also some other things.

We don't have an official event naming convention. It's something we'll be correcting with CB 3.0 as we want to make sure all events have logical names and include phpdoc describing the event so we can parse out documentation for them.

1. Many events have the form "onBeforeUserUpdate" and "onBeforeUpdateUser". It's always been my understanding that the former ("UserUpdate") is when the profile is edited in the front end (typically by the user, but could be by a moderator or administrator) and the latter ("UpdateUser") is when it is edited in the back end. Right?

onAfterUserRegistration = frontend registration
onAfterNewUser = backend registration
onAfterUserUpdate = frontend edit
onAfterUpdateUser = backend edit

2. If the site is setup to only require confirmation and not approval after registration, does the "onAfterUserApproval" event even trigger?

onAfterUserApproval is only used if the user is approved. So if you do not have approval enabled it does not fire.

3. What is the difference between "onAfterUserConfirm" and "onAfterUserConfirmation"? Seems to me the former may be when the user clicks the confirmation link. So what is the latter? When the user's status is changed to "Confirmed" in the back end? Would they ever both trigger at the same time?

onAfterUserConfirm is fired immediately after the user is confirmed in the database and applies to frontend and backend. onAfterUserConfirmation is fired after the user has confirmed using the confirmation URL and can be used to apply a custom URL redirect, output custom messages to the user, etc..

So for example you'd use onAfterUserConfirmation if you want to redirect them to their profile after they confirm. You'd use onAfterUserConfirm if you want to update a field when they are confirmed.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.
The following user(s) said Thank You: bascherz

Please Log in or Create an account to join the conversation.

Moderators: beatnantkrileon
Powered by Kunena Forum