Passed
Push — develop-3.3.x ( 79c962...91aefb )
by Mario
02:34
created

currency::build_sql_data_exists()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2020 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\entity;
12
13
use phpbb\db\driver\driver_interface;
14
use phpbb\language\language;
15
16
/**
17
 * @property driver_interface db                 phpBB Database object
18
 * @property language         language           phpBB Language object
19
 * @property string           lang_key_prefix    Prefix for the messages thrown by exceptions
20
 * @property string           lang_key_suffix    Suffix for the messages thrown by exceptions
21
 */
22
class currency extends main
23
{
24
	/**
25
	 * Data for this entity
26
	 *
27
	 * @var array
28
	 *    currency_id
29
	 *    currency_name
30
	 *    currency_iso_code
31
	 *    currency_symbol
32
	 *    currency_enable
33
	 *    currency_order
34
	 * @access protected
35
	 */
36
	protected $data;
37
	protected $currency_table;
38
39
	/**
40
	 * Constructor
41
	 *
42
	 * @param driver_interface $db         Database object
43
	 * @param language         $language   Language object
44
	 * @param string           $table_name Name of the table used to store data
45
	 *
46
	 * @access public
47
	 */
48
	public function __construct(driver_interface $db, language $language, $table_name)
49
	{
50
		$this->currency_table = $table_name;
51
		parent::__construct(
52
			$db,
53
			$language,
54
			'PPDE_DC',
55
			'CURRENCY',
56
			$table_name,
57
			[
58
				'item_id'       => ['name' => 'currency_id', 'type' => 'integer'],
59
				'item_name'     => ['name' => 'currency_name', 'type' => 'string'],
60
				'item_iso_code' => ['name' => 'currency_iso_code', 'type' => 'string'],
61
				'item_symbol'   => ['name' => 'currency_symbol', 'type' => 'string'],
62
				'item_on_left'  => ['name' => 'currency_on_left', 'type' => 'boolean'],
63
				'item_enable'   => ['name' => 'currency_enable', 'type' => 'boolean'],
64
				'item_order'    => ['name' => 'currency_order', 'type' => 'integer'],
65
			]
66
		);
67
	}
68
69
	/**
70
	 * {@inheritdoc}
71
	 */
72
	public function build_sql_data_exists($iso_code = ''): string
73
	{
74
		return 'SELECT currency_id
75
			FROM ' . $this->currency_table . "
76
			WHERE currency_iso_code = '" . $this->db->sql_escape($iso_code ?: $this->data['currency_iso_code']) . "'";
77
	}
78
79
	/**
80
	 * Get the order number of the currency
81
	 *
82
	 * @return int Order identifier
83
	 * @access public
84
	 */
85
	public function get_currency_order(): int
86
	{
87
		return (int) ($this->data['currency_order'] ?? 0);
88
	}
89
90
	/**
91
	 * Get Currency status
92
	 *
93
	 * @return bool
94
	 * @access public
95
	 */
96
	public function get_currency_position(): bool
97
	{
98
		return (bool) ($this->data['currency_on_left'] ?? false);
99
	}
100
101
	/**
102
	 * Get Currency ISO code
103
	 *
104
	 * @return string ISO code name
105
	 * @access public
106
	 */
107
	public function get_iso_code(): string
108
	{
109
		return (string) ($this->data['currency_iso_code'] ?? '');
110
	}
111
112
	/**
113
	 * Get Currency Symbol
114
	 *
115
	 * @return string Currency symbol
116
	 * @access public
117
	 */
118
	public function get_symbol(): string
119
	{
120
		return (isset($this->data['currency_symbol'])) ? html_entity_decode($this->data['currency_symbol'], ENT_COMPAT | ENT_HTML5, 'UTF-8') : '';
121
	}
122
123
	/**
124
	 * Set Currency status
125
	 *
126
	 * @param bool $enable
127
	 *
128
	 * @return currency $this object for chaining calls; load()->set()->save()
129
	 * @access public
130
	 */
131
	public function set_currency_enable($enable): currency
132
	{
133
		// Set the item type on our data array
134
		$this->data['currency_enable'] = (bool) $enable;
135
136
		return $this;
137
	}
138
139
	/**
140
	 * {@inheritdoc}
141
	 */
142
	public function check_required_field(): bool
143
	{
144
		return empty($this->data['currency_name']) || empty($this->data['currency_iso_code']) || empty($this->data['currency_symbol']);
145
	}
146
147
	/**
148
	 * Set Currency order number
149
	 *
150
	 * @return currency $this object for chaining calls; load()->set()->save()
151
	 * @access public
152
	 */
153
	public function set_order()
154
	{
155
		$order = $this->get_max_order() + 1;
156
157
		/*
158
		* If the data is out of range we'll throw an exception. We use 16777215 as a
159
		* maximum because it matches the MySQL unsigned mediumint maximum value which
160
		* is the lowest amongst the DBMS supported by phpBB.
161
		*/
162
		if ($order < 0 || $order > 16777215)
163
		{
164
			$this->display_warning_message('EXCEPTION_OUT_OF_BOUNDS', 'currency_order');
165
		}
166
167
		$this->data['currency_order'] = $order;
168
169
		return $this;
170
	}
171
172
	/**
173
	 * Get max currency order value
174
	 *
175
	 * @return int Order identifier
176
	 * @access private
177
	 */
178
	private function get_max_order(): int
179
	{
180
		$sql = 'SELECT MAX(currency_order) AS max_order
181
			FROM ' . $this->currency_table;
182
		$result = $this->db->sql_query($sql);
183
		$field = $this->db->sql_fetchfield('max_order');
184
		$this->db->sql_freeresult($result);
185
186
		return (int) $field;
187
	}
188
189
	/**
190
	 * Returns error if the currency is enabled
191
	 *
192
	 * @return void
193
	 * @access protected
194
	 */
195
	protected function check_currency_enable(): void
196
	{
197
		if ($this->get_currency_enable())
198
		{
199
			// Return an error if the currency is enabled
200
			$this->display_warning_message('PPDE_DISABLE_BEFORE_DELETION');
201
		}
202
	}
203
204
	/**
205
	 * Get Currency status
206
	 *
207
	 * @return boolean
208
	 * @access public
209
	 */
210
	public function get_currency_enable(): bool
211
	{
212
		return (bool) ($this->data['currency_enable'] ?? false);
213
	}
214
}
215