-
Notifications
You must be signed in to change notification settings - Fork 0
/
Submissions.php
134 lines (95 loc) · 4.83 KB
/
Submissions.php
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
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Submissions extends Controller {
public function before() {
parent::before();
if (!Group::current('allow_submissions'))
throw new HTTP_Exception_403('Forbidden');
}
public function action_index() {
$id = strval(Arr::get($_GET, 'ticket'));
if (!$id) throw new HTTP_Exception_404('Not found');
$filter = array('_id' => $id);
if (!Group::current('allow_assign'))
$filter['$or'] = array(
array('companies' => intval(User::current('company_id'))),
array('ex' => intval(User::current('company_id'))),
);
$job = Database_Mongo::collection('jobs')->findOne($filter);
if (!$job) throw new HTTP_Exception_404('Not found');
$filter = array('job_key' => $id);
if (!Group::current('allow_assign'))
$filter['user_id'] = array('$in' => DB::select('id')->from('users')->where('company_id', '=', User::current('company_id'))->execute()->as_array(NULL, 'id'));
$result = Database_Mongo::collection('submissions')
->find($filter)->sort(array('update_time' => -1));
Pager::$count = $result->count(true);
$result->limit(Pager::limit())->skip(Pager::offset());
$submissions = array();
$users = array();
while ($row = $result->next()) {
$users[$row['user_id']] = 1;
$submissions[] = $row;
}
if ($users) User::get(array_keys($users));
$companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name');
$view = View::factory('Jobs/Submissions');
$view->bind('submissions', $submissions)
->bind('companies', $companies)
->bind('job', $job);
$this->response->body($view);
}
public function action_approve() {
if (!Group::current('allow_assign')) throw new HTTP_Exception_403('Forbidden');
$id = Arr::get($_GET, 'id');
$submission = Database_Mongo::collection('submissions')->findOne(array(
'_id' => new MongoId($id),
));
if (!$submission || Arr::get($submission, 'active')) throw new HTTP_Exception_404('Not found');
$job = Database_Mongo::collection('jobs')->findOne(array('_id' => $submission['job_key']));
$value = Arr::get($job, $submission['key'], '');
$update_time = time();
if ($value != $submission['value']) {
$archive = array(
'data' => array(
substr($submission['key'], 5) => array(
'old_value' => $value,
'new_value' => $submission['value'],
),
),
'fields' => substr($submission['key'], 5),
'job_key' => $job['_id'],
'user_id' => User::current('id'),
'update_time' => $update_time,
'update_type' => 2,
'filename' => 'MANUAL',
);
$update = array('$set' => array(
'last_update' => $update_time,
));
if ($submission['value'])
$update['$set'][$submission['key']] = $submission['value'];
else
$update['$unset'][$submission['key']] = 1;
if ($submission['key'] == 'data.44') {
$status = preg_replace('/[^a-z]/', '', strtolower($submission['value']));
if ($status == 'built' && !Arr::path($job, 'data.264'))
$update['$set']['data.264'] = $update_time;
if ($status == 'tested' && !Arr::path($job, 'data.265')) {
$update['$set']['data.265'] = $update_time;
if (!Arr::path($job, 'data.264'))
$update['$set']['data.264'] = $update_time;
}
}
$company = intval(User::get($submission['user_id'], 'company_id'));
$sub = array('$set' => array('admin_id' => User::current('id'), 'process_time' => $update_time, 'active' => -1));
$financial = floatval(DB::select('financial')->from('job_columns')->where('id', '=', substr($submission['key'], 5))->execute()->get('financial'));
if ($financial && !in_array($company, Arr::get($job, 'companies', array()), true))
$sub['$set']['financial_time'] = 0;
Database_Mongo::collection('archive')->insert($archive);
Database_Mongo::collection('jobs')->update(array('_id' => $job['_id']), $update);
Database_Mongo::collection('submissions')->update(array('_id' => new MongoId($id)), $sub);
}
die(json_encode(array(
'success' => true,
)));
}
}