|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\League\Csv\Exceptions\CannotInsertRecord; |
|
6
|
|
|
use GeminiLabs\League\Csv\Writer; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Contracts\CommandContract as Contract; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Database\Export; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Request; |
|
11
|
|
|
|
|
12
|
|
|
class ExportReviews implements Contract |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $assigned_posts; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Request $request) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->assigned_posts = $request->assigned_posts; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public function handle() |
|
28
|
|
|
{ |
|
29
|
|
|
$reviews = $this->results(); |
|
30
|
|
|
if (empty($reviews)) { |
|
31
|
|
|
glsr(Notice::class)->addWarning(_x('No reviews found.', 'admin-text', 'site-reviews')); |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
try { |
|
35
|
|
|
$filename = sprintf('%s_%s.csv', date('YmdHi'), glsr()->id); |
|
36
|
|
|
$writer = Writer::createFromString(''); |
|
37
|
|
|
$writer->insertOne(array_keys($reviews[0])); |
|
38
|
|
|
$writer->insertAll($reviews); |
|
39
|
|
|
nocache_headers(); |
|
40
|
|
|
$writer->output($filename); |
|
41
|
|
|
exit; |
|
42
|
|
|
} catch (CannotInsertRecord $e) { |
|
43
|
|
|
glsr(Notice::class)->addError($e->getMessage()); |
|
44
|
|
|
glsr_log() |
|
45
|
|
|
->warning('Unable to insert row into CSV export file') |
|
46
|
|
|
->debug($e->getRecord()); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function results() |
|
54
|
|
|
{ |
|
55
|
|
|
if ('id' === $this->assigned_posts) { |
|
56
|
|
|
$results = glsr(Export::class)->export(); |
|
57
|
|
|
} |
|
58
|
|
|
if ('slug' === $this->assigned_posts) { |
|
59
|
|
|
$results = glsr(Export::class)->exportWithSlugs(); |
|
60
|
|
|
} |
|
61
|
|
|
if (empty($results)) { |
|
62
|
|
|
return []; |
|
63
|
|
|
} |
|
64
|
|
|
return $results; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|