Completed
Push — master ( fab013...631a79 )
by Angus
07:23
created

UpdateStatus   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A index() 0 13 1
A _get_updates() 0 64 3
1
<?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class UpdateStatus extends MY_Controller {
4
	public function __construct() {
5
		parent::__construct();
6
7
		$this->load->library('table');
8
	}
9
10
	public function index() {
11
		$this->header_data['title'] = "Update Status";
12
		$this->header_data['page']  = "update_status";
13
14
		$this->body_data['updates'] = array_merge([['Site', 'Title']], $this->_get_updates());
15
16
		$template = array(
17
			'table_open' => '<table class="table table-striped">'
18
		);
19
20
		$this->table->set_template($template);
21
		$this->_render_page("UpdateStatus");
22
	}
23
24
	private function _get_updates() {
25
		$update_time = $this->Tracker->admin->getNextUpdateTime("%H:%i");
26
		$this->body_data['update_time'] = explode(':', $update_time);
27
		//FIXME: This is just a copy/paste of the query in the admin model. Maybe we should just have a way to grab this normally?
28
		// @formatter:off
29
		$query = $this->db
30
			->select('
31
				tracker_titles.title,
32
				tracker_titles.title_url,
33
				tracker_sites.site_class,
34
				from_unixtime(MAX(auth_users.last_login)) AS timestamp
35
			')
36
			->from('tracker_titles')
37
			->join('tracker_sites', 'tracker_sites.id = tracker_titles.site_id', 'left')
38
			->join('tracker_chapters', 'tracker_titles.id = tracker_chapters.title_id', 'left')
39
			->join('auth_users', 'tracker_chapters.user_id = auth_users.id', 'left')
40
			->where('tracker_sites.status', 'enabled')
41
			->group_start()
42
				//Check if title is marked as on-going...
43
				->where('tracker_titles.status', 0) //TODO: Each title should have specific interval time?
44
				//AND matches one of where queries below
45
				->group_start()
46
					//Then check if it's NULL (only occurs for new series)
47
					->where('latest_chapter', NULL)
48
					//OR if it hasn't updated within the past 12 hours AND isn't a custom update site
49
					->or_group_start()
50
						->where('tracker_sites.use_custom', 'N')
51
						->where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 12 HOUR)")
52
					->group_end()
53
					//OR it is a custom update site and hasn't updated within the past 36 hours
54
					->or_where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 36 HOUR)")
55
				->group_end()
56
			->group_end()
57
			->or_group_start()
58
				//Check if title is marked as complete...
59
				->where('tracker_titles.status', 1)
60
				//Then check if it hasn't updated within the past week
61
				->where("last_checked < DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 1 WEEK)")
62
			->group_end()
63
			//Status 2 (One-shot) & 255 (Ignore) are both not updated intentionally.
64
65
			->group_by('tracker_titles.id, tracker_chapters.active')
66
			//Check if the series is actually being tracked by someone
67
			->having('timestamp IS NOT NULL')
68
			//AND if it's currently marked as active by the user
69
			->having('tracker_chapters.active', 'Y')
70
			//AND if they have been active in the last 120 hours (5 days)
71
			->having("timestamp > DATE_SUB(DATE_ADD(NOW(), INTERVAL '{$update_time}' HOUR_MINUTE), INTERVAL 120 HOUR)")
72
			->order_by('tracker_titles.title', 'ASC')
73
			->get();
74
		// @formatter:on
75
76
		$resultArr = [];
77
		if($query->num_rows() > 0) {
78
			foreach($query->result() as $row) {
79
				$resultArr[] = [
80
					'site'           => $row->site_class,
81
					'full_title_url' => "<a href='".$this->Tracker->sites->{$row->site_class}->getFullTitleURL($row->title_url)."'>{$row->title}</a>"
82
				];
83
			}
84
		}
85
86
		return $resultArr;
87
	}
88
}
89