<?php

$sql_query = 'UPDATE ' . $config['table_prefix'] . 'calc_template SET ';
$sql_query .= <<<'EOT'
calculate_tpl = "$name1 = '';
$name2 = '';
$surname = '';
$i = 0;
$sex = 0;
$lit_sname = {literal}'{Фамилия}'{/literal};
$lit_sname_s = {literal}'{Ф}'{/literal};
$lit_name1 = {literal}'{Имя}'{/literal};
$lit_name1_s = {literal}'{И}'{/literal};
$lit_name2 = {literal}'{Отчество}'{/literal};
$lit_name2_s = {literal}'{О}'{/literal};

if ('{$flex_data}' == '') {
    $pattern = '{literal}{Фамилия} {Имя} {Отчество}{/literal}';
} else {
	$pattern = '{$flex_data}';
}

$array = array_filter(explode(' ', $line['{$field1}']), 'strlen');
if (count($array) > 1) {
	// определение поля по отчеству
	foreach($array as $value) {
		if (mb_strlen($value ) <= 2) continue;
		
		$s1 = mb_substr($value,-3);
		if ($s1 == 'вна' || $s1 == 'чна') {
			$sex = 2;
			break;
		} elseif ($s1 == 'вич' || $s1 == 'ьич') {
			$sex = 1;
		} elseif (mb_substr($value,-3) == 'ова' || mb_substr($value,-3) == 'ева') {
			$sex = 2;
            break;
        }
	}
	
	$ich = 0;
	// количество вхождений -ич
	foreach($array as $value) {
		if (mb_strlen($value )> 2) continue;
		if (mb_substr($value, -2) == 'ич') $ich++;
	}
	
	// определение фамилии
	foreach($array as $value) {
		if (empty($surname) && mb_strlen($value) > 2) {
			$s1 = mb_substr($value,-2);
			$s2 = '';
			if (mb_strlen($value) > 2) {
				$s2 = mb_substr($value,-3);
			}
			$s3 = '';
			if (mb_strlen($value) > 3) {
				$s3 = mb_substr($value,-4);
			}
			if ($s1 == 'ин' || $s1 == 'ев' || $s1 == 'ов' || $s1 == 'ан' || $s1 == 'ын' || $s1 == 'ых' || $s1 == 'ян' || $s1 == 'ко' || $s1 == 'ук' || $s1 == 'ик' || $s1 == 'те' || $s1 == 'ни' || $s1 == 'юк'
				|| $s2 == 'ина' || $s2 == 'ына' || $s2 == 'ева' || $s2 == 'ова' || $s2 == 'янц' || $s2 == 'нок' || $s2 == 'дзе' || $s2 == 'ава' || $s2 == 'сон'
				|| $s3 == 'ских' || $s3 == 'ский' || $s3 == 'ская' || $s3 == 'цких' || $s3 == 'цкий' || $s3 == 'цкая' || $s3 == 'тейн' || $s3 == 'блюм' || $s3 == 'баум' || $s3 == 'берг' || $s3 == 'вили' || $s3 == 'заде' || $s3 == 'оглу') {
				$surname = $value;
				$i++;
				break;
			} elseif ($s1 == 'ич' && (($ich == 1 && $sex == 2) || ($ich == 2 && $sex == 1))) {
				$surname = $value;
				$i++;
				break;
			}
		}
	}

	// определение отчества
	foreach($array as $value) {
		if ($value != $surname && empty($name2) && mb_strlen($value )> 3) {
			$s1 = mb_substr($value,-3);
			if ($s1 == 'вич' || $s1 == 'ьич' || $s1 == 'вна' || $s1 == 'чна') {
				$name2 = $value;
				$i++;
				break;
			}
		}
	}

	// определение имени
	if (count($array) > $i) {
		foreach($array as $value) {
			if ($value != $name2 && $value != $surname) {
				$name1 = $value;
				$i++;
				break;
			}
		}
	}

	// допределение фамилии
	if (count($array) > $i) {
		foreach($array as $value) {
			if ($value != $name1 && $value != $name2) {
				$surname = $value;
				break;
			}
		}
	}
} else {
	$surname = $line['{$field1}'];
}

$sex = $sex ?: 1;
if (empty($name1)) {
    $name1 = $surname;
    $surname = '';
}

// падеж
$case = '{$case_value}';
// падеж для полной фамилии
if (!empty($surname) && $sex > 0 && $case != '1' && mb_strpos($pattern, $lit_sname) !== false) {
	$s1 = mb_substr($surname,-2);
	if ($sex == 1) {
		switch ($case) {
			case '2':
				if ($s1 == 'ов' || $s1 == 'ев' || $s1 == 'ин' || $s1 == 'ын' || $s1 == 'ых' || $s1 == 'ан' || $s1 == 'ян' || $s1 == 'нц' || $s1 == 'ик' || $s1 == 'ук' || $s1 == 'юк' 
					|| $s1 == 'он' || $s1 == 'йн' || $s1 == 'юм' || $s1 == 'ич' || $s1 == 'рг' ){
					$surname = $surname . 'а';
				} elseif ($s1 == 'ий') {
					$surname = mb_substr($surname, 0, -2) . 'ого';
				}
				break;
			case '3':
				if ($s1 == 'ов' || $s1 == 'ев' || $s1 == 'ин' || $s1 == 'ын' || $s1 == 'ых' || $s1 == 'ан' || $s1 == 'ян' || $s1 == 'нц' || $s1 == 'ик' || $s1 == 'ук' || $s1 == 'юк' 
					|| $s1 == 'он' || $s1 == 'йн' || $s1 == 'юм' || $s1 == 'ич' || $s1 == 'рг' ){
					$surname = $surname . 'у';
				} elseif ($s1 == 'ий') { 
					$surname = mb_substr($surname, 0, -2) . 'ому';
				}
				break;
			case '4':
				if ($s1 == 'ов' || $s1 == 'ев' || $s1 == 'ин' || $s1 == 'ын' || $s1 == 'ых' || $s1 == 'ан' || $s1 == 'ян' || $s1 == 'нц' || $s1 == 'ик' || $s1 == 'ук' || $s1 == 'юк' 
					|| $s1 == 'он' || $s1 == 'йн' || $s1 == 'юм' || $s1 == 'ич' || $s1 == 'рг' ) {
					$surname = $surname . 'а';
				} elseif ($s1 == 'ий') {
					$surname = mb_substr($surname, 0, -2) . 'ого';
				}
				break;
			case '5':
				if ($s1 == 'ов' || $s1 == 'ев' || $s1 == 'ин' || $s1 == 'ын') {
					$surname = $surname . 'ым';
				} elseif ($s1 == 'ян' || $s1 == 'ан' || $s1 == 'юк' || $s1 == 'ук' || $s1 == 'юм' || $s1 == 'ик' || $s1 == 'он' || $s1 == 'йн' || $s1 == 'рг') {
					$surname = $surname . 'ом';
				} elseif ($s1 == 'ич' || $s1 == 'нц') {
					$surname = $surname . 'ем';
				} elseif ($s1 == 'ий') {
					$surname = mb_substr($surname, 0, -2) . 'им';
				}
				break;
			case '6':
				if ($s1 == 'ов' || $s1 == 'ев' || $s1 == 'ин' || $s1 == 'ын' || $s1 == 'ых' || $s1 == 'ан' || $s1 == 'ян' || $s1 == 'нц' || $s1 == 'ик' || $s1 == 'ук' || $s1 == 'юк' 
					|| $s1 == 'он' || $s1 == 'йн' || $s1 == 'юм' || $s1 == 'ич' || $s1 == 'рг' ) {
					$surname = $surname . 'е';
				} elseif ($s1 == 'ий') {
					$surname = mb_substr($surname, 0, -2) . 'ом';
				}
				break;
		}
	} else {
		if ($case == '4') {
			if ($s1 == 'ва' || $s1 == 'на') {
				$surname = mb_substr($surname, 0, -1) . 'у';
			} elseif ($s1 == 'ая') {
				$surname = mb_substr($surname, 0, -2) . 'ую';
			}
		} else {
			if ($s1 == 'ва' || $s1 == 'на') {
				$surname = mb_substr($surname, 0, -1) . 'ой';
			} elseif ($s1 == 'ая') {
				$surname = mb_substr($surname, 0, -2) . 'ой';
			}
		}
	}
}

// падеж для полного имени
if (!empty($name1) && $case != '1' && $sex > 0 && mb_strpos($pattern, $lit_name1) !== false) {
	$s1 = mb_substr($name1,-1);
	if ($sex == 1) {
		if ($s1 === 'й' || $s1 === 'ь') {
			switch ($case) {
				case '2':
				case '4':
					$name1 = mb_substr($name1, 0, -1) . 'я';
					break;
				case '3':
					$name1 = mb_substr($name1, 0, -1) . 'ю';
					break;
				case '5':
					$name1 = mb_substr($name1, 0, -1) . 'ем';
					break;
				case '6':
					if (mb_substr($name1,-2) === 'ий') { 
						$name1 = mb_substr($name1, 0, -1) . 'и';
					} else {
						$name1 = mb_substr($name1, 0, -1) . 'е';
					}
					break;
			}
		} elseif ($s1 === 'я') {
			switch ($case) {
				case '2':
					$name1 = mb_substr($name1, 0, -1) . 'и';
					break;
				case '3':
				case '6':
					$name1 = mb_substr($name1, 0, -1) . 'е';
					break;
				case '4':
					$name1 = mb_substr($name1, 0, -1) . 'ю';
					break;
				case '5':
					$name1 = mb_substr($name1, 0, -1) . 'ёй';
					break;
			}
		} elseif ($s1 === 'л') {
			switch ($case) {
				case '2':
				case '4':
					$name1 = mb_substr($name1, 0, -2) . 'ла';
					break;
				case '3':
					$name1 = mb_substr($name1, 0, -2) . 'лу';
					break;
				case '5':
					$name1 = mb_substr($name1, 0, -2) . 'лом';
					break;
				case '6':
					$name1 = mb_substr($name1, 0, -2) . 'ле';
					break;
			}
		} else {
			switch ($case) {
				case '2':
				case '4':
					$name1 = $name1 . 'а';
					break;
				case '3':
					$name1 = $name1 . 'у';
					break;
				case '5':
					$name1 = $name1 . 'ом';
					break;
				case '6':
					$name1 = $name1 . 'е';
					break;
			}
		}
	} else {
		if ($s1 === 'я') {
			switch ($case) {
				case '2':
				case '3':
				case '6':
					$name1 = mb_substr($name1, 0, -1) . 'и';
					break;
				case '4':
					$name1 = mb_substr($name1, 0, -1) . 'ю';
					break;
				case '5':
					$name1 = mb_substr($name1, 0, -1) . 'ей';
					break;
			}
		} elseif ($s1 === 'ь') {
			switch ($case) {
				case '2':
				case '3':
				case '6':
					$name1 = mb_substr($name1, 0, -1) . 'и';
					break;
				case '5':
					$name1 = $name1 . 'ю';
					break;
			}
		}
		else {
			switch ($case) {
				case '2':
					$name1 = mb_substr($name1, 0, -1) . 'ы';
					break;
				case '3':
				case '6':
					$name1 = mb_substr($name1, 0, -1) . 'е';
					break;
				case '4':
					$name1 = mb_substr($name1, 0, -1) . 'у';
					break;
				case '5':
					$name1 = mb_substr($name1, 0, -1) . 'ой';
					break;
			}
		}
	}
}

// падеж для полного отчества
if (!empty($name2) && $case != '1' && mb_strpos($pattern, $lit_name2) !== false) {
	$s1 = mb_substr($name2,-2);
	switch ($case) {
		case '2':
			if ($s1 == 'ич') {
				$name2 = $name2 . 'а';
			} elseif ($s1 == 'на') {
				$name2 = mb_substr($name2, 0, -1) . 'ы';
			}
			break;
		case '3':
			if ($s1 == 'ич') {
				$name2 = $name2 . 'у';
			} elseif ($s1 == 'на') {
				$name2 = mb_substr($name2, 0, -1) . 'е';
			}
			break;
		case '4':
			if ($s1 == 'ич') {
				$name2 = $name2 . 'а';
			} elseif ($s1 == 'на') {
				$name2 = mb_substr($name2, 0, -1) . 'у';
			}
			break;
		case '5':
			if ($s1 == 'ич') {
				$name2 = $name2 . 'ем';
			} elseif ($s1 == 'на') {
				$name2 = mb_substr($name2, 0, -1) . 'ой';
			}
			break;
		case '6':
			if ($s1 == 'ич') {
				$name2 = $name2 . 'е';
			} elseif ($s1 == 'на') {
				$name2 = mb_substr($name2, 0, -1) . 'е';
			}
			break;
	}
}

// шаблон
$s = $pattern;
if (mb_strpos($s, $lit_sname) !== false) {
	$s = str_replace($lit_sname, $surname, $s);
}
if (mb_strpos($s, $lit_sname_s) !== false) {
	$s = str_replace($lit_sname_s, mb_substr($surname,0,1), $s);
}
if (mb_strpos($s, $lit_name1) !== false) {
	$s = str_replace($lit_name1, $name1, $s);
}
if (mb_strpos($s, $lit_name1_s) !== false) {
	$s = str_replace($lit_name1_s, mb_substr($name1,0,1), $s);
}
if (mb_strpos($s, $lit_name2) !== false) {
	$s = str_replace($lit_name2, $name2, $s);
}
if (mb_strpos($s, $lit_name2_s) !== false) {
	$s = str_replace($lit_name2_s, mb_substr($name2,0,1), $s);
}

$line['{$result}'] = $s;
" WHERE id = 83
EOT;
run_query($sql_query);
