Completed
Push — master ( 631a79...21674a )
by Angus
02:45
created

History_Model::userSetMalID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class History_Model extends CI_Model {
4 103
	public function __construct() {
5 103
		parent::__construct();
6
7 103
		$this->load->database();
8 103
	}
9
10
	/*** TITLE HISTORY ***/
11
	public function updateTitleHistory(int $titleID, $oldChapter, string $newChapter, string $newChapterTimestamp) {
12
		$success = TRUE;
13
		if($oldChapter !== $newChapter) {
14
			$success = $this->db->insert('tracker_titles_history', [
15
				'title_id'    => $titleID,
16
17
				'old_chapter' => $oldChapter,
18
				'new_chapter' => $newChapter,
19
20
				'updated_at'  => $newChapterTimestamp
21
			]);
22
			$this->db->cache_delete('history', (string) $titleID);
23
		}
24
		return (bool) $success;
25
	}
26
27
	public function getTitleHistory(int $titleID, int $page = 1) : array {
28
		$rowsPerPage = 50;
29
		$this->db->cache_on();
30
		$query = $this->db
31
			->select('SQL_CALC_FOUND_ROWS
32
			          tt.title_url,
33
			          ts.site_class,
34
			          tth.updated_at, tth.new_chapter', FALSE)
35
			->from('tracker_titles_history AS tth')
36
			->join('tracker_titles AS tt', 'tth.title_id = tt.id', 'left')
37
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
38
			->where('tt.id', $titleID)
39
			->order_by('tth.id DESC')
40
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
41
			->get();
42
		$this->db->cache_off();
43
44
		$arr = ['rows' => [], 'totalPages' => 1];
45
		if($query->num_rows() > 0) {
46
			foreach($query->result() as $row) {
47
				$arrRow = [];
48
49
				$arrRow['updated_at']  = $row->updated_at;
50
51
				$newChapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->new_chapter);
52
				$arrRow['new_chapter']      = "<a href=\"{$newChapterData['url']}\">{$newChapterData['number']}</a>";
53
				$arrRow['new_chapter_full'] = $row->new_chapter;
54
55
				$arr['rows'][] = $arrRow;
56
			}
57
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
58
		}
59
		return $arr;
60
	}
61
62
	/*** USER HISTORY ***/
63
	/*
64
	 * --User history types--
65
	 * 1: Title added
66
	 * 2: Title updated
67
	 * 3: Title removed
68
	 * 4: Tags updated
69
	 * 5: Category updated
70
	 * 6: Favourite added
71
	 * 7: Favourite removed
72
	 * 8: Title ignored
73
	 */
74
75
	public function userAddTitle(int $chapterID, string $chapter, string $category) : bool {
76
		$success = $this->db->insert('tracker_user_history', [
77
			'chapter_id'  => $chapterID,
78
79
			'type'        => '1',
80
			'custom1'     => $chapter,
81
			'custom2'     => $category,
82
83
			'updated_at'  => date('Y-m-d H:i:s')
84
		]);
85
86
		return $success;
87
	}
88
	public function userUpdateTitle(int $chapterID, string $new_chapter) : bool {
89
		$success = $this->db->insert('tracker_user_history', [
90
			'chapter_id'  => $chapterID,
91
92
			'type'        => '2',
93
			'custom1'     => $new_chapter,
94
95
			'updated_at'  => date('Y-m-d H:i:s')
96
		]);
97
98
		return $success;
99
	}
100
	public function userRemoveTitle(int $chapterID) : bool {
101
		$success = $this->db->insert('tracker_user_history', [
102
			'chapter_id'  => $chapterID,
103
104
			'type'        => '3',
105
106
			'updated_at'  => date('Y-m-d H:i:s')
107
		]);
108
109
		return $success;
110
	}
111
	public function userUpdateTags(int $chapterID, string $new_tags) : bool {
112
		$success = $this->db->insert('tracker_user_history', [
113
			'chapter_id'  => $chapterID,
114
115
			'type'        => '4',
116
			'custom1'     => $new_tags,
117
118
			'updated_at'  => date('Y-m-d H:i:s')
119
		]);
120
121
		return $success;
122
	}
123
	public function userUpdateCategory(int $chapterID, string $new_category) : bool {
124
		$success = $this->db->insert('tracker_user_history', [
125
			'chapter_id'  => $chapterID,
126
127
			'type'        => '5',
128
			'custom1'     => $new_category,
129
130
			'updated_at'  => date('Y-m-d H:i:s')
131
		]);
132
133
		return $success;
134
	}
135
	public function userAddFavourite(int $chapterID, string $chapter) : bool {
136
		$success = $this->db->insert('tracker_user_history', [
137
			'chapter_id'  => $chapterID,
138
139
			'type'        => '6',
140
			'custom1'     => $chapter,
141
142
			'updated_at'  => date('Y-m-d H:i:s')
143
		]);
144
145
		return $success;
146
	}
147
	public function userRemoveFavourite(int $chapterID, string $chapter) : bool {
148
		$success = $this->db->insert('tracker_user_history', [
149
			'chapter_id'  => $chapterID,
150
151
			'type'        => '7',
152
			'custom1'     => $chapter,
153
154
			'updated_at'  => date('Y-m-d H:i:s')
155
		]);
156
157
		return $success;
158
	}
159
	public function userIgnoreTitle(int $chapterID, string $new_chapter) : bool {
160
		$success = $this->db->insert('tracker_user_history', [
161
			'chapter_id'  => $chapterID,
162
163
			'type'        => '8',
164
			'custom1'     => $new_chapter,
165
166
			'updated_at'  => date('Y-m-d H:i:s')
167
		]);
168
169
		return $success;
170
	}
171
	public function userSetMalID(int $chapterID, ?int $malID) : bool {
172
		$success = $this->db->insert('tracker_user_history', [
173
			'chapter_id'  => $chapterID,
174
175
			'type'        => '9',
176
			'custom1'     => $malID,
177
178
			'updated_at'  => date('Y-m-d H:i:s')
179
		]);
180
181
		return $success;
182
	}
183
184
	public function userGetHistory(int $page) : array {
185
		$rowsPerPage = 50;
186
		$query = $this->db
187
			->select('SQL_CALC_FOUND_ROWS
188
			          tt.title, tt.title_url,
189
			          ts.site, ts.site_class,
190
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
191
			->from('tracker_user_history AS tuh')
192
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
193
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
194
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
195
			->where('tc.user_id', $this->User->id)
196
			->order_by('tuh.id DESC')
197
			->limit($rowsPerPage, ($rowsPerPage * ($page - 1)))
198
			->get();
199
200
		$arr = ['rows' => [], 'totalPages' => 1];
201
		if($query->num_rows() > 0) {
202
			foreach($query->result() as $row) {
203
				$arrRow = [];
204
205
				$arrRow['updated_at'] = $row->updated_at;
206
				$arrRow['title']      = $row->title;
207
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
208
209
				$arrRow['site'] = $row->site;
210
				$arrRow['site_sprite'] = str_replace('.', '-', $row->site);
211
212
				switch($row->type) {
213
					case 1:
214
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
215
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
216
						break;
217
218
					case 2:
219
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
220
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
221
						break;
222
223
					case 3:
224
						$arrRow['status'] = "Series removed";
225
						break;
226
227
					case 4:
228
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
229
						break;
230
231
					case 5:
232
						$arrRow['status'] = "Category set to '{$row->custom1}'";
233
						break;
234
235
					case 6:
236
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
237
						$arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
238
						break;
239
240
					case 7:
241
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
242
						$arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
243
						break;
244
245
					case 8:
246
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
247
						$arrRow['status'] = "Chapter ignored: '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
248
						break;
249
250
					case 9:
251
						if(!is_null($row->custom1)) {
252
							$arrRow['status'] = "MAL ID to '{$row->custom1}'";
253
						} else {
254
							$arrRow['status'] = "MAL ID removed";
255
						}
256
						break;
257
258
					default:
259
						$arrRow['status'] = "Something went wrong!";
260
						break;
261
				}
262
				$arr['rows'][] = $arrRow;
263
			}
264
			$arr['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage);
265
		}
266
		return $arr;
267
	}
268
269
	public function userGetHistoryAll() : array {
270
		$rowsPerPage = 50;
0 ignored issues
show
Unused Code introduced by
$rowsPerPage is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
271
		$query = $this->db
272
			->select('SQL_CALC_FOUND_ROWS
273
			          tt.title, tt.title_url,
274
			          ts.site, ts.site_class,
275
			          tuh.type, tuh.custom1, tuh.custom2, tuh.custom3, tuh.updated_at', FALSE)
276
			->from('tracker_user_history AS tuh')
277
			->join('tracker_chapters AS tc', 'tuh.chapter_id = tc.id', 'left')
278
			->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left')
279
			->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left')
280
			->where('tc.user_id', $this->User->id)
281
			->order_by('tuh.id DESC')
282
			->get();
283
284
		$arr = [];
285
		if($query->num_rows() > 0) {
286
			foreach($query->result() as $row) {
287
				$arrRow = [];
288
289
				$arrRow['updated_at'] = $row->updated_at;
290
				$arrRow['title']      = $row->title;
291
				$arrRow['title_url']  = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url);
292
293
				$arrRow['site'] = $row->site;
294
295
				switch($row->type) {
296
					case 1:
297
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
298
						$arrRow['status'] = "Series added at '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>' to category '{$row->custom2}'";
299
						break;
300
301
					case 2:
302
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
303
						$arrRow['status'] = "Chapter updated to '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
304
						break;
305
306
					case 3:
307
						$arrRow['status'] = "Series removed";
308
						break;
309
310
					case 4:
311
						$arrRow['status'] = "Tags set to '{$row->custom1}'";
312
						break;
313
314
					case 5:
315
						$arrRow['status'] = "Category set to '{$row->custom1}'";
316
						break;
317
318
					case 6:
319
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
320
						$arrRow['status'] = "Favourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
321
						break;
322
323
					case 7:
324
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
325
						$arrRow['status'] = "Unfavourited '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
326
						break;
327
328
					case 8:
329
						$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->custom1);
330
						$arrRow['status'] = "Chapter ignored: '<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>'";
331
						break;
332
333
					default:
334
						$arrRow['status'] = "Something went wrong!";
335
						break;
336
				}
337
				$arr[] = $arrRow;
338
			}
339
		}
340
		return $arr;
341
	}
342
}
343