public function importData($results, $sessionKey = null) { $firstRow = reset($results); /* * Validation */ if ($this->auto_create_categories && !array_key_exists('categories', $firstRow)) { throw new ApplicationException('Please specify a match for the Categories column.'); } /* * Import */ foreach ($results as $row => $data) { try { if (!($title = array_get($data, 'title'))) { $this->logSkipped($row, 'Missing post title'); continue; } /* * Find or create */ $post = Post::make(); if ($this->update_existing) { $post = $this->findDuplicatePost($data) ?: $post; } $postExists = $post->exists; /* * Set attributes */ $except = ['id', 'categories', 'author_email']; foreach (array_except($data, $except) as $attribute => $value) { $post->{$attribute} = $value ?: null; } if ($author = $this->findAuthorFromEmail($data)) { $post->user_id = $author->id; } $post->forceSave(); if ($categoryIds = $this->getCategoryIdsForPost($data)) { $post->categories()->sync($categoryIds, false); } /* * Log results */ if ($postExists) { $this->logUpdated(); } else { $this->logCreated(); } } catch (Exception $ex) { $this->logError($row, $ex->getMessage()); } } }