You are here

function farm_group_membership_set in farmOS 7

Create a log for assigning assets to group(s).

Parameters

array|FarmAsset $assets: Array of assets to assign to the groups.

array $groups: An array of groups to move to.

int $timestamp: The timestamp of the assignment. Defaults to the current time.

string $log_type: The type of log to create. Defaults to "farm_observation".

bool $done: Boolean indicating whether or not the log should be marked "done". Defaults to TRUE.

Return value

\Log Returns the log that was created.

3 calls to farm_group_membership_set()
farm_group_asset_form_submit in modules/farm/farm_group/farm_group.module
Submit handler for processing the asset group field.
farm_group_asset_membership_action in modules/farm/farm_group/farm_group.module
Action function for farm_group_asset_membership.
_farm_livestock_update_7005_stage3 in modules/farm/farm_livestock/farm_livestock.install
Update 7005 stage 3: Assign animals to new group assets.

File

modules/farm/farm_group/farm_group.module, line 932

Code

function farm_group_membership_set($assets, $groups = array(), $timestamp = REQUEST_TIME, $log_type = 'farm_observation', $done = TRUE) {

  // If there are no groups specified, bail.
  if (empty($groups)) {
    return;
  }

  // If $assets isn't an array, wrap it.
  if (!is_array($assets)) {
    $assets = array(
      $assets,
    );
  }

  // If the log is an observation, set the name to:
  // "Group [assets] into [groups]".
  $log_name = '';
  if ($log_type == 'farm_observation') {
    $assets_summary = farm_log_entity_label_summary('farm_asset', $assets);
    $groups_summary = farm_log_entity_label_summary('farm_asset', $groups);
    $log_name = t('Group !assets into !groups', array(
      '!assets' => $assets_summary,
      '!groups' => $groups_summary,
    ));
  }

  // Create a new farm log entity.
  $log = farm_log_create($log_type, $log_name, $timestamp, $done, $assets);

  // Create a new membership field_collection entity attached to the log.
  $membership = entity_create('field_collection_item', array(
    'field_name' => 'field_farm_membership',
  ));
  $membership
    ->setHostEntity('log', $log);

  // Create an entity wrapper for the membership.
  $membership_wrapper = entity_metadata_wrapper('field_collection_item', $membership);

  // Iterate through the areas and add each to the "Move to" field.
  // If they are not group assets, ignore them.
  foreach ($groups as $group) {
    if ($group->type == 'group') {
      $membership_wrapper->field_farm_group[] = $group;
    }
  }

  // Save the membership.
  $membership_wrapper
    ->save();

  // Return the log.
  return $log;
}