Blame view

genpdf/decide-note.php 7.74 KB
redmine authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php

require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/AbstractApi.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/SimpleXMLElement.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/User.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Issue.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/IssueCategory.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Attachment.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Membership.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Client.php";

require_once "lib/3rdparty/Inflect/Inflect.php";

require_once "config.php";

function redmine_proj_identifier2id ($ident) {
	error_log("redmine_proj_identifier($ident)");
	static $ident2id = array();

	if (empty($ident2id)) {
		$lines = split("\n", file_get_contents('/opt/redmine_projects.csv'));

		foreach ($lines as &$line) {
			$words = split("\t", $line);
			$ident2id[strtolower($words[1])] = $words[0];
		}
	}

	error_log ('RI:'.$ident2id[strtolower($ident)]);
	

	return $ident2id[strtolower($ident)];
}

foreach (array('issueId', 'action', 'signature') as $k)
	if (empty($k))
		die('field "'.$k.'" is empty');

$isGoodSignature = false;

foreach ($SIGNKEYS as $key) {
	if (sha1($_GET['issueId'].$key) == $_GET['signature']) {
		$isGoodSignature = true;
		break;
	}
}

if ($isGoodSignature === false) {
	die('bad signature');
}
redmine authored
51
mail('admin@ut.mephi.ru', 'decide-note.php action '.$_GET['action'], print_r($_GET, 1));
redmine authored
52 53 54 55

$redmine_admin = new Redmine\Client('https://redmine.ut.mephi.ru', REDMINE_LOGIN, REDMINE_PASSWORD);
$issue         = $redmine_admin->api('issue')->show($_GET['issueId'])['issue'];

redmine authored
56
$assignedTo    = $redmine_admin->api('user' )->show($issue['assigned_to']['id'])['user'];
redmine authored
57

redmine authored
58
$api_key = $assignedTo['api_key'];
redmine authored
59 60
$redmine = new Redmine\Client('https://redmine.ut.mephi.ru', $api_key);

redmine authored
61 62
function addUserToProject(&$redmine, $user_id, $project_id, $role_id) {
	error_log("addUserToProject(redmine, $user_id, $project_id, $role_id)");
redmine authored
63 64
	$current_membership = $redmine->api('membership')->all($project_id, array('limit' => '1000'));
	//mail('dyokunev@ut.mephi.ru', 'memberships', print_r($current_membership['memberships'], 1));
redmine authored
65 66 67 68 69
	foreach ($current_membership['memberships'] as $global_key => $member){
		if ($member['user']['id'] == $user_id){
			$cur_roles=array();
			$membership_id = $current_membership['memberships'][$global_key]['id'];
			foreach($current_membership['memberships'][$global_key]['roles'] as $role) {
redmine authored
70
				if ($role['id'] == $role_id) {
redmine authored
71
					error_log("already there!");
redmine authored
72 73
					return true;
				}
redmine authored
74 75
				$cur_roles[] = $role['id'];
			}
redmine authored
76
			$role_ids = array('role_ids' => array_merge($cur_roles, array($role_id)));
redmine authored
77
			error_log("updating");
redmine authored
78 79
			//print_r($role_ids);
			return $redmine->api('membership')->update($membership_id, $role_ids);
redmine authored
80 81
		}
	}
redmine authored
82
	error_log("creating");
redmine authored
83 84 85 86 87
	return $redmine->api('membership')->create($project_id, array('user_id' => $user_id, 'role_ids' => array($role_id)));
}

function removeUserFromProject(&$redmine, $user_id, $project_id, $role_id) {
	error_log("removeUserFromProject(redmine, $user_id, $project_id, $role_id)");
redmine authored
88
	$current_membership = $redmine->api('membership')->all($project_id, array('limit' => '1000')); 
redmine authored
89 90 91 92 93 94 95 96 97 98
	foreach ($current_membership['memberships'] as $global_key => $member){
		if ($member['user']['id'] == $user_id){
			$role_ids=array();
			$membership_id = $current_membership['memberships'][$global_key]['id'];
			foreach($current_membership['memberships'][$global_key]['roles'] as $role) {
				if ($role['id'] == $role_id) {
					continue;
				}
				$role_ids[] = $role['id'];
			}
redmine authored
99
			return $redmine->api('membership')->update($membership_id, array('role_ids' => $role_ids));
redmine authored
100 101 102
		}
	}
	return true;
redmine authored
103 104
}

redmine authored
105 106
switch ($_GET['action']) {
	case 'agree':
redmine authored
107
	case 'move':
redmine authored
108 109 110 111 112 113 114 115 116 117

		$recipientId = NULL;
		foreach ( $issue['custom_fields'] as $cf) {
			if ($cf['id'] == 95) {
				$recipientId = $cf['value'];
				break;
			}
		}

		if (is_null($recipientId)) {
redmine authored
118 119 120
			header('Content-Type: text/html');
			mail('admin@ut.mephi.ru', 'Already confirmed in decide-note.php', print_r($_GET, 1));
			//die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot find recipient custom field in issue: '.$_GET['issueId']);
redmine authored
121
			?><html><head><title>Служебная записка уже подтверждена</title><meta charset="UTF-8"><meta http-equiv="refresh" content="2; https://tasks.mephi.ru/issues/<?=$_GET['issueId']?>" /></head><body><h1>Служебная записка <a href='https://tasks.mephi.ru/issues/<?=$_GET['issueId']?>'>#<?=$_GET['issueId']?></a> уже подтверждена.</h1></body></html><?php
redmine authored
122
			exit(0);
redmine authored
123
		}
redmine authored
124 125 126

		header('Content-Type: text/plain');

redmine authored
127 128 129
		$recipient = $redmine_admin->api('user')->show($recipientId)['user'];

		if (empty($recipient['mail'])) {
redmine authored
130 131
			mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
			die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot find login of user with ID: '.$recipientId);
redmine authored
132 133
		}

redmine authored
134 135
		$project_id = redmine_proj_identifier2id(strtolower(explode('@', $recipient['mail'])[0]));

xaionaro authored
136 137 138 139
		if (empty($project_id)) {
			mail('admin@ut.mephi.ru', 'Got error on decide-note.php', 'empty $project_id, issue '.$issue['id']);
		}

redmine authored
140
		addUserToProject($redmine_admin, $assignedTo['id'], $project_id, ROLE_CUSTOMER);
redmine authored
141
		addUserToProject($redmine_admin, $assignedTo['id'], 15971,	 ROLE_MOVER);
redmine authored
142 143 144
		addUserToProject($redmine_admin, $assignedTo['id'], $project_id, ROLE_MOVER);
		/*$result =*/
		/*if ($result !== TRUE) {
redmine authored
145 146 147 148
			mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
			die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot add user '.$recipientId.' to project '.$issue['project']['id'].': '.print_r($result, 1));
		}*/

redmine authored
149
		//print @json_encode($redmine->api('issue')->update($issue['id'], array('status_id' => STATUSID_NEW, 'project_id' => redmine_proj_identifier2id(strtolower(explode('@', $recipient['mail'])[0])), 'assigned_to_id' => $recipient['id'])));
redmine authored
150 151

		$issue_props = array(
redmine authored
152 153 154
			//'project_id'	 => $project_id,
			'assigned_to_id' => $recipient['id'],
			'tracker_id'	 => TRACKERID_TASK,
redmine authored
155 156 157 158
		);

		if ($_GET['action'] == 'agree')
			$issue_props['status_id'] = STATUSID_NEW;
redmine authored
159

redmine authored
160
		//print_r($issue_props);die();
redmine authored
161 162 163 164 165
		$result = $redmine->api('issue')->update($issue['id'], $issue_props);
		$issue_props = array(
			'project_id'	 => $project_id,
			'notes'		 => "h1. Подтверждаю служебную записку.\n@ @\n@ @",
		);
redmine authored
166 167 168

		$result = $redmine->api('issue')->update($issue['id'], $issue_props);
		removeUserFromProject($redmine_admin, $assignedTo['id'], $project_id, ROLE_MOVER);
redmine authored
169 170 171
		if ($result !== TRUE) {
			mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
			die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot update the issue '.$issue['project']['id'].': '.print_r($result, 1));
redmine authored
172
		}
redmine authored
173
		header('Location: https://tasks.mephi.ru/issues/'.$_GET['issueId']);
redmine authored
174 175 176 177 178 179
		break;
	case 'decline':
		header('Content-Type: text/plain');

		//print @json_encode($redmine->api('issue')->update($_GET['issueId'], array('status_id' => STATUSID_DECLINED)));
		$result = $redmine->api('issue')->update($_GET['issueId'], array('status_id' => STATUSID_DECLINED));
redmine authored
180 181 182
		if ($result !== TRUE) {
			mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
			die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot decline the issue '.$issue['project']['id'].': '.print_r($result, 1));
redmine authored
183
		}
redmine authored
184
		header('Location: https://tasks.mephi.ru/issues/'.$_GET['issueId']);
redmine authored
185 186 187 188 189 190 191
		break;
	default:
		die('Unknown action: '.$_GET['action']);
}


?>