CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Allow User to Choose Location Type On Profile
Pages: [1]

Author Topic: Allow User to Choose Location Type On Profile  (Read 2000 times)

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Allow User to Choose Location Type On Profile
August 07, 2011, 04:15:47 pm
Hello - I'm struggling a bit in my first attempt to develop with Civi.

Here's what I'm trying to do:

I have a profile that exposes "Primary" Address fields (street, city, state, etc). The default location type is configured as "Work". So when someone enters an address on this profile form, it's made the primary and given a location type of "Work". I'm happy with it being made primary as the requirement is to collect only one address. However, I want to know the true location type of that address, so I want to expose a field for the user to choose the location type.

From what I can gather, I need to customize the template that creates the form and add a field (in this case, I just want a select list) for the location type. Then, I need to utilize the postProcess hook to find the value of that submitted form field and utilize the API to update that particular address' location type based on the submitted value.

I can't seem to find enough documentation to actually make that happen. For instance, I don't know anything about the form object or finding it's submitted values.

Am I on the right track? How can I get some help on how to do this? I already know I'll have more custom dev to do, so I need to get my resources in order and start making sense of how to do so with CiviCRM. Any assistance is greatly appreciated!


Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Allow User to Choose Location Type On Profile
August 08, 2011, 03:01:37 am
That sounds like the sort of improvement that would ideally be in core rather than need to be customised. However, writing a patch for core can also be tricky  & you are saying you want to get up to speed on customising anyway.

You might find it helpful to install this module https://svn.fuzion.co.nz/repos/fuzion-code/trunk/drupal/modules/custom/civicrm_developer (required you to have devel installed) - it basically just outputs when various hooks are being called & what is being passed to them & can be a handy way to see what is going on.

Here is the hook specification
http://wiki.civicrm.org/confluence/display/CRMDOC40/CiviCRM+hook+specification

Here's a couple of API usage examples that might be helpful
http://svn.civicrm.org/civicrm/branches/v3.4/api/v3/examples/ConstantGet.php
http://svn.civicrm.org/civicrm/branches/v3.4/api/v3/examples/AddressCreate.php
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Allow User to Choose Location Type On Profile
August 08, 2011, 01:51:27 pm
Thanks for the resources! That developer module might be exactly what I need to fill in the major gaps. Checking it out now...

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Allow User to Choose Location Type On Profile
August 09, 2011, 07:26:08 am
When using the buildForm hook, how can one check if the form is going to create a new record or is being used to edit existing information?

For instance, I want to check whether a profile form is in create or edit mode so that I can set defaults on fields only in create mode.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Allow User to Choose Location Type On Profile
August 09, 2011, 07:45:13 am

in this case, easiest to check: $_GET['q']

create url: civicrm/profile/create
edit: civicrm/profile/edit

u can also do: $form->getVar( '_action' ); and use the value of the returned variable (constants defined in CRM/Core/Action.php)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Allow User to Choose Location Type On Profile
August 18, 2011, 06:59:40 am
I'm still having fussing with making this happen. I'm trying to do this within a profile in a contribution page.

Here's what I've done.

  • Added a custom select field called "Address Type" to the profile , so the field shows up on the page.
  • Retrieve the submitted value of Address Type in the postProcess hook.
  • Figured out the API call to update the addresses location type based on the user selection of Address Type.

What I don't know is how to determine the Address ID, so that I can update the correct address. At the moment, I've hard-coded an Address ID to test what I've done is working as expected.

Code: [Select]
function cdm_civi_tweaks_civicrm_postProcess( $formName, &$form ) {

    if ( $formName == 'CRM_Contribute_Form_Contribution_Confirm' ) {

$form_values = $form->exportValues( );
$location_type = $form_values['custom_22'];

switch ($location_type) {
    case 'Home':
$location_type_id = 1;
break;
    case 'Work':
$location_type_id = 2;
break;
    default:
        $location_type_id = 7; //Unspecified
}

require_once 'api/api.php';

$result = civicrm_api('Address','update',array('id' => 14511, 'location_type_id' => $location_type_id, 'version' => 3));
    }
}

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Allow User to Choose Location Type On Profile
August 18, 2011, 07:48:10 am

you might need to do a query using the $form_values for that contactID (which probably is in $form->_contactID)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Allow User to Choose Location Type On Profile
August 18, 2011, 11:45:46 am
You should be able to get the address id with the get api

http://svn.civicrm.org/civicrm/branches/v3.4/api/v3/examples/AddressGet.php

You would be able to 'chain' this - e.g. this example gets the address for contact_id 1 with location_type_id 1 & changes it to location_type_id = 2

function address_get_example(){
    $params = array(
                  'contact_id'          => '1',
                  'location_type_id'    => '1',
                  'version'       => '3',
                 'api.address.create' => array('location_type_id' =>2)

  );
  require_once 'api/api.php';
  $result = civicrm_api( 'address','get',$params );

  return $result;
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

jaymcgraw

  • I post occasionally
  • **
  • Posts: 106
  • Karma: 6
  • CiviCRM version: 4.1.2
  • CMS version: Drupal 7.12
  • MySQL version: 5.0.92-community
  • PHP version: 5.2.11
Re: Allow User to Choose Location Type On Profile
August 23, 2011, 07:36:03 am
This is all great help and will ultimately lead me to the answer, but I can't seem to get the contact_id, which is key to doing what I need to do.

Apologetically, I may have left out a key detail about where exactly I'm trying to do this. I don't need to do this directly on a profile page, but rather in a contribution page containing that profile. And I can't seem to get a contact_id (nor an address_id) by looking at the form values of the contribution page.

Any thoughts on how to get the contact_id?

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Allow User to Choose Location Type On Profile
August 23, 2011, 12:14:48 pm
For drupal to get logged in user - something like

global $user;
$contact_id = civicrm_api('uf_match','getvalue', array('version' => 3, 'uf_id' => $user->uid, 'return' => array('contact_id');
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion »
  • APIs and Hooks (Moderator: Donald Lobo) »
  • Allow User to Choose Location Type On Profile

This forum was archived on 2017-11-26.