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) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Extending the OnBehalf of Organization Fields
Pages: [1] 2

Author Topic: Extending the OnBehalf of Organization Fields  (Read 5061 times)

johng

  • Guest
Extending the OnBehalf of Organization Fields
May 12, 2010, 06:44:20 pm
Hello, I saw the post [url href="http://wiki.civicrm.org/confluence/display/CRMDOC22/Enhance+Organizational+Membership+Functionality"]here[/url] and liked the idea of extending the On Behalf Of to include some custom fields. That particular wiki page is outdated and so I attempted to do some of this on my own. I am a little stuck however and could use some help to point me in the right direction. For the purpose of this I am wanting to add a field that has the value of custom_1 (id1)  in the database and is named category. I have managed to get it to appear correctly on the frontend but am losing the value somewhere in the processing. Here is where I am at:

Step 1:
I created a custom version (custom PHP directory) of CRM/Contact/BAO/Contact/Utils.php

To this I added the following code inside the buildOnBehalfForm() function at line 511:
CRM_Core_BAO_CustomField::addQuickFormElement($form ,'custom_1',1,false,true,false,ts('Category'));

Step 2:
I created a custom version (custom PHP directory) of CRM/Contribute/Form/ContributionBase.php

To this I added the following code inside the assignToTemplate() function at line 617 (within the is_for_organization if statement)
$this->assign('onBehalfCustom1',   $this->_params['onbehalf_custom']['custom_1']);

Step 3:
I created a custom template at templates/CRM/Contribute/Form/Contribution/3/Main.tpl (3 is ID of the contribution page I am overriding) that duplicates the normal template except the following

In this file I changed the if statement for on behalf of to the following:
{if $is_for_organization}
        {include file=CRM/Contact/Form/3/OnBehalfOf.tpl}
{/if}

Step 4:
I created a custom template at templates/CRM/Contact/Form/3/OnBehalfOf.tpl (3 is ID of the contribution page I am overriding) that duplicates the normal template except the following

At line 123 I added the following:
<div class="section {$form.custom_1.}-section">
            <div class="label">{$form.custom_1.label}</div>
            <div class="content">{$form.custom_1.html}</div>
            <div class="clear"></div>
</div> 

Step 5:
I created a custom version (custom PHP directory) of CRM/Contribute/Form/Contribution/Confirm.php

In the postProcess() function I added the following at line 461
if ( array_key_exists( 'onbehalf_custom', $params ) && is_array( $params['onbehalf_custom'] ) ) {
                foreach ( $params['onbehalf_custom'] as $key => $value ) {
                    $behalfOrganization[$key] = $value;
                }               
                unset($params['onbehalf_custom']);
 }

I also added the following in the processOnBehalfOrganization() function at line 1156
require_once 'CRM/Core/BAO/CustomField.php';
$customFields = CRM_Core_BAO_CustomField::getFields( 'Organization', false, true );
$behalfOrganization['custom'] = CRM_Core_BAO_CustomField::postProcess( $behalfOrganization,
                                                                               $customFields,
                                                                               CRM_Utils_Array::value('organization_id', $behalfOrganization),
                                                                               'Organization', true );

I believe that I am close to having this work but am just a little bit off. The correct HTML for the custom field is displaying in the correct place (it is a multiselect). It just seems for some reason the values aren't getting set in the database. I am hoping a fresh set of eyes could take a look. Thanks!

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Extending the OnBehalf of Organization Fields
May 14, 2010, 10:08:43 am
Hi I wrote those docs, and I will be working on a project like this Monday.  Back with you soon.
Try CiviTeacher: the online video tutorial CiviCRM learning library.

johng

  • Guest
Re: Extending the OnBehalf of Organization Fields
May 14, 2010, 11:03:28 am
Great, perhaps we can help each other out! I have already found a better way to do a portion of this so perhaps we can build some documentation together. Below is my revised findings. Basically I figured out that rather then make custom PHP files I can simply use the civi hooks. I still don't have it so it processes the records but I am getting closer! A lot of var dumping and logging haha.

Step 1: Getting the Initial Form to Display Custom Fields
It turns out this is actually very easy. In this example I am adding 2 custom fields to my "On Behalf of" Section. The fields are custom_1 (category) and custom_3 (description) which correspond to custom data setup in the backend. Category is a multi-select and description is a textbox.

First, you must implement the civiCRM hooks. I did this as a Joomla plugin with the same idea as http://forum.civicrm.org/index.php?topic=10725.0. Once that is done you must first make the html/info for the fields available to the SMARTY template. I did this within the buildForm hook by adding the following code:

$id = $form->getVar( '_id' );      
      
if($formName == 'CRM_Contribute_Form_Contribution_Main' && $id == 3){         
         
   CRM_Core_BAO_CustomField::addQuickFormElement($form ,'custom_1',1,false,true,false,ts('Category'));
   CRM_Core_BAO_CustomField::addQuickFormElement($form ,'custom_3',3,false,true,false,ts('Description'));
}   

Please note that 3 is the id of the contribution page I am wishing to override.   

Once the code is available to the template it is a matter of creating overrides of those templates.

To templates/CRM/Contribute/Form/Contribution/3/Main.tpl I replaced:
{if $is_for_organization}
        {include file=CRM/Contact/Form/OnBehalfOf.tpl}
{/if}

With:
{if $is_for_organization}
        {include file=CRM/Contact/Form/3/OnBehalfOf.tpl}
{/if}

To templates/CRM/Contact/Form/3/OnBehalfOf.tpl I added:

<div class="section {$form.custom_1}-section">
         <div class="label">{$form.custom_1.label}</div>
         <div class="content">{$form.custom_1.html}</div>
         <div class="clear"></div>
</div> 
           
<div class="section {$form.custom_3}-section">
          <div class="label">{$form.custom_3.label}</div>
          <div class="content">{$form.custom_3.html}</div>
          <div class="clear"></div>
</div> 

This was added the place I wanted it to show up on the form.

That's as far as I've gotten at this point but I thing that using the validate and postProcess hooks I should be able to figure out how to get this working all the way through.

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Extending the OnBehalf of Organization Fields
May 14, 2010, 11:44:41 am
I commend you for your initiative.  I'm sorry I can't look at this right now, but next week this project is on my list.  I wrote those docs 9 months ago, but most of the work was actually done by Kurund.  In the last 9 months I've learned a lot, and might be able to help you.
Try CiviTeacher: the online video tutorial CiviCRM learning library.

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: Extending the OnBehalf of Organization Fields
May 14, 2010, 03:53:09 pm

hey john:

please ping us on IRC if you need any help or assistance. Your new approach is significanly better IMO and seems like u r on the right track :)

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

johng

  • Guest
Re: Extending the OnBehalf of Organization Fields
May 14, 2010, 06:11:34 pm
Alright, will do. I tried pinging on IRC yesterday but no one replied and I wasn't sure I was doing it right  ::) I'm more of a skype person then IRC  ;D

I also discovered that by doing it the way I did in the 2nd post it takes care of at least the base validation (which is all I need at this point).

johng

  • Guest
Re: Extending the OnBehalf of Organization Fields
May 18, 2010, 09:06:35 pm
Here is an Update:

Step 1: Getting the Initial Form to Display Custom Fields
It turns out this is actually very easy. In this example I am adding 2 custom fields to my "On Behalf of" Section. The fields are custom_1 (category) and custom_3 (description) which correspond to custom data setup in the backend. Category is a multi-select and description is a textbox.

First, you must implement the civiCRM hooks. I did this as a Joomla plugin with the same idea as http://forum.civicrm.org/index.php?topic=10725.0. Once that is done you must first make the html/info for the fields available to the SMARTY template. I did this within the buildForm hook by adding the following code:

$id = $form->getVar( '_id' );     
     
if($formName == 'CRM_Contribute_Form_Contribution_Main' && $id == 3){         
         
   CRM_Core_BAO_CustomField::addQuickFormElement($form ,'custom_1',1,false,true,false,ts('Category'));
   CRM_Core_BAO_CustomField::addQuickFormElement($form ,'custom_3',3,false,true,false,ts('Description'));
}   

Please note that 3 is the id of the contribution page I am wishing to override.   

Once the code is available to the template it is a matter of creating overrides of those templates.

To templates/CRM/Contribute/Form/Contribution/3/Main.tpl I replaced:
{if $is_for_organization}
        {include file=CRM/Contact/Form/OnBehalfOf.tpl}
{/if}

With:
{if $is_for_organization}
        {include file=CRM/Contact/Form/3/OnBehalfOf.tpl}
{/if}

To templates/CRM/Contact/Form/3/OnBehalfOf.tpl I added:

<div class="section {$form.custom_1}-section">
         <div class="label">{$form.custom_1.label}</div>
         <div class="content">{$form.custom_1.html}</div>
         <div class="clear"></div>
</div>
           
<div class="section {$form.custom_3}-section">
          <div class="label">{$form.custom_3.label}</div>
          <div class="content">{$form.custom_3.html}</div>
          <div class="clear"></div>
</div>

This was added the place I wanted it to show up on the form.

Step 2: Showing the Custom Fields on the Confirm Screen
To the buildForm hook, next we need to add the following code:

if($formName == 'CRM_Contribute_Form_Contribution_Confirm' && $id == 3){
         $params = $form->getVar('_params');         
         $form->assign('onBehalfCustom_1', $params['custom_1']);
         $form->assign('onBehalfCustom_3', $params['custom_3']);
}

Then we need to create a custom version of /templates/CRM/Contribute/Form/Contribution/3/Contribute.tpl and add the following to the On Behalf of Section (around line 162)

<div class="display-block">
        {section name=category loop=$onBehalfCustom_1}
         {$onBehalfCustom_1[category]}<br/>
        {/section}           
</div>
<div class="display-block">
            {$onBehalfCustom_3}
</div>

Keep in mind my "custom_1" is a multiselect.

Next to work on processing and saving in the database! The biggest obstacle I can see at this point is getting the new organization id. Hopefully I (or someone) can get that figured out soon!

gengel

  • Guest
Re: Extending the OnBehalf of Organization Fields
June 10, 2010, 12:41:27 am
johng, any luck getting further with the processing portion? I'm trying to do this very thing (but am fairly new to CiviCRM)

At the risk of sounding oblivious, this seems like something that shouldn't need to be programmed every time someone wants to change these - why not just use a Profile for the org fields instead of having a hard-coded set?
« Last Edit: June 10, 2010, 02:59:52 am by gengel »

gengel

  • Guest
Re: Extending the OnBehalf of Organization Fields
June 10, 2010, 03:30:45 am
Here's a very hackish solution:

$MODULENAME_CREATED = array()

function modulename_civicrm_post($op, $objectName, $objectId, &$objectRef) {
  global $MODULENAME_CREATED;
  /*_dbg('post called'); */
  if ($op == 'create') {
    _dbg(array($objectName, $objectId));
    $MODULENAME_CREATED[] = array($objectName, $objectId);
    _dbg($_SESSION);
  }
}

then in

function moduelname_civicrm_postProcess($formName, &$form)

You can handle the form changes set up in buildForm, pulling the newly created id from $MODULENAME_CREATED

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: Extending the OnBehalf of Organization Fields
June 10, 2010, 07:19:54 am
Quote from: gengel on June 10, 2010, 12:41:27 am
At the risk of sounding oblivious, this seems like something that shouldn't need to be programmed every time someone wants to change these - why not just use a Profile for the org fields instead of having a hard-coded set?

As with many other things, lots of people want a general solution, but no one is willing to spend the time implementing one (or hiring a developer to do so)

would be great if you can help do the needful and generalize it. Ping us on IRC if u need help getting started

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

johng

  • Guest
Re: Extending the OnBehalf of Organization Fields
June 10, 2010, 08:55:47 pm
I did get this mostly working using the APIs to create the data. I did not completely finish though so have not posted. I still need to prepopulate (renwals). Sadly, I have been working on another project recently and the client that wanted this did not want to pursue this any further. When I have some free time I will finish it up and post.

jimmyjam

  • I post occasionally
  • **
  • Posts: 87
  • Karma: 4
Re: Extending the OnBehalf of Organization Fields
June 29, 2010, 12:59:41 pm
Hi John,

Were you able to continue your work on this feature?  If not, let me what it would take to make it over the finish line.

Thanks,
James

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Extending the OnBehalf of Organization Fields
July 02, 2010, 07:11:05 am
Jimmyjam,

I got your PM and responded.  The long and short of it is that I don't think anyone has the budget to sponsor a general solution for ~$3,000 right now.

BUT, if you can hire WebAccess (my recommendation) to make a Drupal module that will "use hooks" extend a particular Contribution Form to include Organizational Custom data in the Behalf Of section and save that data in the database, I'll co-sponsor the work with $250 of my own $.  I would like copy of the Drupal module code that I could then modify for my own purposes/sites.  If more $ is required, let me know, I'll consider it.
« Last Edit: July 02, 2010, 07:14:46 am by Stoob »
Try CiviTeacher: the online video tutorial CiviCRM learning library.

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Extending the OnBehalf of Organization Fields
July 25, 2010, 07:26:45 pm
We will match Stoob's donation and could have our arm twisted if more would help.
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

jimmyjam

  • I post occasionally
  • **
  • Posts: 87
  • Karma: 4
Re: Extending the OnBehalf of Organization Fields
July 26, 2010, 05:48:29 pm
Thanks, Peter. This will help a lot. Stoob and I have enough to cover adding custom fields to the confirmation form, but not to the confirmation page and email receipt. Hopefully, your extra $250 will be enough to complete the project.

I'll check with Claudian and Web Access and let you know.

Best regards,
James

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviContribute (Moderator: Donald Lobo) »
  • Extending the OnBehalf of Organization Fields

This forum was archived on 2017-11-26.