Newer
Older

Yassine Doghri
committed
<?php

Yassine Doghri
committed

Yassine Doghri
committed
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/

Yassine Doghri
committed

Yassine Doghri
committed
namespace App\Controllers\Admin;
use App\Models\CategoryModel;
use App\Models\LanguageModel;
use App\Models\PodcastModel;
use App\Models\EpisodeModel;

Yassine Doghri
committed
use Config\Services;
use League\HTMLToMarkdown\HtmlConverter;

Yassine Doghri
committed
class Podcast extends BaseController
{
/**
* @var \App\Entities\Podcast|null
*/
protected $podcast;

Yassine Doghri
committed
public function _remap($method, ...$params)
{
if (count($params) > 0) {
if (
!($this->podcast = (new PodcastModel())->getPodcastById(
$params[0]
))
) {

Yassine Doghri
committed
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
}
}
return $this->$method();
}

Yassine Doghri
committed
public function list()
{
if (!has_permission('podcasts-list')) {

Yassine Doghri
committed
$data = [
'podcasts' => (new PodcastModel())->getUserPodcasts(user()->id),
];
} else {
$data = ['podcasts' => (new PodcastModel())->findAll()];
return view('admin/podcast/list', $data);
}

Yassine Doghri
committed
{
$data = ['podcast' => $this->podcast];

Yassine Doghri
committed
replace_breadcrumb_params([0 => $this->podcast->title]);
return view('admin/podcast/view', $data);

Yassine Doghri
committed
}
public function create()
{
helper(['form', 'misc']);
$languageOptions = (new LanguageModel())->getLanguageOptions();
$categoryOptions = (new CategoryModel())->getCategoryOptions();
$data = [
'languageOptions' => $languageOptions,
'categoryOptions' => $categoryOptions,

Yassine Doghri
committed
'browserLang' => get_browser_language(
$this->request->getServer('HTTP_ACCEPT_LANGUAGE')
),
];
return 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_id' => $this->request->getPost('category'),
'explicit' => $this->request->getPost('explicit') == 'yes',
'author' => $this->request->getPost('author'),
'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' => $this->request->getPost('block') == 'yes',
'complete' => $this->request->getPost('complete') == 'yes',
'custom_html_head' => $this->request->getPost('custom_html_head'),
'created_by' => user(),
'updated_by' => user(),
]);

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

Yassine Doghri
committed
$db->transStart();

Yassine Doghri
committed

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

Yassine Doghri
committed
$db->transComplete();
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $podcastModel->errors());

Yassine Doghri
committed
}

Yassine Doghri
committed
$authorize = Services::authorization();

Yassine Doghri
committed
$podcastAdminGroup = $authorize->group('podcast_admin');

Yassine Doghri
committed

Yassine Doghri
committed
$podcastModel->addPodcastContributor(

Yassine Doghri
committed
user()->id,

Yassine Doghri
committed
$newPodcastId,
$podcastAdminGroup->id

Yassine Doghri
committed
);
$db->transComplete();

Yassine Doghri
committed
return redirect()->route('podcast-view', [$newPodcastId]);

Yassine Doghri
committed
}
public function import()
{
helper(['form', 'misc']);
$languageOptions = (new LanguageModel())->getLanguageOptions();
$categoryOptions = (new CategoryModel())->getCategoryOptions();
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
$data = [
'languageOptions' => $languageOptions,
'categoryOptions' => $categoryOptions,
'browserLang' => get_browser_language(
$this->request->getServer('HTTP_ACCEPT_LANGUAGE')
),
];
return view('admin/podcast/import', $data);
}
public function attemptImport()
{
helper(['media', 'misc']);
$rules = [
'name' => 'required',
'imported_feed_url' => 'required',
];
if (!$this->validate($rules)) {
return redirect()
->back()
->withInput()
->with('errors', $this->validator->getErrors());
}
try {
$feed = simplexml_load_file(
$this->request->getPost('imported_feed_url')
);
} catch (\ErrorException $ex) {
return redirect()
->back()
->withInput()
->with('errors', [
$ex->getMessage() .
': <a href="' .
$this->request->getPost('imported_feed_url') .
'" rel="noreferrer noopener" target="_blank">' .
$this->request->getPost('imported_feed_url') .
' ⎋</a>',
]);
}
$nsItunes = $feed->channel[0]->children(
'http://www.itunes.com/dtds/podcast-1.0.dtd'
);
$podcast = new \App\Entities\Podcast([
'name' => $this->request->getPost('name'),
'imported_feed_url' => $this->request->getPost('imported_feed_url'),
'title' => $feed->channel[0]->title,
'description' => $feed->channel[0]->description,
'image' => download_file($nsItunes->image->attributes()),
'language' => $this->request->getPost('language'),
'category_id' => $this->request->getPost('category'),
'explicit' => empty($nsItunes->explicit)
? false
: $nsItunes->explicit == 'yes',
'author' => $nsItunes->author,
'owner_name' => $nsItunes->owner->name,
'owner_email' => $nsItunes->owner->email,
'type' => empty($nsItunes->type) ? 'episodic' : $nsItunes->type,
'copyright' => $feed->channel[0]->copyright,
'block' => empty($nsItunes->block)
? false
: $nsItunes->block == 'yes',
'complete' => empty($nsItunes->complete)
? false
: $nsItunes->complete == 'yes',
'episode_description_footer' => '',
'custom_html_head' => '',
'created_by' => user(),
'updated_by' => user(),
]);
$podcastModel = new PodcastModel();
$db = \Config\Database::connect();
$db->transStart();
if (!($newPodcastId = $podcastModel->insert($podcast, true))) {
$db->transComplete();
return redirect()
->back()
->withInput()
->with('errors', $podcastModel->errors());
}
$authorize = Services::authorization();
$podcastAdminGroup = $authorize->group('podcast_admin');
$podcastModel->addPodcastContributor(
user()->id,
$newPodcastId,
$podcastAdminGroup->id
);
$converter = new HtmlConverter();
$numberItems = $feed->channel[0]->item->count();
$lastItem =
!empty($this->request->getPost('max_episodes')) &&
$this->request->getPost('max_episodes') < $numberItems
? $this->request->getPost('max_episodes')
: $numberItems;
$slugs = [];
// For each Episode:
for ($itemNumber = 1; $itemNumber <= $lastItem; $itemNumber++) {
$item = $feed->channel[0]->item[$numberItems - $itemNumber];
$nsItunes = $item->children(
'http://www.itunes.com/dtds/podcast-1.0.dtd'
);
$slug = slugify(
$this->request->getPost('slug_field') == 'title'
? $item->title
: basename($item->link)
);
if (in_array($slug, $slugs)) {
$slugNumber = 2;
while (in_array($slug . '-' . $slugNumber, $slugs)) {
$slugNumber++;
}
$slug = $slug . '-' . $slugNumber;
}
$slugs[] = $slug;
$newEpisode = new \App\Entities\Episode([
'podcast_id' => $newPodcastId,
'guid' => empty($item->guid) ? null : $item->guid,
'title' => $item->title,
'slug' => $slug,
'enclosure' => download_file($item->enclosure->attributes()),
'description' => $converter->convert(
$this->request->getPost('description_field') == 'summary'
? $nsItunes->summary
: ($this->request->getPost('description_field') ==
'subtitle_summary'
? '<h3>' .
$nsItunes->subtitle .
"</h3>\n" .
$nsItunes->summary
: $item->description)
),
'image' => empty($nsItunes->image->attributes())
? null
: download_file($nsItunes->image->attributes()),
'explicit' => $nsItunes->explicit == 'yes',
'number' => $this->request->getPost('force_renumber')
? $itemNumber
: $nsItunes->episode,
'season_number' => empty(
$this->request->getPost('season_number')
)
? $nsItunes->season
: $this->request->getPost('season_number'),
'type' => empty($nsItunes->episodeType)
? 'full'
: $nsItunes->episodeType,
'block' => empty($nsItunes->block)
? false
: $nsItunes->block == 'yes',
'created_by' => user(),
'updated_by' => user(),
]);
$newEpisode->setPublishedAt(
date('Y-m-d', strtotime($item->pubDate)),
date('H:i:s', strtotime($item->pubDate))
);
$episodeModel = new EpisodeModel();
if (!$episodeModel->save($newEpisode)) {
// FIX: What shall we do?
return redirect()
->back()
->withInput()
->with('errors', $episodeModel->errors());
}
}
$db->transComplete();
return redirect()->route('podcast-list');
}

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

Yassine Doghri
committed
$languageOptions = (new LanguageModel())->getLanguageOptions();
$categoryOptions = (new CategoryModel())->getCategoryOptions();
$data = [
'podcast' => $this->podcast,
'languageOptions' => $languageOptions,
'categoryOptions' => $categoryOptions,
];

Yassine Doghri
committed
replace_breadcrumb_params([0 => $this->podcast->title]);
return view('admin/podcast/edit', $data);

Yassine Doghri
committed
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
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_id = $this->request->getPost('category');
$this->podcast->explicit = $this->request->getPost('explicit') == 'yes';
$this->podcast->author = $this->request->getPost('author');
$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 = $this->request->getPost('block') == 'yes';
$this->podcast->complete = $this->request->getPost('complete') == 'yes';
$this->podcast->custom_html_head = $this->request->getPost(
'custom_html_head'
);
$this->updated_by = user();

Yassine Doghri
committed

Yassine Doghri
committed
$podcastModel = new PodcastModel();

Yassine Doghri
committed
if (!$podcastModel->save($this->podcast)) {
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', $podcastModel->errors());

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

Yassine Doghri
committed
}
public function delete()
{

Yassine Doghri
committed
(new PodcastModel())->delete($this->podcast->id);

Yassine Doghri
committed
return redirect()->route('podcast-list');