You are here

function farm_log_create in farmOS 7

Helper function for creating a farm log.

Parameters

string $type: The log type machine name.

string $name: Optionally specify a name for the new log.

int $timestamp: The timestamp of the log (defaults to REQUEST_TIME).

bool $done: Boolean indicating whether or not the log is done.

array $assets: An array of assets to reference in the log.

string $notes: Notes to add to the log.

array $categories: An array of categories to add to the log.

Return value

\Log Returns a saved log entity.

6 calls to farm_log_create()
farm_crop_planting_quick_form_submit in modules/farm/farm_crop/farm_crop.farm_quick.planting.inc
Planting quick form submit.
farm_group_membership_set in modules/farm/farm_group/farm_group.module
Create a log for assigning assets to group(s).
farm_inventory_set in modules/farm/farm_inventory/farm_inventory.module
Create a log for adjusting asset inventory.
farm_livestock_birth_form_submit in modules/farm/farm_livestock/farm_livestock.farm_quick.birth.inc
Submit callback for birth quick form.
farm_movement_create in modules/farm/farm_movement/farm_movement.location.inc
Create a log for moving assets to areas.

... See full list

File

modules/farm/farm_log/farm_log.module, line 782
Code for the Farm Log feature.

Code

function farm_log_create($type, $name = '', $timestamp = REQUEST_TIME, $done = TRUE, $assets = array(), $notes = '', $categories = array()) {

  // Create a new log entity.
  $log = entity_create('log', array(
    'type' => $type,
  ));

  // If a log name is specified, set it.
  if (!empty($name)) {

    // If the name is too long, truncate it.
    if (strlen($name) > 255) {
      truncate_utf8($name, 255, TRUE, TRUE);
    }

    // Set the log name.
    $log->name = $name;
  }

  // Set the timestamp.
  $log->timestamp = $timestamp;

  // Set the log's done status.
  if (!empty($done)) {
    $log->done = TRUE;
  }
  else {
    $log->done = FALSE;
  }

  // Add asset references.
  if (!empty($assets)) {
    foreach ($assets as $asset) {
      if (!empty($asset->id)) {
        $log->field_farm_asset[LANGUAGE_NONE][] = array(
          'target_id' => $asset->id,
        );
      }
    }
  }

  // Add the notes.
  if (!empty($notes)) {
    $log->field_farm_notes[LANGUAGE_NONE][0]['value'] = $notes;
    $log->field_farm_notes[LANGUAGE_NONE][0]['format'] = 'farm_format';
  }

  // Add the categories.
  if (!empty($categories)) {
    foreach ($categories as $category) {

      // Create/load the category term.
      $term = farm_term($category, 'farm_log_categories');

      // Add the category to the log.
      $log->field_farm_log_category[LANGUAGE_NONE][] = array(
        'tid' => $term->tid,
      );
    }
  }

  // Save the log.
  log_save($log);

  // Set a message.
  $label = entity_label('log', $log);
  $uri = entity_uri('log', $log);
  drupal_set_message(t('Log created') . ': ' . l($label, $uri['path']));

  // Return the log.
  return $log;
}