1
|
|
|
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed'); |
2
|
|
|
|
3
|
|
|
class Tracker_Favourites_Model extends Tracker_Base_Model { |
4
|
96 |
|
public function __construct() { |
5
|
96 |
|
parent::__construct(); |
6
|
96 |
|
} |
7
|
|
|
|
8
|
|
|
public function get(int $page) : array { |
9
|
|
|
$rowsPerPage = 50; |
10
|
|
|
$query = $this->db |
11
|
|
|
->select('SQL_CALC_FOUND_ROWS |
12
|
|
|
tt.title, tt.title_url, |
13
|
|
|
ts.site, ts.site_class, |
14
|
|
|
tf.chapter, tf.page, tf.updated_at', FALSE) |
15
|
|
|
->from('tracker_favourites AS tf') |
16
|
|
|
->join('tracker_chapters AS tc', 'tf.chapter_id = tc.id', 'left') |
17
|
|
|
->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left') |
18
|
|
|
->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left') |
19
|
|
|
->where('tc.user_id', $this->User->id) //CHECK: Is this inefficient? Would it be better to have a user_id column in tracker_favourites? |
20
|
|
|
->order_by('tf.id DESC') |
21
|
|
|
->limit($rowsPerPage, ($rowsPerPage * ($page - 1))) |
22
|
|
|
->get(); |
23
|
|
|
|
24
|
|
|
$favourites = ['rows' => [], 'totalPages' => 1]; |
25
|
|
|
if($query->num_rows() > 0) { |
26
|
|
|
foreach($query->result() as $row) { |
27
|
|
|
$arrRow = []; |
28
|
|
|
|
29
|
|
|
$arrRow['updated_at'] = $row->updated_at; |
30
|
|
|
$arrRow['title'] = $row->title; |
31
|
|
|
$arrRow['title_url'] = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url); |
32
|
|
|
|
33
|
|
|
$arrRow['site'] = $row->site; |
34
|
|
|
$arrRow['site_sprite'] = str_replace('.', '-', $row->site); |
35
|
|
|
|
36
|
|
|
$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->chapter); |
37
|
|
|
$arrRow['chapter_url'] = "<a href=\"{$chapterData['url']}\">{$chapterData['number']}</a>"; |
38
|
|
|
|
39
|
|
|
if(!is_null($row->page)) { |
40
|
|
|
$page_url = $this->Tracker->sites->{$row->site_class}->getChapterPageURL($chapterData, (!is_null($row->page) ? (int) $row->page : NULL)); |
41
|
|
|
$chapterNumber = "{$chapterData['number']} | Page: {$row->page}"; |
42
|
|
|
$arrRow['chapter_url'] = !is_null($page_url) ? "<a href=\"{$page_url}\">{$chapterNumber}</a>" : "<a href=\"{$chapterData['url']}\">{$chapterNumber} (No Direct URL)</a>"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$favourites['rows'][] = $arrRow; |
46
|
|
|
} |
47
|
|
|
$favourites['totalPages'] = ceil($this->db->query('SELECT FOUND_ROWS() count;')->row()->count / $rowsPerPage); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $favourites; |
51
|
|
|
} |
52
|
|
|
public function getAll() : array { |
53
|
|
|
$query = $this->db |
54
|
|
|
->select('SQL_CALC_FOUND_ROWS |
55
|
|
|
tt.title, tt.title_url, |
56
|
|
|
ts.site, ts.site_class, |
57
|
|
|
tf.chapter, tf.page, tf.updated_at', FALSE) |
58
|
|
|
->from('tracker_favourites AS tf') |
59
|
|
|
->join('tracker_chapters AS tc', 'tf.chapter_id = tc.id', 'left') |
60
|
|
|
->join('tracker_titles AS tt', 'tc.title_id = tt.id', 'left') |
61
|
|
|
->join('tracker_sites AS ts', 'tt.site_id = ts.id', 'left') |
62
|
|
|
->where('tc.user_id', $this->User->id) //CHECK: Is this inefficient? Would it be better to have a user_id column in tracker_favourites? |
63
|
|
|
->order_by('tf.id DESC') |
64
|
|
|
->get(); |
65
|
|
|
|
66
|
|
|
$favourites = []; |
67
|
|
|
if($query->num_rows() > 0) { |
68
|
|
|
foreach($query->result() as $row) { |
69
|
|
|
$arrRow = []; |
70
|
|
|
|
71
|
|
|
$arrRow['updated_at'] = $row->updated_at; |
72
|
|
|
$arrRow['title'] = $row->title; |
73
|
|
|
$arrRow['title_url'] = $this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url); |
74
|
|
|
|
75
|
|
|
$arrRow['site'] = $row->site; |
76
|
|
|
$arrRow['chapter'] = $row->chapter; |
77
|
|
|
$arrRow['page'] = $row->page; |
78
|
|
|
|
79
|
|
|
$chapterData = $this->Tracker->sites->{$row->site_class}->getChapterData($row->title_url, $row->chapter); |
80
|
|
|
$arrRow['chapter_number'] = $chapterData['number']; |
81
|
|
|
$arrRow['chapter_url'] = $chapterData['url']; |
82
|
|
|
|
83
|
|
|
$favourites[] = $arrRow; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $favourites; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function set(int $userID, string $site, string $title, string $chapter, ?int $page = NULL, bool $removeIfExists = TRUE) : array { |
91
|
|
|
$success = array( |
92
|
|
|
'status' => 'Something went wrong', |
93
|
|
|
'bool' => FALSE |
94
|
|
|
); |
95
|
|
|
if($siteData = $this->Tracker->title->getSiteDataFromURL($site)) { |
96
|
|
|
//Validate user input |
97
|
|
|
if(!$this->sites->{$siteData->site_class}->isValidTitleURL($title)) { |
98
|
|
|
//Error is already logged via isValidTitleURL |
99
|
|
|
$success['status'] = 'Title URL is not valid'; |
100
|
|
|
return $success; |
101
|
|
|
} |
102
|
|
|
if(!$this->sites->{$siteData->site_class}->isValidChapter($chapter)) { |
103
|
|
|
//Error is already logged via isValidChapter |
104
|
|
|
$success['status'] = 'Chapter URL is not valid'; |
105
|
|
|
return $success; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//NOTE: If the title doesn't exist it will be created. This maybe isn't perfect, but it works for now. |
109
|
|
|
$titleID = $this->Tracker->title->getID($title, (int) $siteData->id); |
110
|
|
|
if($titleID === 0) { |
111
|
|
|
//Something went wrong. |
112
|
|
|
log_message('error', "TitleID = 0 for {$title} @ {$siteData->id}"); |
|
|
|
|
113
|
|
|
return $success; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
////We need the series to be tracked |
117
|
|
|
$idCQuery = $this->db->select('id') |
118
|
|
|
->where('user_id', $userID) |
119
|
|
|
->where('title_id', $titleID) |
120
|
|
|
->get('tracker_chapters'); |
121
|
|
|
if(!($idCQuery->num_rows() > 0)) { |
122
|
|
|
//NOTE: This pretty much repeats a lot of what we already did above. Is there a better way to do this? |
123
|
|
|
$this->Tracker->list->update($userID, $site, $title, $chapter, FALSE); |
124
|
|
|
|
125
|
|
|
$idCQuery = $this->db->select('id') |
126
|
|
|
->where('user_id', $userID) |
127
|
|
|
->where('title_id', $titleID) |
128
|
|
|
->get('tracker_chapters'); |
129
|
|
|
} |
130
|
|
|
if($idCQuery->num_rows() > 0) { |
131
|
|
|
$idCQueryRow = $idCQuery->row(); |
132
|
|
|
|
133
|
|
|
//Check if it is already favourited |
134
|
|
|
$idFQuery = $this->db->select('id') |
135
|
|
|
->where('chapter_id', $idCQueryRow->id) |
136
|
|
|
->where('chapter', $chapter) |
137
|
|
|
->where('page', $page) |
138
|
|
|
->get('tracker_favourites'); |
139
|
|
|
if($idFQuery->num_rows() > 0) { |
140
|
|
|
//Chapter is already favourited, so remove it from DB |
141
|
|
|
if($removeIfExists) { |
142
|
|
|
$idFQueryRow = $idFQuery->row(); |
143
|
|
|
|
144
|
|
|
$isSuccess = (bool) $this->db->where('id', $idFQueryRow->id) |
145
|
|
|
->delete('tracker_favourites'); |
146
|
|
|
|
147
|
|
|
if($isSuccess) { |
148
|
|
|
$success = array( |
149
|
|
|
'status' => 'Unfavourited' . ($page ? " page {$page}" : ''), |
150
|
|
|
'bool' => TRUE |
151
|
|
|
); |
152
|
|
|
$this->History->userRemoveFavourite((int) $idCQueryRow->id, $chapter); |
153
|
|
|
} |
154
|
|
|
} else { |
155
|
|
|
$success = array( |
156
|
|
|
'status' => 'Favourite already exists', |
157
|
|
|
'bool' => TRUE |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
} else { |
161
|
|
|
//Chapter is not favourited, so add to DB. |
162
|
|
|
$isSuccess = (bool) $this->db->insert('tracker_favourites', [ |
163
|
|
|
'chapter_id' => $idCQueryRow->id, |
164
|
|
|
'chapter' => $chapter, |
165
|
|
|
'page' => $page, |
166
|
|
|
'updated_at' => date('Y-m-d H:i:s') |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
if($isSuccess) { |
170
|
|
|
$success = array( |
171
|
|
|
'status' => 'Favourited' . ($page ? " page {$page}" : ''), |
172
|
|
|
'bool' => TRUE |
173
|
|
|
); |
174
|
|
|
$this->History->userAddFavourite((int) $idCQueryRow->id, $chapter); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
return $success; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|