2020-07-16 10:08:23 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
/**
|
|
|
|
* @copyright 2020 Podlibre
|
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
2020-07-31 16:05:10 +00:00
|
|
|
use App\Authorization\GroupModel;
|
2020-07-16 10:08:23 +00:00
|
|
|
use App\Models\PodcastModel;
|
2020-07-31 16:05:10 +00:00
|
|
|
use App\Models\UserModel;
|
2020-07-16 10:08:23 +00:00
|
|
|
|
|
|
|
class Contributor extends BaseController
|
|
|
|
{
|
2020-08-05 17:26:04 +00:00
|
|
|
/**
|
|
|
|
* @var \App\Entities\Podcast
|
|
|
|
*/
|
|
|
|
protected $podcast;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \App\Entities\User|null
|
|
|
|
*/
|
|
|
|
protected $user;
|
2020-07-16 10:08:23 +00:00
|
|
|
|
|
|
|
public function _remap($method, ...$params)
|
|
|
|
{
|
2020-09-04 09:09:26 +00:00
|
|
|
$this->podcast = (new PodcastModel())->getPodcastById($params[0]);
|
2020-07-16 10:08:23 +00:00
|
|
|
|
|
|
|
if (count($params) > 1) {
|
|
|
|
if (
|
2020-07-31 16:05:10 +00:00
|
|
|
!($this->user = (new UserModel())->getPodcastContributor(
|
|
|
|
$params[1],
|
|
|
|
$params[0]
|
|
|
|
))
|
2020-07-16 10:08:23 +00:00
|
|
|
) {
|
|
|
|
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->$method();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function list()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
];
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([0 => $this->podcast->title]);
|
|
|
|
return view('admin/contributor/list', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function view()
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'contributor' => (new UserModel())->getPodcastContributor(
|
|
|
|
$this->user->id,
|
|
|
|
$this->podcast->id
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->podcast->title,
|
|
|
|
1 => $this->user->username,
|
|
|
|
]);
|
|
|
|
return view('admin/contributor/view', $data);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add()
|
|
|
|
{
|
2020-08-14 18:27:57 +00:00
|
|
|
helper('form');
|
|
|
|
|
|
|
|
$users = (new UserModel())->findAll();
|
|
|
|
$userOptions = array_reduce(
|
|
|
|
$users,
|
|
|
|
function ($result, $user) {
|
|
|
|
$result[$user->id] = $user->username;
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
$roles = (new GroupModel())->getContributorRoles();
|
|
|
|
$roleOptions = array_reduce(
|
|
|
|
$roles,
|
|
|
|
function ($result, $role) {
|
|
|
|
$result[$role->id] = lang('Contributor.roles.' . $role->name);
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
2020-08-14 18:27:57 +00:00
|
|
|
'userOptions' => $userOptions,
|
|
|
|
'roleOptions' => $roleOptions,
|
2020-07-16 10:08:23 +00:00
|
|
|
];
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([0 => $this->podcast->title]);
|
|
|
|
return view('admin/contributor/add', $data);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptAdd()
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
try {
|
|
|
|
(new PodcastModel())->addPodcastContributor(
|
|
|
|
$this->request->getPost('user'),
|
|
|
|
$this->podcast->id,
|
|
|
|
$this->request->getPost('role')
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', [
|
|
|
|
lang('Contributor.messages.alreadyAddedError'),
|
|
|
|
]);
|
2020-07-31 16:05:10 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-14 18:27:57 +00:00
|
|
|
return redirect()->route('contributor-list', [$this->podcast->id]);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function edit()
|
|
|
|
{
|
2020-08-14 18:27:57 +00:00
|
|
|
helper('form');
|
|
|
|
|
|
|
|
$roles = (new GroupModel())->getContributorRoles();
|
|
|
|
$roleOptions = array_reduce(
|
|
|
|
$roles,
|
|
|
|
function ($result, $role) {
|
|
|
|
$result[$role->id] = lang('Contributor.roles.' . $role->name);
|
|
|
|
return $result;
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2020-07-16 10:08:23 +00:00
|
|
|
$data = [
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'user' => $this->user,
|
2020-08-04 11:25:22 +00:00
|
|
|
'contributorGroupId' => (new PodcastModel())->getContributorGroupId(
|
2020-07-31 16:05:10 +00:00
|
|
|
$this->user->id,
|
|
|
|
$this->podcast->id
|
|
|
|
),
|
2020-08-14 18:27:57 +00:00
|
|
|
'roleOptions' => $roleOptions,
|
2020-07-16 10:08:23 +00:00
|
|
|
];
|
|
|
|
|
2020-08-05 16:10:39 +00:00
|
|
|
replace_breadcrumb_params([
|
|
|
|
0 => $this->podcast->title,
|
|
|
|
1 => $this->user->username,
|
|
|
|
]);
|
|
|
|
return view('admin/contributor/edit', $data);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function attemptEdit()
|
|
|
|
{
|
2020-07-31 16:05:10 +00:00
|
|
|
(new PodcastModel())->updatePodcastContributor(
|
|
|
|
$this->user->id,
|
|
|
|
$this->podcast->id,
|
|
|
|
$this->request->getPost('role')
|
2020-07-16 10:08:23 +00:00
|
|
|
);
|
|
|
|
|
2020-08-14 18:27:57 +00:00
|
|
|
return redirect()->route('contributor-list', [$this->podcast->id]);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function remove()
|
|
|
|
{
|
2020-10-02 15:38:16 +00:00
|
|
|
if ($this->podcast->created_by == $this->user->id) {
|
2020-07-31 16:05:10 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('errors', [
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('Contributor.messages.removeOwnerContributorError'),
|
2020-07-31 16:05:10 +00:00
|
|
|
]);
|
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
$podcastModel = new PodcastModel();
|
2020-07-31 16:05:10 +00:00
|
|
|
if (
|
2020-08-04 11:25:22 +00:00
|
|
|
!$podcastModel->removePodcastContributor(
|
2020-07-31 16:05:10 +00:00
|
|
|
$this->user->id,
|
|
|
|
$this->podcast->id
|
2020-07-16 10:08:23 +00:00
|
|
|
)
|
2020-07-31 16:05:10 +00:00
|
|
|
) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
2020-08-04 11:25:22 +00:00
|
|
|
->with('errors', $podcastModel->errors());
|
2020-07-31 16:05:10 +00:00
|
|
|
}
|
2020-07-16 10:08:23 +00:00
|
|
|
|
2020-07-31 16:05:10 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with(
|
|
|
|
'message',
|
2020-08-04 11:25:22 +00:00
|
|
|
lang('Contributor.messages.removeContributorSuccess', [
|
2020-07-31 16:05:10 +00:00
|
|
|
'username' => $this->user->username,
|
|
|
|
'podcastTitle' => $this->podcast->title,
|
|
|
|
])
|
|
|
|
);
|
2020-07-16 10:08:23 +00:00
|
|
|
}
|
|
|
|
}
|