function _farm_livestock_update_7005_stage4 in farmOS 7
Update 7005 stage 4: Convert egg logs to harvest logs.
See also
1 call to _farm_livestock_update_7005_stage4()
- farm_livestock_update_7005 in modules/
farm/ farm_livestock/ farm_livestock.install - Create group assets for each animal group term, assign animals to new group assets, convert egg logs to harvest logs, and remove old animal groups taxonomy and related features.
File
- modules/
farm/ farm_livestock/ farm_livestock.install, line 320 - Farm livestock install file.
Code
function _farm_livestock_update_7005_stage4(&$sandbox) {
// If we are not on stage 4, bail.
if (empty($sandbox['stage']) || $sandbox['stage'] != 4) {
return;
}
// Prepare to process this stage in batches.
if (!isset($sandbox['stage4_progress'])) {
// Set up progress and max variables.
$sandbox['stage4_progress'] = 0;
$sandbox['stage4_max'] = db_query("SELECT COUNT(id) FROM {log} WHERE type = 'farm_eggs' ORDER BY id ASC")
->fetchField();
// Update all field data and revisions for fields that will be kept.
$egg_log_fields = array(
'field_farm_notes',
'field_farm_log_category',
'field_farm_log_owner',
);
foreach ($egg_log_fields as $field_name) {
$tables = array(
'field_data_' . $field_name,
'field_revision_' . $field_name,
);
foreach ($tables as $table) {
db_query("UPDATE {" . $table . "} SET bundle = 'farm_harvest' WHERE entity_type = 'log' AND bundle = 'farm_eggs'");
}
}
}
// Load the next batch of egg log IDs.
$limit = 25;
$result = db_query_range("SELECT id FROM {log} WHERE type = 'farm_eggs'", 0, $limit);
$egg_log_ids = array();
foreach ($result as $row) {
if (!empty($row->id)) {
$egg_log_ids[] = $row->id;
}
}
// Iterate through egg logs.
foreach ($egg_log_ids as $egg_log_id) {
// Increment the progress.
$sandbox['stage4_progress']++;
// Change the log type to 'farm_harvest'.
db_query("UPDATE {log} SET type = 'farm_harvest' WHERE type = 'farm_eggs' AND id = :id", array(
':id' => $egg_log_id,
));
// Load the log.
$log = log_load($egg_log_id, TRUE);
// Create an entity wrapper for the log.
$log_wrapper = entity_metadata_wrapper('log', $log);
// Find old animal groups associated with this log.
$old_group_ids = array();
$result = db_query("SELECT field_farm_animal_group_tid FROM {field_data_field_farm_animal_group} WHERE entity_type = 'log' AND bundle = 'farm_eggs' AND entity_id = :log_id ORDER BY delta ASC", array(
':log_id' => $egg_log_id,
));
foreach ($result as $row) {
if (!empty($row->field_farm_animal_group_tid)) {
$old_group_ids[] = $row->field_farm_animal_group_tid;
}
}
// Iterate through the old group term IDs and add asset references to the
// new group assets that were created in stage 1 above.
foreach ($old_group_ids as $delta => $old_group_id) {
if (array_key_exists($old_group_id, $sandbox['groups'])) {
$group = farm_asset_load($sandbox['groups'][$old_group_id]);
if (!empty($group)) {
$log_wrapper->field_farm_asset[] = $group;
}
}
}
// Find the old egg quantity value from this log.
$egg_qty = db_query("SELECT field_farm_egg_qty_value FROM {field_data_field_farm_egg_qty} WHERE entity_type = 'log' AND bundle = 'farm_eggs' AND entity_id = :log_id", array(
':log_id' => $egg_log_id,
))
->fetchField();
// If no quantity was found, set it to zero.
if (empty($egg_qty)) {
$egg_qty = 0;
}
// Create a new quantity field_collection entity attached to the log.
$quantity = entity_create('field_collection_item', array(
'field_name' => 'field_farm_quantity',
));
$quantity
->setHostEntity('log', $log);
// Create an entity wrapper for the quantity.
$quantity_wrapper = entity_metadata_wrapper('field_collection_item', $quantity);
// Set the quantity measure.
$quantity_wrapper->field_farm_quantity_measure
->set('count');
// Set the quantity value.
$value_fraction = fraction_from_decimal($egg_qty);
$quantity_wrapper->field_farm_quantity_value->numerator
->set($value_fraction
->getNumerator());
$quantity_wrapper->field_farm_quantity_value->denominator
->set($value_fraction
->getDenominator());
// Create/load the units taxonomy term.
$units = t('egg(s)');
$units_term = farm_term($units, 'farm_quantity_units');
// Set the quantity units.
$quantity_wrapper->field_farm_quantity_units = $units_term;
// Save the quantity.
$quantity_wrapper
->save();
// Save the log.
$log_wrapper
->save();
}
// The overall update is not finished.
$sandbox['#finished'] = 0.75;
// Once we've processed all of the egg logs, we are ready for stage 5.
if ($sandbox['stage4_progress'] >= $sandbox['stage4_max']) {
$sandbox['stage'] = 5;
}
}