Completed
Push — master ( c2cfef...1a5a57 )
by Angus
02:24
created

History_Model::updateTitleHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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