History_Model   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 1.83%

Importance

Changes 0
Metric Value
dl 0
loc 350
rs 9.28
c 0
b 0
f 0
ccs 4
cts 219
cp 0.0183
wmc 39
lcom 1
cbo 6

14 Methods

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