Completed
Push — develop ( 733603...f85365 )
by Daniel
15:29 queued 09:35
created

birthday::get_user_age()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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