Пример #1
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']);
     }
 }
Пример #2
0
 public static function csv2hops($csv, $stops, $o = array())
 {
     $options = array();
     foreach (self::$default_options as $k => $v) {
         $options[$k] = isset($o[$k]) ? $o[$k] : $v;
     }
     extract($options);
     $csv = Sourcemap_Csv::parse($csv);
     $raw_headers = array();
     if ($headers) {
         $raw_headers = array_shift($csv);
         $headers = array();
         for ($i = 0; $i < count($raw_headers); $i++) {
             if (strlen(trim($raw_headers[$i]))) {
                 $headers[] = strtolower($raw_headers[$i]);
             }
         }
         foreach ($headers as $i => $h) {
             if (is_null($fromcol) && preg_match('/^from(_?stop)?$/i', $h)) {
                 $fromcol = $h;
             } elseif (is_null($tocol) && preg_match('/^to(_?stop)?$/i', $h)) {
                 $tocol = $h;
             }
         }
     }
     if (!$fromcol || !$tocol) {
         throw new Exception('To and from columns required.');
     }
     $data = array();
     foreach ($csv as $ri => $row) {
         if ($headers && is_array($headers)) {
             $record = array();
             foreach ($headers as $hi => $k) {
                 if (isset($row[$hi])) {
                     $record[$k] = $row[$hi];
                 }
             }
         } else {
             $record = $row;
         }
         if ($record) {
             $data[] = $record;
         }
     }
     $stops_by_id = array();
     foreach ($stops as $sti => $st) {
         $stops_by_id[(int) $st->local_stop_id] = $st;
     }
     $hops = array();
     foreach ($data as $i => $record) {
         if (!isset($record[$fromcol]) || !is_numeric($record[$fromcol])) {
             throw new Exception('Missing or invalid from field at record #' . ($i + 1) . '.');
         }
         if (!isset($record[$tocol]) || !is_numeric($record[$tocol])) {
             throw new Exception('Missing or invalid to field at record #' . ($i + 1) . '.');
         }
         $from = $record[$fromcol];
         $to = $record[$tocol];
         if (!isset($stops_by_id[(int) $from])) {
             throw new Exception('From stop in hop does not exist in record #' . ($i + 1) . '.');
         }
         if (!isset($stops_by_id[(int) $to])) {
             throw new Exception('To stop in hop does not exist in record #' . ($i + 1) . '.');
         }
         list($type, $fromcoords) = Sourcemap_Wkt::read($stops_by_id[$from]->geometry);
         list($type, $tocoords) = Sourcemap_Wkt::read($stops_by_id[$to]->geometry);
         $frompt = new Sourcemap_Proj_Point($fromcoords);
         $topt = new Sourcemap_Proj_Point($tocoords);
         $geometry = Sourcemap_Wkt::write(Sourcemap_Wkt::MULTILINESTRING, array($frompt, $topt));
         $new_hop = (object) array('from_stop_id' => $from, 'to_stop_id' => $to, 'geometry' => $geometry, 'attributes' => new stdClass());
         foreach ($record as $k => $v) {
             if ($k !== $fromcol && $k !== $tocol) {
                 $new_hop->attributes->{$k} = $v;
             }
         }
         $hops[] = $new_hop;
     }
     return $hops;
 }
Пример #3
0
 protected function _unserialize_csv($csv)
 {
     return Sourcemap_Csv::parse($csv);
 }