You are here

party_log.module in Party 7

Provide a framework for logging things that happen to and with parties.

File

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

/**
 * @file
 * Provide a framework for logging things that happen to and with parties.
 */

/**
 * Implements hook_views_api().
 */
function party_log_views_api() {
  return array(
    "api" => "3.0",
  );
}

/**
 * Implements hook_default_message_type_category().
 *
 * Provide a default message type category for all party messages.
 */
function party_log_default_message_type_category() {
  $categories = array();
  $categories['party_log'] = entity_import('message_type_category', '{
    "name" : "party_log",
    "description" : "Party Log Message"
  }');
  return $categories;
}

/**
 * Implements hook_default_message_type().
 */
function party_log_default_message_type() {
  $items = array();

  // Log a message for the attaching and detaching of different data_sets.
  foreach (party_get_data_set_info() as $info) {
    $items['party_data_set_attach_' . $info['set_name']] = entity_import('message_type', '{
      "name" : "party_data_set_attach_' . $info['set_name'] . '",
      "description" : "Party Attach ' . $info['label'] . '",
      "argument_keys" : [ "@entity_label" ],
      "argument" : [],
      "category" : "party_log",
      "data" : {
        "token options" : { "clear" : 0 },
        "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
      },
      "language" : "",
      "arguments" : null,
      "message_text" : { "und" : [
          {
            "value" : "Attached @entity_label to the ' . $info['label'] . ' data set.",
            "format" : "plain_text",
            "safe_value" : "\\u003Cp\\u003EAttached @entity_label to the ' . $info['label'] . ' data set.\\u003C\\/p\\u003E\\n"
          }
        ]
      },
      "rdf_mapping" : []
    }');
    $items['party_data_set_detach_' . $info['set_name']] = entity_import('message_type', '{
      "name" : "party_data_set_detach_' . $info['set_name'] . '",
      "description" : "Party Detach ' . $info['label'] . '",
      "argument_keys" : [ "@entity_label" ],
      "argument" : [],
      "category" : "party_log",
      "data" : {
        "token options" : { "clear" : 0 },
        "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
      },
      "language" : "",
      "arguments" : null,
      "message_text" : { "und" : [
          {
            "value" : "Detached @entity_label from the ' . $info['label'] . ' data set.",
            "format" : "plain_text",
            "safe_value" : "\\u003Cp\\u003EDetached @entity_label to the ' . $info['label'] . ' data set.\\u003C\\/p\\u003E\\n"
          }
        ]
      },
      "rdf_mapping" : []
    }');
  }
  $items['party_merged_into'] = entity_import('message_type', '{
    "name" : "party_merged_into",
    "description" : "Party is merged into another party.",
    "argument_keys" : [ "@other_party_label" ],
    "argument" : [],
    "category" : "party_log",
    "data" : {
      "token options" : { "clear" : 0 },
      "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
    },
    "language" : "",
    "arguments" : null,
    "message_text" : { "und" : [
        {
          "value" : "This record was merged into @other_party_label.",
          "format" : "plain_text",
          "safe_value" : "\\u003Cp\\u003EThis record was merged into @other_party_label.\\u003C\\/p\\u003E\\n"
        }
      ]
    },
    "rdf_mapping" : []
  }');
  $items['party_had_other_merged_in'] = entity_import('message_type', '{
    "name" : "party_had_other_merged_in",
    "description" : "Party had another party merged into it.",
    "argument_keys" : [ "@other_party_label" ],
    "argument" : [],
    "category" : "party_log",
    "data" : {
      "token options" : { "clear" : 0 },
      "purge" : { "override" : 0, "enabled" : 0, "quota" : "", "days" : "" }
    },
    "language" : "",
    "arguments" : null,
    "message_text" : { "und" : [
        {
          "value" : "@other_party_label was merged into this party.",
          "format" : "plain_text",
          "safe_value" : "\\u003Cp\\u003E@other_party_label was merged into this party.\\u003C\\/p\\u003E\\n"
        }
      ]
    },
    "rdf_mapping" : []
  }');
  return $items;
}

/**
 * Implements hook_field_attach_create_bundle().
 *
 * Add a party reference field to every bundle that is made part of the party
 * log message_type_category.
 */
function party_log_field_attach_create_bundle($entity_type, $bundle) {
  if ($entity_type != 'message') {
    return;
  }
  $message_type = entity_load_single('message_type', $bundle);
  if ($message_type->category != 'party_log') {
    return;
  }

  // Add a party field.
  if (!field_info_field('party')) {
    $field = array(
      'field_name' => 'party',
      'type' => 'entityreference',
      'cardinality' => 1,
      'settings' => array(
        'target_type' => 'party',
        'handler' => 'base',
        'handler_settings' => array(
          'sort' => array(
            'type' => 'none',
          ),
        ),
      ),
    );
    field_create_field($field);
  }
  if (!field_info_instance('message', 'party', $bundle)) {
    $instance = array(
      'field_name' => 'party',
      'entity_type' => 'message',
      'bundle' => $bundle,
      'label' => t('Party'),
      'required' => TRUE,
      'settings' => array(
        'default_value' => '',
      ),
      'widget' => array(
        'type' => 'entityreference_autocomplete',
        'settings' => array(
          'size' => '60',
          'match_operator' => 'CONTAINS',
        ),
      ),
    );
    field_create_instance($instance);
  }

  // If these are related to attaching or detaching entities add a reference
  // field to the appropriate entity type.
  if (in_array(substr($bundle, 0, 21), array(
    'party_data_set_detach',
    'party_data_set_attach',
  ))) {
    $data_set_name = substr($bundle, 22);
    $info = party_get_data_set_info($data_set_name);
    $entity_type = $info['entity type'];
    $field_name = 'party_log_' . $entity_type;

    // Attached entity reference field.
    if (!field_info_field($field_name)) {
      $field = array(
        'field_name' => $field_name,
        'type' => 'entityreference',
        'cardinality' => 1,
        'settings' => array(
          'target_type' => $entity_type,
          'handler' => 'base',
          'handler_settings' => array(
            'sort' => array(
              'type' => 'none',
            ),
          ),
        ),
      );
      field_create_field($field);
    }
    if (!field_info_instance('message', $field_name, $bundle)) {
      $instance = array(
        'field_name' => $field_name,
        'entity_type' => 'message',
        'bundle' => $bundle,
        'label' => $info['label'],
        'required' => TRUE,
        'settings' => array(
          'default_value' => '',
        ),
        'widget' => array(
          'type' => 'entityreference_autocomplete',
          'settings' => array(
            'size' => '60',
            'match_operator' => 'CONTAINS',
          ),
        ),
      );
      field_create_instance($instance);
    }
  }
}

/**
 * Implements hook_entity_merge().
 */
function party_log_entity_merge($target_entity, $entity_1, $entity_2, $entity_type) {
  global $user;
  if ($entity_type != 'party') {
    return;
  }

  // Build message
  $values = array(
    'arguments' => array(
      '@other_party_label' => entity_label($entity_type, $target_entity),
    ),
    'timestamp' => time(),
  );
  $message = message_create('party_merged_into', $values, $user);
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->party = $entity_2;
  $wrapper->other_party = $target_entity;
  $wrapper
    ->save();

  // Build message
  $values = array(
    'arguments' => array(
      '@other_party_label' => entity_label($entity_type, $entity_2),
    ),
    'timestamp' => time(),
  );
  $message = message_create('party_had_other_merged_in', $values, $user);
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->party = $target_entity;
  $wrapper->other_party = $entity_2;
  $wrapper
    ->save();
}