function farm_log_update_7001 in farmOS 7
Migrate Issues to Observations.
File
- modules/
farm/ farm_log/ farm_log.install, line 75 - Farm Log install file.
Code
function farm_log_update_7001(&$sandbox) {
// Create a new "Issue" Observation Type.
$vocab = taxonomy_vocabulary_machine_name_load('farm_observation_types');
$term = entity_create('taxonomy_term', array(
'name' => 'Issue',
'vid' => $vocab->vid,
));
taxonomy_term_save($term);
// Load all issue logs.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'log');
$query
->entityCondition('bundle', 'farm_issue');
$result = $query
->execute();
if (isset($result['log'])) {
$ids = array_keys($result['log']);
$logs = entity_load('log', $ids);
}
// If logs were loaded, iterate through them...
if (!empty($logs)) {
foreach ($logs as $log) {
// Build a list of values for the new observation log.
$values = array(
'type' => 'farm_observation',
'name' => $log->name,
'created' => $log->created,
'changed' => $log->changed,
'uid' => $log->uid,
);
// Create a new observation log.
$observation = entity_create('log', $values);
// Assign it to the new "Issue" Observation Type.
$observation->field_farm_observation_type[LANGUAGE_NONE][] = array(
'tid' => $term->tid,
);
// Copy fields from the issue log.
$observation->field_farm_asset = $log->field_farm_assets;
$observation->field_farm_notes = $log->field_farm_notes;
// Save the new log.
log_save($observation);
// Manually copy the timestamp and done properties, in case this
// is running at the same time as log_update_7001 and log_update_7002.
$result = db_query('SELECT timestamp, done FROM {log} WHERE id=:id', array(
':id' => $log->id,
));
if ($result) {
while ($row = $result
->fetchAssoc()) {
db_query('UPDATE {log} SET timestamp=:timestamp, done=:done WHERE id=:id', array(
':timestamp' => $row['timestamp'],
':done' => $row['done'],
':id' => $observation->id,
));
}
}
// Delete the old log.
log_delete($log);
}
}
// Delete the View, if it still exists.
$view = views_get_view('farm_log_issues');
if (!empty($view)) {
views_delete_view($view);
}
// Delete the Priority Levels vocabulary.
$vocab = taxonomy_vocabulary_machine_name_load('farm_priority');
if (!empty($vocab)) {
taxonomy_vocabulary_delete($vocab->vid);
}
// Delete the issue log type.
$log_type = log_type_load('farm_issue');
if (!empty($log_type)) {
log_type_delete($log_type);
}
}