Exemplo n.º 1
0
 public function action_post()
 {
     $posted = $this->request->posted_data;
     if (is_object($posted) && isset($posted->stop_file) && $posted->stop_file instanceof Sourcemap_Upload) {
         $stop_csv = $posted->stop_file->get_contents();
         if (isset($posted->hop_file) && $posted->hop_file instanceof Sourcemap_Upload) {
             $hop_csv = $posted->hop_file->get_contents;
         }
     } elseif (($posted = (object) $_POST) && isset($posted->stop_csv)) {
         $stop_csv = $posted->stop_csv;
         if (isset($posted->hop_csv)) {
             $hop_csv = $posted->hop_csv;
         }
     } else {
         return $this->_bad_request('No C.S.V. uploaded or posted.');
     }
     $sc = Sourcemap_Import_Csv::csv2sc($stop_csv, $hop_csv, (array) $posted);
     $this->response = (object) array('supplychain' => $sc);
 }
Exemplo n.º 2
0
 public function action_index()
 {
     if (!Auth::instance()->get_user()) {
         Message::instance()->set('You must be signed in to use the importer.');
         $this->request->redirect('/auth?next=/tools/import/csv');
     }
     $current_user = Auth::instance()->get_user();
     $import_role = ORM::factory('role')->where('name', '=', 'import')->find();
     $admin_role = ORM::factory('role')->where('name', '=', 'admin')->find();
     if ($current_user->has('roles', $import_role) || $current_user->has('roles', $admin_role)) {
         // pass
     } else {
         Message::instance()->set('You don\'t have access to the Google Docs importer.');
         $this->request->redirect('/home');
     }
     $this->layout->scripts = array('sourcemap-core');
     if (strtolower(Request::$method) === 'post') {
         $posted = (object) array_merge($_POST, Sourcemap_Upload::get_uploads());
         if (isset($posted->stop_file) && $posted->stop_file instanceof Sourcemap_Upload && $posted->stop_file->ok()) {
             $stop_csv = $posted->stop_file->get_contents();
             if (isset($posted->hop_file) && $posted->hop_file instanceof Sourcemap_Upload && $posted->hop_file->ok()) {
                 $hop_csv = $posted->hop_file->get_contents();
             } else {
                 $hop_csv = null;
             }
             try {
                 $sc = Sourcemap_Import_Csv::csv2sc($stop_csv, $hop_csv, $posted);
             } catch (Exception $e) {
                 die($e);
                 Message::instance()->set('Problem with import: ' . $e->getMessage());
                 $this->request->redirect('tools/import/csv');
             }
             $sc->user_id = Auth::instance()->get_user()->id;
             $update = false;
             if (isset($posted->replace_into) && $posted->replace_into > 0) {
                 if (!(ORM::factory('supplychain', $posted->replace_into)->owner->id == $sc->user_id)) {
                     Message::instance()->set('That supplychain doesn\'t exist or doesn\'t belong to you.');
                     $this->request->redirect('tools/import/csv');
                 } else {
                     $update = (int) $posted->replace_into;
                 }
             }
             if ($update) {
                 $new_sc_id = ORM::factory('supplychain')->save_raw_supplychain($sc, $update);
             } else {
                 $new_sc_id = ORM::factory('supplychain')->save_raw_supplychain($sc);
             }
             $new_sc = ORM::factory('supplychain', $new_sc_id);
             if (isset($posted->publish) && $posted->publish) {
                 $new_sc->other_perms |= Sourcemap::READ;
                 $new_sc->save();
             }
             if (isset($posted->supplychain_name) && is_string($posted->supplychain_name)) {
                 $attr = ORM::factory('supplychain_attribute');
                 $attr->supplychain_id = $new_sc_id;
                 $attr->key = 'title';
                 $attr->value = substr($posted->supplychain_name, 0, 64);
                 $attr->save();
                 $attr = ORM::factory('supplychain_attribute');
                 $attr->supplychain_id = $new_sc_id;
                 $attr->key = 'name';
                 $attr->value = substr($posted->supplychain_name, 0, 64);
                 $attr->save();
             }
             if ($new_sc_id) {
                 Request::instance()->redirect('view/' . $new_sc_id);
             } else {
                 return $this->_internal_server_error('Could not save.');
             }
         } else {
             return $this->_bad_request('Stop file required.');
         }
     } else {
         $this->template->user_supplychains = ORM::factory('supplychain')->where('user_id', '=', Auth::instance()->get_user()->id)->find_all();
     }
 }
Exemplo n.º 3
0
 public function action_import()
 {
     if (!($acc_token = Session::instance()->get('g_oauth_access_token'))) {
         Message::instance()->set('You haven\'t given us permission to fetch spreadsheets.');
         $this->request->redirect('/tools/import/google/');
     }
     if (Request::$method !== 'POST') {
         Message::instance()->set('Please choose a spreadsheet to import.');
         $this->request->redirect('/tools/import/google/list');
     }
     // TODO: validation
     if (!isset($_POST['k'], $_POST['stops-wsid'])) {
         Message::instance()->set('Spreadsheet key and worksheet id required.');
         $this->request->redirect('/tools/import/google/list');
     }
     $csv = Sourcemap_Csv::arr2csv(Google_Spreadsheets::get_worksheet_cells($acc_token, $_POST['k'], $_POST['stops-wsid']));
     if ($csv && isset($_POST['hops-wsid']) && $_POST['hops-wsid']) {
         $hops_csv = Sourcemap_Csv::arr2csv(Google_Spreadsheets::get_worksheet_cells($acc_token, $_POST['k'], $_POST['hops-wsid']));
     } else {
         $hops_csv = null;
     }
     $new_sc = Sourcemap_Import_Csv::csv2sc($csv, $hops_csv, array('headers' => true));
     if (isset($_POST['replace-into']) && $_POST['replace-into']) {
         $exists = ORM::factory('supplychain')->where('id', '=', $_POST['replace-into'])->find();
         if ($exists && $exists->user_id == Auth::instance()->get_user()->id) {
             $replace_into = $exists->id;
         } else {
             Message::instance()->set('The supplychain you tried to replace is invalid.');
             $this->request->redirect('/tools/import/google/worksheets/?k=' . $_POST['k']);
         }
     } else {
         $replace_into = null;
     }
     try {
         $new_sc->user_id = Auth::instance()->get_user()->id;
         $title = false;
         $title = isset($_POST['supplychain_name']) && $_POST['supplychain_name'] ? $_POST['supplychain_name'] : false;
         if ($replace_into && !$title && ($keeptitle = $exists->attributes->where('key', 'in', array('title', 'name'))->find())) {
             $title = $keeptitle->value;
         }
         $new_sc->attributes = (object) array('title' => $title ? $title : 'Imported Sourcemap');
         $scid = ORM::factory('supplychain')->save_raw_supplychain($new_sc, $replace_into);
         $new_sc = ORM::factory('supplychain', $scid);
         $new_sc->other_perms |= Sourcemap::READ;
         $new_sc->save();
         Message::instance()->set('Your spreadsheet was imported.', Message::SUCCESS);
         $this->request->redirect('view/' . $scid);
     } catch (Exception $e) {
         Message::instance()->set('There was a problem importing your spreadsheet: ' . $e);
         $this->request->redirect('/tools/import/google/worksheets/?k=' . $_POST['k']);
     }
 }