/** * Handle setting link type fields and create linked records if specified * @param string $modelName The model class being imported * @param X2Model $model The currently importing model record * @param Fields $fieldRecord Field to set * @param string $importAttribute Value to set field * @param array $relationships Attributes of Relationships to be created * @param array $modelContainer Attributes of Models to be created * @param array $createdLinkedModels Linked models that will be created */ protected function importRecordLinkAttribute($modelName, X2Model &$model, Fields $fieldRecord, $importAttribute, &$relationships, &$modelContainer, &$createdLinkedModels) { $fieldName = $fieldRecord->fieldName; $className = ucfirst($fieldRecord->linkType); if (ctype_digit($importAttribute)) { $lookup = X2Model::model($className)->findByPk($importAttribute); $model->{$fieldName} = $importAttribute; if (!empty($lookup)) { // Create a link to the existing record $model->{$fieldName} = $lookup->nameId; $relationship = new Relationships(); $relationship->firstType = $modelName; $relationship->secondType = $className; $relationship->secondId = $importAttribute; $relationships[count($relationships) - 1][] = $relationship->attributes; } } else { $lookup = X2Model::model($className)->findByAttributes(array('name' => $importAttribute)); if (isset($lookup)) { $model->{$fieldName} = $lookup->nameId; $relationship = new Relationships(); $relationship->firstType = $modelName; $relationship->secondType = $className; $relationship->secondId = $lookup->id; $relationships[count($relationships) - 1][] = $relationship->attributes; } elseif (isset($_SESSION['createRecords']) && $_SESSION['createRecords'] == 1 && !($modelName === 'BugReports' && $fieldRecord->linkType === 'BugReports')) { // Skip creating related bug reports; the created report wouldn't hold any useful info. $className = ucfirst($fieldRecord->linkType); if (class_exists($className)) { $lookup = new $className(); if ($_SESSION['skipActivityFeed'] === 1) { $lookup->createEvent = false; } $lookup->name = $importAttribute; if ($className === 'Contacts' || $modelName === 'X2Leads') { $this->fixupImportedContactName($lookup); } if ($lookup->hasAttribute('visibility')) { $lookup->visibility = 1; } if ($lookup->hasAttribute('description')) { $lookup->description = "Generated by " . $modelName . " import."; } if ($lookup->hasAttribute('createDate')) { $lookup->createDate = time(); } if (!array_key_exists($className, $modelContainer)) { $modelContainer = array_merge($modelContainer, array($className => array())); } // Ensure this linked record has not already been accounted for $createNewLinkedRecord = true; if ($model->hasAttribute('name')) { $model->{$fieldName} = $lookup->name; } else { $model->{$fieldName} = $importAttribute; } foreach ($modelContainer[$className] as $record) { if ($record['name'] === $lookup->name) { $createNewLinkedRecord = false; break; } } if ($createNewLinkedRecord) { $modelContainer[$className][] = $lookup->attributes; if (isset($_SESSION['created'][$className])) { $_SESSION['created'][$className]++; } else { $_SESSION['created'][$className] = 1; } } $relationship = new Relationships(); $relationship->firstType = $modelName; $relationship->secondType = $className; $relationships[count($relationships) - 1][] = $relationship->attributes; $createdLinkedModels[] = $model->{$fieldName}; } } else { $model->{$fieldName} = $importAttribute; } } }