3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00
farmOS/modules/farm/farm_movement/farm_movement.install

225 lines
7.1 KiB
Plaintext

<?php
/**
* @file
* Code for farm movement installation/updates.
*/
/**
* Create new movement field collection.
*/
function farm_movement_update_7000(&$sandbox) {
// Revert this module's field_base and field_instance components
features_revert(array('farm_movement' => array('field_base', 'field_instance')));
// Flush all caches.
drupal_flush_all_caches();
// Create the movement log type, if it doesn't exist. This may happen during
// an upgrade from beta12 to beta13, because we are removing the farm_movement
// log type definition from this module, but we still need to migrate data
// from it.
$log_type = log_type_load('farm_movement');
if (empty($log_type)) {
$farm_movement = new LogType();
$farm_movement->type = 'farm_movement';
$farm_movement->label = 'Movement (deprecated)';
$farm_movement->name_pattern = 'Move [log:field-farm-asset] to [log:field-farm-move-to]';
$farm_movement->name_edit = 0;
$farm_movement->done = 1;
$farm_movement->save();
}
// Create the field_farm_movement field instance, if it doesn't exist (for the
// same reason described above).
$instance = field_info_instance('log', 'field_farm_movement', 'farm_movement');
if (empty($instance)) {
$instance = array(
'field_name' => 'field_farm_movement',
'entity_type' => 'log',
'bundle' => 'farm_movement',
'label' => 'Movement',
);
field_create_instance($instance);
}
}
/**
* Migrate movement data into new field_farm_movement field collections.
*/
function farm_movement_update_7001(&$sandbox) {
// Process this in passes of 50 movements at a time.
$sandbox['#finished'] = 0;
$limit = 10;
// Keep track of progress.
if (!isset($sandbox['progress'])) {
// Start out at zero.
$sandbox['progress'] = 0;
// Count how many movement logs there are.
$sandbox['max'] = db_select('log')
->fields(NULL, array('id'))
->condition('type', 'farm_movement')
->countQuery()
->execute()
->fetchField();
}
// Fetch the next set of movements.
$query = db_select('log', 'l')
->fields('l', array('id'))
->condition('type', 'farm_movement')
->orderBy('id', 'ASC')
->range($sandbox['progress'], $limit);
$results = $query->execute();
// Iterate over the results.
while ($id = $results->fetchField()) {
// Increment progress.
$sandbox['progress']++;
// If the id is empty, skip it.
if (empty($id)) {
continue;
}
// Load the log.
$log = log_load($id);
// If the log didn't load for whatever reason, skip it.
if (empty($log)) {
continue;
}
// Create an entity wrapper for the log.
$log_wrapper = entity_metadata_wrapper('log', $log);
// If the movement field collection does not exist, create it.
if (empty($log_wrapper->field_farm_movement->value())) {
// Create a new movement field_collection entity.
$movement = entity_create('field_collection_item', array('field_name' => 'field_farm_movement'));
// Attach the movement to the log.
$movement->setHostEntity('log', $log);
}
// Copy the "movement to" and "geometry" fields from the movement log
// itself to the new field collection.
$fields = array(
'field_farm_move_to',
'field_farm_geofield',
);
foreach ($fields as $field) {
if (!empty($log_wrapper->{$field}->value())) {
$log_wrapper->field_farm_movement->{$field}->set($log_wrapper->{$field}->value());
}
}
// Save the movement field collection and log.
$log_wrapper->field_farm_movement->save(TRUE);
log_save($log);
}
// Tell Drupal whether or not we're finished.
if ($sandbox['max'] > 0) {
$sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
}
else {
$sandbox['#finished'] = 1;
}
// If we are finished, delete the old fields from movement logs.
if ($sandbox['#finished'] == 1) {
// Delete field_farm_move_to.
$field = field_info_instance('log', 'field_farm_move_to', 'farm_movement');
field_delete_instance($field);
// Delete field_farm_geofield.
$field = field_info_instance('log', 'field_farm_geofield', 'farm_movement');
field_delete_instance($field);
}
}
/**
* Migrate movement logs to activity logs.
*/
function farm_movement_update_7002(&$sandbox) {
// Search for fields that exist on movements, but not on activities, so we can
// move them to activities. This would only be the case if a user added their
// own fields, movements do not have any extra fields in default farmOS.
// (They used to, but the "move to" and "geometry" moved to the new
// field_farm_movement field collection. See farm_movement_update_7001()).
$log_types = array(
'farm_movement',
'farm_activity',
);
$log_fields = array();
foreach ($log_types as $type) {
$query = db_select('field_config_instance', 'fci');
$query->addField('fci', 'field_name');
$query->condition('fci.entity_type', 'log');
$query->condition('fci.bundle', $type);
$query->condition('fci.deleted', 0);
$log_fields[$type] = $query->execute()->fetchAllKeyed(0, 0);
}
$movement_only_fields = array_keys(array_diff($log_fields['farm_movement'], $log_fields['farm_activity']));
// Move fields that are unique to movements over to activity log types.
foreach ($movement_only_fields as $field_name) {
db_query("UPDATE {field_config_instance} SET bundle = 'farm_activity' WHERE entity_type = 'log' AND bundle = 'farm_movement' AND field_name = :field_name", array(':field_name' => $field_name));
}
// Change all movement logs to activity logs directly in the {log} table.
db_query("UPDATE {log} SET type = 'farm_activity' WHERE type = 'farm_movement'");
// Update the bundles in all field data and revision tables.
foreach ($log_fields['farm_movement'] as $key => $field_name) {
db_query("UPDATE {field_data_" . $field_name . "} SET bundle = 'farm_activity' WHERE entity_type = 'log' AND bundle = 'farm_movement'");
db_query("UPDATE {field_revision_" . $field_name . "} SET bundle = 'farm_activity' WHERE entity_type = 'log' AND bundle = 'farm_movement'");
}
// Mark all movement log field instances for deletion.
foreach ($log_fields['farm_movement'] as $key => $field_name) {
$field = field_info_instance('log', $field_name, 'farm_movement');
field_delete_instance($field);
}
}
/**
* Remove movement log type.
*/
function farm_movement_update_7003(&$sandbox) {
// Delete the movement log type, if it exists.
$log_type = log_type_load('farm_movement');
if (!empty($log_type)) {
log_type_delete($log_type);
}
}
/**
* Delete the "Movement from" field from movement field collections.
*/
function farm_movement_update_7004(&$sandbox) {
$field = field_info_instance('field_collection_item', 'field_farm_move_from', 'field_farm_movement');
if (!empty($field)) {
field_delete_instance($field);
}
}
/**
* Fix Movement Geometry field settings.
*/
function farm_movement_update_7005(&$sandbox) {
$field = field_info_instance('field_collection_item', 'field_farm_geofield', 'field_farm_movement');
$field['widget']['settings']['geocoder_field'] = 'field_farm_move_to';
field_update_instance($field);
}