Newer
Older
<?php

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

Yassine Doghri
committed
use App\Entities\Podcast;
use App\Entities\User;
use CodeIgniter\Exceptions\PageNotFoundException;
use Exception;

Yassine Doghri
committed
use App\Authorization\GroupModel;
use App\Models\PodcastModel;

Yassine Doghri
committed
use App\Models\UserModel;
class ContributorController extends BaseController

Yassine Doghri
committed
* @var Podcast
*/
protected $podcast;
/**

Yassine Doghri
committed
* @var User|null
*/
protected $user;
public function _remap($method, ...$params)
{
$this->podcast = (new PodcastModel())->getPodcastById($params[0]);

Yassine Doghri
committed
if (count($params) <= 1) {
return $this->$method();
}

Yassine Doghri
committed
if (
$this->user = (new UserModel())->getPodcastContributor(
$params[1],
$params[0],
)
) {
return $this->$method();
}
throw PageNotFoundException::forPageNotFound();
}
public function list()
{
$data = [
'podcast' => $this->podcast,
];
replace_breadcrumb_params([0 => $this->podcast->title]);
return view('admin/contributor/list', $data);
}
public function view()
{
$data = [
'contributor' => (new UserModel())->getPodcastContributor(
$this->user->id,

Yassine Doghri
committed
$this->podcast->id,
),
];
replace_breadcrumb_params([
0 => $this->podcast->title,
1 => $this->user->username,
]);
return view('admin/contributor/view', $data);
}
public function add()
{
helper('form');
$users = (new UserModel())->findAll();
$userOptions = array_reduce(
$users,
function ($result, $user) {
$result[$user->id] = $user->username;
return $result;
},

Yassine Doghri
committed
[],
);
$roles = (new GroupModel())->getContributorRoles();
$roleOptions = array_reduce(
$roles,
function ($result, $role) {
$result[$role->id] = lang('Contributor.roles.' . $role->name);
return $result;
},

Yassine Doghri
committed
[],
$data = [
'podcast' => $this->podcast,
'userOptions' => $userOptions,
'roleOptions' => $roleOptions,
];
replace_breadcrumb_params([0 => $this->podcast->title]);
return view('admin/contributor/add', $data);
}
public function attemptAdd()
{

Yassine Doghri
committed
try {
(new PodcastModel())->addPodcastContributor(
$this->request->getPost('user'),
$this->podcast->id,

Yassine Doghri
committed
$this->request->getPost('role'),

Yassine Doghri
committed
);

Yassine Doghri
committed
} catch (Exception $exception) {

Yassine Doghri
committed
return redirect()
->back()
->withInput()

Yassine Doghri
committed
->with('errors', [
lang('Contributor.messages.alreadyAddedError'),
]);

Yassine Doghri
committed
}
return redirect()->route('contributor-list', [$this->podcast->id]);
}
public function edit()
{
helper('form');
$roles = (new GroupModel())->getContributorRoles();
$roleOptions = array_reduce(
$roles,
function ($result, $role) {
$result[$role->id] = lang('Contributor.roles.' . $role->name);
return $result;
},

Yassine Doghri
committed
[],
$data = [
'podcast' => $this->podcast,
'user' => $this->user,

Yassine Doghri
committed
'contributorGroupId' => (new PodcastModel())->getContributorGroupId(

Yassine Doghri
committed
$this->user->id,

Yassine Doghri
committed
$this->podcast->id,

Yassine Doghri
committed
),
'roleOptions' => $roleOptions,
];
replace_breadcrumb_params([
0 => $this->podcast->title,
1 => $this->user->username,
]);
return view('admin/contributor/edit', $data);
}
public function attemptEdit()
{

Yassine Doghri
committed
(new PodcastModel())->updatePodcastContributor(
$this->user->id,
$this->podcast->id,

Yassine Doghri
committed
$this->request->getPost('role'),
);
return redirect()->route('contributor-list', [$this->podcast->id]);
}
public function remove()
{
if ($this->podcast->created_by === $this->user->id) {

Yassine Doghri
committed
return redirect()
->back()
->with('errors', [

Yassine Doghri
committed
lang('Contributor.messages.removeOwnerContributorError'),

Yassine Doghri
committed
]);
}

Yassine Doghri
committed
$podcastModel = new PodcastModel();

Yassine Doghri
committed
if (

Yassine Doghri
committed
!$podcastModel->removePodcastContributor(

Yassine Doghri
committed
$this->user->id,

Yassine Doghri
committed
$this->podcast->id,

Yassine Doghri
committed
) {
return redirect()
->back()

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

Yassine Doghri
committed
}

Yassine Doghri
committed
return redirect()
->back()
->with(
'message',

Yassine Doghri
committed
lang('Contributor.messages.removeContributorSuccess', [

Yassine Doghri
committed
'username' => $this->user->username,
'podcastTitle' => $this->podcast->title,

Yassine Doghri
committed
]),

Yassine Doghri
committed
);