Passed
Push — develop ( b3eda6...9f2d35 )
by Daniel
03:59 queued 40s
created

birthday::get_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\blocks;
12
13
use blitze\sitemaker\services\blocks\driver\block;
14
15
/**
16
 * Birthday Block
17
 */
18
class birthday extends block
19
{
20
	/** @var \phpbb\cache\driver\driver_interface */
21
	protected $cache;
22
23
	/** @var \phpbb\db\driver\driver_interface */
24
	protected $db;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var string */
33
	protected $time;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \phpbb\cache\driver\driver_interface	$cache		Cache driver interface
39
	 * @param \phpbb\db\driver\driver_interface		$db     	Database connection
40
	 * @param \phpbb\template\template				$template	Template object
41
	 * @param \phpbb\user                           $user		User object
42
	 * @param string								$time		String in a format accepted by strtotime().
43 4
	 */
44
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $time = 'now')
45 4
	{
46 4
		$this->cache = $cache;
47 4
		$this->db = $db;
48 4
		$this->template = $template;
49 4
		$this->user = $user;
50 4
		$this->time = $time;
51
	}
52
53
	/**
54
	 * {@inheritdoc}
55 3
	 */
56
	public function display(array $bdata, $edit_mode = false)
57 3
	{
58 3
		if (($data = $this->cache->get('pt_block_data_' . $bdata['bid'])) === false)
59 3
		{
60 3
			$data = ['birthdays' => $this->find_birthday_users()];
61 3
62 2
			// we only check birthdays every hour, may make this an admin choice
63
			$this->cache->put('pt_block_data_' . $bdata['bid'], $data, 3600);
64
		}
65 2
66 2
		$this->template->assign_var('S_DISPLAY_BIRTHDAY_LIST', false);
67 3
68
		return array(
69 3
			'title'	=> 'BIRTHDAYS',
70
			'data'	=> array_filter($data),
71
		);
72 3
	}
73 3
74 3
	/**
75
	 * @return array
76
	 */
77
	private function find_birthday_users()
78
	{
79
		$time = $this->user->create_datetime($this->time);
80 3
		$now = phpbb_gmgetdate($time->getTimestamp() + $time->getOffset());
81
82 3
		$leap_year_birthdays = $this->adjust_leap_year($now, $time);
83 3
84
		$sql = 'SELECT u.user_id, u.username, u.user_colour, u.user_birthday
85 3
				FROM ' . USERS_TABLE . ' u
86
				LEFT JOIN ' . BANLIST_TABLE . ' b ON (u.user_id = b.ban_userid)
87
				WHERE (b.ban_id IS NULL
88 3
					OR b.ban_exclude = 1)
89 3
					AND (u.user_birthday ' . $this->db->sql_like_expression(sprintf('%2d-%2d-', $now['mday'], $now['mon']) . $this->db->get_any_char()) . " $leap_year_birthdays)
90
					AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
91
				ORDER BY u.username ASC';
92 3
		$result = $this->db->sql_query($sql);
93 3
94 3
		$birthdays = [];
95 3
		while ($row = $this->db->sql_fetchrow($result))
96
		{
97 3
			$birthdays[] = array(
98 3
				'USERNAME'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
99
				'USER_AGE'		=> $this->get_user_age($row['user_birthday'], $now['year']),
100 2
			);
101 2
		}
102 2
		$this->db->sql_freeresult($result);
103 2
104 2
		return $birthdays;
105 2
	}
106 3
107
	/**
108 3
	 * Display birthdays of 29th february on 28th february in non-leap-years
109
	 *
110
	 * @param array $now
111
	 * @param \DateTime $time
112
	 * @return string
113
	 */
114
	private function adjust_leap_year(array $now, \DateTime $time)
115
	{
116
		$leap_year_birthdays = '';
117
		if ($now['mday'] == 28 && $now['mon'] == 2 && !$time->format('L'))
118 3
		{
119
			$leap_year_birthdays = ' OR u.user_birthday ' . $this->db->sql_like_expression(sprintf('%2d-%2d-', 29, 2) . $this->db->get_any_char());
120 3
		}
121 3
122 3
		return $leap_year_birthdays;
123 1
	}
124 1
125
	/**
126 3
	 * @param string $user_birthday
127
	 * @param int $year
128
	 * @return string
129
	 */
130
	private function get_user_age($user_birthday, $year)
131
	{
132
		$birthday_year = (int) substr($user_birthday, -4);
133
		return ($birthday_year) ? max(0, $year - $birthday_year) : '';
134 2
	}
135
136 2
	/**
137 2
	 * {@inheritdoc}
138
	 */
139
	public function get_template()
140
	{
141
		return '@blitze_sitemaker/blocks/birthday.html';
142
	}
143
}
144