You are here

function farm_asset_property_set in farmOS 7

Set an asset's property.

Parameters

$asset_id: The ID of the asset.

$name: The property name.

$value: The property value.

Return value

bool Returns TRUE on success, FALSE on failure.

1 call to farm_asset_property_set()
farm_asset_property_entity_insert in modules/farm/farm_asset/farm_asset_property/farm_asset_property.module
Implements hook_entity_insert().

File

modules/farm/farm_asset/farm_asset_property/farm_asset_property.module, line 56
Farm asset property.

Code

function farm_asset_property_set($asset_id, $name, $value) {

  // Make sure $asset_id and $name are not empty.
  if (empty($asset_id) || empty($name)) {
    return FALSE;
  }

  // Make sure the property name is a string that is less than 128 characters
  // in length.
  if (!is_string($name) || strlen($name) > 128) {
    return FALSE;
  }

  // Make sure that the value is a scalar.
  if (!is_scalar($value)) {
    return FALSE;
  }

  // Convert the value to a string, and make sure it's length is less than 128.
  $value = (string) $value;
  if (strlen($value) > 128) {
    return FALSE;
  }

  // Make sure the asset exists.
  $exists = db_query('SELECT COUNT(*) FROM {farm_asset} WHERE id = :id', array(
    ':id' => $asset_id,
  ))
    ->fetchField();
  if (empty($exists)) {
    return FALSE;
  }

  // Assemble a row for insert/update.
  $row = array(
    'id' => $asset_id,
    'name' => $name,
    'value' => $value,
  );

  // Check to see if the property is already set for this asset.
  $set = farm_asset_property_is_set($asset_id, $name);

  // Insert/update the record in the database.
  $property = $set ? array(
    'id',
    'name',
  ) : array();
  $result = drupal_write_record('farm_asset_property', $row, $property);

  // Return TRUE or FALSE.
  return !empty($result) ? TRUE : FALSE;
}