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