Newer
Older

Yassine Doghri
committed
<?php
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers\Admin;
use App\Models\CategoryModel;
use App\Models\LanguageModel;
use App\Models\PodcastModel;
class Podcast extends BaseController
{
protected ?\App\Entities\Podcast $podcast;
public function _remap($method, ...$params)
{
if (count($params) > 0) {
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
switch ($method) {
case 'edit':
if (
!has_permission('podcasts-edit') ||
!has_permission("podcasts:$params[0]-edit")
) {
throw new \RuntimeException(
lang('Auth.notEnoughPrivilege')
);
}
case 'delete':
if (
!has_permission('podcasts-delete') ||
!has_permission("podcasts:$params[0]-delete")
) {
throw new \RuntimeException(
lang('Auth.notEnoughPrivilege')
);
}
case 'listContributors':
case 'addContributor':
case 'editContributor':
case 'deleteContributor':
if (
!has_permission('podcasts-manage_contributors') ||
!has_permission(
"podcasts:$params[0]-manage_contributors"
)
) {
throw new \RuntimeException(
lang('Auth.notEnoughPrivilege')
);
}
}

Yassine Doghri
committed
$podcast_model = new PodcastModel();
if (!($this->podcast = $podcast_model->find($params[0]))) {

Yassine Doghri
committed
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
}
return $this->$method();
}
public function myPodcasts()
{
$data = [
'all_podcasts' => (new PodcastModel())->getUserPodcasts(user()->id),
];
return view('admin/podcast/list', $data);
}

Yassine Doghri
committed
public function list()
{
$podcast_model = new PodcastModel();
$data = ['all_podcasts' => $podcast_model->findAll()];
return view('admin/podcast/list', $data);
}
public function create()
{
helper(['form', 'misc']);
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
$languageModel = new LanguageModel();
$categoryModel = new CategoryModel();
$data = [
'languages' => $languageModel->findAll(),
'categories' => $categoryModel->findAll(),
'browser_lang' => get_browser_language(
$this->request->getServer('HTTP_ACCEPT_LANGUAGE')
),
];
echo view('admin/podcast/create', $data);
}
public function attemptCreate()
{
$rules = [
'image' => 'uploaded[image]|is_image[image]|ext_in[image,jpg,png]',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
$podcast = new \App\Entities\Podcast([
'title' => $this->request->getPost('title'),
'name' => $this->request->getPost('name'),
'description' => $this->request->getPost('description'),
'episode_description_footer' => $this->request->getPost(
'episode_description_footer'
),
'image' => $this->request->getFile('image'),
'language' => $this->request->getPost('language'),
'category' => $this->request->getPost('category'),
'explicit' => (bool) $this->request->getPost('explicit'),
'author_name' => $this->request->getPost('author_name'),
'author_email' => $this->request->getPost('author_email'),
'owner' => user(),
'owner_name' => $this->request->getPost('owner_name'),
'owner_email' => $this->request->getPost('owner_email'),
'type' => $this->request->getPost('type'),
'copyright' => $this->request->getPost('copyright'),
'block' => (bool) $this->request->getPost('block'),
'complete' => (bool) $this->request->getPost('complete'),
'custom_html_head' => $this->request->getPost('custom_html_head'),
]);

Yassine Doghri
committed
$podcast_model = new PodcastModel();
$db = \Config\Database::connect();

Yassine Doghri
committed
$db->transStart();

Yassine Doghri
committed
if (!($new_podcast_id = $podcast_model->insert($podcast, true))) {

Yassine Doghri
committed
$db->transComplete();
return redirect()
->back()
->withInput()
->with('errors', $podcast_model->errors());

Yassine Doghri
committed
}
$podcast_model->addContributorToPodcast(user()->id, $new_podcast_id);
$db->transComplete();
return redirect()->route('podcast_list');

Yassine Doghri
committed
}
public function edit()
{
helper('form');

Yassine Doghri
committed
$languageModel = new LanguageModel();
$categoryModel = new CategoryModel();
$data = [
'podcast' => $this->podcast,
'languages' => $languageModel->findAll(),
'categories' => $categoryModel->findAll(),
];

Yassine Doghri
committed
echo view('admin/podcast/edit', $data);
}

Yassine Doghri
committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
public function attemptEdit()
{
$rules = [
'image' =>
'uploaded[image]|is_image[image]|ext_in[image,jpg,png]|permit_empty',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
$this->podcast->title = $this->request->getPost('title');
$this->podcast->name = $this->request->getPost('name');
$this->podcast->description = $this->request->getPost('description');
$this->podcast->episode_description_footer = $this->request->getPost(
'episode_description_footer'
);
$image = $this->request->getFile('image');
if ($image->isValid()) {
$this->podcast->image = $image;
}
$this->podcast->language = $this->request->getPost('language');
$this->podcast->category = $this->request->getPost('category');
$this->podcast->explicit = (bool) $this->request->getPost('explicit');
$this->podcast->author_name = $this->request->getPost('author_name');
$this->podcast->author_email = $this->request->getPost('author_email');
$this->podcast->owner_name = $this->request->getPost('owner_name');
$this->podcast->owner_email = $this->request->getPost('owner_email');
$this->podcast->type = $this->request->getPost('type');
$this->podcast->copyright = $this->request->getPost('copyright');
$this->podcast->block = (bool) $this->request->getPost('block');
$this->podcast->complete = (bool) $this->request->getPost('complete');
$this->podcast->custom_html_head = $this->request->getPost(
'custom_html_head'
);

Yassine Doghri
committed
$podcast_model = new PodcastModel();
if (!$podcast_model->save($this->podcast)) {
return redirect()
->back()
->withInput()
->with('errors', $podcast_model->errors());

Yassine Doghri
committed
}
return redirect()->route('podcast_list');

Yassine Doghri
committed
}
public function delete()
{
$podcast_model = new PodcastModel();
$podcast_model->delete($this->podcast->id);
return redirect()->route('podcast_list');
}
}