summaryrefslogtreecommitdiff
path: root/general_deletion.inc
blob: ca584cb1a765170da0bd4fffdec63e500808d9f9 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/******************************************************************************/
/****************************** DELETION FUNCTIONS ****************************/
/******************************************************************************/
function circuit_simulation_delete_abstract_file($abstract_id)
{
	$status = TRUE;
	$root_path = circuit_simulation_path();
	$abstract_q = db_query("SELECT * from esim_circuit_simulation_proposal dfp
LEFT JOIN esim_circuit_simulation_submitted_abstracts dfsa ON dfp.id = dfsa.proposal_id
WHERE dfsa.id = :abstract_id", array(
		":abstract_id" => $abstract_id
	));
	$abstract_data = $abstract_q->fetchObject();
	if (!$abstract_data)
	{
		drupal_set_message('Invalid circuit simulation project abstract.', 'error');
		return FALSE;
	} //!$abstract_data
	/* deleting solutions */
	$query = db_select('esim_circuit_simulation_submitted_abstracts_file');
	$query->fields('esim_circuit_simulation_submitted_abstracts_file');
	$query->condition('submitted_abstract_id', $abstract_id);
	$abstract_f_q = $query->execute();
	$delete_project_folder = FALSE;
	while ($abstract_f_data = $abstract_f_q->fetchObject())
	{
		$delete_project_folder = TRUE;
		if (!_circuit_simulation_delete_abstract_file($abstract_f_data->id))
			$status = FALSE;
	} //$abstract_f_data = $abstract_f_q->fetchObject()
	if (!$delete_project_folder)
	{
		return TRUE;
	} //!$delete_project_folder
	if ($status)
	{
		$dir_path_udc = $root_path . $abstract_f_data->directory_name . '/user_defined_compound';
		if (is_dir($dir_path_udc))
		{
			unlink($root_path . $abstract_f_data->directory_name . '/' . $abstract_f_data->user_defined_compound_filepath);
			$res = rmdir($dir_path_udc);
			if (!$res)
			{
				drupal_set_message(t('Error in deleting user defiend folder !folder', array(
					'!folder' => $abstract_f_data->directory_name
				)), 'error');
				/* sending email to admins */
				$email_to = variable_get('esim_circuit_simulation_emails', '');
				$from = variable_get('esim_circuit_simulation_from_email', '');
				$bcc = "";
				$cc = variable_get('esim_circuit_simulation_cc_emails', '');
				$params['standard']['subject'] = "[ERROR] Error deleting experiment folder";
				$params['standard']['body'] = "Error deleting folder " . $dir_path;
				$params['standard']['headers'] = array(
					'From' => $from,
					'MIME-Version' => '1.0',
					'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
					'Content-Transfer-Encoding' => '8Bit',
					'X-Mailer' => 'Drupal',
					'Cc' => $cc,
					'Bcc' => $bcc
				);
				if (!drupal_mail('circuit_simulation', 'standard', $email_to, language_default(), $params, $from, TRUE))
					drupal_set_message('Error sending email message.', 'error');
				return FALSE;
			} //!$res
			else
			{
				return TRUE;
			}
		} //is_dir($dir_path_udc)
	} //$status
	return FALSE;
}
function circuit_simulation_abstract_delete_project($proposal_id)
{
	$status = TRUE;
	$root_path = circuit_simulation_path();
	//$proposal_q = db_query("SELECT * FROM {circuit_simulation_proposal} WHERE id = %d", $lab_id);
	$query = db_select('esim_circuit_simulation_proposal');
	$query->fields('esim_circuit_simulation_proposal');
	$query->condition('id', $proposal_id);
	$proposal_q = $query->execute();
	$proposal_data = $proposal_q->fetchObject();
	if (!$proposal_data)
	{
		drupal_set_message('Invalid Flowsheeting Project.', 'error');
		return FALSE;
	} //!$proposal_data
	$query = db_select('esim_circuit_simulation_submitted_abstracts');
	$query->fields('esim_circuit_simulation_submitted_abstracts');
	$query->condition('proposal_id', $proposal_data->id);
	$abstract_q = $query->execute();
	while ($abstract_data = $abstract_q->fetchObject())
	{
		if (!circuit_simulation_delete_abstract_file($abstract_data->id))
		{
			$status = FALSE;
		} //!circuit_simulation_delete_abstract_file($abstract_data->id)
	} //$abstract_data = $abstract_q->fetchObject()
	db_delete('esim_circuit_simulation_submitted_abstracts')->condition('proposal_id', $proposal_data->id)->execute();
	$dir_path_udc = $root_path . $proposal_data->directory_name . '/user_defined_compound';
	if (is_dir($dir_path_udc))
	{
		unlink($root_path . $proposal_data->directory_name . '/' . $proposal_data->user_defined_compound_filepath);
		$res = rmdir($dir_path_udc);
	} //is_dir($dir_path_udc)
	$dir_main_path = $root_path . $proposal_data->directory_name;
	if (is_dir($dir_main_path))
	{
		$res_main = rmdir($dir_main_path);
	} //is_dir($dir_main_path)
	db_delete('esim_circuit_simulation_proposal')->condition('id', $proposal_data->id)->execute();
	return $status;
}
function _circuit_simulation_delete_abstract_file($abstract_id)
{
	global $user;
	$root_path = circuit_simulation_path();
	$query = db_select('esim_circuit_simulation_proposal');
	$query->fields('esim_circuit_simulation_proposal');
	$query->condition('id', $abstract_id);
	$abstract_q = $query->execute();
	$abstract_data = $abstract_q->fetchObject();
	$abstract_q = db_query("SELECT * from esim_circuit_simulation_proposal dfp
LEFT JOIN esim_circuit_simulation_submitted_abstracts dfsa ON dfp.id = dfsa.proposal_id
WHERE dfsa.id = :abstract_id", array(
		":abstract_id" => $abstract_id
	));
	$abstract_data = $abstract_q->fetchObject();
	if (!$abstract_data)
	{
		drupal_set_message('Invalid circuit simulation project abstract.', 'error');
		return FALSE;
	} //!$abstract_data
	/* deleting abstract files */
	$query = db_select('esim_circuit_simulation_submitted_abstracts_file');
	$query->fields('esim_circuit_simulation_submitted_abstracts_file');
	$query->condition('submitted_abstract_id', $abstract_id);
	$abstract_f_q = $query->execute();
	while ($abstract_f_data = $abstract_f_q->fetchObject())
	{
		if (!file_exists($root_path . $abstract_data->directory_name . '/' . $abstract_f_data->filepath))
		{
			$status = FALSE;
			drupal_set_message(t('Error deleting !file. File does not exists.', array(
				'!file' => $abstract_data->directory_name . '/' . $abstract_f_data->filepath
			)), 'error');
			continue;
		} //!file_exists($root_path . $abstract_data->directory_name . '/' . $abstract_f_data->filepath)
		/* removing solution file */
		if (!unlink($root_path . $abstract_data->directory_name . '/' . $abstract_f_data->filepath))
		{
			$status = FALSE;
			drupal_set_message(t('Error deleting !file', array(
				'!file' => $abstract_data->directory_name . '/' . $abstract_f_data->filepath
			)), 'error');
			/* sending email to admins */
			$from = variable_get('esim_circuit_simulation_from_email', '');
			$bcc = variable_get('esim_circuit_simulation_emails', '');
			$cc = variable_get('esim_circuit_simulation_cc_emails', '');
			$params['standard']['subject'] = "[ERROR] Error deleting example file";
			$params['standard']['body'] = "Error deleting solution files by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " :
        file id : " . $abstract_data->id . "
        file path : " . $abstract_data->directory_name . '/' . $abstract_f_data->filepath;
			$params['standard']['headers'] = array(
				'From' => $from,
				'MIME-Version' => '1.0',
				'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
				'Content-Transfer-Encoding' => '8Bit',
				'X-Mailer' => 'Drupal',
				'Cc' => $cc,
				'Bcc' => $bcc
			);
			if (!drupal_mail('circuit_simulation', 'standard', $email_to, language_default(), $params, $from, TRUE))
				drupal_set_message('Error sending email message.', 'error');
		} //!unlink($root_path . $abstract_data->directory_name . '/' . $abstract_f_data->filepath)
		else
		{
			/* deleting example files database entries */
			db_delete('esim_circuit_simulation_submitted_abstracts_file')->condition('id', $abstract_f_data->id)->execute();
		}
	} //$abstract_f_data = $abstract_f_q->fetchObject()
	if (!$status)
	{
		return FALSE;
	} //!$status
	return $status;
}