donation_pages::set_message_option()   B
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
c 0
b 0
f 0
nc 8
nop 3
dl 0
loc 27
rs 8.8333
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\config\config;
14
use phpbb\db\driver\driver_interface;
15
use phpbb\language\language;
16
use phpbb\user;
17
18
/**
19
 * @property driver_interface db                 phpBB Database object
20
 * @property language         language           phpBB Language object
21
 * @property string           lang_key_prefix    Prefix for the messages thrown by exceptions
22
 * @property string           lang_key_suffix    Suffix for the messages thrown by exceptions
23
 */
24
class donation_pages extends main
25
{
26
	/**
27
	 * Data for this entity
28
	 *
29
	 * @var array
30
	 *    page_id
31
	 *    page_title
32
	 *    page_lang_id
33
	 *    page_content
34
	 *    page_content_bbcode_bitfield
35
	 *    page_content_bbcode_uid
36
	 *    page_content_bbcode_options
37
	 * @access protected
38
	 */
39
	protected $data;
40
	protected $config;
41
	protected $donation_pages_table;
42
	protected $user;
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param config           $config     Config object
48
	 * @param driver_interface $db         Database object
49
	 * @param language         $language   Language user object
50
	 * @param user             $user       User object
51
	 * @param string           $table_name Name of the table used to store data
52
	 *
53
	 * @access public
54
	 */
55
	public function __construct(
56
		config $config,
57
		driver_interface $db,
58
		language $language,
59
		user $user,
60
		$table_name
61
	)
62
	{
63
		$this->config = $config;
64
		$this->donation_pages_table = $table_name;
65
		$this->user = $user;
66
		parent::__construct(
67
			$db,
68
			$language,
69
			'PPDE_DP',
70
			'DONATION_PAGES',
71
			$table_name,
72
			[
73
				'item_id'                      => ['name' => 'page_id', 'type' => 'integer'],
74
				'item_name'                    => ['name' => 'page_title', 'type' => 'string'],
75
				'item_lang_id'                 => ['name' => 'page_lang_id', 'type' => 'integer'],
76
				'item_content'                 => ['name' => 'page_content', 'type' => 'string'],
77
				'item_content_bbcode_bitfield' => ['name' => 'page_content_bbcode_bitfield', 'type' => 'string'],
78
				'item_content_bbcode_uid'      => ['name' => 'page_content_bbcode_uid', 'type' => 'string'],
79
				'item_content_bbcode_options'  => ['name' => 'page_content_bbcode_options', 'type' => 'integer'],
80
			]
81
		);
82
	}
83
84
	/**
85
	 * Get language id
86
	 *
87
	 * @return int Lang identifier
88
	 * @access public
89
	 */
90
	public function get_lang_id(): int
91
	{
92
		return (int) ($this->data['page_lang_id'] ?? 0);
93
	}
94
95
	/**
96
	 * Get message for edit
97
	 *
98
	 * @return string
99
	 * @access public
100
	 */
101
	public function get_message_for_edit(): string
102
	{
103
		// Use defaults if these haven't been set yet
104
		$message = $this->data['page_content'] ?? '';
105
		$uid = $this->data['page_content_bbcode_uid'] ?? '';
106
		$options = (int) ($this->data['page_content_bbcode_options'] ?? 0);
107
108
		// Generate for edit
109
		$message_data = generate_text_for_edit($message, $uid, $options);
110
111
		return $message_data['text'];
112
	}
113
114
	/**
115
	 * Get message for display
116
	 *
117
	 * @param bool $censor_text True to censor the text (Default: true)
118
	 *
119
	 * @return string
120
	 * @access public
121
	 */
122
	public function get_message_for_display($censor_text = true): string
123
	{
124
		// If these haven't been set yet; use defaults
125
		$message = $this->data['page_content'] ?? '';
126
		$uid = $this->data['page_content_bbcode_uid'] ?? '';
127
		$bitfield = $this->data['page_content_bbcode_bitfield'] ?? '';
128
		$options = (int) ($this->data['page_content_bbcode_options'] ?? 0);
129
130
		// Generate for display
131
		return generate_text_for_display($message, $uid, $bitfield, $options, $censor_text);
132
	}
133
134
	/**
135
	 * Enable bbcode on the message
136
	 *
137
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
138
	 * @access public
139
	 */
140
	public function message_enable_bbcode()
141
	{
142
		$this->set_message_option(OPTION_FLAG_BBCODE);
143
144
		return $this;
145
	}
146
147
	/**
148
	 * Set option helpers
149
	 *
150
	 * @param int  $option_value    Value of the option
151
	 * @param bool $negate          Negate (unset) option (Default: False)
152
	 * @param bool $reparse_message Reparse the message after setting option (Default: True)
153
	 *
154
	 * @return void
155
	 * @access protected
156
	 */
157
	protected function set_message_option($option_value, $negate = false, $reparse_message = true): void
158
	{
159
		// Set item_text_bbcode_options to 0 if it does not yet exist
160
		$this->data['page_content_bbcode_options'] = (int) ($this->data['page_content_bbcode_options'] ?? 0);
161
162
		// If we're setting the option and the option is not already set
163
		if (!$negate && !($this->data['page_content_bbcode_options'] & $option_value))
164
		{
165
			// Add the option to the options
166
			$this->data['page_content_bbcode_options'] += $option_value;
167
		}
168
169
		// If we're unsetting the option and the option is already set
170
		if ($negate && $this->data['page_content_bbcode_options'] & $option_value)
171
		{
172
			// Subtract the option from the options
173
			$this->data['page_content_bbcode_options'] -= $option_value;
174
		}
175
176
		// Reparse the message
177
		if ($reparse_message && !empty($this->data['page_content']))
178
		{
179
			$message = $this->data['page_content'];
180
181
			decode_message($message, $this->data['page_content_bbcode_uid']);
182
183
			$this->set_message($message);
184
		}
185
	}
186
187
	/**
188
	 * Set message
189
	 *
190
	 * @param string $message
191
	 *
192
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
193
	 * @access public
194
	 */
195
	public function set_message($message)
196
	{
197
		// Prepare the text for storage
198
		$uid = $bitfield = $flags = '';
199
		generate_text_for_storage($message, $uid, $bitfield, $flags, $this->message_bbcode_enabled(), $this->message_magic_url_enabled(), $this->message_smilies_enabled());
0 ignored issues
show
Bug introduced by
$flags of type string is incompatible with the type integer expected by parameter $flags of generate_text_for_storage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
		generate_text_for_storage($message, $uid, $bitfield, /** @scrutinizer ignore-type */ $flags, $this->message_bbcode_enabled(), $this->message_magic_url_enabled(), $this->message_smilies_enabled());
Loading history...
200
201
		// Set the message to our data array
202
		$this->data['page_content'] = $message;
203
		$this->data['page_content_bbcode_uid'] = $uid;
204
		$this->data['page_content_bbcode_bitfield'] = $bitfield;
205
206
		return $this;
207
	}
208
209
	/**
210
	 * Check if bbcode is enabled on the message
211
	 *
212
	 * @return bool
213
	 * @access public
214
	 */
215
	public function message_bbcode_enabled(): bool
216
	{
217
		return (bool) ($this->data['page_content_bbcode_options'] & OPTION_FLAG_BBCODE);
218
	}
219
220
	/**
221
	 * Check if magic_url is enabled on the message
222
	 *
223
	 * @return bool
224
	 * @access public
225
	 */
226
	public function message_magic_url_enabled(): bool
227
	{
228
		return (bool) ($this->data['page_content_bbcode_options'] & OPTION_FLAG_LINKS);
229
	}
230
231
	/**
232
	 * Check if smilies are enabled on the message
233
	 *
234
	 * @return bool
235
	 * @access public
236
	 */
237
	public function message_smilies_enabled(): bool
238
	{
239
		return (bool) ($this->data['page_content_bbcode_options'] & OPTION_FLAG_SMILIES);
240
	}
241
242
	/**
243
	 * Disable bbcode on the message
244
	 *
245
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
246
	 * @access public
247
	 */
248
	public function message_disable_bbcode()
249
	{
250
		$this->set_message_option(OPTION_FLAG_BBCODE, true);
251
252
		return $this;
253
	}
254
255
	/**
256
	 * Enable magic url on the message
257
	 *
258
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
259
	 * @access public
260
	 */
261
	public function message_enable_magic_url()
262
	{
263
		$this->set_message_option(OPTION_FLAG_LINKS);
264
265
		return $this;
266
	}
267
268
	/**
269
	 * Disable magic url on the message
270
	 *
271
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
272
	 * @access public
273
	 */
274
	public function message_disable_magic_url()
275
	{
276
		$this->set_message_option(OPTION_FLAG_LINKS, true);
277
278
		return $this;
279
	}
280
281
	/**
282
	 * Enable smilies on the message
283
	 *
284
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
285
	 * @access public
286
	 */
287
	public function message_enable_smilies()
288
	{
289
		$this->set_message_option(OPTION_FLAG_SMILIES);
290
291
		return $this;
292
	}
293
294
	/**
295
	 * Disable smilies on the message
296
	 *
297
	 * @return donation_pages $this object for chaining calls; load()->set()->save()
298
	 * @access public
299
	 */
300
	public function message_disable_smilies()
301
	{
302
		$this->set_message_option(OPTION_FLAG_SMILIES, true);
303
304
		return $this;
305
	}
306
307
	/**
308
	 * {@inheritdoc}
309
	 */
310
	public function build_sql_data_exists(): string
311
	{
312
		$sql_ary = [
313
			'SELECT' => 'dp.page_id',
314
			'FROM'   => [$this->donation_pages_table => 'dp'],
315
			'WHERE'  => "dp.page_title = '" . $this->db->sql_escape($this->data['page_title']) . "'
316
				AND dp.page_lang_id = " . (int) $this->data['page_lang_id'],
317
		];
318
319
		return $this->db->sql_build_query('SELECT', $sql_ary);
320
	}
321
322
	/**
323
	 * {@inheritdoc}
324
	 */
325
	public function check_required_field(): bool
326
	{
327
		return empty($this->data['page_title']) || empty($this->data['page_lang_id']);
328
	}
329
}
330