Skip to content
Snippets Groups Projects
ContributorController.php 5.21 KiB
Newer Older
/**
 * @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\Entities\Podcast;
use App\Entities\User;
use CodeIgniter\Exceptions\PageNotFoundException;
use Exception;
class ContributorController extends BaseController

    public function _remap($method, ...$params)
    {
        $this->podcast = (new PodcastModel())->getPodcastById($params[0]);
        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,
            ),
        ];

        replace_breadcrumb_params([
            0 => $this->podcast->title,
            1 => $this->user->username,
        ]);
        return view('admin/contributor/view', $data);
        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;
            },
        $data = [
            'podcast' => $this->podcast,
            'userOptions' => $userOptions,
            'roleOptions' => $roleOptions,
        replace_breadcrumb_params([0 => $this->podcast->title]);
        return view('admin/contributor/add', $data);
        try {
            (new PodcastModel())->addPodcastContributor(
                $this->request->getPost('user'),
                $this->podcast->id,
                ->with('errors', [
                    lang('Contributor.messages.alreadyAddedError'),
                ]);
        return redirect()->route('contributor-list', [$this->podcast->id]);
        helper('form');

        $roles = (new GroupModel())->getContributorRoles();
        $roleOptions = array_reduce(
            $roles,
            function ($result, $role) {
                $result[$role->id] = lang('Contributor.roles.' . $role->name);
                return $result;
            },
        $data = [
            'podcast' => $this->podcast,
            'user' => $this->user,
            'contributorGroupId' => (new PodcastModel())->getContributorGroupId(
            'roleOptions' => $roleOptions,
        replace_breadcrumb_params([
            0 => $this->podcast->title,
            1 => $this->user->username,
        ]);
        return view('admin/contributor/edit', $data);
        (new PodcastModel())->updatePodcastContributor(
            $this->user->id,
            $this->podcast->id,
        return redirect()->route('contributor-list', [$this->podcast->id]);
        if ($this->podcast->created_by === $this->user->id) {
                    lang('Contributor.messages.removeOwnerContributorError'),
            !$podcastModel->removePodcastContributor(
                ->with('errors', $podcastModel->errors());
                lang('Contributor.messages.removeContributorSuccess', [
                    'username' => $this->user->username,
                    'podcastTitle' => $this->podcast->title,