Completed
Push — develop ( 6555aa...f3a44e )
by Timothy
02:41
created

src/Aviat/AnimeClient/Controller/Manga.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 View Code Duplication
	public function __construct(ContainerInterface $container)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
	 * Form to add an manga
96
	 *
97
	 * @return void
98
	 */
99 View Code Duplication
	public function add_form()
100
	{
101
		$raw_status_list = MangaReadingStatus::getConstList();
102
103
		$statuses = [];
104
105
		foreach ($raw_status_list as $status_item)
106
		{
107
			$statuses[$status_item] = (string)$this->string($status_item)
108
				->underscored()
109
				->humanize()
110
				->titleize();
111
		}
112
113
		$this->set_session_redirect();
114
		$this->outputHTML('manga/add', [
115
			'title' => $this->config->get('whose_list') .
116
				"'s Manga List &middot; Add",
117
			'action_url' => $this->urlGenerator->url('manga/add'),
118
			'status_list' => $statuses
119
		]);
120
	}
121
122
	/**
123
	 * Add an manga to the list
124
	 *
125
	 * @return void
126
	 */
127
	public function add()
128
	{
129
		$data = $this->request->post->get();
130
		if ( ! array_key_exists('id', $data))
131
		{
132
			$this->redirect("manga/add", 303);
133
		}
134
135
		$result = $this->model->add($data);
136
137
		if ($result['statusCode'] >= 200 && $result['statusCode'] < 300)
138
		{
139
			$this->set_flash_message('Added new manga to list', 'success');
140
		}
141
		else
142
		{
143
			$this->set_flash_message('Failed to add new manga to list' . $result['body'], 'error');
144
		}
145
146
		$this->session_redirect();
147
	}
148
149
	/**
150
	 * Show the manga edit form
151
	 *
152
	 * @param string $id
153
	 * @param string $status
154
	 * @return void
155
	 */
156
	public function edit($id, $status = "All")
157
	{
158
		$this->set_session_redirect();
159
		$item = $this->model->get_library_item($id, $status);
160
		$title = $this->config->get('whose_list') . "'s Manga List &middot; Edit";
161
162
		$this->outputHTML('manga/edit', [
163
			'title' => $title,
164
			'status_list' => MangaReadingStatus::getConstList(),
165
			'item' => $item,
166
			'action' => $this->container->get('url-generator')
167
				->url('/manga/update_form'),
168
		]);
169
	}
170
171
	/**
172
	 * Search for a manga to add to the list
173
	 *
174
	 * @return void
175
	 */
176
 	public function search()
177
 	{
178
 		$query = $this->request->query->get('query');
179
 		$this->outputJSON($this->model->search($query));
180
 	}
181
182
	/**
183
	 * Update an anime item via a form submission
184
	 *
185
	 * @return void
186
	 */
187
	public function form_update()
188
	{
189
		$post_data = $this->request->post->get();
190
191
		// Do some minor data manipulation for
192
		// large form-based updates
193
		$transformer = new MangaListTransformer();
194
		$post_data = $transformer->untransform($post_data);
195
		$full_result = $this->model->update($post_data);
196
197
		$result = $full_result['body'];
198
199
		if (array_key_exists('manga', $result))
200
		{
201
			$m =& $result['manga'][0];
202
			$title = ( ! empty($m['english_title']))
203
				? "{$m['romaji_title']} ({$m['english_title']})"
204
				: "{$m['romaji_title']}";
205
206
			$this->set_flash_message("Successfully updated {$title}.", 'success');
207
		}
208
		else
209
		{
210
			$this->set_flash_message('Failed to update manga.', 'error');
211
		}
212
213
		$this->session_redirect();
214
	}
215
216
	/**
217
	 * Update an anime item
218
	 *
219
	 * @return boolean|null
220
	 */
221
	public function update()
222
	{
223
		$result = $this->model->update($this->request->post->get());
224
		$this->outputJSON($result['body'], $result['statusCode']);
225
	}
226
}
227
// End of MangaController.php