-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathPostImport.php
More file actions
155 lines (123 loc) · 4.16 KB
/
PostImport.php
File metadata and controls
155 lines (123 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php namespace RainLab\Blog\Models;
use Backend\Models\ImportModel;
use Backend\Models\User as AuthorModel;
use ApplicationException;
use Exception;
/**
* Post Import Model
*/
class PostImport extends ImportModel
{
public $table = 'rainlab_blog_posts';
/**
* Validation rules
*/
public $rules = [
'title' => 'required',
'content' => 'required'
];
protected $authorEmailCache = [];
protected $categoryNameCache = [];
public function getDefaultAuthorOptions()
{
return AuthorModel::all()->lists('full_name', 'email');
}
public function getCategoriesOptions()
{
return Category::lists('name', 'id');
}
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) {
if (in_array($attribute, $post->getDates()) && empty($value)) {
continue;
}
$post->{$attribute} = isset($value) ? $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());
}
}
}
protected function findAuthorFromEmail($data)
{
if (!$email = array_get($data, 'email', $this->default_author)) {
return null;
}
if (isset($this->authorEmailCache[$email])) {
return $this->authorEmailCache[$email];
}
$author = AuthorModel::where('email', $email)->first();
return $this->authorEmailCache[$email] = $author;
}
protected function findDuplicatePost($data)
{
if ($id = array_get($data, 'id')) {
return Post::find($id);
}
$title = array_get($data, 'title');
$post = Post::where('title', $title);
if ($slug = array_get($data, 'slug')) {
$post->orWhere('slug', $slug);
}
return $post->first();
}
protected function getCategoryIdsForPost($data)
{
$ids = [];
if ($this->auto_create_categories) {
$categoryNames = $this->decodeArrayValue(array_get($data, 'categories'));
foreach ($categoryNames as $name) {
if (!$name = trim($name)) {
continue;
}
if (isset($this->categoryNameCache[$name])) {
$ids[] = $this->categoryNameCache[$name];
}
else {
$newCategory = Category::firstOrCreate(['name' => $name]);
$ids[] = $this->categoryNameCache[$name] = $newCategory->id;
}
}
}
elseif ($this->categories) {
$ids = (array) $this->categories;
}
return $ids;
}
}