371
371
// explicit declarations are good
372
372
$rawFacilityCodes = array();
373
$uniqFacilities = array();
374
$facilityEntities = array();
374
376
// loop our import data and pick up any existing facility code
375
377
foreach ($this->importData as $rowId => $rowData)
377
379
$rawData = $rowData['_rawData'];
379
if (array_key_exists('facility_code', $rawData))
381
if (isset($rawData['facility_code']))
381
$rawFacilityCodes[$rowId] = strtolower($rawData['facility_code']);
383
$uniqFacilities[strtolower($rawData['facility_code'])][] = $rowId;
384
$rawFacilityCodes[$rowId] = strtolower($rawData['facility_code']); // >>>> REMOVE?
387
390
$q = agDoctrineQuery::create($conn)
389
392
->addSelect('s.*')
390
->from('agFacility f')
393
->from('agFacility f INDEXBY f.facility_code')
391
394
->innerJoin('f.agSite s')
392
->whereIn('f.facility_code', $rawFacilityCodes);
395
->whereIn('f.facility_code', array_keys($uniqFacilities));
393
396
$facilityColl = $q->execute();
395
398
$this->eh->logDebug('{' . count($facilityColl) . '} facility entities found in the database.');
400
403
// Add entity id and facility id to $this->importData['primaryKeys'].
401
404
// While we're looping the existing facility, make facility update where necessary.
402
foreach ($facilityColl AS $facility)
405
foreach ($facilityColl AS $facilityCode => $facility)
404
$rowId = array_search(strtolower($facility['facility_code']), $rawFacilityCodes);
405
$this->importData[$rowId]['primaryKeys']['entity_id'] = $facility->agSite['entity_id'];
406
$this->importData[$rowId]['primaryKeys']['facility_id'] = $facility['id'];
407
foreach ($uniqFacilities[$facilityCode] as $index => $rowId) {
408
$this->importData[$rowId]['primaryKeys']['entity_id'] = $facility->agSite['entity_id'];
409
$this->importData[$rowId]['primaryKeys']['facility_id'] = $facility['id'];
407
412
$facility['facility_name'] = $this->importData[$rowId]['_rawData']['facility_name'];
408
unset($rawFacilityCodes[$rowId]);
413
unset($uniqFacilities[$facilityCode]);
410
415
$facilityColl->save($conn);
411
416
$facilityColl->free(TRUE);
418
423
$siteTable = $conn->getTable('agSite');
419
424
$siteColl = new Doctrine_Collection('agSite');
420
425
$facilityTable = $conn->getTable('agFacility');
426
$facilityColl = new Doctrine_Collection('agFacility');
422
428
// Create new entity using rowId as the key to $entityColl.
423
foreach ($rawFacilityCodes AS $rowId => $facilityCode)
429
foreach ($uniqFacilities AS $facilityCode => $rowIds)
425
431
$entity = new agEntity($entityTable, TRUE);
426
$entityColl->add($entity, $rowId);
427
unset($rawFacilityCodes[$rowId]);
432
$entityColl->add($entity, $facilityCode);
429
434
$entityColl->save($conn);
431
436
// Create new site using rowId as the key to $siteColl.
432
foreach ($entityColl AS $rowId => $entity)
437
foreach ($entityColl AS $facilityCode => $entity)
434
439
$entityId = $entity['id'];
435
440
$site = new agSite($siteTable, TRUE);
436
441
$site['entity_id'] = $entityId;
437
$siteColl->add($site, $rowId);
442
$siteColl->add($site, $facilityCode);
439
$this->importData[$rowId]['primaryKeys']['entity_id'] = $entityId;
444
$facilityEntities[$facilityCode] = $entityId;
441
446
$siteColl->save($conn);
442
447
$entityColl->free(TRUE);
443
448
unset($entityColl);
445
450
// Create new facility using rowId as the key to $facilityColl.
446
$facilityColl = new Doctrine_Collection('agFacility');
447
foreach ($siteColl AS $rowId => $site)
451
foreach ($siteColl AS $facilityCode => $site)
449
$rawData = $this->importData[$rowId]['_rawData'];
453
// whoah! what??? how do you not isset()? well, because of how uniq is built we are guaranteed
454
// that each entry has at least one rowId, eg, position [0]
455
$rawData = $this->importData[$uniqFacilities[$facilityCode][0]]['_rawData'];
450
456
$facility = new agFacility($facilityTable, TRUE);
451
457
$facility['site_id'] = $site['id'];
452
458
$facility['facility_name'] = $rawData['facility_name'];
453
$facility['facility_code'] = $rawData['facility_code'];
454
$facilityColl->add($facility, $rowId);
459
$facility['facility_code'] = $facilityCode;
460
$facilityColl->add($facility, $facilityCode);
456
462
$facilityColl->save($conn);
458
// Store new facility ids to $this->importData['primaryKeys'].
459
foreach ($this->importData AS $rowId => &$rowData)
461
if (!isset($rowData['primaryKeys']['facility_id']))
463
$rowData['primaryKeys']['facility_id'] = $facilityColl[$rowId]['id'];
464
foreach ($uniqFacilities as $facilityCode => $rowIds) {
465
foreach ($rowIds as $rowId) {
466
$this->importData[$rowId]['primaryKeys']['entity_id'] = $facilityEntities[$facilityCode];
467
$this->importData[$rowId]['primaryKeys']['facility_id'] = $facilityColl[$facilityCode]['id'];
466
470
$facilityColl->free(TRUE);
467
471
unset($facilityColl);
473
unset($facilityEntities);
474
unset($uniqFacilities);