Completed
Push — master ( 631a79...21674a )
by Angus
02:45
created

AdminPanel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
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 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() {
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 convert_mal_tags() {
41
		set_time_limit(0);
42
		$this->_update_mal_id();
43
	}
44
45
	private function _list_complete_titles() {
46
		$query = $this->db->select('tracker_titles.id, tracker_sites.site_class, tracker_titles.title, tracker_titles.title_url')
47
		                  ->from('tracker_chapters')
48
		                  ->join('tracker_titles', 'tracker_chapters.title_id = tracker_titles.id', 'left')
49
		                  ->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
50
		                  ->like('tracker_chapters.tags', 'complete')
51
		                  ->where('tracker_titles.status', 0)
52
		                  ->get();
53
54
		$completeList = [];
55
		if($query->num_rows() > 0) {
56
			foreach($query->result() as $row) {
57
				$data = [
58
					'id'         => $row->id,
59
					'site_class' => $row->site_class,
60
					'url'        => "<a href='".$this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url)."'>{$row->title}</a>"
61
				];
62
				$completeList[] = $data;
63
			}
64
		}
65
66
		return $completeList;
67
	}
68
69
	private function _update_mal_id() : void {
70
		$query = $this->db->select('id, tags')
71
		                  ->from('tracker_chapters')
72
		                  ->where('tags REGEXP "[[:<:]]mal:([0-9]+|none)[[:>:]]"', NULL, FALSE)
73
		                  ->where('mal_id', NULL)
74
		                  ->get();
75
76
77
		if($query->num_rows() > 0) {
78
			foreach($query->result() as $row) {
79
				preg_match('/\\bmal:([0-9]+|none)\\b/', $row->tags, $matches);
80
81
				if(!empty($matches)) {
82
					$malID = ($matches[1] !== 'none' ? $matches[1] : '0');
83
84
					$this->db->set(['mal_id' => $malID, 'last_updated' => NULL])
85
					         ->where('id', $row->id)
86
					         ->update('tracker_chapters');
87
				}
88
			}
89
		}
90
	}
91
}
92