View source
<?php
namespace Drupal\farm_migrate\Plugin\migrate\source\d7;
use Drupal\Core\Site\Settings;
use Drupal\farm_migrate\Traits\FarmQuickEntity;
use Drupal\log\Plugin\migrate\source\d7\Log;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
class FarmLog extends Log {
use FarmQuickEntity;
public function prepareRow(Row $row) {
$result = parent::prepareRow($row);
if (!$result) {
return FALSE;
}
$this
->prepareMovement($row);
$this
->prepareGroup($row);
$this
->prepareQuantity($row);
$this
->prepareSoilTest($row);
$this
->prepareQuickEntityRow($row, 'log');
return TRUE;
}
protected function prepareMovement(Row $row) {
$id = $row
->getSourceProperty('id');
$allow_overwrite = Settings::get('farm_migrate_allow_movement_overwrite', FALSE);
$is_movement = FALSE;
$movement_values = $this
->getFieldValues('log', 'field_farm_movement', $id);
if (!empty($movement_values)) {
$field_collection_item_ids = [];
foreach ($movement_values as $movement_value) {
if (!empty($movement_value['value'])) {
$field_collection_item_ids[] = $movement_value['value'];
}
}
$fcid = reset($field_collection_item_ids);
$query = $this
->select('field_collection_item', 'fci');
$query
->leftJoin('field_data_field_farm_move_to', 'fdffmt', "fdffmt.entity_id = fci.item_id AND fdffmt.entity_type = 'field_collection_item' AND fdffmt.bundle = 'field_farm_movement' AND fdffmt.deleted = 0");
$query
->addField('fdffmt', 'field_farm_move_to_tid', 'tid');
$query
->condition('fci.item_id', $fcid);
$result = $query
->execute()
->fetchCol();
$movement_areas = FALSE;
if (!empty($result)) {
foreach ($result as $col) {
if (!empty($col)) {
$movement_areas[] = [
'tid' => $col,
];
}
}
}
$query = $this
->select('field_collection_item', 'fci');
$query
->leftJoin('field_data_field_farm_geofield', 'fdffg', "fdffg.entity_id = fci.item_id AND fdffg.entity_type = 'field_collection_item' AND fdffg.bundle = 'field_farm_movement' AND fdffg.deleted = 0");
$query
->addField('fdffg', 'field_farm_geofield_geom', 'geom');
$query
->condition('fci.item_id', $fcid);
$result = $query
->execute()
->fetchField();
$movement_geometry = FALSE;
if (!empty($result)) {
$movement_geometry = [
[
'geom' => $result,
],
];
}
$log_areas = $this
->getFieldValues('log', 'field_farm_area', $id);
$log_geometry = $this
->getFieldValues('log', 'field_farm_geofield', $id);
if (!empty($movement_areas)) {
$is_movement = TRUE;
}
if (empty($movement_areas) && !empty($movement_geometry)) {
$message = 'Movement has a geometry but no areas (log ' . $id . ').';
$this->idMap
->saveMessage([
'id' => $id,
], $message, MigrationInterface::MESSAGE_WARNING);
}
if (!empty($log_areas) && !empty($movement_areas) && $log_areas != $movement_areas) {
$message = 'Log ' . $id . ' has both area references and movement area references. See https://github.com/farmOS/farmOS/blob/2.x/docs/hosting/migration.md#movement-logs';
if (!$allow_overwrite) {
throw new MigrateException($message);
}
else {
$this->idMap
->saveMessage([
'id' => $id,
], $message, MigrationInterface::MESSAGE_WARNING);
}
}
if (!empty($log_geometry[0]['geom']) && !empty($movement_geometry[0]['geom']) && $log_geometry[0]['geom'] != $movement_geometry[0]['geom']) {
$message = 'Log ' . $id . ' has both a geometry and a movement geometry. See https://github.com/farmOS/farmOS/blob/2.x/docs/hosting/migration.md#movement-logs';
if (!$allow_overwrite) {
throw new MigrateException($message);
}
else {
$this->idMap
->saveMessage([
'id' => $id,
], $message, MigrationInterface::MESSAGE_WARNING);
}
}
if (!empty($movement_areas)) {
$row
->setSourceProperty('field_farm_area', $movement_areas);
}
if (!empty($movement_geometry)) {
$row
->setSourceProperty('field_farm_geofield', $movement_geometry);
}
}
$row
->setSourceProperty('is_movement', $is_movement);
}
protected function prepareGroup(Row $row) {
$id = $row
->getSourceProperty('id');
$is_group_assignment = FALSE;
$membership_values = $this
->getFieldValues('log', 'field_farm_membership', $id);
if (!empty($membership_values)) {
$field_collection_item_ids = [];
foreach ($membership_values as $membership_value) {
if (!empty($membership_value['value'])) {
$field_collection_item_ids[] = $membership_value['value'];
}
}
$fcid = reset($field_collection_item_ids);
$query = $this
->select('field_collection_item', 'fci');
$query
->leftJoin('field_data_field_farm_group', 'fdffg', "fdffg.entity_id = fci.item_id AND fdffg.entity_type = 'field_collection_item' AND fdffg.bundle = 'field_farm_membership' AND fdffg.deleted = 0");
$query
->addField('fdffg', 'field_farm_group_target_id', 'target_id');
$query
->condition('fci.item_id', $fcid);
$result = $query
->execute()
->fetchCol();
$membership_groups = FALSE;
if (!empty($result)) {
foreach ($result as $col) {
if (!empty($col)) {
$membership_groups[] = [
'target_id' => $col,
];
}
}
}
if (!empty($membership_groups)) {
$is_group_assignment = TRUE;
}
if (!empty($membership_groups)) {
$row
->setSourceProperty('log_groups', $membership_groups);
}
}
$row
->setSourceProperty('is_group_assignment', $is_group_assignment);
}
protected function prepareQuantity(Row $row) {
$id = $row
->getSourceProperty('id');
$log_quantities = $this
->getFieldvalues('log', 'field_farm_quantity', $id);
$log_inventories = $this
->getFieldvalues('log', 'field_farm_inventory', $id);
$quantity_ids = [];
foreach (array_merge($log_quantities, $log_inventories) as $field_collection) {
if (!empty($field_collection['value'])) {
$quantity_ids[] = $field_collection['value'];
}
}
$row
->setSourceProperty('log_quantities', $quantity_ids);
}
protected function prepareSoilTest(Row $row) {
$id = $row
->getSourceProperty('id');
$soil_name_tids = [];
foreach ($this
->getFieldvalues('log', 'field_farm_soil_names', $id) as $value) {
if (!empty($value['tid'])) {
$soil_name_tids[] = $value['tid'];
}
}
if (empty($soil_name_tids)) {
return;
}
$query = $this
->select('taxonomy_term_data', 't');
$query
->addField('t', 'name');
$query
->condition('t.tid', $soil_name_tids, 'IN');
$result = $query
->execute()
->fetchCol();
$soil_names = [];
if (!empty($result)) {
foreach ($result as $col) {
if (!empty($col)) {
$soil_names[] = $col;
}
}
}
if (empty($soil_names)) {
return;
}
if (count($soil_names) == 1) {
$summary = $this
->t('Soil name: @name', [
'@name' => $soil_names[0],
]);
}
else {
$summary = $this
->t("Soil names:\n@names", [
'@names' => implode("\n", $soil_names),
]);
}
$notes = $this
->getFieldvalues('log', 'field_farm_notes', $id);
if (!empty($notes)) {
$summary = "\n\n" . $summary;
}
$row
->setSourceProperty('soil_name_summary', $summary);
}
}