Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
 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')));
 }