|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Coyote\Http\Controllers\Job; |
|
4
|
|
|
|
|
5
|
|
|
use Coyote\Events\JobDeleting; |
|
6
|
|
|
use Coyote\Events\PostWasSaved; |
|
7
|
|
|
use Coyote\Events\TopicWasSaved; |
|
8
|
|
|
use Coyote\Http\Controllers\Controller; |
|
9
|
|
|
use Coyote\Repositories\Contracts\ForumRepositoryInterface as ForumRepository; |
|
10
|
|
|
use Coyote\Repositories\Contracts\PostRepositoryInterface as PostRepository; |
|
11
|
|
|
use Coyote\Repositories\Contracts\StreamRepositoryInterface as StreamRepository; |
|
12
|
|
|
use Coyote\Repositories\Contracts\TopicRepositoryInterface as TopicRepository; |
|
13
|
|
|
use Coyote\Services\Forum\TreeBuilder; |
|
14
|
|
|
use Coyote\Services\UrlBuilder\UrlBuilder; |
|
15
|
|
|
use Illuminate\Http\Request; |
|
16
|
|
|
use Coyote\Job; |
|
17
|
|
|
use Coyote\Services\Stream\Activities\Move as Stream_Move; |
|
18
|
|
|
use Coyote\Services\Stream\Objects\Job as Stream_Job; |
|
19
|
|
|
use Coyote\Services\Stream\Objects\Forum as Stream_Forum; |
|
20
|
|
|
|
|
21
|
|
|
class MoveController extends Controller |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ForumRepository |
|
25
|
|
|
*/ |
|
26
|
|
|
private $forum; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var TopicRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $topic; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var PostRepository |
|
35
|
|
|
*/ |
|
36
|
|
|
private $post; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var StreamRepository |
|
40
|
|
|
*/ |
|
41
|
|
|
private $stream; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ForumRepository $forum |
|
45
|
|
|
* @param TopicRepository $topic |
|
46
|
|
|
* @param PostRepository $post |
|
47
|
|
|
* @param StreamRepository $stream |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( |
|
50
|
|
|
ForumRepository $forum, |
|
51
|
|
|
TopicRepository $topic, |
|
52
|
|
|
PostRepository $post, |
|
53
|
|
|
StreamRepository $stream |
|
54
|
|
|
) { |
|
55
|
|
|
parent::__construct(); |
|
56
|
|
|
|
|
57
|
|
|
$this->forum = $forum; |
|
58
|
|
|
$this->topic = $topic; |
|
59
|
|
|
$this->post = $post; |
|
60
|
|
|
$this->stream = $stream; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Job $job |
|
65
|
|
|
* @return \Illuminate\View\View |
|
66
|
|
|
*/ |
|
67
|
|
|
public function index($job) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->breadcrumb->push([ |
|
70
|
|
|
'Praca' => route('job.home'), |
|
71
|
|
|
$job->title => route('job.offer', [$job->id, $job->slug]), |
|
72
|
|
|
'Przenieś ofertę pracy' => '' |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$treeBuilder = new TreeBuilder(); |
|
76
|
|
|
|
|
77
|
|
|
return $this->view('job.move')->with([ |
|
78
|
|
|
'forumList' => $treeBuilder->listById($this->forum->list()), |
|
79
|
|
|
'preferred' => $this->forum->findBy('name', 'Ogłoszenia drobne', ['id']), |
|
80
|
|
|
'job' => $job, |
|
81
|
|
|
'subscribed' => $this->userId ? $job->subscribers()->forUser($this->userId)->exists() : false |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Job $job |
|
87
|
|
|
* @param Request $request |
|
88
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
89
|
|
|
*/ |
|
90
|
|
|
public function move($job, Request $request) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->validate($request, [ |
|
93
|
|
|
'forum_id' => 'required|integer|exists:forums,id' |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
$forum = $this->forum->find($request->input('forum_id')); |
|
97
|
|
|
/** @var \Coyote\Topic $topic */ |
|
98
|
|
|
$topic = $this->topic->newInstance(); |
|
|
|
|
|
|
99
|
|
|
/** @var \Coyote\Post $post */ |
|
100
|
|
|
$post = $this->post->newInstance(); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
$topic->fill(['subject' => $job->title]); |
|
103
|
|
|
$topic->forum()->associate($forum); |
|
104
|
|
|
|
|
105
|
|
|
$log = $this->stream->findWhere(['object.objectType' => 'job', 'object.id' => $job->id, 'verb' => 'create'])->first(); |
|
106
|
|
|
|
|
107
|
|
|
$post->forceFill([ |
|
108
|
|
|
'user_id' => $job->user_id, |
|
109
|
|
|
'text' => $job->description, |
|
110
|
|
|
'ip' => $log->ip, |
|
|
|
|
|
|
111
|
|
|
'browser' => $log->browser, |
|
|
|
|
|
|
112
|
|
|
'host' => gethostbyaddr($log->ip) |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
$this->transaction(function () use ($job, $forum, $topic, $post) { |
|
116
|
|
|
$topic->save(); |
|
117
|
|
|
|
|
118
|
|
|
$post->forum()->associate($forum); |
|
119
|
|
|
$post->topic()->associate($topic); |
|
120
|
|
|
|
|
121
|
|
|
$post->save(); |
|
122
|
|
|
|
|
123
|
|
|
if ($job->user_id !== $job->user->id) { |
|
124
|
|
|
$post->subscribers()->create(['user_id' => $job->user_id]); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$log = new \Coyote\Post\Log(); |
|
128
|
|
|
$log->fillWithPost($post)->fill(['subject' => $topic->subject]); |
|
129
|
|
|
|
|
130
|
|
|
event(new JobDeleting($job)); |
|
131
|
|
|
|
|
132
|
|
|
$job->delete(); |
|
133
|
|
|
|
|
134
|
|
|
// fire the event. it can be used to index a content and/or add page path to "pages" table |
|
135
|
|
|
event(new TopicWasSaved($topic)); |
|
136
|
|
|
// add post to elasticsearch |
|
137
|
|
|
event(new PostWasSaved($post)); |
|
138
|
|
|
|
|
139
|
|
|
stream(Stream_Move::class, (new Stream_Job())->map($job), (new Stream_Forum())->map($forum)); |
|
140
|
|
|
}); |
|
141
|
|
|
|
|
142
|
|
|
return redirect() |
|
143
|
|
|
->to(UrlBuilder::post($post)) |
|
144
|
|
|
->with('success', 'Ogłoszenie zostało przeniesione.'); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|