Completed
Push — master ( 2f1390...b9211a )
by Angus
03:24
created

AdminPanel::populate_db()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
ccs 0
cts 14
cp 0
crap 12
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class AdminPanel extends Admin_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
7
		$this->load->library('table');
8
9
		$this->load->helper('form');
10
		$this->load->library('form_validation');
11
	}
12
13
	public function index() : void {
14
		$this->header_data['title'] = "Admin Panel";
15
		$this->header_data['page']  = "admin-panel";
16
17
		$this->body_data['complete_list'] = array_merge([['id', 'site_class', 'url']], $this->_list_complete_titles());
18
		$this->body_data['id_sql']        = 'SELECT * FROM `tracker_titles` WHERE id IN('.implode(',', array_column($this->body_data['complete_list'], 'id')).')';
19
20
		$template = array(
21
			'table_open' => '<table class="table table-striped">'
22
		);
23
24
		$this->table->set_template($template);
25
		$this->_render_page('AdminPanel');
26
	}
27
28
	public function update_normal() {
29
		set_time_limit(0);
30
		$this->Tracker->admin->updateLatestChapters();
31
	}
32
	public function update_custom() {
33
		set_time_limit(0);
34
		$this->Tracker->admin->updateCustom();
35
	}
36
	public function update_titles() {
37
		set_time_limit(0);
38
		$this->Tracker->admin->updateTitles();
0 ignored issues
show
Bug introduced by
The method updateTitles() does not seem to exist on object<Tracker_Admin_Model>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
	}
40
	public function update_mal_id() {
41
		set_time_limit(0);
42
		$this->_update_mal_backend();
43
		print 'Success.';
44
	}
45
	public function populate_db() {
46
		if(ENVIRONMENT === 'development') {
47
			$randomUpdateData = [
48
				['mangadex.org', '18806:--:English', '12612:--:v1/c1'],
49
				['helveticascans.com', 'mousou-telepathy', 'en/0/1'],
50
				['elpsycongroo.tk', 'otomedanshi', 'en/1/1']
51
			];
52
			foreach($randomUpdateData as $updateData) {
53
				$updateData[] = TRUE; //Active marker
54
				$this->Tracker->list->update($this->User->id, ...$updateData);
0 ignored issues
show
Bug introduced by
The call to update() misses some required arguments starting with $title.
Loading history...
Documentation introduced by
$updateData is of type array<integer,string|boolean>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
56
			}
57
58
			print 'Dev DB populated with data';
59
		}
60
	}
61
62
	private function _list_complete_titles() {
63
		$query = $this->db->select('tracker_titles.id, tracker_sites.site_class, tracker_titles.title, tracker_titles.title_url')
64
		                  ->from('tracker_chapters')
65
		                  ->join('tracker_titles', 'tracker_chapters.title_id = tracker_titles.id', 'left')
66
		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
67
		                  ->like('tracker_chapters.tags', 'complete')
68
		                  ->where('tracker_titles.status', 0)
69
		                  ->get();
70
71
		$completeList = [];
72
		if($query->num_rows() > 0) {
73
			foreach($query->result() as $row) {
74
				$data = [
75
					'id'         => $row->id,
76
					'site_class' => $row->site_class,
77
					'url'        => "<a href='".$this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url)."'>{$row->title}</a>"
78
				];
79
				$completeList[] = $data;
80
			}
81
		}
82
83
		return $completeList;
84
	}
85
86
	private function _update_mal_backend() : void {
87
		//Would prefer to use the query generator here, but don't think it's possible with what I'd like to do here.
88
89
		//Set backend MAL id if more than one person has it set as the same ID.
90
		//- This should be bumped up as we get more users to avoid abuse.
91
		$this->db->query('
92
			UPDATE
93
				tracker_titles dest,
94
				(
95
					SELECT tt.id, tc.mal_id
96
					FROM `tracker_chapters` tc
97
					LEFT JOIN `tracker_titles` tt ON tt.`id` = tc.`title_id`
98
					WHERE tt.mal_id IS NULL AND tc.mal_id IS NOT NULL
99
					GROUP BY tt.id, tc.mal_id
100
					HAVING COUNT(tc.mal_id) > 1
101
				) src
102
			SET dest.mal_id = src.mal_id
103
			WHERE dest.id = src.id
104
		');
105
106
		//Set backend MAL id if an admin has it set.
107
		//TODO: Preferably we'd have a trusted users group, but that is for later down the line...
108
		$this->db->query('
109
			UPDATE
110
				tracker_titles dest,
111
				(
112
					SELECT tt.id, tc.mal_id
113
					FROM `tracker_chapters` tc
114
					LEFT JOIN `tracker_titles` tt ON tt.`id` = tc.`title_id`
115
					LEFT JOIN `auth_users_groups` aug ON tc.`user_id` = aug.`user_id`
116
					WHERE tc.mal_id IS NOT NULL
117
					AND aug.`group_id` = 1
118
				) src
119
			SET dest.mal_id = src.mal_id
120
			WHERE dest.id = src.id
121
		');
122
	}
123
}
124