public function action_indexes() { $columns = DB::select('column_id')->distinct(true)->from('group_columns')->where('search', '=', 2)->execute()->as_array(NULL, 'column_id'); foreach ($columns as $column) { $index = array(); $type = Columns::get_type($column); $name = 'data.' . $column; switch ($type) { case 'text': //$index[$name] = 'text'; break; default: $index[$name] = true; } if ($index) { Database_Mongo::collection('jobs')->ensureIndex($index, array('sparse' => true)); } } die('Done'); }
public function action_search() { $id = Arr::get($_GET, 'id'); if (!DB::select('id')->from('job_columns')->where('id', '=', $id)->execute()->get('id')) { throw new HTTP_Exception_403(); } $group = Arr::get($_GET, 'group'); if (!Group::get($group)) { throw new HTTP_Exception_403(); } $state = intval(Arr::get($_GET, 'state')); DB::query(Database::INSERT, DB::expr("INSERT INTO `group_columns` (`group_id`, `column_id`, `search`) VALUES (:group, :id, :state) ON DUPLICATE KEY UPDATE `search` = :state", array(':group' => $group, ':id' => $id, ':state' => $state))->compile())->execute(); if ($state == 2) { if (Columns::get_type($id) != 'text') { Database_Mongo::collection('jobs')->ensureIndex(array('data.' . $id => true), array('sparse' => true)); } } else { if (!DB::select('group_id')->from('group_columns')->where('column_id', '=', $id)->and_where('search', '=', 2)->execute()->get('group_id')) { Database_Mongo::collection('jobs')->deleteIndex(array('data.' . $id => true)); } } die(json_encode(array('success' => true))); }
public function action_index() { $id = $this->request->param('id'); $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($id))); if (!$job) { throw new HTTP_Exception_404('Not found'); } if (!Group::current('show_all_jobs') && !in_array((int) User::current('company_id'), Arr::get($job, 'companies', array()), true) && !in_array((int) User::current('company_id'), Arr::get($job, 'ex', array()), true)) { throw new HTTP_Exception_403('Forbidden'); } if (!Group::current('show_all_jobs') && !in_array((int) User::current('company_id'), Arr::get($job, 'companies', array()), true)) { Columns::set_readonly(); } $job['attachments'] = DB::select()->from('attachments')->where('job_id', '=', $id)->and_where('uploaded', '>', '0')->order_by('folder', 'asc')->order_by('numbering', 'asc')->execute()->as_array('id'); if (isset($_GET['delete']) && Group::current('allow_assign')) { $file_id = intval($_GET['delete']); if (isset($job['attachments'][$file_id])) { if (file_exists(DOCROOT . 'storage/' . $file_id)) { unlink(DOCROOT . 'storage/' . $file_id); } $attachment = $job['attachments'][$file_id]; $data = array('filename' => $attachment['folder'] . ' / ' . $attachment['fda_id'] . ' / ' . $attachment['address'] . ' / ' . $attachment['filename'], 'uploaded' => time(), 'user_id' => User::current('id'), 'job_id' => $id, 'action' => 2); Database::instance()->begin(); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); DB::delete('attachments')->where('id', '=', $file_id)->execute(); Database::instance()->commit(); die(json_encode(array('success' => true))); } else { throw new HTTP_Exception_404('Not found'); } } $users = array(); $job['region'] = DB::select('name')->from('regions')->where('id', '=', $job['region'])->execute()->get('name'); $tabs = DB::select()->from('job_tabs')->execute()->as_array('id'); foreach ($job['attachments'] as $attachment) { $users[$attachment['user_id']] = 1; } $job_types = DB::select('id', 'name')->from('job_types')->order_by('name', 'asc')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->order_by('name', 'asc')->execute()->as_array('id', 'name'); $submissions = array(); if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->find(array('job_key' => $id))->sort(array('update_time' => -1, 'user_id' => 1, 'key' => 1)); } else { $u = DB::select('id')->from('users')->where('company_id', '=', User::current('company_id'))->execute()->as_array(NULL, 'id'); $result = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'user_id' => array('$in' => $u)))->sort(array('update_time' => -1, 'user_id' => 1, 'key' => 1)); } foreach ($result as $submission) { $users[$submission['user_id']] = 1; $key = substr($submission['key'], 5); $data = array('id' => $submission['_id'], 'key' => str_replace('.', '-', $submission['key']), 'user_id' => $submission['user_id'], 'time' => $submission['update_time'], 'name' => Columns::get_name($key), 'type' => Columns::get_type($key), 'active' => Arr::get($submission, 'active'), 'value' => $submission['value']); if (isset($submission['version'])) { $data['version'] = $submission['version']; } if (Group::current('allow_assign') && Arr::get($submission, 'active') == 1) { $tabs[Columns::get_tab($key)]['submissions'][$key] = 1; $submissions[$submission['key']][] = $data; } $submissions['list'][(string) $data['id']] = $data; } foreach (Columns::get_visible() as $key => $name) { $tabs[Columns::get_tab($key)]['columns'][$key] = $name; } User::get(array_keys($users)); if ($_POST) { $update_time = time(); if (!Arr::path($_FILES, 'attachment.error', -1) && is_uploaded_file($_FILES['attachment']['tmp_name'])) { $data = array('filename' => $_FILES['attachment']['name'], 'mime' => $_FILES['attachment']['type'], 'uploaded' => $update_time, 'user_id' => User::current('id'), 'job_id' => $id); Database::instance()->begin(); $result = DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(); $image_id = Arr::get($result, 0); if ($image_id && move_uploaded_file($_FILES['attachment']['tmp_name'], DOCROOT . 'storage/' . $image_id)) { Database::instance()->commit(); Messages::save("File " . $data['filename'] . ' was successfully uploaded!', 'success'); } else { Database::instance()->rollback(); } } if (Group::current('allow_assign')) { $update = array(); $archive = array(); foreach (Arr::get($_POST, 'data', array()) as $key => $value) { $value = Columns::parse($value, Columns::get_type($key)); if (Arr::path($job, 'data.' . $key) != $value) { if ($value) { $update['$set']['data.' . $key] = $value; } else { $update['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; } } $list = Arr::get($submissions, 'list', array()); $pending = array(); $accepted = array(); $ignored = array(); foreach ($list as $submission) { $key = 'submission-' . str_replace('-', '.', $submission['key']); $value = Arr::path($_POST, $key); if ($value !== NULL) { $pending[] = $submission['id']; if ($value) { $accepted[] = $submission['id']; } else { $ignored[] = $submission['id']; } } } foreach (Arr::get($_POST, 'submission-data', array()) as $key => $value) { if ($value && isset($list[$value])) { $value = Arr::path($list, $value . '.value'); if ($key == 177 || $key == 205) { $value = $value ? 1 : 0; } if (Arr::path($job, 'data.' . $key) != $value) { if ($value) { $update['$set']['data.' . $key] = $value; unset($update['$unset']['data.' . $key]); } else { $update['$unset']['data.' . $key] = 1; unset($update['$set']['data.' . $key]); } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; } } } if ($pending) { Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => $ignored)), array('$set' => array('admin_id' => User::current('id'), 'process_time' => $update_time), '$unset' => array('active' => 1)), array('multiple' => 1)); Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => $accepted)), array('$set' => array('admin_id' => User::current('id'), 'process_time' => $update_time, 'active' => -1)), array('multiple' => 1)); } $ass = false; $values = array(); foreach (Arr::get($_POST, 'assigned', array()) as $key => $value) { if (Arr::path($job, 'assigned.' . $key) != $value) { if ($ass < 1) { $ass = $value ? 1 : -1; } $values[] = array('time' => $update_time, 'user_id' => User::current('id'), 'company_id' => max($value, 0), 'job_id' => $job['_id'], 'type' => $key); $unassigned = Arr::path($job, 'assigned.' . $key); if ($value) { $job['assigned'][$key] = $value; $users = DB::select('id')->from('users')->where('company_id', '=', $value)->execute()->as_array(NULL, 'id'); $update['$set']['data.266'] = $update_time; $message = '1 ticket was allocated on ' . date('d-m-Y H:i', $update_time) . '. <a href="' . URL::base() . 'search/view/' . $job['_id'] . '">View ticket</a>'; $insert = DB::insert('notifications', array('user_id', 'message')); foreach ($users as $user) { $insert->values(array($user, $message)); } $insert->execute(); } else { unset($job['assigned'][$key]); } if ($unassigned) { $users = DB::select('id')->from('users')->where('company_id', '=', $unassigned)->execute()->as_array(NULL, 'id'); $subs = Database_Mongo::collection('submissions')->findOne(array('job_key' => $job['_id'], 'active' => 1)); if ($subs) { Messages::save('Unable to unassign the job - please, approve or reject all submissions first!', 'danger'); } else { $subs = Database_Mongo::collection('submissions')->find(array('job_key' => $job['_id'], 'user_id' => array('$in' => $users), 'financial' => array('$exists' => 0), 'active' => -1))->sort(array('process_time' => 1)); $list = array(); foreach ($subs as $sub) { $list[$sub['key']] = $sub['_id']; } $financial = DB::select('id')->from('job_columns')->where('financial', '>', 0)->execute()->as_array(NULL, 'id'); $result = array(); foreach ($financial as $column) { if (isset($list['data.' . $column])) { $result[$column] = $list['data.' . $column]; } } Database_Mongo::collection('submissions')->update(array('_id' => array('$in' => array_values($result))), array('$set' => array('financial_time' => 0)), array('multiple' => 1)); } } } } if ($ass) { $update['$set']['assigned'] = $job['assigned']; $insert = DB::insert('assign_log', array_keys($values[0])); foreach ($values as $value) { $insert->values($value); } $insert->execute(); $result = Database_Mongo::collection('submissions')->distinct('user_id', array('job_key' => $job['_id'])); if ($result) { $result = DB::select('company_id')->distinct(true)->from('users')->where('id', 'IN', $result)->and_where('company_id', '>', 0)->execute()->as_array(NULL, 'company_id'); } $companies = array_flip($result); foreach ($companies as $key => $value) { $companies[$key] = 1; } $update['$set']['ex'] = array_keys($companies); } if (!Arr::get($update, '$unset')) { unset($update['$unset']); } $companies = array(); if (isset($job['assigned'])) { foreach ($job['assigned'] as $company) { if ($company) { $companies[$company] = 1; } } } foreach (Columns::$settings as $key => $value) { if (!in_array($key, Columns::$settings_read_only, true)) { if (Arr::get($job, $key) != Arr::get($_POST, $key)) { $value = Arr::get($_POST, $key) ? 1 : 0; $update[$value ? '$set' : '$unset'][$key] = 1; $job[$key] = $value; } } } $discrepancies = Database_Mongo::collection('discrepancies')->find(array('job_key' => $job['_id']))->sort(array('update_time' => -1))->getNext(); if (Group::current('allow_reports') && $discrepancies) { $fl = 0; $set = array(); $ignores = Arr::get($_POST, 'ignore-discrepancy'); foreach ($discrepancies['data'] as $key => $values) { $val = Arr::get($values, 'ignore') ? 1 : 0; $new_val = Arr::get($ignores, $key) ? 1 : 0; if ($val != $new_val) { if ($new_val) { if (!$fl) { $fl = -1; } $set['$set']['data.' . $key . '.ignore'] = 1; $discrepancies['data'][$key]['ignore'] = 1; } else { if ($values['old_value'] != Arr::get($job['data'], $key, '')) { $fl = 1; } $set['$unset']['data.' . $key . '.ignore'] = 1; $discrepancies['data'][$key]['ignore'] = 0; } } } if ($set) { Database_Mongo::collection('discrepancies')->update(array('_id' => new MongoId($discrepancies['_id'])), $set); } if ($fl > 0 && !Arr::get($job, 'discrepancies')) { $update['$set']['discrepancies'] = 1; } elseif ($fl <= 0 && Arr::get($job, 'discrepancies')) { $fl = true; foreach ($discrepancies['data'] as $key => $values) { if (!Arr::get($values, 'ignore')) { $value = $values['old_value']; $new_value = Arr::get($job['data'], $key, ''); if ($key == 44) { $value = preg_replace('/[^a-z]/i', '', strtolower($value)); $new_value = preg_replace('/[^a-z]/i', '', strtolower($new_value)); } if ($value != $new_value) { $fl = false; } } } if ($fl) { $update['$unset']['discrepancies'] = 1; } } } if ($update) { Utils::calculate_financial($job); $status = preg_replace('/[^a-z]/', '', strtolower(Arr::path($update, array('$set', 'data.44'), ''))); 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; } } $update['$set']['companies'] = array_keys($companies); $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); if ($companies && $status == Enums::STATUS_UNALLOC) { $update['$set']['status'] = Enums::STATUS_ALLOC; } elseif (!$companies && $status == Enums::STATUS_ALLOC) { $update['$unset']['status'] = 1; } $update['$set']['last_update'] = $update_time; if (isset($update['$set']['data.8'])) { $update['$set']['address'] = MapQuest::parse($update['$set']['data.8']); } elseif (isset($update['$unset']['data.8'])) { $update['$unset']['address'] = 1; } Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); if ($archive) { foreach (Columns::get_static() as $key => $value) { $archive['static'][$key] = Arr::path($job, 'data.' . $key); } $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = $update_time; $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } Messages::save("Changes were succesfully saved!", 'success'); } else { Messages::save("No changes were submitted", 'warning'); } if ($pending) { $count = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'active' => 1))->count(); $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); if ($status == Enums::STATUS_PENDING && !$count) { $update = array('$set' => array('last_update' => $update_time, 'status' => Enums::STATUS_COMPLETE)); Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); } } elseif (in_array(Arr::get($job, 'status', Enums::STATUS_UNALLOC), array(Enums::STATUS_UNALLOC, Enums::STATUS_ALLOC))) { if ($companies) { $status = Enums::STATUS_ALLOC; } else { $status = Enums::STATUS_UNALLOC; } if ($status != Arr::get($job, 'status', Enums::STATUS_UNALLOC)) { $update = array('$set' => array('last_update' => $update_time)); if ($status == Enums::STATUS_UNALLOC) { $update['$unset']['status'] = 1; } else { $update['$set']['status'] = $status; } Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); } } elseif ($ass == 1 && in_array(Arr::get($job, 'status', Enums::STATUS_UNALLOC), array(Enums::STATUS_COMPLETE, Enums::STATUS_ARCHIVE))) { $update = array('$set' => array('last_update' => $update_time, 'status' => Enums::STATUS_ALLOC)); Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); } if ($submissions) { Messages::save(count($pending) . '/' . count($submissions['list']) . ' submission(s) were processed.', 'info'); } } else { $submissions = array(); foreach (Arr::get($_POST, 'data', array()) as $key => $value) { $value = Columns::parse($value, Columns::get_type($key)); if (Arr::path($job, 'data.' . $key) != $value) { $submissions[$key] = $value; } } if ($submissions) { $submission = array('job_key' => $id, 'user_id' => User::current('id'), 'active' => 1, 'update_time' => $update_time); if (Arr::get($_POST, 'location')) { $submission['location'] = $_POST['location']; } $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); $update = array(); $approval = false; $archive = array(); foreach ($submissions as $key => $value) { if (Columns::get_direct($key)) { if ($value) { $update['$set']['data.' . $key] = $value; } else { $update['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = -1; $submission['process_time'] = $submission['update_time']; Database_Mongo::collection('submissions')->insert($submission); unset($submission['process_time']); unset($submission['_id']); } else { $approval = true; if ($status != Enums::STATUS_PENDING) { $status = $update['$set']['status'] = Enums::STATUS_PENDING; } $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = 1; Database_Mongo::collection('submissions')->insert($submission); unset($submission['_id']); } } if ($update) { $update['$set']['last_update'] = $update_time; Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); if ($archive) { foreach (Columns::get_static() as $key => $value) { $archive['static'][$key] = Arr::path($job, 'data.' . $key); } $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = $update_time; $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } } Messages::save("Changes were succesfully submitted. " . ($approval ? 'Manager will review changes and confirm them.' : ''), 'success'); } else { Messages::save("No changes were submitted", 'warning'); } } $this->redirect('/search'); } $values = array(); if (!Group::current('allow_assign')) { $last = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'active' => 1, 'user_id' => array('$in' => DB::select('id')->from('users')->where('company_id', '=', User::current('company_id'))->execute()->as_array(NULL, 'id'))))->sort(array('update_time' => 1)); foreach ($last as $submission) { $values[str_replace('.', '', $submission['key'])] = $submission['value']; } } $job['discr'] = array(); if (Group::current('allow_reports')) { $job['discr'] = Database_Mongo::collection('discrepancies')->find(array('job_key' => $id))->sort(array('update_time' => -1))->getNext(); } if (Group::current('time_machine')) { $result = Database_Mongo::collection('archive')->find(array('job_key' => $job['_id']))->sort(array('update_time' => -1)); $archive = array(); $ids = array(); foreach ($result as $item) { $archive[] = $item; $ids[Arr::get($item, 'user_id', 0)] = 1; } User::get(array_keys($ids)); } if (Group::current('allow_finance')) { $query = DB::select('company_id', 'payment_time', 'admin_id', array('payments.amount', 'total'), array('payment_jobs.amount', 'amount'))->from('payment_jobs')->join('payments')->on('payment_jobs.payment_id', '=', 'payments.id')->where('job_key', '=', $job['_id'])->order_by('payment_time', 'desc'); if (!Group::current('show_all_jobs')) { $query->and_where('company_id', '=', User::current('company_id')); } $job['payments'] = $query->execute()->as_array(); $ids = array(); foreach ($job['payments'] as $payment) { $ids[$payment['admin_id']] = 1; } if ($ids) { User::get(array_keys($ids)); } } $forms = array(); if (Group::current('allow_custom_forms')) { $forms = array('job' => $job['_id']); if (!Group::current('show_all_jobs')) { $forms['company'] = User::current('company_id'); } $result = Database_Mongo::collection('forms-data')->find($forms, array('data' => 0))->sort(array('last_update' => -1)); $forms = array(); $ids = array(); foreach ($result as $form) { $ids[$form['form_id']] = 1; $forms[] = $form; } $list = array(); foreach ($ids as $key => $dummy) { $list[] = new MongoId($key); } $result = Database_Mongo::collection('forms')->find(array('_id' => array('$in' => $list)), array('name' => 1)); $list = array(); foreach ($result as $form) { $list[strval($form['_id'])] = $form['name']; } foreach ($forms as $key => $form) { if (isset($list[$form['form_id']])) { $forms[$key]['name'] = $list[$form['form_id']]; } } } $view = View::factory('Jobs/View')->bind('job', $job)->bind('tabs', $tabs)->bind('job_types', $job_types)->bind('companies', $companies)->bind('submissions', $submissions)->bind('values', $values)->bind('archive', $archive)->bind('forms', $forms); $this->response->body($view); }
public function action_index() { if (!Group::current('allow_assign')) { throw new HTTP_Exception_403('Forbidden'); } header('Content-type: application/json'); $action = $this->request->param('id'); $result = array(); $columns = DB::select('id')->from('job_columns')->where('editable', '=', 1)->execute()->as_array('id', 'id'); switch ($action) { case 'get': $data = array(); $result['columns'] = array(); foreach ($columns as $column) { $value = array('id' => intval($column), 'name' => Columns::get_name($column)); $type = Columns::get_type($column); if (substr($type, 0, 4) == 'enum') { $id = substr($type, 5); $type = Enums::is_multi($id) ? 'multi' : 'list'; $value['values'] = array_values(Enums::get_values($id)); } $value['type'] = $type ?: 'string'; $result['columns'][] = $value; $data[] = 'data.' . $column; } $ids = explode(',', strval(Arr::get($_GET, 'id', ''))); $values = array(); if ($ids) { $query = array('_id' => array('$in' => $ids)); if (!Group::current('show_all_jobs')) { $query['companies'] = intval(User::current('company_id')); } $jobs = Database_Mongo::collection('jobs')->find($query, $data); foreach ($jobs as $job) { foreach ($job['data'] as $key => $value) { $job['data'][$key] = Columns::output($value, Columns::get_type($key), true); } $job['id'] = $job['_id']; unset($job['_id']); $values[] = $job; } } $result['jobs'] = $values; break; case 'set': if (User::current('login') !== Arr::get($_POST, 'username')) { die('Wrong username! Please, check it and submit data again.'); } $data = Arr::get($_POST, 'jobs'); $values = array(); foreach ($data as $job) { $id = strval(Arr::get($job, 'id', '')); foreach (Arr::get($job, 'data') as $key => $value) { if (isset($columns[$key])) { $values[$id][$key] = strval($value); } } } $query = array('_id' => array('$in' => array_keys($values))); if (!Group::current('show_all_jobs')) { $query['companies'] = intval(User::current('company_id')); } $data = array(); foreach ($columns as $column) { $data['data.' . $column] = 1; } $count = 0; $jobs = Database_Mongo::collection('jobs')->find($query, $data); foreach ($jobs as $job) { if (isset($values[$job['_id']])) { $id = $job['_id']; $new = array(); $archive = array(); foreach ($values[$id] as $key => $value) { $value = $value ? Columns::parse($value, Columns::get_type($key)) : ''; $old = Arr::path($job, 'data.' . $key); if (($value || $old) && $value != $old) { if ($value) { $new['$set']['data.' . $key] = $value; } else { $new['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => $old, 'new_value' => $value); } } if ($new) { $new['$set']['last_update'] = time(); Database_Mongo::collection('jobs')->update(array('_id' => $id), $new); $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = time(); $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); $count++; } } } $result = array('success' => true, 'count' => $count); break; } die(json_encode($result)); }
<tr class="time-machine-item <?=Arr::get($classes, $history['update_type'])?>" data-saved="<?=Arr::get($classes, $history['update_type'])?>" data-id="<?=$history['_id']?>"> <td nowrap="nowrap"><?=date('d-m-Y H:i', $history['update_time'])?></td> <td><?=User::get(Arr::get($history, 'user_id'), 'login') ? : 'Unknown'?></td> <td><?=Arr::get($actions, $history['update_type'])?></td> <td><a href="<?=URL::base() . 'imex/reports?file=' . urlencode($history['filename'])?>"><?=HTML::chars($history['filename'])?></a></td> <td colspan="3"> <?php if ($history['update_type'] == 2):?> <table class="table subtable"> <tr> <th>Name</th> <th>Old value:</th> <th>New value:</th> <th>Current value</th> </tr> <?php foreach($history['data'] as $id => $value): $type = Columns::get_type($id);?> <tr class="same-yellow"> <td><?=HTML::chars(Columns::get_name($id))?></td> <td <?=strlen($value['old_value']) > 100 ? 'class="shorten"' : ''?>><?=Columns::output($value['old_value'], $type)?></td> <td <?=strlen($value['new_value']) > 100 ? 'class="shorten"' : ''?>><?=Columns::output($value['new_value'], $type)?></td> <td <?=strlen(Arr::get($job['data'], $id)) > 100 ? 'class="shorten"' : ''?>><?=Columns::output(Arr::get($job['data'], $id), $type)?></td> </tr> <?php endforeach;?> </table> <?php else:?> N/A <?php endif;?> </td> </tr> <?php endforeach;?> </table>
public function action_fill() { $id = Arr::get($_GET, 'id'); if ($id) { $filters = array('_id' => new MongoId($id)); if (!Group::current('show_all_jobs')) { $filters['company'] = User::current('company_id'); } $form_data = Database_Mongo::collection('forms-data')->findOne($filters); $form_id = $form_data['form_id']; } else { $data = explode('/', Arr::get($_GET, 'form', '')); $form_id = $data[0]; $form_data = array(); } $form = Database_Mongo::collection('forms')->findOne(array('_id' => new MongoId($form_id))); if (!$form) { throw new HTTP_Exception_404('Not found'); } if (!$form_data) { $form_data['form_id'] = $form_id; $form_data['data'] = array(); foreach ($form['data'] as $key => $table) { if (is_array($table) && Arr::get($table, 'type') == 'table') { foreach ($table['data'] as $row => $cells) { foreach ($cells as $cell => $input) { if (Arr::get($input, 'name')) { $form_data['data'][$input['name']] = ''; } } } } } } switch ($form['type']) { case Form::FORM_TYPE_COMMON: break; case Form::FORM_TYPE_TICKET: if (isset($form_data['job'])) { $job_id = $form_data['job']; } else { $job_id = $data[1]; $form_data['job'] = $job_id; } $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($job_id))); if (!$job) { throw new HTTP_Exception_404('Not found'); } if (!Group::current('show_all_jobs') && !in_array(User::current('company_id'), array_merge(Arr::get($job, 'companies', array()), Arr::get($job, 'ex', array())))) { throw new HTTP_Exception_404('Not found'); } foreach ($form['data'] as $key => $table) { if (is_array($table) && Arr::get($table, 'type') == 'table') { foreach ($table['data'] as $row => $cells) { foreach ($cells as $cell => $input) { switch (Arr::get($input, 'type')) { case 'ticket': $form['data'][$key]['data'][$row][$cell]['type'] = 'label'; $form['data'][$key]['data'][$row][$cell]['placeholder'] = Arr::get($input, 'value') ? Columns::output(Arr::path($job, 'data.' . $input['value']), Columns::get_type($input['value'])) : $job['_id']; $form['data'][$key]['data'][$row][$cell]['destination'] = Arr::get($input, 'destination'); unset($form['data'][$key]['data'][$row][$cell]['value']); break; case 'timestamp': $form['data'][$key]['data'][$row][$cell] = array('type' => 'timestamp', 'placeholder' => Arr::get($form_data, 'last_update') ? date('d-m-Y H:i', $form_data['last_update']) : '', 'destination' => Arr::get($input, 'destination')); break; case 'revision': $form['data'][$key]['data'][$row][$cell] = array('type' => 'revision', 'placeholder' => Arr::get($form_data, 'revision', 1), 'destination' => Arr::get($input, 'destination')); break; } } } } } break; } if (isset($_GET['load']) || isset($_GET['print']) || $_POST) { header('Content-type: application/json'); if (isset($form_data['job'])) { $job = Database_Mongo::collection('jobs')->findOne(array('_id' => is_array($form_data['job']) ? array('$in' => $form_data['job']) : $form_data['job'])); } if ($_POST || isset($_GET['print'])) { if (isset($_GET['print'])) { $columns = DB::select('id')->from('report_columns')->where('report_id', '=', Arr::get($form, 'report'))->execute()->as_array('id', 'id'); $report = array(); $colors = array(); $update = array(); $hide = array(); foreach ($form['data'] as $key => $table) { if (is_array($table) && Arr::get($table, 'type') == 'table') { if (Arr::get($table, 'data-related-option') && Arr::get($table, 'data-related-value') != Arr::get($form_data['data'], $table['data-related-option'])) { $hide[] = $key; } else { foreach ($table['data'] as $row => $cells) { foreach ($cells as $cell => $input) { switch (Arr::get($input, 'type')) { case 'revision': $form['data'][$key]['data'][$row][$cell]['placeholder'] = $input['placeholder'] = Arr::get($form_data, 'revision', 1); break; case 'timestamp': $form['data'][$key]['data'][$row][$cell]['placeholder'] = $input['placeholder'] = Arr::get($form_data, 'last_update') ? date('d-m-Y H:i', $form_data['last_update']) : ''; break; } if (Arr::get($input, 'name')) { $form['data'][$key]['data'][$row][$cell]['value'] = $input['value'] = Arr::path($form_data, array('data', $input['name']), ''); } if (Arr::get($input, 'destination') && isset($columns[$input['destination']])) { if (isset($input['colors'])) { try { $list = array_combine(explode(',', $input['options']), explode(',', $input['colors'])); $color = Arr::get($list, $input['value']); } catch (Exception $e) { $color = false; } if ($color) { $colors[$input['destination']] = $color; } } $report[$input['destination']] = Arr::get($input, in_array(Arr::get($input, 'type', ''), array('text', 'number', 'float', 'date', 'options')) ? 'value' : 'placeholder'); } if (Arr::get($input, 'bindValue')) { $bind = explode(',', $input['bindValue']); foreach ($bind as $target) { $update[$target] = array('value' => Arr::get($input, in_array(Arr::get($input, 'type', ''), array('text', 'number', 'float', 'date', 'options')) ? 'value' : 'placeholder'), 'type' => 'replace'); } } } } } } } foreach ($hide as $key) { unset($form['data'][$key]); } $view = View::factory('Forms/PDF')->bind('name', $form['name'])->bind('form', $form['data'])->set('attachments', Arr::get($form_data, 'attachments', array())); require_once APPPATH . 'mpdf/mpdf.php'; $pdf = new mPDF('UTF-8', 'A4'); $pdf->ignore_invalid_utf8 = true; $pdf->shrink_tables_to_fit = 1; $pdf->keep_table_proportions = true; $pdf->WriteHTML($view); $content = $pdf->Output('', 'S'); $name = trim(preg_replace('/-{2,}/', '-', preg_replace('/[^a-z0-9]/i', '-', $form['name'])), '-'); switch ($form['type']) { case Form::FORM_TYPE_COMMON: $jobs = array(0); break; case Form::FORM_TYPE_TICKET: $jobs = array($job['_id']); break; } $company = DB::select('name')->from('companies')->where('id', '=', $form_data['company'])->execute()->get('name'); $uploaded = time(); $submissions = array(); $submission = array('user_id' => User::current('id'), 'active' => 1, 'update_time' => $uploaded); foreach ($jobs as $job) { if ($job) { $job = Database_Mongo::collection('jobs')->findOne(array('_id' => $job), array('data' => 1)); $new = array(); $archive = array(); foreach ($update as $key => $value) { if ($value['type'] == 'append') { switch (Columns::get_type($key)) { case 'int': case 'number': case 'float': $value = Arr::path($job, 'data.' . $key, 0) + $value['value']; break; default: $value = Arr::path($job, 'data.' . $key, '') . "\n" . $value['value']; break; } } else { $value = $value['value']; } if ($value) { $value = Columns::parse($value, Columns::get_type($key)); } if (Group::current('allow_assign') || Columns::get_direct($key)) { if ($value) { $new['$set']['data.' . $key] = $value; } else { $new['$unset']['data.' . $key] = 1; } if (!Group::current('allow_assign')) { $submission['job_key'] = $job['_id']; $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = -1; $submission['process_time'] = $uploaded; Database_Mongo::collection('submissions')->insert($submission); unset($submission['process_time']); unset($submission['_id']); } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key, ''), 'new_value' => $value); } else { $new['$set']['status'] = Enums::STATUS_PENDING; $submissions[$job['_id']][$key] = $value; } } if ($new) { $new['$set']['last_update'] = $uploaded; Database_Mongo::collection('jobs')->update(array('_id' => $job['_id']), $new); if ($archive) { $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $job['_id']; $archive['user_id'] = User::current('id'); $archive['update_time'] = $uploaded; $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } } } $filename = $name . ' (' . $company . ') -' . date('dmY-His') . '.pdf'; $data = array('filename' => $filename, 'mime' => 'application/pdf', 'uploaded' => $uploaded, 'user_id' => $form_data['user_id'], 'job_id' => $job ? $job['_id'] : 0, 'folder' => 'Reports', 'fda_id' => $job ? Arr::path($job, 'data.14') : 'Unattached', 'address' => $job ? trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', Arr::path($job, 'data.8'))), '-') : 'Unattached', 'title' => ''); Database::instance()->begin(); $result = DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(); $image_id = Arr::get($result, 0); if ($image_id && file_put_contents(DOCROOT . 'storage/' . $image_id, $content)) { unset($data['mime']); $data = array('filename' => 'Reports / ' . ($job ? Arr::path($job, 'data.14') : 'Unattached') . ' / ' . ($job ? $data['address'] : 'Unattached') . ' / ' . $data['filename'], 'uploaded' => $uploaded, 'user_id' => User::current('id'), 'job_id' => $job ? $job['_id'] : 0, 'action' => 1); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); Database::instance()->commit(); Database_Mongo::collection('forms-data')->remove(array('_id' => new MongoId($id))); if ($report) { $data = $report; $report = array('report_id' => intval(Arr::get($form, 'report')), 'user_id' => $form_data['user_id'], 'company' => $form_data['company'], 'attachment_id' => $image_id, 'attachment' => $filename, 'uploaded' => $uploaded, 'colors' => $colors); if (Arr::get($form_data, 'attachments')) { $report['attachments'] = $form_data['attachments']; } if (isset($form_data['geo'])) { $report['geo'] = $form_data['geo']; } $columns = DB::select('id', 'type')->from('report_columns')->where('report_id', '=', $report['report_id'])->execute()->as_array('id', 'type'); foreach ($columns as $key => $value) { $report[$key] = Arr::get($data, $key) ? Columns::parse($data[$key], $value) : ''; } Database_Mongo::collection('reports')->insert($report); } } else { Messages::save('Error occurred during report processing... Please try again later'); } } foreach ($submissions as $job_key => $values) { foreach ($values as $key => $value) { $submission['job_key'] = $job_key; $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = 1; Database_Mongo::collection('submissions')->insert($submission); unset($submission['_id']); } } $target = 'attachments'; } else { $target = 'forms'; $fl = false; foreach ($form_data['data'] as $key => $value) { if (Arr::get($_POST, $key) != $value) { $form_data['data'][$key] = Arr::get($_POST, $key); $fl = true; } } if ($fl) { $form_data['last_update'] = time(); if (Arr::get($form, 'geo')) { $form_data['geo'] = Arr::get($_POST, 'geo'); } } if ($id) { if ($fl) { $form_data['revision']++; } Database_Mongo::collection('forms-data')->update(array('_id' => new MongoId($id)), $form_data); } else { $form_data['created'] = time(); $form_data['user_id'] = User::current('id'); $form_data['company'] = User::current('company_id'); $form_data['revision'] = 1; $form_data['last_update'] = time(); Database_Mongo::collection('forms-data')->insert($form_data); $id = strval($form_data['_id']); } } header('Content-type: application/json'); switch ($form['type']) { case Form::FORM_TYPE_TICKET: $url = URL::base() . 'search/view/' . $form_data['job'] . '#' . $target; break; case Form::FORM_TYPE_COMMON: $url = URL::base() . 'form/unattached'; break; } die(json_encode(array('success' => true, 'id' => $id, 'url' => $url))); } foreach ($form['data'] as $key => $table) { if (is_array($table) && Arr::get($table, 'type') == 'table') { foreach ($table['data'] as $row => $cells) { foreach ($cells as $cell => $input) { if (Arr::get($input, 'name')) { $form['data'][$key]['data'][$row][$cell]['value'] = Arr::get($form_data['data'], $input['name']); } } } } } die(json_encode(array('form' => $form['data'], 'attachments' => Arr::get($form_data, 'attachments')))); } $view = View::factory('Forms/Form')->set('form_id', $form_id)->set('id', $id)->set('allow_geo', Arr::get($form, 'geo'))->set('allow_attachment', Arr::get($form, 'attachment'))->set('name', $form['name']); $this->response->body($view); }
public function action_export() { $id = $this->request->param('id'); if ($id == 'all') { ini_set('memory_limit', '4G'); } $result = $this->get_results($id == 'all'); $reports = array(); foreach (Columns::get_static() as $column => $value) { $reports[$column] = Columns::get_name($column); } $file = tmpfile(); $actions = array('1' => 'Created', '2' => 'Updated', '3' => 'Removed'); $data = array('Ticket ID', 'Date', 'Action', 'File name'); foreach ($reports as $id => $name) { $data[] = $name; } $data[] = "Column name"; $data[] = "Old value"; $data[] = "New value"; fputcsv($file, $data); foreach ($result as $ticket) { $ticket['update_time'] = date('d-m-Y H:i', $ticket['update_time']); $ticket['type'] = Arr::get($actions, $ticket['update_type'], 'Unknown'); $data = array($ticket['job_key'], $ticket['update_time'], $ticket['type'], $ticket['filename']); foreach ($reports as $id => $name) { $data[] = Columns::output(Arr::path($ticket, 'static.' . $id), Columns::get_type($id), true); } if ($ticket['update_type'] == 2) { if ($ticket['data']) { $base = $data; foreach ($ticket['data'] as $key => $value) { $data = $base; $date = Columns::get_type($key) == 'date'; $data[] = Columns::get_name($key); $data[] = $date && $value['old_value'] ? date('d-m-Y H:i', $value['old_value']) : $value['old_value']; $data[] = $date && $value['new_value'] ? date('d-m-Y H:i', $value['new_value']) : $value['new_value']; fputcsv($file, $data); } } else { $data[] = "Non-relevant"; fputcsv($file, $data); } } else { $data[] = "N/A"; fputcsv($file, $data); } } rewind($file); header('Content-type: text/csv'); header('Content-disposition: filename=Export.csv'); fpassthru($file); fclose($file); die; }
<?php if (Group::current('allow_assign')):?><th>Current value</th><?php endif;?> <th>Paid value</th> <th>Max value</th> <th>Rate</th> <?php if (Group::current('allow_assign')):?><th> </th><?php endif;?> </tr> <?php foreach ($submissions as $job => $list): $region = Arr::path($jobs, array($job, 'region'), 0);?> <tr class="ticket-id <?=isset($discrepancies[$job])? 'discrepancy text-center' . (Arr::get($_GET, 'discrepancy') ? ' hidden' : '') : 'text-center'?>"> <th colspan="<?=Group::current('allow_assign') ? 12 : 9?>"> <a href="<?=URL::base()?>search/view/<?=$job?>"><?=$job?>, <?=Arr::path($jobs, array($job, 'data', 9))?>, <?=Arr::path($jobs, array($job, 'data', 14))?>, <?=Arr::path($jobs, array($job, 'data', 8))?></a> </th> </tr> <?php foreach ($list as $submission): $key = substr($submission['key'], 5); $type = Columns::get_type($key); $discr = Group::current('allow_assign') && Arr::path($jobs, $job . '.' . $submission['key']) != $submission['value']; $rate = Arr::path($rates, array(User::get($submission['user_id'], 'company_id'), $region, $key), Arr::path($rates, array(User::get($submission['user_id'], 'company_id'), 0, $key), 0));?> <tr class="submission text-center <?=$discr ? 'rose' : (Arr::get($_GET, 'discrepancy') ? 'hidden ' : '') . (Arr::get($submission, 'financial_time') ? 'lgreen' : 'yellow')?>" data-id="<?=$job?>"> <td><?=date('d-m-Y H:i', $submission['update_time'])?></td> <td><?=Arr::get($submission, 'process_time') ? date('d-m-Y H:i', $submission['process_time']) : ''?></td> <td class="time"><?=Arr::get($submission, 'financial_time') ? date('d-m-Y H:i', $submission['financial_time']) : ''?></td> <td><?=User::get($submission['user_id'], 'login')?></td> <?php if (Group::current('allow_assign')):?><td><?=Arr::get($companies, User::get($submission['user_id'], 'company_id'), 'Unknown')?></td><?php endif;?> <td><?=Columns::get_name($key)?></td> <td <?=strlen(Columns::output($submission['value'], $type)) > 100 ? 'class="shorten"' : ''?>><?=Columns::output($submission['value'], $type);?></td> <?php if (Group::current('allow_assign')):?><td><?=Arr::path($jobs, $job . '.' . $submission['key']) ? Columns::output($jobs[$job]['data'][$key], $type) : ''?></td><?php endif;?> <td class="paid"><?=Arr::get($submission, 'paid')?></td> <td><?=floatval(Arr::get($columns, $key))?></td> <td class="rate"><?=Arr::get($submission, 'rate') ? number_format($submission['rate'], 2) : $rate?></td> <?php if (Group::current('allow_assign')):?>
<th>User</th> <th>Source</th> <th>Status</th> <th>Processed</th> <th>Company</th> <th>Location</th> <th>Column name</th> <th>Value</th> <th>Current value</th> <?php if (Group::current('allow_assign')):?> <th> </th> <?php endif;?> </tr> <?php foreach ($submissions as $submission): $name = Columns::get_name(substr($submission['key'], 5)); $type = Columns::get_type(substr($submission['key'], 5)); $value = Columns::output($submission['value'], $type); $current = Columns::output(Arr::path($job, 'data.' . substr($submission['key'], 5)), $type); ?> <tr class="<?=Arr::get($submission, 'active') ? ($submission['active'] > 0 ? 'bg-warning' : 'bg-success') : 'bg-danger'?>"> <td><?=date('d-m-Y H:i:s', $submission['update_time'])?></td> <td><?=User::get($submission['user_id'], 'login')?></td> <td><?=isset($submission['version']) ? 'Mobile app (' . ($submission['version'] ? : 'Unknown') . ')' : 'Web-app'?></td> <td><span class="glyphicon glyphicon-<?=Arr::get($submission, 'active') ? ($submission['active'] < 0 ? 'ok text-success' : 'edit text-info') : 'remove text-danger'?>"></span></td> <td><?=Arr::get($submission, 'process_time') ? date('d-m-Y H:i:s', $submission['process_time']) . ' by ' . User::get(Arr::get($submission, 'admin_id'), 'login') : ''?></td> <td><?=Arr::get($companies, User::get($submission['user_id'], 'company_id'))?></td> <td><?=Arr::get($submission, 'location') ? '<a target="_blank" href="https://www.google.com/maps/@' . $submission['location'] . ',19z">Location</a>' : ''?></td> <td><?=HTML::chars($name)?></td> <td class="<?=strlen($value) > 100 ? 'shorten' : ''?>"><?=$value?></td> <td class="<?=strlen($current) > 100 ? 'shorten' : ''?>"><?=$current?></td> <?php if (Group::current('allow_assign')):?>
<td <?=strlen($value['new_value']) > 100 ? 'class="shorten"' : ''?>><?=HTML::chars($date ? date("d-m-Y H:i", $value['new_value'] ? : 0) : $value['new_value'])?></td> </tr> <?php endforeach;?> </table> </td> </tr> <?php else:?> <tr class="<?=Arr::get($classes, $ticket['update_type'])?> text-center"> <td><a href="<?=URL::base() . 'imex/reports?ticket=' . $ticket['job_key']?>"><?=$ticket['job_key']?></a></td> <td nowrap="nowrap"><?=date('d-m-Y H:i', $ticket['update_time'])?></td> <td><?=User::get(Arr::get($ticket, 'user_id'), 'login') ? : 'Unknown'?></td> <td><?=Arr::get($actions, $ticket['update_type'])?></td> <td><a href="<?=URL::base() . 'imex/reports?file=' . urlencode($ticket['filename'])?>"><?=HTML::chars($ticket['filename'])?></a></td> <?php foreach ($reports as $id => $name):?> <td><?=Columns::output(Arr::path($ticket, 'static.'.$id), Columns::get_type($id))?></td> <?php endforeach;?> <td colspan="3">N/A</td> </tr> <?php endif;?> <?php endforeach;?> </table> <div class="col-xs-12 text-center"> <?php if (Pager::pages() > 1) echo $pager = View::factory('Pager');?> </div> <div class="col-xs-12"> <a class="pull-right btn btn-simple" href="<?=URL::base()?>imex/reports/export<?=URL::query()?>"><span class="glyphicon glyphicon-export"></span>Export to CSV</a> </div> <div class="clearfix"> </div> <?php endif;?>
public function action_index() { $id = $this->request->param('id'); $job = Database_Mongo::collection('jobs')->findOne(array('_id' => strval($id))); if (!$job) { throw new HTTP_Exception_404('Not found'); } if (Arr::get($job, 'locked')) { throw new HTTP_Exception_403('Forbidden'); } $job['region'] = DB::select('name')->from('regions')->where('id', '=', $job['region'])->execute()->get('name'); $types = array(); foreach (Arr::get($job, 'assigned', array()) as $type => $company) { if ($company == User::current('company_id')) { $types[] = $type; } } if (!$types) { throw new HTTP_Exception_403('Forbidden'); } $columns_sets = Form::$columns; if (Arr::get($_POST, 'signature') && Arr::get($_POST, 'signed')) { $completed = Arr::get($_POST, 'completed'); $data = Arr::get($_POST, 'data-' . $completed); $submissions = array(); foreach ($columns_sets as $title => $columns) { if ($completed == crc32($title)) { foreach ($columns as $map => $value) { $value = Arr::get($data, $map, ''); if ($value === '') { continue; } $map = explode(',', $map); foreach ($map as $key) { if (is_numeric($key)) { $val = Columns::parse($value, Columns::get_type($key)); $submissions[$key] = $val; } } } if (Arr::get($data, 'ot1') || Arr::get($data, 'ot2')) { $submissions['43'] = '1310-' . Arr::get($data, 'ot1', '') . ' 1625-' . Arr::get($data, 'ot2', ''); } if ($submissions) { $submissions['245'] = strpos($title, 'Yes') === 0 ? 'Yes' : 'No'; } break; } } if (Arr::get($_POST, 'upload-count')) { Messages::save(intval($_POST['upload-count']) . ' file(s) were successfully uploaded', 'success'); } if ($submissions) { $data = array('filename' => 'Submission-' . date('dmY-His') . '-signature.png', 'mime' => 'image/png', 'uploaded' => time(), 'user_id' => User::current('id'), 'job_id' => $id, 'folder' => 'Signatures', 'fda_id' => Arr::path($job, 'data.14'), 'address' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', Arr::path($job, 'data.8'))), '-'), 'title' => ''); Database::instance()->begin(); $result = DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(); $image_id = Arr::get($result, 0); $content = explode(',', Arr::get($_POST, 'signature')); if ($image_id && file_put_contents(DOCROOT . 'storage/' . $image_id, base64_decode(Arr::get($content, 1, '')))) { unset($data['mime']); $data = array('filename' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', 'Signatures / ' . Arr::path($job, 'data.14') . ' / ' . Arr::path($job, 'data.8') . ' / Submission-' . date('dmY-His') . '-signature.png')), '-'), 'uploaded' => time(), 'user_id' => User::current('id'), 'job_id' => $id, 'action' => 1); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); Database::instance()->commit(); $submission = array('job_key' => $id, 'user_id' => User::current('id'), 'update_time' => time()); if (Arr::get($_POST, 'location')) { $submission['location'] = $_POST['location']; } $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); $update = array(); $approval = false; $archive = array(); foreach ($submissions as $key => $value) { if (Columns::get_direct($key)) { if (Arr::path($job, 'data.' . $key) != $value) { if ($value) { $update['$set']['data.' . $key] = $value; } else { $update['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; } $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = -1; $submission['process_time'] = $submission['update_time']; Database_Mongo::collection('submissions')->insert($submission); unset($submission['process_time']); unset($submission['_id']); } else { $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = 1; Database_Mongo::collection('submissions')->insert($submission); unset($submission['_id']); } } if ($status != Enums::STATUS_PENDING) { $status = $update['$set']['status'] = Enums::STATUS_PENDING; } if ($update) { $update['$set']['last_update'] = time(); $update['$set']['last_submit'] = time(); Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); if ($archive) { foreach (Columns::get_static() as $key => $value) { $archive['static'][$key] = Arr::path($job, 'data.' . $key); } $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = time(); $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } } Messages::save("Changes were succesfully submitted. " . ($approval ? 'Manager will review changes and confirm them.' : ''), 'success'); } else { Database::instance()->rollback(); Messages::save("Unable to save signature image! Please, try again in few minutes", 'danger'); } } else { Messages::save("No changes were submitted", 'warning'); } $this->redirect('search'); } $last = Database_Mongo::collection('submissions')->find(array('job_key' => $id, 'active' => array('$exists' => 1), 'user_id' => array('$in' => DB::select('id')->from('users')->where('company_id', '=', User::current('company_id'))->execute()->as_array(NULL, 'id'))))->sort(array('update_time' => 1)); $values = array(); foreach ($last as $submission) { $values[str_replace('.', '', $submission['key'])] = array('status' => Arr::get($submission, 'active', 0), 'value' => $submission['value']); } $view = View::factory('Jobs/Form')->bind('job', $job)->bind('job_values', $values)->bind('columns', $columns_sets); $this->response->body($view); }
public function action_columns() { $list = array(array('id' => 0, 'name' => 'Ticket ID')); foreach (Columns::get_all() as $key => $value) { $column = array('id' => $key, 'name' => $value); $type = Columns::get_type($key); if (substr($type, 0, 4) == 'enum') { $id = substr($type, 5); $column['type'] = Enums::is_multi($id) ? 'multi' : 'enum'; $column['values'] = Enums::get_values($id); } else { $column['type'] = $type ?: 'string'; } $list[] = $column; } die(json_encode($list)); }
public function action_index() { $start = Arr::get($_GET, 'start') ? strtotime($_GET['start']) : strtotime('first day of this month'); $end = Arr::get($_GET, 'end') ? strtotime($_GET['end']) + 86399 : time(); $query = array('financial_time' => isset($_GET['excel']) ? array('$gt' => 0) : array('$exists' => 1), 'update_time' => array('$gt' => intval($start), '$lt' => intval($end))); if (Arr::get($_GET, 'app-start')) { $query['process_time']['$gt'] = strtotime($_GET['app-start']); } if (Arr::get($_GET, 'app-end')) { $query['process_time']['$lt'] = strtotime($_GET['app-end']) + 86399; } if (Arr::get($_GET, 'fin-start')) { $query['financial_time']['$gt'] = strtotime($_GET['fin-start']); } if (Arr::get($_GET, 'fin-end')) { $query['financial_time']['$lt'] = strtotime($_GET['fin-end']) + 86399; } if (Group::current('allow_assign')) { $companies = DB::select('id', 'name')->from('companies')->order_by('name', 'asc')->execute()->as_array('id', 'name'); } if (!Group::current('allow_assign') || Arr::get($_GET, 'company')) { if (Group::current('allow_assign')) { $company = $_GET['company']; if (!is_array($company)) { $company = explode(',', $company); } $company = array_map('intval', $company); } else { $company = array(User::current('company_id')); } $query['user_id'] = array('$in' => DB::select('id')->from('users')->where('company_id', 'IN', $company)->execute()->as_array(NULL, 'id')); } $jobs = array(); if (Arr::get($_GET, 'ticket')) { $tickets = explode(',', $_GET['ticket']); $q = array(); foreach ($tickets as $ticket) { $ticket = preg_replace('/[^a-z0-9]/i', '', strval($ticket)); if (!$ticket) { continue; } if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $q[] = $ticket; } else { $q[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } if (count($q) > 1) { $jobs['_id'] = array('$in' => $q); } elseif ($q) { $jobs['_id'] = $q[0]; } } if (Arr::get($_GET, 'fsa')) { $values = is_array($_GET['fsa']) ? $_GET['fsa'] : explode(',', $_GET['fsa']); $jobs['data.12'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'fsam')) { $values = is_array($_GET['fsam']) ? $_GET['fsam'] : explode(',', $_GET['fsam']); $jobs['data.13'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'fda')) { $values = is_array($_GET['fda']) ? $_GET['fda'] : explode(',', $_GET['fda']); $jobs['data.14'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'address')) { $jobs['data.8'] = new MongoRegex('/.*' . strval($_GET['address']) . '.*/mi'); } if ($jobs) { if (count($jobs) == 1 && isset($jobs['_id'])) { $query['job_key'] = $jobs['_id']; } else { $query['job_key'] = array('$in' => Database_Mongo::collection('jobs')->distinct('_id', $jobs)); } } $sort = array('job_key' => 1); if (!Arr::get($_GET, 'sort')) { $_GET['sort'] = array('-submission'); } foreach ($_GET['sort'] as $s) { $dir = substr($s, 0, 1) == '-' ? -1 : 1; $s = substr($s, 1); switch ($s) { case 'submission': $sort['update_time'] = $dir; break; case 'approval': $sort['process_time'] = $dir; break; case 'financial': $sort['financial_time'] = $dir; break; } } $result = Database_Mongo::collection('submissions')->find($query)->sort($sort); $submissions = array(); $users = array(); $jobs = array(); $keys = array('region' => 1, 'data.8' => 1, 'data.9' => 1, 'data.14' => 1); foreach ($result as $submission) { $jobs[$submission['job_key']] = 1; $keys[$submission['key']] = 1; $submissions[$submission['job_key']][] = $submission; $users[$submission['user_id']] = 1; } $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($jobs))), $keys); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = $job; } if ($users) { User::get(array_keys($users)); } $rates = array(); $result = DB::select()->from('rates')->execute(); foreach ($result as $row) { $rates[$row['company_id']][$row['region_id']][$row['column_id']] = $row['rate']; } $columns = Columns::get_financial(); $approved = array(); $duplicates = array(); $discr = array(); $partial = array(); $full = array(); $skip = array(); if (Group::current('allow_assign') && isset($_GET['approve']) && Arr::get($_GET, 'company') && count($_GET['company']) == 1) { $rates = Arr::get($rates, $_GET['company'][0], array()); foreach ($submissions as $job => $list) { $region = $jobs[$job]['region']; $data = array(); $partial_fl = false; $full_fl = true; $dup_fl = false; $discr_fl = false; $skip_fl = false; foreach ($list as $submission) { $data[$submission['key']][] = $submission; } foreach ($data as $key => $values) { $key = substr($key, 5); $rate = isset($rates[$region][$key]) ? $rates[$region][$key] : (isset($rates[0][$key]) ? $rates[0][$key] : 0); $value = array_shift($values); if (count($values)) { $dup_fl = true; $full_fl = false; } elseif ($value['value'] != Arr::path($jobs, $job . '.data.' . $key)) { $discr_fl = true; $full_fl = false; } elseif (!$rate) { $skip_fl = true; $full_fl = false; } elseif (!$value['financial_time']) { $approved[] = array('id' => $value['_id'], 'rate' => $rate, 'paid' => min(floatval($value['value']), Arr::get($columns, $key))); $partial_fl = true; } } if ($partial_fl) { if ($full_fl) { $full[$job] = 1; } else { $partial[$job] = 1; } } if ($skip_fl) { $skip[$job] = 1; } if ($dup_fl) { $duplicates[$job] = 1; } if ($discr_fl) { $discr[$job] = 1; } $submissions[$job] = $data; } $time = time(); foreach ($approved as $value) { Database_Mongo::collection('submissions')->update(array('_id' => $value['id']), array('$set' => array('paid' => $value['paid'], 'rate' => $value['rate'], 'financial_time' => $time))); } $count = count($jobs); $jobs = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($jobs)))); foreach ($jobs as $job) { Utils::calculate_financial($job); } Messages::save(sprintf('%d/%d tickets were successfully approved.', count($full), $count), 'success'); if ($partial) { Messages::save(sprintf('%d tickets were partially approved.', count($partial)), 'warning'); } if ($discr) { Messages::save(sprintf('%d tickets contain discrepancies.', count($discr)), 'danger'); } if ($duplicates) { Messages::save(sprintf('%d tickets contain duplicates.', count($duplicates)), 'danger'); } if ($skip) { Messages::save(sprintf('%d tickets contain submissions with unknown rates.', count($skip)), 'danger'); } $this->redirect($this->request->uri() . URL::query(array('approve' => NULL))); } elseif (isset($_GET['export'])) { $discr = isset($_GET['discrepancy']); header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $file = tmpfile(); $headers = array('Tickets ID', 'Submission Date', 'Approval Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } $headers[] = 'Column'; $headers[] = 'Value'; $keys = array(); fputcsv($file, $headers); foreach ($submissions as $job => $list) { foreach ($list as $submission) { if (!$discr || Arr::path($jobs, $job . '.' . $submission['key']) != $submission['value']) { $keys[$submission['key']] = 1; $key = substr($submission['key'], 5); $data = array($job, date('d-m-Y H:i', $submission['update_time']), Arr::get($submission, 'process_time') ? date('d-m-Y H:i', $submission['process_time']) : '', User::get($submission['user_id'], 'login')); if (Group::current('allow_assign')) { $data[] = Arr::get($companies, User::get($submission['user_id'], 'company_id'), 'Unknown'); } $data[] = Columns::get_name($key); $data[] = Columns::output($submission['value'], Columns::get_type($key), true); fputcsv($file, $data); } } } fseek($file, 0); fpassthru($file); fclose($file); die; } elseif (isset($_GET['excel'])) { $doc = PHPExcel_IOFactory::load(DOCROOT . 'financial.template.xlsx'); $sheet = $doc->getSheet(); $sheet->setTitle(date('d-m-Y', $start) . ' - ' . date('d-m-Y', $end)); $s = date('M Y', $start); $e = date('M Y', $end); if ($s == $e) { $month = $s; } else { $month = $s . ' - ' . $e; } $map = array('G' => '190', 'H' => '191', 'I' => '192', 'J' => '193', 'K' => '194', 'L' => '195', 'M' => '196', 'N' => '197', 'O' => '198', 'P' => '199', 'Q' => '200', 'R' => '201', 'S' => '202', 'T' => '203', 'U' => '204', 'V' => '205', 'W' => '206', 'X' => '207', 'Y' => '208', 'Z' => '43', 'AA' => '257'); $i = 10; $total = array(); $amount = array(); $users = array(); if (count($submissions)) { foreach (array_merge($map, array('AC' => 0, 'AD' => 0)) as $column => $key) { $sheet->setCellValue($column . '7', '=SUM(' . $column . '11:' . $column . (count($submissions) + 10) . ')'); } $styleArray = array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN))); $sheet->getStyle('B11:AD' . (count($submissions) + 10))->applyFromArray($styleArray); } foreach ($submissions as $job => $list) { $job = Database_Mongo::collection('jobs')->findOne(array('_id' => $job)); $data = Arr::get($job, 'data'); $inst_min = false; $inst_max = false; $app_min = false; $app_max = false; $result = array(); $rate = array(); foreach ($list as $submission) { $users[$submission['user_id']] = 1; $inst = $submission['update_time']; if ($inst_min === false || $inst_min > $inst) { $inst_min = $inst; } if ($inst_max === false || $inst_max < $inst) { $inst_max = $inst; } $app = Arr::get($submission, 'process_time', false); if ($app_min === false && $app !== false || $app_min > $app) { $app_min = $app; } if ($app_max === false && $app !== false || $app_max < $app) { $app_max = $app; } $key = substr($submission['key'], 5); $result[$key] = $submission['paid']; $rate[$key] = $submission['rate']; } $sum = 0; foreach ($result as $key => $value) { $sum += $value * $rate[$key]; $total[$key] = Arr::get($total, $key, 0) + $value * $rate[$key]; $amount[$key] = Arr::get($amount, $key, 0) + $value; } $i++; $inst_min = date('d-m-Y', $inst_min); $inst_max = date('d-m-Y', $inst_max); $app_min = date('d-m-Y', $app_min); $app_max = date('d-m-Y', $app_max); $sheet->setCellValue('B' . $i, $inst_min == $inst_max ? $inst_min : $inst_min . ' - ' . $inst_max)->setCellValue('C' . $i, $app_min == $app_max ? $app_min : $app_min . ' - ' . $app_max)->setCellValue('D' . $i, $job['_id'])->setCellValue('E' . $i, Arr::get($data, 8))->setCellValue('F' . $i, Arr::get($data, 14))->setCellValue('AC' . $i, $sum); foreach ($map as $key => $value) { $sheet->setCellValue($key . $i, Arr::get($result, $value) ? $result[$value] : ''); } } foreach ($map as $key => $value) { $sheet->setCellValue($key . '10', Arr::get($amount, $value) ? $total[$value] / $amount[$value] : ''); } if ($users) { $companies = DB::select('name')->from('companies')->where('id', 'IN', DB::select('company_id')->distinct(true)->from('users')->where('id', 'IN', array_keys($users)))->execute()->as_array(NULL, 'name'); } else { $companies = array('None'); } $sheet->setCellValue('D2', implode(', ', $companies))->setCellValue('D3', $month); header('Content-type: application/xlsx'); header('Content-disposition: filename="Report.xlsx"'); $name = tempnam(sys_get_temp_dir(), 'excel'); $writer = new PHPExcel_Writer_Excel2007($doc); $writer->setPreCalculateFormulas(true); $writer->save($name); readfile($name); unlink($name); die; } $discrepancies = array(); foreach ($submissions as $job => $list) { $fl = false; foreach ($list as $submission) { if (Arr::path($jobs, $job . '.' . $submission['key']) != $submission['value']) { $fl = true; break; } } if (!$fl) { $discrepancies[$job] = 1; } } $view = View::factory("Reports/Financial")->set('approve_all', $start > 0 && date('m', $start) == date('m', $end) && Arr::get($_GET, 'company') && count($_GET['company']) == 1 && !Arr::get($_GET, 'fin-start'))->bind('companies', $companies)->bind('submissions', $submissions)->bind('discrepancies', $discrepancies)->bind('columns', $columns)->bind('jobs', $jobs)->bind('rates', $rates); $this->response->body($view); }
public function action_index() { $action = $this->request->param('id'); $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { throw new HTTP_Exception_404('Not found'); } $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $query = array(); if ($regs) { $query['region'] = array('$in' => array_values($regs)); } if (!Group::current('show_all_jobs')) { $query['$or'] = array(array('ex' => intval(User::current('company_id'))), array('companies' => intval(User::current('company_id')))); } $query['_id'] = array('$in' => $ids); $jobs = Database_Mongo::collection('jobs')->find($query); $static = array_flip(explode(',', Group::current('columns'))); $header = array('Ticket ID'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($action == 'excel') { $result = DB::select()->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->and_where('folder', '<>', 'Signatures')->execute()->as_array(); $attachments = array(); $attachments_list = array(); foreach ($result as $row) { $attachments[$row['job_id']] = Arr::get($attachments, $row['job_id'], 0) + 1; if (preg_match('/^image\\/.*$/i', $row['mime'])) { $attachments_list[$row['job_id']][] = $row['id']; } } } elseif ($ids && $static['attachments']) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->and_where('folder', '<>', 'Signatures')->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } if (isset($static['last_update'])) { $header[] = 'Last update'; } if (isset($static['last_submit'])) { $header[] = 'Last submit'; } if (isset($static['status']) && Group::current('show_all_jobs')) { $header[] = 'Job status'; } if (isset($static['types'])) { $header[] = 'Assigned works'; } if (isset($static['companies'])) { $header[] = 'Assigned companies'; } if (isset($static['ex'])) { $header[] = 'Previously assigned companies'; } if (isset($static['pending'])) { $header[] = 'Pending submissions'; } if (isset($static['attachments'])) { $header[] = 'Attachments'; } foreach (Columns::get_search() as $id => $type) { $header[] = Columns::get_name($id); } if ($action == 'excel') { $excel = new PHPExcel(); $sheet = $excel->getActiveSheet(); $sheet->setTitle('Search Results'); $sheet->fromArray($header, NULL, 'A1'); $i = 1; foreach (range('A', $sheet->getHighestDataColumn()) as $col) { $sheet->getColumnDimension($col)->setAutoSize(true); } $x = $attachments_list ? max(array_map('count', $attachments_list)) : 0; foreach (range(0, $x - 1) as $col) { $sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex(count($header) + $col))->setWidth(14); } } else { $file = tmpfile(); fputcsv($file, $header); } foreach ($jobs as $ticket) { $row = array($ticket['_id']); if (isset($static['last_update'])) { $row[] = date('d-m-Y H:i', Arr::get($ticket, 'last_update', $ticket['created'])); } if (isset($static['last_submit'])) { $row[] = Arr::get($ticket, 'last_submit') ? date('d-m-Y H:i', $ticket['last_submit']) : ''; } if (isset($static['status']) && Group::current('show_all_jobs')) { $row[] = Arr::get(Enums::$statuses, Arr::get($ticket, 'status', 0), 'Unknown'); } if (isset($static['types'])) { if (Group::current('allow_assign')) { $row[] = implode(', ', array_intersect_key($types, Arr::get($ticket, 'assigned', array()))); } else { $row[] = implode(', ', array_intersect_key($types, array_filter(Arr::get($ticket, 'assigned', array()), function ($x) { return $x == User::current('company_id'); }))); } } if (isset($static['companies'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'assigned', array())))); } if (isset($static['ex'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'ex', array())))); } if (isset($static['pending'])) { $row[] = Arr::get($submissions, $ticket['_id']); } if (isset($static['attachments'])) { $row[] = Arr::get($attachments, $ticket['_id']); } foreach (Columns::get_search() as $id => $type) { $row[] = Arr::path($ticket, array('data', $id)) ? Columns::output($ticket['data'][$id], Columns::get_type($id), true) : ''; } if ($action == 'excel') { $i++; $sheet->fromArray($row, NULL, 'A' . $i); $x = count($row); if (isset($attachments_list[$ticket['_id']])) { $sheet->getRowDimension($i)->setRowHeight(80); } foreach (Arr::get($attachments_list, $ticket['_id'], array()) as $image) { if (!file_exists(DOCROOT . 'storage/' . $image . '.thumb')) { if (!file_exists(DOCROOT . 'storage/' . $image)) { continue; } $data = file_get_contents(DOCROOT . 'storage/' . $image); $er = error_reporting(0); $img = imagecreatefromstring($data); error_reporting($er); if (!$img) { continue; } $x = imagesx($img); $y = imagesy($img); $size = max($x, $y); $x = round($x / $size * 96); $y = round($y / $size * 96); $thumb = imagecreatetruecolor($x, $y); imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled($thumb, $img, 0, 0, 0, 0, $x, $y, imagesx($img), imagesy($img)); imagepng($thumb, DOCROOT . 'storage/' . $image . '.thumb', 9); } $data = file_get_contents(DOCROOT . 'storage/' . $image . '.thumb'); $img = imagecreatefromstring($data); $objDrawing = new PHPExcel_Worksheet_MemoryDrawing(); $objDrawing->setImageResource($img); $coord = PHPExcel_Cell::stringFromColumnIndex($x++); $objDrawing->setCoordinates($coord . $i); $objDrawing->setOffsetY(5); $objDrawing->setResizeProportional(true); $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG); $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT); $objDrawing->setWorksheet($sheet); //$sheet->getCell($coord . $i)->setHyperlink(new PHPExcel_Cell_Hyperlink(URL::site('download/attachment/' . $image, 'http'), 'Show Image')); } } else { fputcsv($file, $row); } } if ($action == 'excel') { $name = tempnam(sys_get_temp_dir(), 'excel'); header('Content-type: application/xlsx'); header('Content-disposition: filename="SearchResults.xlsx"'); $writer = new PHPExcel_Writer_Excel2007($excel); $writer->save($name); readfile($name); unlink($name); } else { fseek($file, 0); header('Content-type: text/csv'); header('Content-disposition: filename="SearchResults.csv"'); fpassthru($file); fclose($file); } die; }
<label><?=$key?>:</label> <div class="width100"> <table class="flatten-sub-table width100"> <?php foreach ($values as $id => $value): $index = 0; $status = Arr::path($job_values, 'data' . intval($id) . '.status'); $old = Arr::path($job_values, 'data' . intval($id) . '.value'); ?> <?php if (0 == $index++ % 2) :?> <tr> <?php endif; ?> <td> <div class="status-cell <?=$status === -1 ? 'bg-success has-success' : ($status === 1 ? 'bg-warning has-warning' : '')?>"> <label class=""><?=$value?>:</label> <?php if ($old):?> <br/><label class="old_value control-label <?=strlen($old) > 100 ? 'shorten' : ''?> " >Last submitted value: <span class=""><?=Columns::output($old, Columns::get_type(intval($id)))?></span></label> <?php endif;?> <p class="column-value"> <?=Columns::input('data-' . crc32($key) . '[' . $id . ']', NULL, Columns::get_type(intval($id)), $id == 242 ? Arr::path($job, 'data.242') : '', '', Arr::path(Form::$required, array($key, $id)))?> </p> </div> </td> <?php if (0 == $index % 2) :?> </tr> <?php endif; ?> <?php endforeach;?> </table> </div> </td> <?php if ($tdIndex++==0 && sizeof($columns) == $index): ?> <td class="extra"></td> <?php endif; ?>
public function action_index() { define('JOB_STATUS_COLUMN', 'data.44'); if (!rand(0, 1000)) { Database_Mongo::collection('search')->remove(array('expires' => array('$lt' => time()))); } if ($_POST) { $data = array('request' => $_POST, 'expires' => time() + 3600); Database_Mongo::collection('search')->insert($data); $search_id = strval($data['_id']); Session::instance()->set('search-settings', $search_id); header('Content-type: application/json'); die(json_encode(array('success' => true, 'id' => $search_id))); } elseif (!$_GET && Session::instance()->get('search-settings')) { $params = Session::instance()->get('search-settings'); if (!is_array($params)) { $params = json_decode($params); } if ($params !== NULL) { $data = array('request' => $params, 'expires' => time() + 3600); Database_Mongo::collection('search')->insert($data); $search_id = strval($data['_id']); Session::instance()->set('search-settings', $search_id); } else { $search_id = Session::instance()->get('search-settings'); } if ($search_id) { $this->redirect('search?id=' . $search_id); } } elseif (isset($_GET['clear'])) { Session::instance()->delete('search-settings'); $_GET = array(); } elseif (isset($_GET['id'])) { $data = Database_Mongo::collection('search')->findOne(array('_id' => new MongoId($_GET['id']))); if ($data) { Session::instance()->set('search-settings', $_GET['id']); $_GET = $data['request']; } else { Session::instance()->delete('search-settings'); } } $actions = array('contain', 'does not contain', '=', '<', '<=', '>', '>=', '<>', 'empty', 'not empty'); $actions_mongo = array('=' => '$eq', '<' => '$lt', '<=' => '$lte', '>' => '$gt', '>=' => '$gte', '<>' => '$ne'); $columns = Columns::get_visible(); $reports = Columns::get_static(); $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $regions = DB::select('id', 'name')->from('regions'); if ($regs) { $regions->where('id', 'IN', $regs); } $regions = $regions->execute()->as_array('id', 'name'); $query = array(); $list_query = array(); if (Arr::get($_GET, 'submissions')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('active' => 1)), array('$group' => array('_id' => '$job_key')))); $result = array_column($result['result'], '_id'); $query['_id'] = array('$in' => $result); } if (Group::current('show_all_jobs')) { if (Arr::get($_GET, 'company')) { $companies = explode(',', $_GET['company']); if (count($companies) > 1) { $companies = array('$in' => array_map("intval", $companies)); } else { $companies = intval($_GET['company']); } if (Arr::get($_GET, 'ex')) { $list_query['$or'] = $query['$or'] = array(array('companies' => $companies), array('ex' => $companies)); } else { $list_query['companies'] = $query['companies'] = $companies; } } } else { if (Arr::get($_GET, 'status', -1) == 0) { $list_query['ex'] = $query['ex'] = intval(User::current('company_id')); } else { $list_query['companies'] = $query['companies'] = intval(User::current('company_id')); } } if (Group::current('allow_assign')) { foreach (Columns::$settings as $key => $value) { if (isset($_GET['settings'][$key])) { if ($_GET['settings'][$key]) { $query[$key] = array('$exists' => 1); } else { $query[$key] = array('$exists' => 0); } } } } if (Arr::get($_GET, 'ticket')) { $tickets = explode(',', $_GET['ticket']); $q = array(); foreach ($tickets as $ticket) { $ticket = preg_replace('/[^a-z0-9]/i', '', strval($ticket)); if (!$ticket) { continue; } if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $q[] = $ticket; } else { $q[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } if (count($q) > 1) { $query['_id'] = array('$in' => $q); } elseif ($q) { $query['_id'] = $q[0]; } } if (Arr::get($_GET, 'start')) { $query['last_update']['$gt'] = Columns::parse($_GET['start'], 'date'); } if (Arr::get($_GET, 'end')) { $query['last_update']['$lt'] = Columns::parse($_GET['end'], 'date'); } if (Arr::get($_GET, 'submit-start')) { $query['last_submit']['$gt'] = Columns::parse($_GET['submit-start'], 'date'); } if (Arr::get($_GET, 'submit-end')) { $query['last_submit']['$lt'] = Columns::parse($_GET['submit-end'], 'date'); } $status = Arr::get($_GET, 'status', Group::current('allow_assign') ? -1 : Enums::STATUS_ALLOC); $_GET['status'] = intval($status); if ($status != -1 && (Group::current('show_all_jobs') || $status)) { $query['status'] = intval($status) ?: array('$exists' => 0); } if (!isset($_GET['region'])) { $_GET['region'] = User::current('default_region'); } if (Arr::get($_GET, 'region') && (!$regs || isset($regs[$_GET['region']]))) { $list_query['region'] = $query['region'] = strval($_GET['region']); } elseif ($regs) { $list_query['region'] = $query['region'] = array('$in' => array_values($regs)); } if (Arr::get($_GET, 'type')) { $query['assigned.' . $_GET['type']] = array('$exists' => 1); } foreach (Arr::get($_GET, 'columns', array()) as $id => $column) { if ($column) { $op = Arr::get($actions, Arr::path($_GET, 'actions.' . $id), 0); $value = Arr::path($_GET, 'values.' . $id, ''); $value = Columns::parse($value, Columns::get_type($column)); if (substr(Columns::get_type($column), 0, 4) == 'enum' && Enums::is_multi(substr(Columns::get_type($column), 5)) && $op == '=' && $value) { $op = 'contain'; } if (Columns::get_type($column) == 'date') { if ($op === 'contain') { $op = '='; } elseif ($op === 'does not contain') { $op = '<>'; } elseif ($op == '<=') { $value += 86399; } if ($op == '=' && date('His', $value) === '000000') { $query['data.' . $column]['$lte'] = $value + 86399; $op = '>='; } } if ($op === 'contain') { $op = '$eq'; $value = new MongoRegex('/.*' . preg_replace('/[^a-z0-9,.+:;!? -]/i', '', $value) . '.*/i'); } elseif ($op === 'does not contain') { $op = '$not'; $value = new MongoRegex('/.*' . preg_replace('/[^a-z0-9,.+:;!? -]/i', '', $value) . '.*/i'); } elseif ($op === 'empty') { $op = '$in'; $value = array(NULL, '', 0); } elseif ($op === 'not empty') { $op = '$nin'; $value = array(NULL, '', 0); } else { $op = Arr::get($actions_mongo, $op, '$eq'); } if ($op == '$eq' && !$value) { $value = NULL; } if (isset($query['data.' . $column]) && $op == '$eq') { if (isset($query['data.' . $column]['$in'])) { $query['data.' . $column]['$in'][] = $value; } elseif (isset($query['data.' . $column]['$eq'])) { $query['data.' . $column] = array('$in' => array($query['data.' . $column]['$eq'], $value)); } else { $query['data.' . $column]['$in'] = array($value); } } else { $query['data.' . $column][$op] = $value; } } } foreach ($query as $key => $ops) { if (substr($key, 0, 5) == 'data.' && count($ops) == 1 && key($ops) == '$eq') { $query[$key] = array_shift($ops); } } $jobs = Database_Mongo::collection('jobs'); $list_values = array(); foreach (Columns::get_search() as $key => $value) { if ($value == 2) { $list_values[$key] = $jobs->distinct('data.' . $key, $query ?: NULL) ?: array(); if (substr(Columns::get_type($key), 0, 4) == 'enum') { $list = array(); foreach ($list_values[$key] as $values) { if ($values) { $values = explode(', ', $values); foreach ($values as $value) { $list[$value] = 1; } } else { $list[''] = 1; } } $list_values[$key] = array_keys($list); } if ($list_values[$key]) { sort($list_values[$key]); } } } Pager::$count = $jobs->count($query); $show = array('region' => 1, 'status' => 1, 'created' => 1, 'last_update' => 1, 'last_submit' => 1, 'assigned' => 1, 'companies' => 1, 'ex' => 1); foreach (Columns::$settings as $key => $value) { $show[$key] = 1; } foreach (Columns::get_search() as $column => $type) { $show['data.' . $column] = 1; } $result = $jobs->find($query, $show); $sort = Arr::get($_GET, 'sort'); if (is_array($sort)) { $save = implode('|', $sort); DB::update('users')->set(array('default_sort' => $save))->where('id', '=', User::current('id'))->execute(); } else { $sort = explode('|', User::current('default_sort') ?: '-update'); $_GET['sort'] = $sort; } $sorting = array(); foreach ($sort as $order) { $dir = substr($order, 0, 1) == '-' ? -1 : 1; $order = substr($order, 1); switch ($order) { case 'id': $order = '_id'; break; case 'update': $order = 'last_update'; break; case 'submit': $order = 'last_submit'; break; case 'status': $order = 'status'; break; case 'data-8': $sorting['address'] = $dir; $order = false; break; default: if (substr($order, 0, 5) == 'data-') { $order = 'data.' . intval(substr($order, 5)); } else { $order = false; } } if ($order) { $sorting[$order] = $dir; } } $result->sort($sorting)->limit(Pager::limit())->skip(Pager::offset()); $tickets = array(); while ($row = $result->next()) { $tickets[] = $row; } $ids = array_column($tickets, '_id'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($ids) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->and_where('folder', '<>', 'Signatures')->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } $result = Database_Mongo::collection('forms')->find(array(), array('_id' => 1, 'type' => 1, 'name' => 1)); $forms = array(); foreach ($result as $form) { $forms[$form['type']][strval($form['_id'])] = $form['name']; } $view = View::factory('Jobs/Search')->bind('reports', $reports)->bind('regions', $regions)->bind('columns', $columns)->bind('actions', $actions)->bind('tickets', $tickets)->bind('submissions', $submissions)->bind('attachments', $attachments)->bind('list_values', $list_values)->bind('types', $types)->bind('sort', $sort)->bind('companies', $companies)->bind('forms', $forms)->bind('query', $query); $this->response->body($view); }
public function action_index() { $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { throw new HTTP_Exception_404('Not found'); } $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $query = array(); if ($regs) { $query['region'] = array('$in' => array_values($regs)); } if (!Group::current('show_all_jobs')) { $query['$or'] = array(array('ex' => intval(User::current('company_id'))), array('companies' => intval(User::current('company_id')))); } $query['_id'] = array('$in' => $ids); $jobs = Database_Mongo::collection('jobs')->find($query); $static = array_flip(explode(',', Group::current('columns'))); $header = array('Ticket ID'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($ids) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } if (isset($static['last_update'])) { $header[] = 'Last update'; } if (isset($static['last_submit'])) { $header[] = 'Last submit'; } if (isset($static['status']) && Group::current('show_all_jobs')) { $header[] = 'Job status'; } if (isset($static['types'])) { $header[] = 'Assigned works'; } if (isset($static['companies'])) { $header[] = 'Assigned companies'; } if (isset($static['pending'])) { $header[] = 'Pending submissions'; } if (isset($static['attachments'])) { $header[] = 'Attachments'; } foreach (Columns::get_search() as $id => $type) { $header[] = Columns::get_name($id); } $list = array(); $request = array(); foreach ($jobs as $job) { if (!isset($job['lat'])) { $request[$job['_id']] = Arr::path($job, 'data.8'); } } if ($request) { $coords = MapQuest::locate($request); foreach ($coords as $key => $value) { Database_Mongo::collection('jobs')->update(array('_id' => $key), array('$set' => array('lng' => $value['lng'], 'lat' => $value['lat']))); } } else { $coords = array(); } foreach ($jobs as $ticket) { if (isset($coords[$ticket['_id']])) { $ticket['lng'] = $coords[$ticket['_id']]['lng']; $ticket['lat'] = $coords[$ticket['_id']]['lat']; } if (!isset($ticket['lat'])) { continue; } $row = array($ticket['_id']); if (isset($static['last_update'])) { $row[] = date('d-m-Y H:i', Arr::get($ticket, 'last_update', $ticket['created'])); } if (isset($static['last_submit'])) { $row[] = Arr::get($ticket, 'last_submit') ? date('d-m-Y H:i', $ticket['last_submit']) : ''; } if (isset($static['status']) && Group::current('show_all_jobs')) { $row[] = Arr::get(Enums::$statuses, Arr::get($ticket, 'status', 0), 'Unknown'); } if (isset($static['types'])) { if (Group::current('allow_assign')) { $row[] = implode(', ', array_intersect_key($types, Arr::get($ticket, 'assigned', array()))); } else { $row[] = implode(', ', array_intersect_key($types, array_filter(Arr::get($ticket, 'assigned', array()), function ($x) { return $x == User::current('company_id'); }))); } } if (isset($static['companies'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'assigned', array())))); } if (isset($static['pending'])) { $row[] = Arr::get($submissions, $ticket['_id']); } if (isset($static['attachments'])) { $row[] = Arr::get($attachments, $ticket['_id']); } foreach (Columns::get_search() as $id => $type) { $row[] = Arr::path($ticket, array('data', $id)) ? Columns::output($ticket['data'][$id], Columns::get_type($id), true) : ''; } $row = array_combine($header, $row); $row['lat'] = $ticket['lat']; $row['lng'] = $ticket['lng']; $list[] = $row; } if (!$list) { throw new HTTP_Exception_404('Not found'); } DB::delete('maps')->where('expire', '<', time())->execute(); $ids = DB::select('map_id')->distinct(true)->from('maps')->execute()->as_array('map_id', 'map_id'); do { $id = Text::random('alnum', 32); } while (isset($ids[$id])); $expire = time() + 60 * 60 * 24 * 7; $query = DB::insert('maps', array('map_id', 'job_key', 'lng', 'lat', 'info', 'expire')); foreach ($list as $values) { $lat = $values['lat']; $lng = $values['lng']; unset($values['lng']); unset($values['lat']); $query->values(array($id, $values['Ticket ID'], $lng, $lat, json_encode($values), $expire)); } $query->execute(); $this->redirect('/map.html#' . $id); }
public function action_index() { if (!Group::current('allow_reports')) { throw new HTTP_Exception_403('Forbidden'); } $regions = DB::select('id', 'name')->from('regions')->execute()->as_array('id', 'name'); $groups = DB::select('id', 'name')->from('groups')->execute()->as_array('id', 'name'); $form = new Form(); $form->add('region', 'Region', Form::SELECT, array('' => 'Please, select region') + $regions, array('not_empty')); if (isset($_POST['group']) && isset($_POST['csv'])) { if (isset($_POST['job'])) { $query = array('_id' => array('$in' => array_keys($_POST['job']))); $region = array('name' => 'PARTIAL'); } else { if (!Arr::get($_POST, 'region')) { throw new HTTP_Exception_404('Not found'); } $region = DB::select('id', 'name')->from('regions')->where('id', '=', $_POST['region'])->execute()->current(); $query = array('region' => $region['id']); } $jobs = Database_Mongo::collection('jobs')->find($query); $columns = array(); $csv = $_POST['csv']; if ($_POST['group']) { if (Group::get($_POST['group'], 'is_admin')) { $result = DB::select('id')->from('job_columns')->execute()->as_array(NULL, 'id'); } else { $result = DB::select('column_id')->from('group_columns')->where('group_id', '=', $_POST['group'])->and_where('permissions', '>', 0)->execute()->as_array(NULL, 'column_id'); } if ($csv != 'none') { foreach ($result as $column) { if ($csv == 'all' || !($csv == 'csv' xor Columns::get_csv($column))) { $columns[$column] = Columns::get_name($column); } } } } else { foreach (Arr::get($_POST, 'columns', array()) as $column => $value) { $columns[$column] = Columns::get_name($column); } } header("Content-type: text/csv"); header('Content-disposition: filename="' . date('Ymd') . '_EXEL_' . $region['name'] . '_EOD.csv"'); $file = tmpfile(); fputcsv($file, array(0 => 'Ticket Of Work') + $columns); while ($job = $jobs->next()) { $data[0] = $job['_id']; $i = 1; foreach ($columns as $key => $value) { $data[$i++] = iconv("CP1251", 'CP1251//ignore', Columns::output(Arr::get($job['data'], $key, ''), Columns::get_type($key), true)); } fputcsv($file, $data); } rewind($file); fpassthru($file); fclose($file); die; } $view = View::factory('Jobs/Export')->bind('regions', $regions)->bind('groups', $groups); $this->response->body($view); }
public function action_export() { $id = $this->request->param('id'); if ($id == 'all') { ini_set('memory_limit', '512M'); } header('Content-type: text/csv'); header('Content-disposition: filename=Discrepancy.csv'); $result = $this->get_results($id == 'all'); $reports = array(); foreach (Columns::get_static() as $column => $value) { $reports[$column] = Columns::get_name($column); } $file = tmpfile(); $data = array('Ticket ID', 'Date', 'File name'); foreach ($reports as $id => $name) { $data[] = $name; } $data[] = "Column name"; $data[] = "Old value"; $data[] = "New value"; $data[] = 'Current value'; fputcsv($file, $data); foreach ($result as $ticket) { if (!$ticket['discr'] || !isset($_GET['discrepancy'])) { $ticket['update_time'] = date('d-m-Y H:i', $ticket['update_time']); $data = array($ticket['job_key'], $ticket['update_time'], $ticket['filename']); foreach ($reports as $id => $name) { $data[] = Arr::path($ticket, 'static.' . $id); } $base = $data; foreach ($ticket['data'] as $key => $value) { if (!isset($_GET['discrepancy']) || $ticket['current'][$key] != $value['old_value']) { $data = $base; $date = Columns::get_type($key) == 'date'; $data[] = Columns::get_name($key); $data[] = $date && $value['old_value'] ? date('d-m-Y H:i', $value['old_value']) : $value['old_value']; $data[] = $date && $value['new_value'] ? date('d-m-Y H:i', $value['new_value']) : $value['new_value']; $current = $ticket['current'][$key]; $data[] = $date && $current ? date('d-m-Y H:i', $current) : $current; fputcsv($file, $data); } } } } rewind($file); fpassthru($file); die; }
<th class="sortable" data-id="submission">Submission date</th> <th class="sortable" data-id="approval">Approval date</th> <th>User</th> <?php if (Group::current('allow_assign')):?><th>Company</th><?php endif;?> <th>Column</th> <th>Value</th> </tr> <?php foreach ($submissions as $job => $list):?> <tr class="text-center"> <th colspan="<?=(Group::current('allow_assign')) ? 6 : 5?>"> <a href="<?=URL::base()?>search/view/<?=$job?>"><?=$job?>, <?=Arr::path($jobs, array($job, 'l'))?>, <?=Arr::path($jobs, array($job, 'f'))?>, <?=Arr::path($jobs, array($job, 'a'))?></a> </th> </tr> <?php foreach ($list as $submission): $key = substr($submission['key'], 5); $status = Arr::get($submission, 'active', 0);?> <tr class="text-center <?=$status > 0 ? 'yellow' : ($status < 0 ? 'lgreen' : 'rose')?>"> <td><?=date('d-m-Y H:i', $submission['update_time'])?></td> <td><?=isset($submission['process_time']) ? date('d-m-Y H:i', $submission['process_time']) : ''?></td> <td><?=User::get($submission['user_id'], 'login')?></td> <?php if (Group::current('allow_assign')):?><td><?=Arr::get($companies, User::get($submission['user_id'], 'company_id'), 'Unknown')?></td><?php endif;?> <td><?=Columns::get_name($key)?></td> <td class="<?=strlen(Columns::output($submission['value'], Columns::get_type($key))) > 2 ? 'shorten' : ''?>"><?=Columns::output($submission['value'], Columns::get_type($key))?></td> </tr> <?php endforeach;?> <?php endforeach;?> </table> <div class="upload-buttons text-right"> <a href="?export&company=<?=Arr::get($_GET, 'company')?>&start=<?=Arr::get($_GET, 'start', date('d-m-Y', $week))?>&end=<?=Arr::get($_GET, 'end', date('d-m-Y'))?>" class="btn btn-info"><span class="glyphicon glyphicon-export"></span> Export</a> <a href="?export2&company=<?=Arr::get($_GET, 'company')?>&start=<?=Arr::get($_GET, 'start', date('d-m-Y', $week))?>&end=<?=Arr::get($_GET, 'end', date('d-m-Y'))?>" class="btn btn-primary"><span class="glyphicon glyphicon-export"></span> Export grouped</a> <a href="?export3&company=<?=Arr::get($_GET, 'company')?>&start=<?=Arr::get($_GET, 'start', date('d-m-Y', $week))?>&end=<?=Arr::get($_GET, 'end', date('d-m-Y'))?>" class="btn btn-warning"><span class="glyphicon glyphicon-export"></span> Export all columns</a> </div>
<?php if (isset($columns['attachments'])):?> <td class="hidden-xs"> <strong><?=Arr::get($attachments, $ticket['_id'])?></strong> <?php if (Group::current('allow_assign') && isset($ticket['download-by'])):?> <small><br/>Last download:<br/> <?=User::get($ticket['download-by'], 'login')?><br/> <?=Arr::get($companies, User::get($ticket['download-by'], 'company_id'))?> <?php if (isset($ticket['download-at'])):?> <br/><?=date('d-m-Y H:i', $ticket['download-at'])?> <?php endif;?> </small> <?php endif;?> </td> <?php endif;?> <?php foreach (Columns::get_search() as $id => $name): $value = Columns::output(Arr::path($ticket, 'data.'.$id), Columns::get_type($id));?> <td <?=strlen($value) > 100 ? 'class="shorten data-editable-cell"' : 'data-editable-cell'?>><?=$value?></td> <?php endforeach;?> <td class="table-buttons"> <?php if (Group::current('allow_forms') && !Arr::get($ticket, 'locked') && in_array(User::current('company_id'), Arr::get($ticket, 'assigned', array(), true))):?> <a href="<?=URL::base()?>search/form/<?=$ticket['_id']?>" class="btn btn-success col-xs-12" data-toggle="tooltip" data-placement="top"><span class="glyphicon glyphicon-list-alt"></span> Submit data</a> <?php endif;?> <a href="<?=URL::base()?>search/view/<?=$ticket['_id']?>" class="btn btn-info col-xs-12" data-toggle="tooltip" data-placement="top"><span class="glyphicon glyphicon-search"></span> View</a> <?php if (Group::current('allow_reports')):?> <a href="<?=URL::base()?>imex/reports?ticket=<?=$ticket['_id']?>" class="btn btn-warning col-xs-12" data-toggle="tooltip" data-placement="top"><span class="glyphicon glyphicon-list"></span> Reports</a> <a href="<?=URL::base()?>assign?ticket=<?=$ticket['_id']?>" class="btn btn-primary col-xs-12" data-toggle="tooltip" data-placement="top"><span class="glyphicon glyphicon-pencil"></span> Assign logs</a> <?php endif;?> <?php if (Group::current('allow_submissions')):?>
public function action_index() { $start = Arr::get($_GET, 'start') ? strtotime($_GET['start']) : strtotime('this week', strtotime('this week') > time() ? strtotime('yesterday') : time()); $end = Arr::get($_GET, 'end') ? strtotime($_GET['end']) + 86399 : time(); $query = array('update_time' => array('$gt' => intval($start), '$lt' => intval($end))); if (Arr::get($_GET, 'app-start')) { $query['process_time']['$gt'] = strtotime($_GET['app-start']); } if (Arr::get($_GET, 'app-end')) { $query['process_time']['$lt'] = strtotime($_GET['app-end']) + 86399; } if (Group::current('allow_assign')) { $companies = DB::select('id', 'name')->from('companies')->order_by('name', 'asc')->execute()->as_array('id', 'name'); } if (!Group::current('allow_assign') || Arr::get($_GET, 'company')) { $query['user_id'] = array('$in' => DB::select('id')->from('users')->where('company_id', '=', Group::current('allow_assign') ? $_GET['company'] : User::current('company_id'))->execute()->as_array(NULL, 'id')); } $jobs = array(); if (Arr::get($_GET, 'finished')) { $jobs['data.245'] = $_GET['finished']; } if (Arr::get($_GET, 'ticket')) { $tickets = explode(',', $_GET['ticket']); $q = array(); foreach ($tickets as $ticket) { $ticket = preg_replace('/[^a-z0-9]/i', '', strval($ticket)); if (!$ticket) { continue; } if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $q[] = $ticket; } else { $q[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } if (count($q) > 1) { $jobs['_id'] = array('$in' => $q); } elseif ($q) { $jobs['_id'] = $q[0]; } } if (Arr::get($_GET, 'fsa')) { $values = is_array($_GET['fsa']) ? $_GET['fsa'] : explode(',', $_GET['fsa']); $jobs['data.12'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'fsam')) { $values = is_array($_GET['fsam']) ? $_GET['fsam'] : explode(',', $_GET['fsam']); $jobs['data.13'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'fda')) { $values = is_array($_GET['fda']) ? $_GET['fda'] : explode(',', $_GET['fda']); $jobs['data.14'] = count($values) > 1 ? array('$in' => array_values($values)) : current($values); } if (Arr::get($_GET, 'address')) { $jobs['data.8'] = new MongoRegex('/.*' . strval($_GET['address']) . '.*/mi'); } if ($jobs) { if (count($jobs) == 1 && isset($jobs['_id'])) { $query['job_key'] = $jobs['_id']; } else { $query['job_key'] = array('$in' => Database_Mongo::collection('jobs')->distinct('_id', $jobs)); } } $sort = array('job_key' => 1); if (!Arr::get($_GET, 'sort')) { $_GET['sort'] = array('-submission'); } foreach ($_GET['sort'] as $s) { $dir = substr($s, 0, 1) == '-' ? -1 : 1; $s = substr($s, 1); switch ($s) { case 'submission': $sort['update_time'] = $dir; break; case 'approval': $sort['process_time'] = $dir; break; } } $result = Database_Mongo::collection('submissions')->find($query)->sort($sort); $submissions = array(); $users = array(); foreach ($result as $submission) { $submissions[$submission['job_key']][] = $submission; $users[$submission['user_id']] = 1; } if ($users) { User::get(array_keys($users)); } if (isset($_GET['export'])) { header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $file = tmpfile(); $headers = array('Tickets ID', 'FDA ID', 'LOC ID', 'Address', 'Submission Date', 'Approval Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } $headers[] = 'Column'; $headers[] = 'Value'; fputcsv($file, $headers); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('a' => Arr::path($job, 'data.8', ''), 'f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', '')); } foreach ($submissions as $job => $list) { foreach ($list as $submission) { $key = substr($submission['key'], 5); $data = array($job, Arr::path($jobs, array($job, 'f')), Arr::path($jobs, array($job, 'l')), Arr::path($jobs, array($job, 'a')), date('d-m-Y H:i', $submission['update_time']), Arr::get($submission, 'process_time') ? date('d-m-Y H:i', $submission['process_time']) : '', User::get($submission['user_id'], 'login')); if (Group::current('allow_assign')) { $data[] = Arr::get($companies, User::get($submission['user_id'], 'company_id'), 'Unknown'); } $data[] = Columns::get_name($key); $data[] = Columns::output($submission['value'], Columns::get_type($key), true); fputcsv($file, $data); } } fseek($file, 0); fpassthru($file); fclose($file); die; } elseif (isset($_GET['export2'])) { //header('Content-type: text/plain'); header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $result = array(); $columns = array(); foreach ($submissions as $job => $list) { foreach ($list as $submission) { $key = substr($submission['key'], 5); $result[$job][$submission['update_time']][$submission['user_id']][$key] = $submission['value']; $columns[$key] = Columns::get_name($key); } } $submissions = $result; ksort($columns); $headers = array('Tickets ID', 'FDA ID', 'LOC ID', 'Address', 'Submission Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } /*$columns = Columns::get_visible();*/ foreach ($columns as $column) { $headers[] = $column; } $columns = array_keys($columns); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', ''), 'a' => Arr::path($job, 'data.8', '')); } $file = tmpfile(); fputcsv($file, $headers); foreach ($submissions as $job => $list) { foreach ($list as $time => $values) { foreach ($values as $user => $submission) { $row = array($job, Arr::path($jobs, $job . '.f'), Arr::path($jobs, $job . '.l'), Arr::path($jobs, $job . '.a'), date('d-m-Y H:i', $time), User::get($user, 'login')); if (Group::current('allow_assign')) { $row[] = Arr::get($companies, User::get($user, 'company_id')); } foreach ($columns as $column) { $row[] = Columns::output(Arr::get($submission, $column, ''), Columns::get_type($column), true); } fputcsv($file, $row); } } } fseek($file, 0); fpassthru($file); fclose($file); //print_r($result); die; } elseif (isset($_GET['export3'])) { //header('Content-type: text/plain'); header('Content-type: text/csv'); header('Content-disposition: filename="Submissions.' . date('Ymd', $start) . '-' . date('Ymd', $end) . '.' . date('YmdHi', time()) . '.csv"'); $result = array(); foreach ($submissions as $job => $list) { foreach ($list as $submission) { $key = substr($submission['key'], 5); $result[$job][$submission['update_time']][$submission['user_id']][$key] = $submission['value']; } } $submissions = $result; $headers = array('Tickets ID', 'FDA ID', 'LOC ID', 'Address', 'Submission Date', 'User'); if (Group::current('allow_assign')) { $headers[] = 'Company'; } $columns = Columns::get_visible(); foreach ($columns as $column) { $headers[] = $column; } $columns = array_keys($columns); $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', ''), 'a' => Arr::path($job, 'data.8', '')); } $file = tmpfile(); fputcsv($file, $headers); foreach ($submissions as $job => $list) { foreach ($list as $time => $values) { foreach ($values as $user => $submission) { $row = array($job, Arr::path($jobs, $job . '.f'), Arr::path($jobs, $job . '.l'), Arr::path($jobs, $job . '.a'), date('d-m-Y H:i', $time), User::get($user, 'login')); if (Group::current('allow_assign')) { $row[] = Arr::get($companies, User::get($user, 'company_id')); } foreach ($columns as $column) { $row[] = Arr::get($submission, $column) ? Columns::output($submission[$column], Columns::get_type($column), true) : ''; } fputcsv($file, $row); } } } fseek($file, 0); fpassthru($file); fclose($file); //print_r($result); die; } $result = Database_Mongo::collection('jobs')->find(array('_id' => array('$in' => array_keys($submissions))), array('data.8' => 1, 'data.9' => 1, 'data.14' => 1)); $jobs = array(); foreach ($result as $job) { $jobs[$job['_id']] = array('f' => Arr::path($job, 'data.14', ''), 'l' => Arr::path($job, 'data.9', ''), 'a' => Arr::path($job, 'data.8', '')); } $view = View::factory("Reports/Submissions")->bind('companies', $companies)->bind('submissions', $submissions)->bind('jobs', $jobs); $this->response->body($view); }
public function action_process() { if (!isset($_POST['pos'])) { die('Error: file position not defined!'); } $pos = $_POST['pos']; $region = Arr::get($_POST, 'region', 0); $date = strtotime(Arr::get($_POST, 'date')); $tod = Arr::get($_POST, 'tod', 0); if ($tod) { $date += 18 * 60 * 60; } $time = $tod ? 'EOD' : 'SOD'; if ($region > 0) { $reg_name = DB::select('name')->from('regions')->where('id', '=', $region)->execute()->get('name'); } else { $reg_name = 'PARTIAL'; } ini_set("memory_limit", "512M"); $filename = $this->request->param('id'); if (!$filename || !file_exists(DOCROOT . 'files/' . $filename)) { die(json_encode(array('success' => true, 'error' => 'Warning: Import aborted - file not found!'))); } $file = fopen(DOCROOT . '/files/' . $filename, 'r'); $header = fgetcsv($file); $columns = array(-1 => 'Ticket Of Work', 0 => 'Please, select a column...', -2 => '<<< IGNORE >>>'); $result = DB::select('id', 'name')->from('job_columns')->execute()->as_array('id', 'name'); foreach ($result as $key => $value) { $columns[$key] = $value; } $map = array('ticketofwork' => -1); foreach ($result as $key => $name) { $id = preg_replace('/[^a-z0-9]/', '', strtolower($name)); if (isset($map[$id])) { if (is_array($map[$id])) { $map[$id][] = $key; } else { $map[$id] = array($map[$id], $key); } } else { $map[$id] = $key; } } foreach ($header as $value) { $id = str_replace("(YYYY-MM-DD)", '', $value); $id = str_replace("(in m)", '', $id); $id = preg_replace('/[^a-z0-9]/', '', strtolower($id)); foreach ($columns as $key => $name) { if (preg_replace('/[^a-z0-9]/', '', strtolower($name)) == $id) { unset($columns[$key]); break; } } } $keys = array(); $data = Arr::get($_POST, 'data', array()); foreach ($header as $key => $value) { $id = str_replace("(YYYY-MM-DD)", '', $value); $id = str_replace("(in m)", '', $id); $id = preg_replace('/[^a-z0-9]/', '', strtolower($id)); if (isset($map[$id])) { if (is_array($map[$id])) { $keys[$key] = array_shift($map[$id]); if (!$map[$id]) { unset($map[$id]); } } else { $keys[$key] = $map[$id]; unset($map[$id]); } } else { if (isset($data[$key]) && isset($columns[$data[$key]])) { $keys[$key] = $data[$key]; } } } if (count($keys) != count($header)) { die(json_encode(array('success' => true, 'error' => 'Error: Unable to map all columns!'))); } $primary_key_id = -1; foreach ($keys as $key => $id) { if ($id == -1) { $primary_key_id = $key; } } if ($primary_key_id < 0) { die(json_encode(array('success' => true, 'error' => 'Error: Unable to map Ticket ID!'))); } else { unset($keys[$primary_key_id]); } if ($pos) { fseek($file, $pos); } $types = DB::select('id', 'type')->from('job_columns')->where('csv', '=', 1)->execute()->as_array('id', 'type'); $static = DB::select('id', 'name')->from('job_columns')->where('csv', '=', 1)->and_where('show_reports', '=', 1)->execute()->as_array('id', 'name'); $start = microtime(true); $inserted = 0; $updated = 0; $skipped = 0; $deleted = 0; $update_time = time(); $jobs = Database_Mongo::collection('jobs'); $archive = Database_Mongo::collection('archive'); $ids = Session::instance()->get($filename); if ($region > 0 && $ids === NULL && !$pos) { $result = $jobs->find(array('region' => $region), array('_id')); $ids = array(); while ($row = $result->next()) { $ids[$row['_id']] = 1; } } while (true) { $row = fgetcsv($file); $id = Arr::get($row, $primary_key_id); if ($id && strlen($id) == 15 && substr($id, 0, 3) == 'T1W') { try { unset($row[$primary_key_id]); $data = array(); foreach ($row as $key => $value) { if ($keys[$key] > 0) { switch (Columns::get_type($keys[$key])) { case 'date': case 'datetime': $value = $value ? strtotime(str_replace('/', '-', $value)) : ''; break; case 'int': $value = intval($value); break; case 'float': $value = floatval($value); break; default: $value = iconv("CP1251", 'UTF-8//ignore', $value); break; } $data[$keys[$key]] = $value; } } $job = $jobs->findOne(array('_id' => $id)); if ($job) { $diff = array(); $new = array(); foreach ($data as $key => $value) { $old = Arr::get($job['data'], $key, ''); if (($old || $value) && $old != $value) { if (!Columns::get_persistent($key)) { if ($value) { $new['$set']['data.' . $key] = $value; } else { $new['$unset']['data.' . $key] = 1; } } $diff[$key] = array('old_value' => Arr::get($job['data'], $key), 'new_value' => $value ?: ''); } } if (Arr::get($job, 'removed')) { $new['$unset']['removed'] = 1; } if ($diff || $new) { if (isset($data[44])) { $status = preg_replace('/[^a-z]/', '', strtolower(Arr::path($diff, '44.old_value'))); $status2 = preg_replace('/[^a-z]/', '', strtolower(Arr::path($new, array('$set', 'data.44')))); $status_updated = isset($new['$set']['data.44']); $discrepancy = array(); if ($status_updated && ($status == 'tested' && $status2 != 'tested' || $status == 'built' && ($status2 != 'built' && $status2 != 'tested') || $status != $status2 && in_array($status2, array('deferred', 'dirty', 'heldnbn'), true))) { $discrepancy[44] = $diff[44]; } foreach (Columns::get_track() as $key) { if ($key != 44 && isset($diff[$key]) && !$diff[$key]['new_value'] && $diff[$key]['old_value']) { $discrepancy[$key] = $diff[$key]; } } if ($discrepancy) { $discrepancy = array('job_key' => $id, 'update_time' => $update_time, 'user_id' => User::current('id'), 'filename' => $filename, 'data' => $discrepancy, 'fields' => array_keys($discrepancy)); Database_Mongo::collection('discrepancies')->insert($discrepancy); Database_Mongo::collection('jobs')->update(array('_id' => $id), array('$set' => array('discrepancies' => $discrepancy['_id']))); } } if ($new) { $status = preg_replace('/[^a-z]/', '', strtolower(Arr::path($new, array('$set', 'data.44')))); if ($status == 'built' && !Arr::path($job, 'data.264')) { $new['$set']['data.264'] = $update_time; } if ($status == 'tested' && !Arr::path($job, 'data.265')) { $new['$set']['data.265'] = $update_time; if (!Arr::path($job, 'data.264')) { $new['$set']['data.264'] = $update_time; } } $new['$set']['last_update'] = $update_time; if (isset($new['$set']['data.8'])) { $new['$set']['address'] = MapQuest::parse($new['$set']['data.8']); } elseif (isset($new['$unset']['data.8'])) { $new['$unset']['address'] = 1; } $jobs->update(array('_id' => $id), $new); foreach (array_keys($diff) as $key) { if (Columns::get_persistent($key)) { unset($diff[$key]); } } if ($diff) { $archive->insert(array('job_key' => $id, 'update_time' => $update_time, 'update_type' => 2, 'user_id' => User::current('id'), 'filename' => $filename, 'static' => array_intersect_key($data, $static), 'data' => $diff, 'fields' => array_keys($diff))); } $updated++; } else { $skipped++; } } else { $skipped++; } } elseif ($region > 0) { $inserted++; $job = array('_id' => $id, 'region' => $region, 'created' => $update_time, 'last_update' => $update_time, 'data' => $data); if (isset($data[8])) { $job['address'] = MapQuest::parse($data[8]); } $jobs->insert($job); $archive->insert(array('job_key' => $id, 'update_time' => $update_time, 'update_type' => 1, 'user_id' => User::current('id'), 'filename' => $filename, 'static' => array_intersect_key($data, $static), 'data' => array(), 'fields' => array())); } unset($ids[$id]); } catch (Exception $e) { print_r($e); die; } } $pos = ftell($file); if (feof($file) || microtime(true) - $start > 1) { break; } } $finished = filesize(DOCROOT . '/files/' . $filename) == $pos ? 1 : 0; if ($finished) { if ($ids && $region > 0 && !isset($_POST['skip-deleted'])) { $jobs->update(array('_id' => array('$in' => array_keys($ids))), array('$set' => array('removed' => "1")), array('multiple' => 1)); $result = $archive->find(array('job_key' => array('$in' => array_keys($ids))), array('job_key', 'update_type', 'update_time')); $list = array(); while ($value = $result->next()) { if (!isset($list[$value['job_key']])) { $list[$value['job_key']] = array('date' => $value['update_time'], 'state' => $value['update_type']); } elseif ($list[$value['job_key']]['date'] < $value['update_time']) { $list[$value['job_key']]['state'] = $value['update_type']; } } $cnt = 0; foreach ($ids as $id => $v) { if (Arr::path($list, array($id, 'state'), 0) != 3) { $cnt++; $values = array('job_key' => $id, 'update_type' => 3, 'update_time' => $update_time, 'user_id' => User::current('id'), 'filename' => $filename, 'data' => array(), 'fields' => array()); $archive->insert($values); } } $deleted = $cnt; } if (Kohana::$environment == Kohana::PRODUCTION) { unlink(DOCROOT . 'files/' . $filename); } } else { Session::instance()->set($filename, $ids); } $result = array('success' => true, 'time' => microtime(true) - $start, 'memory' => memory_get_peak_usage(true), 'inserted' => $inserted, 'updated' => $updated, 'deleted' => $deleted, 'skipped' => $skipped, 'position' => $pos, 'progress' => $finished ? 100 : number_format($pos * 100 / filesize(DOCROOT . '/files/' . $filename)), 'done' => $finished); if ($finished) { $result['import_name'] = $reg_name . '_' . date('Ymd', $date) . '_' . $time; DB::update('regions')->set(array('last_update' => $date))->where('id', '=', $region)->execute(); } die(json_encode($result)); }
<td><a href="<?=URL::base() . 'search/view/' . $ticket['job_key']?>"><?=$ticket['job_key']?></a></td> <td nowrap="nowrap"><?=date('d-m-Y H:i', $ticket['update_time'])?></td> <td><?=User::get(Arr::get($ticket, 'user_id'), 'login') ? : 'Unknown'?></td> <td><a href="<?=URL::base() . 'imex/discrepancies?file=' . urlencode($ticket['filename'])?>"><?=HTML::chars($ticket['filename'])?></a></td> <?php foreach ($reports as $key => $name):?> <td><?=Columns::output(Arr::path($ticket, 'static.'.$key), Columns::get_type($key))?></td> <?php endforeach;?> <td> <table class="table subtable"> <tr class="same-yellow"> <th>Column name</th> <th>Old value</th> <th>New value</th> <th>Current value</th> </tr> <?php foreach($ticket['data'] as $id => $value): $date = Columns::get_type($id) == 'date';?> <tr class="submission <?=$value['old_value'] != $ticket['current'][$id] ? 'rose' : 'same-yellow' . (isset($_GET['discrepancy']) ? ' hidden' : '')?>"> <td><?=HTML::chars(Columns::get_name($id));?></td> <td><?=HTML::chars($date ? date("d-m-Y H:i", $value['old_value'] ? : 0) : $value['old_value'])?></td> <td><?=HTML::chars($date ? date("d-m-Y H:i", $value['new_value'] ? : 0) : $value['new_value'])?></td> <td><?=HTML::chars($date ? date("d-m-Y H:i", $ticket['current'][$id] ? : 0) : $ticket['current'][$id])?></td> </tr> <?php endforeach;?> </table> </td> </tr> <?php endforeach;?> </table> <div class="col-xs-12 text-center"> <?php if (Pager::pages() > 1) echo $pager = View::factory('Pager');?> </div>
public function action_submit() { $token = Arr::get($_REQUEST, 'token'); if (!API::check($token)) { die(json_encode(array('success' => false, 'error' => 'forbidden'))); } $id = strval(Arr::get($_REQUEST, 'id')); if (!$id) { die(json_encode(array('success' => false, 'error' => 'not found'))); } $job = Database_Mongo::collection('jobs')->findOne(array('_id' => $id)); if (!$job) { die(json_encode(array('success' => false, 'error' => 'not found'))); } $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); if ($regs) { $query['region'] = array('$in' => $regs); } if ($regs && !isset($regs[$job['region']])) { die(json_encode(array('success' => false, 'error' => 'not found'))); } if (!isset($job['companies']) || !in_array(intval(User::current('company_id')), $job['companies'], true)) { die(json_encode(array('success' => false, 'error' => 'not found'))); } $time = time(); $signature = strval(Arr::get($_REQUEST, 'signature')); $submissions = Arr::get($_REQUEST, 'data'); $completed = Arr::get($_REQUEST, 'completed'); $location = strval(Arr::get($_REQUEST, 'location')); if (!$signature || !$submissions || !$completed) { die(json_encode(array('success' => false, 'error' => 'wrong data'))); } foreach (Form::$columns as $key => $columns) { if ($key == $completed) { $data = array('filename' => 'Submission-' . date('dmY-His') . '-signature.png', 'mime' => 'image/png', 'uploaded' => $time, 'user_id' => User::current('id'), 'job_id' => $id, 'folder' => 'Signatures', 'fda_id' => Arr::path($job, 'data.14'), 'address' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', Arr::path($job, 'data.8'))), '-'), 'title' => ''); Database::instance()->begin(); $result = DB::insert('attachments', array_keys($data))->values(array_values($data))->execute(); $image_id = Arr::get($result, 0); if ($image_id && file_put_contents(DOCROOT . 'storage/' . $image_id, base64_decode($signature))) { unset($data['mime']); $data = array('filename' => trim(preg_replace('/-{2,}/', '-', preg_replace('/[^0-9a-z\\-]/i', '-', 'Signatures / ' . Arr::path($job, 'data.14') . ' / ' . Arr::path($job, 'data.8') . ' / Submission-' . date('dmY-His') . '-signature.png')), '-'), 'uploaded' => $time, 'user_id' => User::current('id'), 'job_id' => $id, 'action' => 1); DB::insert('upload_log', array_keys($data))->values(array_values($data))->execute(); Database::instance()->commit(); $submission = array('job_key' => $id, 'user_id' => User::current('id'), 'update_time' => $time, 'version' => Arr::get($_REQUEST, 'ver')); if ($location) { $submission['location'] = $location; } $status = Arr::get($job, 'status', Enums::STATUS_UNALLOC); $update = array(); $approval = false; $archive = array(); foreach ($submissions as $key => $value) { if (is_numeric($key)) { $value = Columns::parse($value, Columns::get_type($key)); if (Columns::get_direct($key)) { if (Arr::path($job, 'data.' . $key) != $value) { if ($value) { $update['$set']['data.' . $key] = $value; } else { $update['$unset']['data.' . $key] = 1; } $archive['data'][$key] = array('old_value' => Arr::path($job, 'data.' . $key), 'new_value' => $value); $job['data'][$key] = $value; } $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = -1; $submission['process_time'] = $submission['update_time']; Database_Mongo::collection('submissions')->insert($submission); unset($submission['process_time']); unset($submission['_id']); } else { $submission['key'] = 'data.' . $key; $submission['value'] = $value; $submission['active'] = 1; Database_Mongo::collection('submissions')->insert($submission); unset($submission['_id']); } } } if ($status != Enums::STATUS_PENDING) { $update['$set']['status'] = Enums::STATUS_PENDING; } if ($update) { $update['$set']['last_update'] = $time; $update['$set']['last_submit'] = $time; Database_Mongo::collection('jobs')->update(array('_id' => $id), $update); if ($archive) { foreach (Columns::get_static() as $key => $value) { $archive['static'][$key] = Arr::path($job, 'data.' . $key); } $archive['fields'] = array_keys($archive['data']); $archive['job_key'] = $id; $archive['user_id'] = User::current('id'); $archive['update_time'] = $time; $archive['update_type'] = 2; $archive['filename'] = 'MANUAL'; Database_Mongo::collection('archive')->insert($archive); } } } else { Database::instance()->rollback(); die(json_encode(array('success' => false, 'error' => 'signature problem'))); } if (Kohana::$environment == Kohana::DEVELOPMENT) { Database_Mongo::collection('api')->insert($_REQUEST); } die(json_encode(array('success' => true, 'time' => $time))); } } die(json_encode(array('success' => false, 'error' => 'wrong key'))); }