1
|
|
|
<?php namespace Anomaly\Streams\Platform\Http\Controller; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\AddonCollection; |
4
|
|
|
use Anomaly\Streams\Platform\Entry\Contract\EntryRepositoryInterface; |
5
|
|
|
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface; |
6
|
|
|
use Anomaly\Streams\Platform\Stream\Contract\StreamRepositoryInterface; |
7
|
|
|
use Anomaly\Streams\Platform\Support\Authorizer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EntryController |
11
|
|
|
* |
12
|
|
|
* @link http://pyrocms.com/ |
13
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
* @package Anomaly\Streams\Platform\Http\Controller |
16
|
|
|
*/ |
17
|
|
|
class EntryController extends AdminController |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The addon collection. |
22
|
|
|
* |
23
|
|
|
* @var AddonCollection |
24
|
|
|
*/ |
25
|
|
|
protected $addons; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The stream repository. |
29
|
|
|
* |
30
|
|
|
* @var StreamRepositoryInterface |
31
|
|
|
*/ |
32
|
|
|
protected $streams; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The authorizer service. |
36
|
|
|
* |
37
|
|
|
* @var Authorizer |
38
|
|
|
*/ |
39
|
|
|
protected $authorizer; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The entry repository. |
43
|
|
|
* |
44
|
|
|
* @var EntryRepositoryInterface |
45
|
|
|
*/ |
46
|
|
|
protected $repository; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new EntryController instance. |
50
|
|
|
* |
51
|
|
|
* @param AddonCollection $addons |
52
|
|
|
* @param Authorizer $authorizer |
53
|
|
|
* @param StreamRepositoryInterface $streams |
54
|
|
|
* @param EntryRepositoryInterface $repository |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
AddonCollection $addons, |
58
|
|
|
Authorizer $authorizer, |
59
|
|
|
StreamRepositoryInterface $streams, |
60
|
|
|
EntryRepositoryInterface $repository |
61
|
|
|
) { |
62
|
|
|
parent::__construct(); |
63
|
|
|
|
64
|
|
|
$this->addons = $addons; |
65
|
|
|
$this->streams = $streams; |
66
|
|
|
$this->authorizer = $authorizer; |
67
|
|
|
$this->repository = $repository; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Restore an entry. |
72
|
|
|
* |
73
|
|
|
* @param $addon |
74
|
|
|
* @param $namespace |
75
|
|
|
* @param $stream |
76
|
|
|
* @param $id |
77
|
|
|
* @return \Illuminate\Http\RedirectResponse |
78
|
|
|
*/ |
79
|
|
|
public function restore($addon, $namespace, $stream, $id) |
80
|
|
|
{ |
81
|
|
|
$addon = $this->addons->get($addon); |
82
|
|
|
|
83
|
|
|
/* @var StreamInterface $stream */ |
84
|
|
|
$stream = $this->streams->findBySlugAndNamespace($stream, $namespace); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Resolve the model and set |
88
|
|
|
* it on the repository. |
89
|
|
|
*/ |
90
|
|
|
$this->repository->setModel($this->container->make($stream->getEntryModelName())); |
91
|
|
|
|
92
|
|
|
$entry = $this->repository->findTrashed($id); |
93
|
|
|
|
94
|
|
|
if (!$this->authorizer->authorize($addon->getNamespace($stream->getSlug() . '.restore'))) { |
95
|
|
|
abort(403); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!$entry->isRestorable()) { |
99
|
|
|
|
100
|
|
|
$this->messages->error('streams::message.restore_failed'); |
101
|
|
|
|
102
|
|
|
return $this->redirect->back(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->repository->restore($entry); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$this->messages->success('streams::message.restore_success'); |
108
|
|
|
|
109
|
|
|
return $this->redirect->back(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Export all entries. |
114
|
|
|
* |
115
|
|
|
* @param $addon |
116
|
|
|
* @param $namespace |
117
|
|
|
* @param $stream |
118
|
|
|
* @return \Illuminate\Http\RedirectResponse |
119
|
|
|
*/ |
120
|
|
|
public function export($addon, $namespace, $stream) |
121
|
|
|
{ |
122
|
|
|
$addon = $this->addons->get($addon); |
123
|
|
|
|
124
|
|
|
/* @var StreamInterface $stream */ |
125
|
|
|
$stream = $this->streams->findBySlugAndNamespace($stream, $namespace); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Resolve the model and set |
129
|
|
|
* it on the repository. |
130
|
|
|
*/ |
131
|
|
|
$this->repository->setModel($this->container->make($stream->getEntryModelName())); |
132
|
|
|
|
133
|
|
|
if (!$this->authorizer->authorize($addon->getNamespace($stream->getSlug() . '.export'))) { |
134
|
|
|
abort(403); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$headers = [ |
138
|
|
|
'Content-Disposition' => 'attachment; filename=' . $stream->getSlug() . '.csv', |
139
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', |
140
|
|
|
'Content-type' => 'text/csv', |
141
|
|
|
'Pragma' => 'public', |
142
|
|
|
'Expires' => '0' |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
View Code Duplication |
$callback = function () { |
|
|
|
|
146
|
|
|
|
147
|
|
|
$output = fopen('php://output', 'w'); |
148
|
|
|
|
149
|
|
|
foreach ($this->repository->all() as $k => $entry) { |
150
|
|
|
|
151
|
|
|
if ($k == 0) { |
152
|
|
|
fputcsv($output, array_keys($entry->toArray())); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
fputcsv($output, $entry->toArray()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
fclose($output); |
159
|
|
|
}; |
160
|
|
|
|
161
|
|
|
return $this->response->stream($callback, 200, $headers); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: