You are here

civicrm_entity_views_extras.module in CiviCRM Entity 7.2

Provide CiviCRM entity views extras, Views relationships and other handlers for entities exposed by CiviCRM Entity

File

modules/civicrm_entity_views_extras/civicrm_entity_views_extras.module
View source
<?php

/**
 * @file
 * Provide CiviCRM entity views extras, Views relationships and other handlers for entities exposed by CiviCRM Entity
 */

/**
 * Implements hook_views_api().
 */
function civicrm_entity_views_extras_views_api() {
  return array(
    'api' => 3,
  );
}

/**
 * Implements hook_views_data().
 */
function civicrm_entity_views_extras_views_data() {
  $enabled_entities = _civicrm_entity_enabled_entities();
  if (!empty($enabled_entities['civicrm_price_set'])) {
    $data['civicrm_price_set_entity']['table']['group'] = t('CiviCRM Price Set Entities');
    $data['civicrm_price_set_entity']['table']['base'] = [
      'field' => 'id',
      'title' => t('CiviCRM Price Set Entities'),
      'help' => t("View displays CiviCRM Price Set to Entity mapping info"),
    ];

    //----------------------------------------------------------------

    // CIVICRM Price Set Entity - FIELDS

    //----------------------------------------------------------------

    // Numeric: Price Set ID
    $data['civicrm_price_set_entity']['price_set_id'] = [
      'title' => t('Price Set ID'),
      'help' => t('The numeric ID of the Price Set'),
      'field' => [
        'handler' => 'views_handler_field_numeric',
        'click sortable' => TRUE,
      ],
      'argument' => [
        'handler' => 'views_handler_argument_numeric',
        'numeric' => TRUE,
      ],
      'filter' => [
        'handler' => 'views_handler_filter_numeric',
      ],
      'sort' => [
        'handler' => 'views_handler_sort',
      ],
    ];

    // Entity Table
    $data['civicrm_price_set_entity']['entity_table'] = [
      'title' => t('Entity Table'),
      'help' => t('The entity table for the Price Set'),
      'field' => [
        'handler' => 'views_handler_field',
        'click sortable' => TRUE,
      ],
      'argument' => [
        'handler' => 'views_handler_argument_string',
      ],
      'filter' => [
        'handler' => 'views_handler_filter_string',
        'allow empty' => TRUE,
      ],
      'sort' => [
        'handler' => 'views_handler_sort',
      ],
    ];

    //----------------------------------------------------------------

    // CIVICRM Price Set Entity - TABLE JOINS

    //----------------------------------------------------------------
    $data['civicrm_price_set_entity']['table']['join'] = [
      // Directly links to event table.
      'civicrm_event' => [
        'left_field' => 'id',
        'field' => 'entity_id',
      ],
    ];

    //----------------------------------------------------------------

    // CIVICRM Price Set Entity - RELATIONSHIPS

    //----------------------------------------------------------------

    // Price Set
    $data['civicrm_price_set_entity']['civicrm_price_set'] = array(
      'title' => t('Price Set'),
      'help' => t('Price Set for this Entity'),
      'real field' => 'price_set_id',
      'relationship' => array(
        'base' => 'civicrm_price_set',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('Price Set'),
      ),
    );
    if (!empty($enabled_entities['civicrm_event'])) {
      $data['civicrm_event']['table']['join']['civicrm_price_set_entity'] = array(
        'left_field' => 'entity_id',
        'field' => 'id',
      );
    }
    if (!empty($enabled_entities['civicrm_contribution_page'])) {

      // Contribution page to price set entity
      $data['civicrm_contribution_page']['civicrm_price_set_entity'] = array(
        'title' => t('Price Set Entity'),
        'help' => t('Price Set Entity'),
        'real field' => 'id',
        'relationship' => array(
          'base' => 'civicrm_price_set_entity',
          'base field' => 'entity_id',
          'handler' => 'views_handler_relationship',
          'label' => t('Price Set Entity'),
        ),
      );
    }
    if (!empty($enabled_entities['civicrm_price_field'])) {
      $data['civicrm_price_set']['civicrm_price_field'] = array(
        'group' => t('Price Field'),
        'title' => t('CiviCRM Price field'),
        'help' => t('Joins with the civicrm_price_field table'),
        'relationship' => array(
          'handler' => 'views_handler_relationship',
          'label' => t('Price Field'),
          'title' => t('Price Field'),
          'base' => 'civicrm_price_field',
          'base field' => 'price_set_id',
          'relationship field' => 'id',
        ),
      );
    }
  }

  // end if !empty($enabled_entities['civicrm_price_set'])
  if (!empty($enabled_entities['civicrm_price_field']) && !empty($enabled_entities['civicrm_price_field_value'])) {

    // Price Field Value
    $data['civicrm_price_field']['civicrm_price_field_value'] = array(
      'title' => t('Price Field Value'),
      'help' => t('Value(s) for this Price Field'),
      'real field' => 'id',
      'relationship' => array(
        'base' => 'civicrm_price_field_value',
        'base field' => 'price_field_id',
        'handler' => 'views_handler_relationship',
        'label' => t('Price Field Value'),
      ),
    );
  }

  //----------------------------------------------------------------

  // CIVICRM Membership Payment - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_membership_payment'])) {

    // Membership
    $data['civicrm_membership']['civicrm_membership_payment'] = [
      'title' => t('CiviCRM Membership Payment'),
      'help' => t('Relates Membership Payment to Membership'),
      'real field' => 'id',
      'relationship' => [
        'base' => 'civicrm_membership_payment',
        'base field' => 'membership_id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Membership Payment'),
      ],
    ];
    $data['civicrm_membership_payment']['civicrm_membership'] = [
      'title' => t('CiviCRM Membership'),
      'help' => t('Relates Membership to Membership Payment'),
      'real field' => 'membership_id',
      'relationship' => [
        'base' => 'civicrm_membership',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Memberships'),
      ],
    ];

    // Contribution
    $data['civicrm_membership_payment']['civicrm_contribution'] = [
      'title' => t('CiviCRM Contributions'),
      'help' => t('Relates Contribution to Membership Payment'),
      'real field' => 'contribution_id',
      'relationship' => [
        'base' => 'civicrm_contribution',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Contributions'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Participant Payment - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_participant_payment'])) {

    // Participant
    $data['civicrm_participant']['civicrm_participant_payment'] = [
      'title' => t('CiviCRM Participant Payment'),
      'help' => t('Relates Participant Payment to Participant'),
      'real field' => 'id',
      'relationship' => [
        'base' => 'civicrm_participant_payment',
        'base field' => 'participant_id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Participant Payment'),
      ],
    ];
    $data['civicrm_participant_payment']['civicrm_participant'] = [
      'title' => t('CiviCRM Participant'),
      'help' => t('Relates Participant to Participant Payment'),
      'real field' => 'participant_id',
      'relationship' => [
        'base' => 'civicrm_participant',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Participant'),
      ],
    ];

    // Contribution
    $data['civicrm_participant_payment']['civicrm_contribution'] = [
      'title' => t('CiviCRM Contributions'),
      'help' => t('Relates Contribution to Membership Payment'),
      'real field' => 'contribution_id',
      'relationship' => [
        'base' => 'civicrm_contribution',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Contributions'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Relationship - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_entity_relationship_type'])) {
    $data['civicrm_relationship']['civicrm_relationship_type'] = [
      'title' => t('CiviCRM Relationship Type'),
      'help' => t('Relates Relationship Type to Relationships'),
      'real field' => 'relationship_type_id',
      'relationship' => [
        'base' => 'civicrm_relationship_type',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Relationship Type'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Contributions - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_line_item'])) {
    $data['civicrm_contribution']['civicrm_line_item'] = [
      'title' => t('CiviCRM Line Items'),
      'help' => t('Relates Line Items to Contributions'),
      'real field' => 'id',
      'relationship' => [
        'base' => 'civicrm_line_item',
        'base field' => 'contribution_id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Line Items'),
      ],
    ];
  }
  if (!empty($enabled_entities['civicrm_entity_financial_trxn'])) {
    $data['civicrm_contribution']['civicrm_entity_financial_trxn'] = [
      'title' => t('CiviCRM Line Items'),
      'help' => t('Relates Entity Financial Trxn to Contributions'),
      'real field' => 'id',
      'relationship' => [
        'base' => 'civicrm_entity_financial_trxn',
        'base field' => 'entity_id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Entity Financial Trxn'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Entity Financial Trxn - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_entity_financial_txrn']) && !empty($enabled_entities['civicrm_financial_trxn'])) {
    $data['civicrm_entity_financial_txrn']['civicrm_financial_trxn'] = [
      'title' => t('Financial Trxn'),
      'help' => t('Relates Financial Trxn to Entity Financial Trxn'),
      'real field' => 'financial_trxn_id',
      'relationship' => [
        'base' => 'civicrm_financial_trxn',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Financial Txrn'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Financial Trxn - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_financial_txrn']) && !empty($enabled_entities['civicrm_payment_processor'])) {
    $data['civicrm_financial_txrn']['civicrm_payment_processor'] = [
      'title' => t('Payment Processor'),
      'help' => t('Relates Payment Processor to Financial Trxn'),
      'real field' => 'payment_processor_id',
      'relationship' => [
        'base' => 'civicrm_payment_processor',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Payment Processor'),
      ],
    ];
  }
  if (!empty($enabled_entities['civicrm_financial_txrn']) && !empty($enabled_entities['civicrm_financial_account'])) {
    $data['civicrm_financial_txrn']['civicrm_financial_account_from'] = [
      'title' => t('Financial Account (From)'),
      'help' => t('Relates Financial Trxn to Financial Account (From)'),
      'real field' => 'from_financial_account_id',
      'relationship' => [
        'base' => 'civicrm_financial_account',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Financial Account (From)'),
      ],
    ];
    $data['civicrm_financial_txrn']['civicrm_financial_account_to'] = [
      'title' => t('Financial Account (To)'),
      'help' => t('Relates Financial Trxn to Financial Account (To)'),
      'real field' => 'to_financial_account_id',
      'relationship' => [
        'base' => 'civicrm_financial_account',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Financial Account (To)'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Payment Processor - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_payment_processor']) && !empty($enabled_entities['civicrm_payment_processor_type'])) {
    $data['civicrm_payment_processor']['civicrm_payment_processor_type'] = [
      'title' => t('Payment Processor type'),
      'help' => t('Relates Payment Processor Type to Payment Processor'),
      'real field' => 'payment_processor_type_id',
      'relationship' => [
        'base' => 'civicrm_payment_processor_type',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Payment Processor Type'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Line Items - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_line_item']) && !empty($enabled_entities['civicrm_price_field'])) {
    $data['civicrm_line_item']['civicrm_price_field'] = [
      'title' => t('CiviCRM Price Fields'),
      'help' => t('Relates Price Fields to Line Items'),
      'real field' => 'price_field_id',
      'relationship' => [
        'base' => 'civicrm_price_field',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Price Field'),
      ],
    ];
  }
  if (!empty($enabled_entities['civicrm_line_item']) && !empty($enabled_entities['civicrm_price_field_value'])) {
    $data['civicrm_line_item']['civicrm_price_field_value'] = [
      'title' => t('CiviCRM Price Field Value'),
      'help' => t('Relates Price Field Values to Line Items'),
      'real field' => 'price_field_value_id',
      'relationship' => [
        'base' => 'civicrm_price_field_value',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Price Field Value'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM World Region

  //----------------------------------------------------------------
  $data['civicrm_worldregion']['table']['group'] = t('CiviCRM World Region');
  $data['civicrm_worldregion']['table']['base'] = array(
    'field' => 'id',
    'title' => t('CiviCRM World Regions'),
    'help' => t("View displays CiviCRM World Regions"),
  );

  //----------------------------------------------------------------

  // CIVICRM World Region - FIELDS

  //----------------------------------------------------------------

  // ID
  $data['civicrm_worldregion']['id'] = array(
    'title' => t('ID'),
    'help' => t('The numeric ID of the world region'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
      'numeric' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  // Name
  $data['civicrm_worldregion']['name'] = array(
    'title' => t('Name'),
    'help' => t('The world region name'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
      'allow empty' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );

  //----------------------------------------------------------------

  // CIVICRM Country Relationships

  //----------------------------------------------------------------
  $data['civicrm_address']['civicrm_country'] = array(
    'title' => t('CiviCRM Country'),
    'help' => t('Relates Country to Address'),
    'real field' => 'country_id',
    'relationship' => array(
      'base' => 'civicrm_country',
      'base field' => 'id',
      'handler' => 'views_handler_relationship',
      'label' => t('CiviCRM Country'),
    ),
  );
  $data['civicrm_country']['civicrm_worldregion'] = array(
    'title' => t('CiviCRM World Region'),
    'help' => t('Relates World Region to Country'),
    'real field' => 'region_id',
    'relationship' => array(
      'base' => 'civicrm_worldregion',
      'base field' => 'id',
      'handler' => 'views_handler_relationship',
      'label' => t('CiviCRM World Region'),
    ),
  );
  if (!empty($enabled_entities['civicrm_im'])) {

    //----------------------------------------------------------------

    // CIVICRM IM

    //----------------------------------------------------------------
    $data['civicrm_im']['location_type'] = [
      'title' => t('Location Type'),
      'real field' => 'location_type_id',
      'help' => t('The location type for the IM'),
      'field' => [
        'handler' => 'civicrm_handler_field_pseudo_constant',
        'pseudo class' => 'CRM_Core_PseudoConstant',
        'pseudo method' => 'get',
        'dao class' => 'CRM_Core_DAO_IM',
        'click sortable' => TRUE,
      ],
      'argument' => [
        'handler' => 'views_handler_argument',
      ],
      'filter' => [
        'handler' => 'civicrm_handler_filter_pseudo_constant',
        'pseudo class' => 'CRM_Core_PseudoConstant',
        'pseudo method' => 'get',
        'dao class' => 'CRM_Core_DAO_IM',
      ],
      'sort' => [
        'handler' => 'views_handler_sort',
      ],
    ];
    $data['civicrm_im']['provider'] = [
      'title' => t('Provider'),
      'real field' => 'provider_id',
      'help' => t('The provider for the IM'),
      'field' => [
        'handler' => 'civicrm_handler_field_pseudo_constant',
        'pseudo class' => 'CRM_Core_PseudoConstant',
        'pseudo method' => 'get',
        'dao class' => 'CRM_Core_DAO_IM',
        'click sortable' => TRUE,
      ],
      'argument' => [
        'handler' => 'views_handler_argument',
      ],
      'filter' => [
        'handler' => 'civicrm_handler_filter_pseudo_constant',
        'pseudo class' => 'CRM_Core_PseudoConstant',
        'pseudo method' => 'get',
        'dao class' => 'CRM_Core_DAO_IM',
      ],
      'sort' => [
        'handler' => 'views_handler_sort',
      ],
    ];
    $data['civicrm_contact']['civicrm_im'] = [
      'title' => t('IM'),
      'help' => t('Instant Message Accounts'),
      'real field' => 'id',
      'relationship' => [
        'base' => 'civicrm_im',
        'base field' => 'contact_id',
        'handler' => 'views_handler_relationship',
        'label' => t('Contact IM'),
      ],
    ];
    $data['civicrm_im']['civicrm_contact'] = [
      'title' => t('Contact'),
      'help' => t('Contact for the instant message account'),
      'real field' => 'contact_id',
      'relationship' => [
        'base' => 'civicrm_contact',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('Contact'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Membership Type - RELATIONSHIPS

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_membership_type'])) {
    $data['civicrm_membership']['civicrm_membership_type'] = [
      'title' => t('Membership Type'),
      'help' => t('Relates Membership Type table to Memberships'),
      'real field' => 'membership_type_id',
      'relationship' => [
        'base' => 'civicrm_membership_type',
        'base field' => 'id',
        'handler' => 'views_handler_relationship',
        'label' => t('CiviCRM Membership Type'),
      ],
    ];
  }

  //----------------------------------------------------------------

  // CIVICRM Event Extras

  //----------------------------------------------------------------
  if (!empty($enabled_entities['civicrm_event'])) {
    $data['civicrm_event']['event_is_full'] = [
      'title' => t('Event Is Full'),
      'help' => t('Provided by CiviCRM Entity: 1 if event is full 0 otherwise.'),
      'field' => [
        'handler' => 'civicrm_entity_views_extras_handler_event_is_full',
      ],
    ];
  }
  return $data;
}

/**
 * Implements hook_views_data_alter().
 */
function civicrm_entity_views_extras_views_data_alter(&$data) {
  $enabled_entities = _civicrm_entity_enabled_entities();
  if (!empty($enabled_entities['civicrm_im'])) {
    if (!empty($data['civicrm_im'])) {
      $data['civicrm_im']['provider_id']['title'] = 'Provider ID';
      $data['civicrm_im']['location_type_id']['title'] = 'IM Location Type ID';
    }
  }
}