Completed
Push — master ( 6f4587...d33bf7 )
by Angus
02:36
created

AdminPanel::update_normal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
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();
39
	}
40
	public function update_mal_id() {
41
		set_time_limit(0);
42
		$this->_update_mal_backend();
43
		print "Success.";
44
	}
45
46
	private function _list_complete_titles() {
47
		$query = $this->db->select('tracker_titles.id, tracker_sites.site_class, tracker_titles.title, tracker_titles.title_url')
48
		                  ->from('tracker_chapters')
49
		                  ->join('tracker_titles', 'tracker_chapters.title_id = tracker_titles.id', 'left')
50
		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
51
		                  ->like('tracker_chapters.tags', 'complete')
52
		                  ->where('tracker_titles.status', 0)
53
		                  ->get();
54
55
		$completeList = [];
56
		if($query->num_rows() > 0) {
57
			foreach($query->result() as $row) {
58
				$data = [
59
					'id'         => $row->id,
60
					'site_class' => $row->site_class,
61
					'url'        => "<a href='".$this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url)."'>{$row->title}</a>"
62
				];
63
				$completeList[] = $data;
64
			}
65
		}
66
67
		return $completeList;
68
	}
69
70
	private function _update_mal_backend() : void {
71
		//Would prefer to use the query generator here, but don't think it's possible with what I'd like to do here.
72
73
		//Set backend MAL id if more than one person has it set as the same ID.
74
		//- This should be bumped up as we get more users to avoid abuse.
75
		$this->db->query('
76
			UPDATE
77
				tracker_titles dest,
78
				(
79
					SELECT tt.id, tc.mal_id
80
					FROM `tracker_chapters` tc
81
					LEFT JOIN `tracker_titles` tt ON tt.`id` = tc.`title_id`
82
					WHERE tt.mal_id IS NULL AND tc.mal_id IS NOT NULL
83
					GROUP BY tt.id, tc.mal_id
84
					HAVING COUNT(tc.mal_id) > 1
85
				) src
86
			SET dest.mal_id = src.mal_id
87
			WHERE dest.id = src.id
88
		');
89
90
		//Set backend MAL id if an admin has it set.
91
		//TODO: Preferably we'd have a trusted users group, but that is for later down the line...
92
		$this->db->query('
93
			UPDATE
94
				tracker_titles dest,
95
				(
96
					SELECT tt.id, tc.mal_id
97
					FROM `tracker_chapters` tc
98
					LEFT JOIN `tracker_titles` tt ON tt.`id` = tc.`title_id`
99
					LEFT JOIN `auth_users_groups` aug ON tc.`user_id` = aug.`user_id`
100
					WHERE tc.mal_id IS NOT NULL
101
					AND aug.`group_id` = 1
102
				) src
103
			SET dest.mal_id = src.mal_id
104
			WHERE dest.id = src.id
105
		');
106
	}
107
}
108