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 (Moderator: Donald Lobo) »
  • miscelaneous custom code tweaks for civicrm 4.4
Pages: [1]

Author Topic: miscelaneous custom code tweaks for civicrm 4.4  (Read 476 times)

Erich Schulz

  • I post frequently
  • ***
  • Posts: 142
  • Karma: 5
    • When no-one understands what you are going on about its time to start a blog
  • CiviCRM version: 4.4
  • CMS version: Drupal 7
  • MySQL version: 5.somthing
  • PHP version: 5.3.3
miscelaneous custom code tweaks for civicrm 4.4
January 14, 2014, 12:01:26 am
Hi as part of upgrading a legacy module to civicrm 4.4 I had to do the following... I *think* this is right - but not sure... so posting here for review - also because googling never found me a concrete reference page so this may help others.

Code: [Select]
diff --git a/e/utils.php b/e/utils.php
index fe82bf1..98e8961 100644
--- a/e/utils.php
+++ b/e/utils.php
@@ -139,12 +139,14 @@ function GetFieldOptionGroupList($fieldId) {
 }
 
 function LookupPhoneTypeLabel($phonetypeid) {
-  $phtypes = CRM_Core_PseudoConstant::phoneType();
+  $phtypes = CRM_Core_PseudoConstant::get('CRM_Core_BAO_Phone', 'phone_type_id');
   return $phtypes[$phonetypeid];
 }
 
 function LookupLocationTypeLabel($locationtypeid) {
-  $loctypes = CRM_Core_PseudoConstant::locationType();
+  $loctypes = CRM_Core_PseudoConstant::get('CRM_Core_BAO_Address', 'location_type_id');
   return $loctypes[$locationtypeid];
 }
 
@@ -153,13 +155,17 @@ function LookupStateProvinceLabel($state_province_id) {
   return $stateProvinceList[$state_province_id];
 }
 
+function GetActivities( $contactId, $activityTypeId ) {
-    $query ="SELECT * FROM civicrm_activity activity, civicrm_activity_target target
+    $query ="SELECT * FROM civicrm_activity activity, civicrm_activity_contact target
              WHERE activity.id = target.activity_id
-             AND target.target_contact_id = $contactId
+             AND target.contact_id = $contactId
              AND activity.activity_type_id = $activityTypeId
              ORDER BY activity_date_time DESC";
+    // and record_type_id = 1 ???
     $dao = CRM_Core_DAO::executeQuery( $query, CRM_Core_DAO::$_nullArray );
     $activities = array();
     while( $dao->fetch() ){


Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: miscelaneous custom code tweaks for civicrm 4.4
January 14, 2014, 09:03:27 am
That will work, but I have 2 recommendations for better ways to do it:
Better way 1: Use the api.
Code: [Select]
function LookupPhoneTypeLabel($phonetypeid) {
-  $phtypes = CRM_Core_PseudoConstant::phoneType();
-  return $phtypes[$phonetypeid];
+  $phtypes = civicrm_api('phone', 'getoptions', array('field' => 'phone_type_id'));
+  return $phtypes['values'][$phonetypeid];
 }
Better way 2: Get rid of the function entirely.
Code: [Select]
// Lookup some phone type
-  $label = LookupPhoneTypeLabel(2);
+  $label = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_Phone', 'phone_type_id', 2);
For your GetActivities function I think this could almost certainly use the api.
Try asking your question on the new CiviCRM help site.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: miscelaneous custom code tweaks for civicrm 4.4
January 14, 2014, 10:59:14 am
I don't think it's relevant to your code snippet, Erich, but it's also worth noting that in general if you know the label you don't need to translate it to an id - eg. for phone.get location_type_id will accept either 'Billing' or 5
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

Coleman Watts

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 2346
  • Karma: 183
  • CiviCRM version: The Bleeding Edge
  • CMS version: Various
Re: miscelaneous custom code tweaks for civicrm 4.4
January 14, 2014, 01:17:52 pm
Correction to that - the api translates names not labels. A subtle difference if your site is in English, but an important one.
Here's the documentation for all this, should have given you this link before.
Pseudoconstant (option list) Reference
Try asking your question on the new CiviCRM help site.

Erich Schulz

  • I post frequently
  • ***
  • Posts: 142
  • Karma: 5
    • When no-one understands what you are going on about its time to start a blog
  • CiviCRM version: 4.4
  • CMS version: Drupal 7
  • MySQL version: 5.somthing
  • PHP version: 5.3.3
Re: miscelaneous custom code tweaks for civicrm 4.4
January 14, 2014, 10:54:02 pm
sweet - thanks!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • miscelaneous custom code tweaks for civicrm 4.4

This forum was archived on 2017-11-26.