Completed
Push — master ( 2961ef...fe1e91 )
by Nazar
03:58
created

Text::get()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.009

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 4
nop 3
dl 0
loc 18
ccs 13
cts 14
cp 0.9286
crap 5.009
rs 8.8571
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
10
/**
11
 * @method static $this instance($check = false)
12
 */
13
class Text {
14
	use Singleton;
15
	/**
16
	 * Gets text on current language
17
	 *
18
	 * @param int      $database
19
	 * @param int|null $id             Getting may be done with group and label or with id
20
	 * @param bool     $store_in_cache If `true` - text will be stored in cache
21
	 *
22
	 * @return false|string
23
	 */
24 2
	function get ($database, $id = null, $store_in_cache = false) {
25 2
		$Cache     = Cache::instance();
26 2
		$L         = Language::instance();
27 2
		$id        = (int)$id;
28 2
		$cache_key = "texts/$database/{$id}_$L->clang";
29 2
		if ($store_in_cache && ($text = $Cache->$cache_key) !== false) {
30 2
			return $text;
31
		}
32 2
		$cdb  = DB::instance()->db($database);
33 2
		$text = $this->get_text_by_id($id, $cdb, $L);
34 2
		if (!$text) {
35
			return false;
36
		}
37 2
		if ($store_in_cache) {
38 2
			$Cache->$cache_key = $text['text'];
39
		}
40 2
		return $text['text'];
41
	}
42
	/**
43
	 * @param int          $id
44
	 * @param DB\_Abstract $cdb
45
	 * @param Language     $L
46
	 *
47
	 * @return false|string[]
48
	 */
49 2
	protected function get_text_by_id ($id, $cdb, $L) {
50 2
		$text = $cdb->qf(
51
			"SELECT
52
				`d`.`id`,
53
				`d`.`lang`,
54
				`d`.`text`
55
			FROM `[prefix]texts` AS `t`
56
				LEFT JOIN `[prefix]texts_data` AS `d`
57
			ON `t`.`id` = `d`.`id`
58
			WHERE
59 2
				`t`.`id`	= $id AND
60
				`d`.`lang`	= '%s'
61 2
			LIMIT 1",
62 2
			$L->clang
63
		);
64 2
		if (!$text) {
65 2
			$text = $cdb->qf(
66
				"SELECT
67
					`d`.`id`,
68
					`d`.`lang`,
69
					`d`.`text`
70
				FROM `[prefix]texts` AS `t`
71
					LEFT JOIN `[prefix]texts_data` AS `d`
72
				ON `t`.`id` = `d`.`id`
73 2
				WHERE `t`.`id` = $id
74 2
				LIMIT 1"
75
			);
76
		}
77 2
		return $text;
78
	}
79
	/**
80
	 * Search for text regardless language
81
	 *
82
	 * @param int    $database
83
	 * @param string $group
84
	 * @param string $label
85
	 * @param string $text
86
	 *
87
	 * @return array[]|false Array of items `['id' => id, 'lang' => lang]` on success, `false` otherwise
88
	 */
89
	function search ($database, $group, $label, $text) {
90
		return DB::instance()->db($database)->qfa(
91
			"SELECT
92
				`t`.`id`,
93
				`d`.`lang`
94
			FROM `[prefix]texts` AS `t`
95
				INNER JOIN `[prefix]texts_data` AS `d`
96
			ON `t`.`id` = `d`.`id`
97
			WHERE
98
				`t`.`group`		= '%s' AND
99
				`t`.`label`		= '%s' AND
100
				`d`.`text_md5`	= '%s'",
101
			$group,
102
			$label,
103
			md5($text)
104
		);
105
	}
106
	/**
107
	 * Sets text on current language
108
	 *
109
	 * @param int    $database
110
	 * @param string $group
111
	 * @param string $label
112
	 * @param string $text
113
	 *
114
	 * @return false|string If multilingual support enabled or was enabled and then disabled but translations remains - returns {¶<i>id</i>}, otherwise returns
115
	 *                      original text
116
	 */
117 2
	function set ($database, $group, $label, $text) {
118 2
		$Cache  = Cache::instance();
119 2
		$Config = Config::instance();
120 2
		$L      = Language::instance();
121 2
		$cdb    = DB::instance()->db_prime($database);
122
		/**
123
		 * Security check, do not allow to silently substitute text from another item
124
		 */
125 2
		if (preg_match('/^\{¶(\d+)\}$/', $text)) {
126
			return false;
127
		}
128
		/**
129
		 * @var \cs\DB\_Abstract $cdb
130
		 */
131
		// Find existing text id
132 2
		$id = $cdb->qfs(
133
			"SELECT `id`
134
			FROM `[prefix]texts`
135
			WHERE
136
				`label`	= '%s' AND
137
				`group`	= '%s'
138 2
			LIMIT 1",
139
			$label,
140
			$group
141
		);
142 2
		if (!$id) {
143
			// If not found - either return text directly or add new text entry and obtain id
144 2
			if (!$Config->core['multilingual']) {
145
				return $text;
146
			} else {
147 2
				$cdb->q(
148
					"INSERT INTO `[prefix]texts`
149
						(
150
							`label`,
151
							`group`
152
						) VALUES (
153
							'%s',
154
							'%s'
155 2
						)",
156
					$label,
157
					$group
158
				);
159 2
				$id = $cdb->id();
160 2
				if (!$id) {
161
					return $text;
162
				}
163
			}
164
		}
165
		unset(
166 2
			$Cache->{"texts/$database/{$id}_$L->clang"},
167 2
			$Cache->{"texts/$database/".md5($group).md5($label)."_$L->clang"}
168
		);
169 2
		return $this->set_text($id, $text, $cdb, $Config, $L);
170
	}
171
	/**
172
	 * @param int          $id
173
	 * @param string       $text
174
	 * @param DB\_Abstract $cdb
175
	 * @param Config       $Config
176
	 * @param Language     $L
177
	 *
178
	 * @return mixed
179
	 */
180 2
	protected function set_text ($id, $text, $cdb, $Config, $L) {
181 2
		$exists_for_current_language = $cdb->qfs(
182
			"SELECT `id`
183
			FROM `[prefix]texts_data`
184
			WHERE
185
				`id`	= '%s' AND
186
				`lang`	= '%s'
187 2
			LIMIT 1",
188
			$id,
189 2
			$L->clang
190
		);
191 2
		if ($exists_for_current_language) {
192
			if ($cdb->q(
193
				"UPDATE `[prefix]texts_data`
194
				SET
195
					`text`		= '%s',
196
					`text_md5`	= '%s'
197
				WHERE
198
					`id` = '%s' AND
199
					`lang` = '%s'",
200
				$text,
201
				md5($text),
202
				$id,
203
				$L->clang
204
			)
205
			) {
206
				return "{¶$id}";
207
			}
208 2
		} elseif ($Config->core['multilingual']) {
209 2
			if ($cdb->q(
210
				"INSERT INTO `[prefix]texts_data`
211
					(
212
						`id`,
213
						`id_`,
214
						`lang`,
215
						`text`,
216
						`text_md5`
217
					) VALUES (
218
						'%s',
219
						'%s',
220
						'%s',
221
						'%s',
222
						'%s'
223 2
					)",
224
				$id,
225 2
				"{¶$id}",
226 2
				$L->clang,
227
				$text,
228
				md5($text)
229
			)
230
			) {
231 2
				return "{¶$id}";
232
			}
233
		}
234
		return $text;
235
	}
236
	/**
237
	 * Deletes text on all languages
238
	 *
239
	 * @param int    $database
240
	 * @param string $group
241
	 * @param string $label
242
	 *
243
	 * @return bool
244
	 */
245
	function del ($database, $group, $label) {
246
		$Cache = Cache::instance();
247
		$cdb   = DB::instance()->db_prime($database);
248
		$id    = $cdb->qfs(
249
			[
250
				"SELECT `id`
251
				FROM `[prefix]texts`
252
				WHERE
253
					`group`	= '%s' AND
254
					`label`	= '%s'
255
				LIMIT 1",
256
				$group,
257
				$label
258
			]
259
		);
260
		if ($id) {
261
			$L = Language::instance();
262
			unset(
263
				$Cache->{"texts/$database/{$id}_$L->clang"},
264
				$Cache->{"texts/$database/".md5($group).md5($label)."_$L->clang"}
265
			);
266
			return $cdb->q(
267
				[
268
					"DELETE FROM `[prefix]texts`
269
					WHERE `id` = '%s'",
270
					"DELETE FROM `[prefix]texts_data`
271
					WHERE `id` = '%s'"
272
				],
273
				$id
274
			);
275
		}
276
		return true;
277
	}
278
	/**
279
	 * Process text, and replace {¶([0-9]+)} on real text, is used before showing multilingual information
280
	 *
281
	 * @param int             $database
282
	 * @param string|string[] $data
283
	 * @param bool            $store_in_cache If <b>true</b> - text will be stored in cache
284
	 *
285
	 * @return string|string[]
1 ignored issue
show
Documentation introduced by
Should the return type not be array|string? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
286
	 */
287 36
	function process ($database, $data, $store_in_cache = false) {
288 36
		if (is_array($data)) {
289
			foreach ($data as &$d) {
290
				$d = $this->process($database, $d);
291
			}
292
			return $data;
293
		}
294 36
		return preg_replace_callback(
295 36
			'/^\{¶(\d+)\}$/',
296 36
			function ($input) use ($database, $store_in_cache) {
297 2
				return $this->get($database, $input[1], $store_in_cache);
298 36
			},
299
			$data
300
		);
301
	}
302
}
303