AnsweredAssumed Answered

Custom code blocks creation of relationship

Question asked by community-support on Jan 13, 2014
Latest reply on Jan 13, 2014 by community-support
I have created a custom module - Events - and connected it to the Contacts module.  This is a revision to my earlier Event Management module where I am trying to update the code and create a 'cleaner' solution that is more upgrade-proof.

I need to have some information as part of the EventsContacts (ManyMany) relationship, like:

- Attendee Status: Invited, Confirmed, NeverShowed, Participated, Graduated (dropdown to allow for massupdate)

- # Hours of Continuing Education Credits (dropdown to allow for massupdate)

My previous method in doing this was to match the OpportunitiesContacts method where they store Role, but this required substantial file edits and could not be done completely in the Custom directory, so this time, I am using an intermediate module - EventParticipants with a

  - Many... and I am working on that (any suggestions welcome; I cannot (yet) figure out how to do the double table joins in $bean->) but that is not, I suspect, the issue so I really need to get the blocking stopped.

I am doing this on a fresh install of SugarCRM CE 6.5.16 which sits on a fresh CentOS 6.4 LAMP stack.

Details:

The package was created in Module Builder with:

Package Name: EvMgr

Events Module Name: Evs (which becomes Module EvMgr_Evs)

Events Participants Module Name: EvParts (which becomes Module EvMgr_EvParts)

In the custom code for the EventParticipants module, I had it calculate the Name field by concatenating the EventName and the Contact Name.  This way, we know the naming convention will be consistent.  To do this, I added, into:

(using info from  http://developer.sugarcrm.com/2011/10... )

/modules/EvMgr_EvParts/EvMgr_EvParts.php

[CODE]

...License information ...

/**

  * THIS CLASS IS FOR DEVELOPERS TO MAKE CUSTOMIZATIONS IN

  */

require_once('modules/EvMgr_EvParts/EvMgr_EvParts_sugar.php');

class EvMgr_EvParts extends EvMgr_EvParts_sugar {

  function EvMgr_EvParts(){

   parent::EvMgr_EvParts_sugar();

  }

  function retrieve($id) {

   // Custom logic here to emulate the logic hook of 'before_retrieve'

   parent::retrieve($id);

   // Custom logic here to emulate the logic hook of 'after_retrieve'

   // Check to see if the entry Name is still equal to the concatenation of the Event Name and the Attendee Name

   // If not (one or other may have ben changed after original entry)

   // then re-save so the entry Name is equal to the concatenation of the Event Name and the Attendee Name

   $current_name = $this->name;

   $event_name = $this->evmgr_evparts_evmgr_evs_name;

   $attendee_name = $this-> evmgr_evparts_contacts_name;

   $calc_name = $event_name . ' - ' . $attendee_name;

   if ( '$current_name' !== '$calc_name')

   {

    $this->save();

   }

  }

  function save($check_notify = FALSE) {

   // Custom logic here to emulate the logic hook of 'before_save'

   // Concatenate the Event Name and the Attendee Name to create a Name for the new entry

   $event_name = $this->evmgr_evparts_evmgr_evs_name;

   $attendee_name = $this-> evmgr_evparts_contacts_name;

   $calc_name = $event_name . ' - ' . $attendee_name;

   $this->name = $calc_name;

   parent::save($check_notify);

   // Custom logic here to emulate the logic hook of 'after_save'

  }

}

?>

[/CODE]

That worked great

Then I added the custom code into

/modules/EvMgr_Evs/EvMgr_Evs.php

[CODE]

...License information ...

/**

  * THIS CLASS IS FOR DEVELOPERS TO MAKE CUSTOMIZATIONS IN

  */

require_once('modules/EvMgr_Evs/EvMgr_Evs_sugar.php');

class EvMgr_Evs extends EvMgr_Evs_sugar {

  function EvMgr_Evs(){

   parent::EvMgr_Evs_sugar();

  }

  function retrieve($id) {

   // Custom logic here to emulate the logic hook of 'before_retrieve'

   parent::retrieve($id);

   // Custom logic here to emulate the logic hook of 'after_retrieve'

   global $db;

   $event_id = $this->id;

   $need_save = false;

   // Calculate the count of Attendees for this Event for each of the 5 Attendee Status options

   $attendee_status_array = array('Invited','Confirmed','NeverShowed','Participated','Graduated');

   $attendee_status_count_array = array('num_invited','num_confirmed','num_never_showed','num_participated','num_graduated');

   for ( $counter=0 ; $counter{$event_attendee_status_count};

    $query = 'SELECT COUNT(*) AS number_counted'.

    ' FROM evmgr_evs'.

    ' LEFT JOIN evmgr_evparts_evmgr_evs_c ON evmgr_evparts_evmgr_evs_c.evmgr_evparts_evmgr_evsevmgr_evs_ida = evmgr_evs.id'.

    ' LEFT JOIN evmgr_evparts ON evmgr_evparts.id = evmgr_evparts_evmgr_evs_c.evmgr_evparts_evmgr_evsevmgr_evparts_idb'.

    ' WHERE'.

    ' evmgr_evparts_evmgr_evs_c.deleted = '0' AND'.

    ' evmgr_evparts_evmgr_evs_c.evmgr_evparts_evmgr_evsevmgr_evs_ida = '$event_id' AND'.

    ' evmgr_evparts.deleted = '0' AND'.

    ' evmgr_evparts.attendee_status = '$event_attendee_status';';

    $results = $this->db->query($query, true);

    $row = $this->db->fetchByAssoc($results);

    $calc_num = $row['number_counted'];

    if ($calc_num != $current_num)

    {

     $this->{$event_attendee_status_count} = $calc_num;

     $need_save = true;

    }

   }

   if ($need_save)

   {

    $this->save();

   }

  }

  function save($check_notify = FALSE) {

   // Custom logic here to emulate the logic hook of 'before_save'

   // Calculate the totals for Budget and Actual Costs

   $this->budget_total = $this->budget_facilitator + $this->budget_venue + $this->budget_caterer + $this->budget_material + $this->budget_other;

   $this->actual_cost_total = $this->actual_cost_facilitator + $this->actual_cost_venue + $this->actual_cost_caterer + $this->actual_cost_material + $this->actual_cost_other;

   parent::save($check_notify);

   // Custom logic here to emulate the logic hook of 'after_save'

  }

}

?>

[/CODE]

and now, when I use EventParticipants and try to create the relationship, it appears to work until I Save the entry and it just wipes out the half of the relationship pointing to the Events module.  Nothing is entered in the database for teh Events module, but the relationship to the Contacts module is fine.

By the way, the $this->budget_total is additional code working fine to total up some budget (and actual) numbers.

Any ideas how to have custom code in both the EventParticipants module and the Events module without one blocking the other?

(from Ramblin)

Outcomes