Completed
Pull Request — master (#2)
by Michael
07:48
created

AdminerSlugify::AdminerSlugify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/** Prefill field containing "_slug" with slugified value of a previous field (JavaScript)
4
* @link https://www.adminer.org/plugins/#use
5
* @author Jakub Vrana, http://www.vrana.cz/
6
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
7
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
8
*/
9
class AdminerSlugify {
10
	/** @access protected */
11
	var $from, $to;
12
13
	/**
14
	* @param string find these characters ...
15
	* @param string ... and replace them by these
16
	*/
17
	function __construct($from = 'áčďéěíňóřšťúůýž', $to = 'acdeeinorstuuyz') {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
		$this->from = $from;
19
		$this->to = $to;
20
	}
21
22
	function editInput($table, $field, $attrs, $value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
		static $slugify;
24
		if (!$_GET['select'] && !$_GET['where']) {
25
			if ($slugify === null) {
26
				$slugify = array();
27
				$prev = null;
28
				foreach (fields($table) as $name => $val) {
29
					if ($prev && preg_match('~(^|_)slug(_|$)~', $name)) {
30
						$slugify[$prev] = $name;
31
					}
32
					$prev = $name;
33
				}
34
			}
35
			$slug = $slugify[$field['field']];
36
			if ($slug !== null) {
37
				return "<input value='" . h($value) . "' maxlength='$field[length]' size='40'$attrs onchange=\"var find = '$this->from'; var repl = '$this->to'; this.form['fields[$slug]'].value = this.value.toLowerCase().replace(new RegExp('[' + find + ']', 'g'), function (str) { return repl[find.indexOf(str)]; }).replace(/[^a-z0-9_]+/g, '-').replace(/^-|-\$/g, '').substr(0, $field[length]);\">";
38
			}
39
		}
40
	}
41
42
}
43