-
Notifications
You must be signed in to change notification settings - Fork 1
/
general_deletion.inc
executable file
·241 lines (230 loc) · 10.4 KB
/
general_deletion.inc
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/******************************************************************************/
/****************************** DELETION FUNCTIONS ****************************/
/******************************************************************************/
function lab_migration_delete_solution($solution_id)
{
$user = \Drupal::currentUser();
$root_path = lab_migration_path();
$status = TRUE;
$query = \Drupal::database()->select('lab_migration_solution');
$query->fields('lab_migration_solution');
$query->condition('id', $solution_id);
$solution_q = $query->execute();
$solution_data = $solution_q->fetchObject();
if (!$solution_data)
{
\Drupal::messenger()->addError(t('Invalid solution.'));
return FALSE;
}
$query = \Drupal::database()->select('lab_migration_experiment');
$query->fields('lab_migration_experiment');
$query->condition('id', $solution_data->experiment_id);
$experiment_q = $query->execute();
$experiment_data = $experiment_q->fetchObject();
$query = \Drupal::database()->select('lab_migration_proposal');
$query->fields('lab_migration_proposal');
$query->condition('id', $experiment_data->proposal_id);
$proposal_q = $query->execute();
$proposal_data = $proposal_q->fetchObject();
if (!$experiment_data)
{
\Drupal::messenger()->addError(t('Invalid experiment.'));
return FALSE;
}
/* deleting solution files */
$query = \Drupal::database()->select('lab_migration_solution_files');
$query->fields('lab_migration_solution_files');
$query->condition('solution_id', $solution_id);
$solution_files_q = $query->execute();
while ($solution_files_data = $solution_files_q->fetchObject())
{
$ex_path = $proposal_data->directory_name . '/' . $solution_files_data->filepath;
$dir_path = $root_path . $ex_path;
if (!file_exists($dir_path))
{
$status = FALSE;
\Drupal::messenger()->addError(t('Error deleting !file. File does not exists.', array('!file' => $dir_path)));
continue;
}
/* removing solution file */
if (!unlink($dir_path))
{
$status = FALSE;
\Drupal::messenger()->addError(t('Error deleting !file', array('!file' => $dir_path)));
/* sending email to admins */
$email_to = \Drupal::config('lab_migration.settings')->get('lab_migration_emails');
$from = \Drupal::config('lab_migration.settings')->get('lab_migration_from_email');
$bcc="";
$cc=\Drupal::config('lab_migration.settings')->get('lab_migration_cc_emails');
$param['standard']['subject'] = "[ERROR] Error deleting example file";
$param['standard']['body'] = "Error deleting solution files by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . " :
solution id : " . $solution_id . "
file id : " . $solution_files_data->id . "
file path : " . $solution_files_data->filepath."
PDF path : " . $PdfStatus;
$param['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('lab_migration', 'standard', $email_to, language_default(), $param, $from, TRUE))
\Drupal::messenger()->addError('Error sending email message.');
}
else {
/* deleting example files database entries */
\Drupal::database()->delete('lab_migration_solution_files')->condition('id', $solution_files_data->id)->execute();
}
}
if (!$status)
return FALSE;
$query = \Drupal::database()->select('lab_migration_solution');
$query->fields('lab_migration_solution');
$query->condition('id', $solution_id);
$solution_q = $query->execute();
$solution_data = $solution_q->fetchObject();
if (!$solution_data)
{
\Drupal::messenger()->addError(t('Invalid solution.'));
return FALSE;
}
$query = \Drupal::database()->select('lab_migration_experiment');
$query->fields('lab_migration_experiment');
$query->condition('id', $solution_data->experiment_id);
$experiment_q = $query->execute();
$experiment_data = $experiment_q->fetchObject();
$query = \Drupal::database()->select('lab_migration_proposal');
$query->fields('lab_migration_proposal');
$query->condition('id', $experiment_data->proposal_id);
$proposal_q = $query->execute();
$proposal_data = $proposal_q->fetchObject();
$dir_path = $root_path . $proposal_data->directory_name . '/EXP' . $experiment_data->number . '/CODE' . $solution_data->code_number;
if (is_dir($dir_path))
{
if (!rmdir($dir_path))
{
\Drupal::messenger()->addError(t('Error deleting folder !folder', array('!folder' => $dir_path)));
/* sending email to admins */
$email_to = \Drupal::config('lab_migration.settings')->get('lab_migration_emails');
$from = \Drupal::config('lab_migration.settings')->get('lab_migration_from_email');
$bcc="";
$cc=\Drupal::config('lab_migration.settings')->get('lab_migration_cc_emails');
$param['standard']['subject'] = "[ERROR] Error deleting folder";
$param['standard']['body'] = "Error deleting folder " . $dir_path . " by " . $user->uid . " at " . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$param['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('lab_migration', 'standard', $email_to, language_default(), $param, $from, TRUE))
\Drupal::messenger()->addError('Error sending email message.');
return FALSE;
}
}
else
{
\Drupal::messenger()->addError(t('Cannot delete solution folder. !folder does not exists.', array('!folder' => $dir_path)));
return FALSE;
}
/* deleting solution dependency and solution database entries */
\Drupal::database()->delete('lab_migration_solution_dependency')->condition('solution_id', $solution_id)->execute();
\Drupal::database()->delete('lab_migration_solution')->condition('id', $solution_id)->execute();
return $status;
}
function lab_migration_delete_experiment($experiment_id)
{
$status = TRUE;
$root_path = lab_migration_path();
$query = \Drupal::database()->select('lab_migration_experiment');
$query->fields('lab_migration_experiment');
$query->condition('id', $experiment_id);
$experiment_q = $query->execute();
$experiment_data = $experiment_q->fetchObject();
$query = \Drupal::database()->select('lab_migration_proposal');
$query->fields('lab_migration_proposal');
$query->condition('id', $experiment_data->proposal_id);
$proposal_q = $query->execute();
$proposal_data = $proposal_q->fetchObject();
if (!$experiment_data)
{
\Drupal::messenger()->addError('Invalid experiment.');
return FALSE;
}
/* deleting solutions */
$query = \Drupal::database()->select('lab_migration_solution');
$query->fields('lab_migration_solution');
$query->condition('experiment_id', $experiment_id);
$solution_q = $query->execute();
$delete_exp_folder = FALSE;
while ($solution_data = $solution_q->fetchObject())
{
$delete_exp_folder = TRUE;
if (!lab_migration_delete_solution($solution_data->id))
$status = FALSE;
}
if (!$delete_exp_folder)
{
return TRUE;
}
if ($status)
{
$dir_path = $root_path . $proposal_data->directory_name . '/EXP' . $experiment_data->number;
if (is_dir($dir_path))
{
$res = rmdir($dir_path);
if (!$res)
{
\Drupal::messenger()->addError(t('Error deleting experiment folder !folder', array('!folder' => $dir_path)));
/* sending email to admins */
$email_to = \Drupal::config('lab_migration.settings')->get('lab_migration_emails');
$from = \Drupal::config('lab_migration.settings')->get('lab_migration_from_email');
$bcc="";
$cc=\Drupal::config('lab_migration.settings')->get('lab_migration_cc_emails');
$param['standard']['subject'] = "[ERROR] Error deleting experiment folder";
$param['standard']['body'] = "Error deleting folder " . $dir_path;
$param['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('lab_migration', 'standard', $email_to, language_default(), $param,$from, TRUE))
\Drupal::messenger()->addError('Error sending email message.');
return FALSE;
}
else
{
return TRUE;
}
}
else {
\Drupal::messenger()->addError(t('Cannot delete experiment folder. !folder does not exists.', array('!folder' => $dir_path)));
return FALSE;
}
}
return FALSE;
}
function lab_migration_delete_lab($lab_id)
{
$status = TRUE;
$root_path = lab_migration_path();
$query = \Drupal::database()->select('lab_migration_proposal');
$query->fields('lab_migration_proposal');
$query->condition('id', $lab_id);
$proposal_q = $query->execute();
$proposal_data = $proposal_q->fetchObject();
if (!$proposal_data)
{
\Drupal::messenger()->addError('Invalid Lab.');
return FALSE;
}
/* delete experiments */
$query = \Drupal::database()->select('lab_migration_experiment');
$query->fields('lab_migration_experiment');
$query->condition('proposal_id', $proposal_data->id);
$experiment_q = $query->execute();
while ($experiment_data = $experiment_q->fetchObject())
{
if (!lab_migration_delete_experiment($experiment_data->id))
{
$status = FALSE;
}
}
return $status;
}