You are here

function redhen_relation_install in RedHen CRM 7

Implements hook_install().

File

modules/redhen_relation/redhen_relation.install, line 70
Install, update and uninstall functions for the redhen relations module.

Code

function redhen_relation_install() {

  // Create default Admin and Member roles.
  $default_roles = array(
    'admin' => array(
      'name' => 'admin',
      'label' => 'Admin',
      'permissions' => array(
        'add_relation' => 'add_relation',
        'edit_relation' => 'edit_relation',
        'delete_relation' => 'delete_relation',
      ),
      'default_role' => 0,
    ),
    'member' => array(
      'name' => 'member',
      'label' => 'Member',
      'permissions' => array(),
      'default_role' => 1,
    ),
  );
  foreach ($default_roles as $default_role) {
    $relation_role = entity_create('redhen_relation_role', $default_role);
    $relation_role
      ->save();
  }

  // Add default relationships.
  $relation_types_info = array(
    array(
      'relation_type' => REDHEN_RELATION_CONNECTION,
      'label' => t('Personal connection'),
      'source_bundles' => array(
        'redhen_contact:*',
      ),
      'r_unique' => TRUE,
    ),
    array(
      'relation_type' => REDHEN_RELATION_AFFILIATION,
      'label' => t('Organizational affiliation'),
      'reverse_label' => t('Affiliated with'),
      'directional' => TRUE,
      'r_unique' => TRUE,
      'source_bundles' => array(
        'redhen_contact:*',
      ),
      'target_bundles' => array(
        'redhen_org:*',
      ),
    ),
  );
  foreach ($relation_types_info as $relation_type_info) {
    $relation_type = relation_type_create($relation_type_info);
    relation_type_save($relation_type);
  }

  // Add relation status field.
  $status_field = array(
    'field_name' => REDHEN_RELATION_STATUS_FIELD,
    'type' => 'list_boolean',
    'locked' => TRUE,
    'cardinality' => 1,
    'settings' => array(
      'allowed_values' => array(
        'Inactive',
        'Active',
      ),
    ),
  );
  field_create_field($status_field);
  $bundles = array(
    REDHEN_RELATION_AFFILIATION => 'Active',
    REDHEN_RELATION_CONNECTION => 'Active',
  );
  foreach ($bundles as $key => $label) {
    field_create_instance(array(
      'field_name' => REDHEN_RELATION_STATUS_FIELD,
      'entity_type' => 'relation',
      'bundle' => $key,
      'label' => $label,
      'required' => 0,
      'default_value' => array(
        array(
          'value' => 1,
        ),
      ),
      'widget' => array(
        'type' => 'options_onoff',
        'settings' => array(
          'display_label' => 1,
        ),
      ),
    ));
  }

  // Add relation role field.
  $role_field = array(
    'field_name' => REDHEN_RELATION_ROLES_FIELD,
    'type' => 'entityreference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'target_type' => 'redhen_relation_role',
      'target_bundles' => array(
        'redhen_relation_role',
      ),
    ),
  );
  field_create_field($role_field);

  // Get default value: ID of Member role.
  $member_role_entity = entity_load('redhen_relation_role', FALSE, array(
    'name' => 'member',
  ));
  $member_role_id = (int) array_shift($member_role_entity)->redhen_relation_role_id;
  field_create_instance(array(
    'field_name' => REDHEN_RELATION_ROLES_FIELD,
    'entity_type' => 'relation',
    'bundle' => REDHEN_RELATION_AFFILIATION,
    'label' => 'Organization Role',
    'description' => 'Select the roles this contact has in this organization.',
    'required' => 0,
    'default_value' => array(
      array(
        'target_id' => $member_role_id,
      ),
    ),
    'widget' => array(
      'type' => 'options_buttons',
    ),
  ));
  entity_info_cache_clear();
}