Completed
Push — develop ( de7659...415144 )
by Daniel
09:44
created

birthday::adjust_leap_year()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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