/**
  * Ensure all profiles and custom fields are created.
  */
 function upgrade_1003()
 {
     // These are indempotent.
     if (FALSE === petitionemail_create_custom_fields()) {
         return FALSE;
     }
     if (FALSE === petitionemail_get_profile_id('petitionemail_profile_matching_fields')) {
         return FALSE;
     }
     if (FALSE === petitionemail_get_profile_id('petitionemail_profile_default_contact')) {
         return FALSE;
     }
     if (FALSE === petitionemail_get_profile_id('petitionemail_profile_default_activity')) {
         return FALSE;
     }
     return TRUE;
 }
/**
 * Get fields from the special petition email profile.
 *
 * Filter out un-supported fields.
 */
function petitionemail_get_matching_field_options()
{
    $session = CRM_Core_Session::singleton();
    $ret = array();
    $uf_group_id = petitionemail_get_profile_id('petitionemail_profile_matching_fields');
    $fields = CRM_Core_BAO_UFGroup::getFields($uf_group_id);
    $allowed = petitionemail_get_allowed_matching_fields();
    if (is_array($fields)) {
        reset($fields);
        while (list($id, $value) = each($fields)) {
            $include = FALSE;
            // Check to see if it's a custom field
            if (preg_match('/^custom_/', $id)) {
                $ret[$id] = $value['title'];
                continue;
            } else {
                // Check to see if it's an address field
                $field_pieces = petitionemail_split_address_field($id);
                if ($field_pieces) {
                    if ($field_pieces['location_name'] != 'Primary') {
                        $session->setStatus(ts("Only primary address fields are support at this time."));
                        continue;
                    }
                    if (array_key_exists($field_pieces['field_name'], $allowed)) {
                        $ret[$id] = $value['title'];
                        continue;
                    }
                }
            }
            // Warn the user about a field that is not allowed
            $session->setStatus(ts("The field {$id} is not supported as a matching field at this time."));
        }
    }
    return $ret;
}