Completed
Push — master ( 645554...205c7a )
by Timothy
05:48
created

Manga::form_update()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
rs 8.8571
cc 3
eloc 15
nc 3
nop 0
1
<?php
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * @package     HummingbirdAnimeClient
8
 * @author      Timothy J. Warren
9
 * @copyright   Copyright (c) 2015 - 2016
10
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
11
 * @license     MIT
12
 */
13
namespace Aviat\AnimeClient\Controller;
14
15
use Aviat\Ion\Di\ContainerInterface;
16
use Aviat\AnimeClient\Controller;
17
use Aviat\AnimeClient\Config;
18
use Aviat\AnimeClient\Model\Manga as MangaModel;
19
use Aviat\AnimeClient\Hummingbird\Enum\MangaReadingStatus;
20
use Aviat\AnimeClient\Hummingbird\Transformer\MangaListTransformer;
21
22
/**
23
 * Controller for manga list
24
 */
25
class Manga extends Controller {
26
27
	use \Aviat\Ion\StringWrapper;
28
29
	/**
30
	 * The manga model
31
	 * @var object $model
32
	 */
33
	protected $model;
34
35
	/**
36
	 * Data to ve sent to all routes in this controller
37
	 * @var array $base_data
38
	 */
39
	protected $base_data;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param ContainerInterface $container
45
	 */
46
	public function __construct(ContainerInterface $container)
47
	{
48
		parent::__construct($container);
49
50
		$this->model = $container->get('manga-model');
51
		$this->base_data = array_merge($this->base_data, [
52
			'menu_name' => 'manga_list',
53
			'config' => $this->config,
54
			'url_type' => 'manga',
55
			'other_type' => 'anime'
56
		]);
57
	}
58
59
	/**
60
	 * Get a section of the manga list
61
	 *
62
	 * @param string $status
63
	 * @param string $view
64
	 * @return void
65
	 */
66
	public function index($status = "all", $view = "")
67
	{
68
		$map = [
69
			'all' => 'All',
70
			'plan_to_read' => MangaModel::PLAN_TO_READ,
71
			'reading' => MangaModel::READING,
72
			'completed' => MangaModel::COMPLETED,
73
			'dropped' => MangaModel::DROPPED,
74
			'on_hold' => MangaModel::ON_HOLD
75
		];
76
77
		$title = $this->config->get('whose_list') . "'s Manga List &middot; {$map[$status]}";
78
79
		$view_map = [
80
			'' => 'cover',
81
			'list' => 'list'
82
		];
83
84
		$data = ($status !== 'all')
85
			? [$map[$status] => $this->model->get_list($map[$status])]
86
			: $this->model->get_all_lists();
87
88
		$this->outputHTML('manga/' . $view_map[$view], [
89
			'title' => $title,
90
			'sections' => $data,
91
		]);
92
	}
93
94
	/**
95
	 * Show the manga edit form
96
	 *
97
	 * @param string $id
98
	 * @param string $status
99
	 * @return void
100
	 */
101
	public function edit($id, $status = "All")
102
	{
103
		$this->set_session_redirect();
104
		$item = $this->model->get_library_item($id, $status);
105
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
106
107
		$this->outputHTML('manga/edit', [
108
			'title' => $title,
109
			'status_list' => MangaReadingStatus::getConstList(),
1 ignored issue
show
Bug introduced by
The method getConstList() cannot be called from this context as it is declared protected in class Aviat\Ion\Enum.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
110
			'item' => $item,
111
			'action' => $this->container->get('url-generator')
112
				->url('/manga/update_form'),
113
		]);
114
	}
115
116
	/**
117
	 * Update an anime item via a form submission
118
	 *
119
	 * @return void
120
	 */
121
	public function form_update()
122
	{
123
		$post_data = $this->request->post->get();
124
125
		// Do some minor data manipulation for
126
		// large form-based updates
127
		$transformer = new MangaListTransformer();
128
		$post_data = $transformer->untransform($post_data);
129
		$full_result = $this->model->update($post_data);
130
131
		$result = $full_result['body'];
132
133
		if (array_key_exists('manga', $result))
134
		{
135
			$m =& $result['manga'][0];
136
			$title = ( ! empty($m['english_title']))
137
				? "{$m['romaji_title']} ({$m['english_title']})"
138
				: "{$m['romaji_title']}";
139
140
			$this->set_flash_message("Successfully updated {$title}.", 'success');
141
		}
142
		else
143
		{
144
			$this->set_flash_message('Failed to update anime.', 'error');
145
		}
146
147
		$this->session_redirect();
148
	}
149
150
	/**
151
	 * Update an anime item
152
	 *
153
	 * @return boolean|null
154
	 */
155
	public function update()
156
	{
157
		$this->outputJSON($this->model->update($this->request->post->get()));
158
	}
159
}
160
// End of MangaController.php