function handleContactMethod($o, $data, $fieldname) { $set = 'set' . ucfirst($fieldname); try { $o->{$set}(new ContactMethod($data[$fieldname])); } catch (Exception $e) { $c = new ContactMethod(); $c->setName($data[$fieldname]); $c->save(); $o->{$set}($c); } }
function loadPeople($data, &$errors = array(), &$warnings = array()) { $system = system::Instance(); $db = DB::Instance(); $flash = Flash::Instance(); if ($this->abort_action == 'A') { // Set encompassing transaction if need to abort all on error $db->StartTrans(); } $data_in = $this->convertData($data); if (is_array($data_in) && count($data_in) > 0) { foreach ($data_in as $line) { $model_data = array(); foreach ($line as $model => $fields) { switch ($model) { case 'Person': $model_data['Party']['type'] = $model; $model_data[$model] = $fields; $model_data[$model]['is_lead'] = empty($fields['accountnumber']) ? true : false; break; case 'PersonAddress': $model_data['Address'] = $fields; $values = false; foreach ($fields as $field => $value) { if (!empty($value)) { $values = true; break; } } if ($values) { $model_data['PartyAddress']['main'] = true; // Check for existing entry $address = new PersonAddress(); $address->loadBy(array('street1', 'street2', 'street3', 'town', 'county', 'postcode'), array($fields['street1'], $fields['street2'], $fields['street3'], $fields['town'], $fields['county'], $fields['postcode'])); if ($address->isLoaded()) { $model_data['PartyAddress']['address_id'] = $model_data['Address'][$address->idField] = $address->{$address->idField}; } } else { unset($model_data['Address']); } break; case 'ContactMethod': foreach ($fields as $type => $value) { if (!empty($value)) { $model_data[$type]['ContactMethod']['contact'] = $value; $model_data[$type]['PartyContactMethod']['main'] = true; switch ($type) { case 'phone': $model_data[$type]['PartyContactMethod']['type'] = 'T'; break; case 'email': $model_data[$type]['PartyContactMethod']['type'] = 'E'; break; case 'fax': $model_data[$type]['PartyContactMethod']['type'] = 'F'; break; case 'mobile': $model_data[$type]['PartyContactMethod']['type'] = 'M'; break; default: $model_data[$type]['PartyContactMethod']['type'] = ''; } // Check for existing entry $contactmethod = new ContactMethod(); $contactmethod->loadBy('contact', $value); if ($contactmethod->isLoaded()) { $model_data[$type]['PartyContactMethod']['contactmethod_id'] = $model_data[$type]['ContactMethod'][$contactmethod->idField] = $contactmethod->{$contactmethod->idField}; } } } break; } } if (!$system->controller->save_model('Person', $model_data, $errors, $warnings, $this->duplicates_action)) { if ($this->abort_action != 'S') { // Abort on Error if ($this->abort_action == 'A') { // Rollback all imported records $db->FailTrans(); $db->CompleteTrans(); } // If not abort_all_on_error, // then imported records up to this point will be saved return false; } // Skip error record and continue, so reset errors array $warnings[] = 'Ignored'; $warnings = array_merge($warnings, $errors); $errors = array(); $flash->clear(); } $system->controller->clearSavedModels(); } } if ($this->abort_action == 'A') { // Everything is OK here, just need to complete transaction // if mode is set to abort all on error $db->CompleteTrans(); } return array('internal_id' => null, 'internal_identifier_field' => '', 'internal_identifier_value' => ''); }
$o = new Resolution(); $o->handleUpdate($r); $o->save(); echo "Resolution: {$o->getName()}\n"; } $result = $mongo->actions->find(); foreach ($result as $r) { $o = new Action(); $o->handleUpdate($r); $o->save(); echo "Action: {$o->getName()}\n"; } $result = $mongo->lookups->findOne(array('name' => 'contactMethods')); $methods = $result['items']; foreach ($methods as $m) { $o = new ContactMethod(); $o->setName($m); $o->save(); echo "ContactMethod: {$o->getName()}\n"; } $result = $mongo->lookups->findOne(array('name' => 'types')); $types = $result['items']; foreach ($types as $t) { $o = new IssueType(); $o->setName($t); $o->save(); echo "IssueType: {$o->getName()}\n"; } $result = $mongo->lookups->findOne(array('name' => 'labels')); $labels = $result['items']; foreach ($labels as $l) {