127 lines
4 KiB
PHP
127 lines
4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Podcast\ImportedPodcast;
|
|
use App\Models\Podcast\Podcast;
|
|
use App\Models\Podcast\PodcastEpisode;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class PodcastController extends Controller
|
|
{
|
|
public function index(Request $request) {
|
|
if (!isset($request->per_page) || is_null($request)) {
|
|
$pagination = 5;
|
|
} else {
|
|
$pagination = $request->per_page;
|
|
}
|
|
return Podcast::searchFilter($request)
|
|
->where('id', '!=', 1)
|
|
->with(['episodes', 'imported', 'owner'])
|
|
->paginate($pagination)
|
|
->toJson();
|
|
}
|
|
|
|
public function store(Request $request) {
|
|
return $this->save($request);
|
|
}
|
|
|
|
/**
|
|
* Method used to update a smart block
|
|
* @param Request $request
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function update(Request $request) {
|
|
return $this->save($request);
|
|
}
|
|
|
|
public function destroy($id) {
|
|
try {
|
|
PodcastEpisode::where('podcast_id', $id)->delete();
|
|
ImportedPodcast::where('podcast_id', $id)->delete();
|
|
Podcast::destroy($id);
|
|
return true;
|
|
} catch(\Exception $e) {
|
|
return response()->json(['message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function show(Request $request, $id) {
|
|
$xml = simplexml_load_file($request->url);
|
|
return $xml->channel;
|
|
}
|
|
|
|
public function loadPodcastDataFromXml(Request $request) {
|
|
|
|
try {
|
|
$xml = simplexml_load_file($request->url, null, LIBXML_NOCDATA);
|
|
$xmlArray = (array) $xml->channel;
|
|
|
|
$newArray = [];
|
|
|
|
foreach ($xmlArray['item'] as $key => $item) {
|
|
$item = (array) $item;
|
|
$newArray[$key] = $item;
|
|
$downloading = PodcastEpisode::where('episode_guid','=',$item['guid'])->first();
|
|
if (!is_null($downloading)) {
|
|
$newArray[$key]['imported'] = $downloading->third_party_track_reference->celery_task->status;
|
|
} else {
|
|
$newArray[$key]['imported'] = 0;
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
$xmlArray = $newArray = false;
|
|
}
|
|
|
|
return json_encode([
|
|
'podcast' => $xmlArray,
|
|
'episodes' => $newArray
|
|
]);
|
|
}
|
|
|
|
protected function save(Request $request)
|
|
{
|
|
$user = Auth::user();
|
|
$xml = simplexml_load_file($request->url);
|
|
$itunes = $xml->channel->children('itunes', TRUE);
|
|
|
|
try {
|
|
$dbPodcast = Podcast::firstOrNew(['id' => $request->id]);
|
|
DB::beginTransaction();
|
|
$dbPodcast->fill([
|
|
'url' => $request->url,
|
|
'title' => $request->title,
|
|
'creator' => $itunes->author,
|
|
'description' => $itunes->subtitle,
|
|
'language' => $xml->channel->language,
|
|
'copyright' => $xml->channel->copyright,
|
|
'link' => $xml->channel->link,
|
|
'itunes_author'=> $itunes->author,
|
|
'itunes_keywords' => '',
|
|
'itunes_summary' => $itunes->summary,
|
|
'itunes_subtitle' => $itunes->subtitle,
|
|
'itunes_category' => '',
|
|
'itunes_explicit' => $itunes->explicit,
|
|
'owner' => $user->id,
|
|
])->save();
|
|
|
|
$dbImportedPodcast = ImportedPodcast::firstOrNew(['podcast_id' => $dbPodcast->id]);;
|
|
$dbImportedPodcast->fill([
|
|
'auto_ingest' => !isset($request->auto_ingest) ? true : false,
|
|
'podcast_id' => $dbPodcast->id
|
|
])->save();
|
|
|
|
DB::commit();
|
|
|
|
return response()->json([
|
|
'podcast' => $dbPodcast,
|
|
'episodes' => $xml->channel->children('item', TRUE)
|
|
]);
|
|
} catch (\Exception $e) {
|
|
return response()->json(['message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|