/** * CountriesController::admin_import() * * @return void */ public function import() { if ($this->Common->isPosted()) { if (!empty($this->request->data['Form'])) { $count = 0; foreach ($this->request->data['Form'] as $key => $val) { $this->Countries->create(); $data = ['iso3' => $val['iso3'], 'iso2' => $val['iso2'], 'name' => $val['name']]; if (empty($val['confirm'])) { # do nothing } elseif ($this->Countries->save($data)) { $count++; unset($this->request->data['Form'][$key]); } else { //$this->request->data['Form'][$key]['confirm'] = 0; $this->request->data['Error'][$key] = $this->Countries->validationErrors; } } $this->Flash->success(__('record import {0} saved', $count)); } else { $list = $this->request->data['Country']['import_content']; if (!empty($this->request->data['Country']['import_separator_custom'])) { $separator = $this->request->data['Country']['import_separator_custom']; $separator = str_replace(['{SPACE}', '{TAB}'], [Country::separators(SEPARATOR_SPACE, true), Country::separators(SEPARATOR_TAB, true)], $separator); } else { $separator = $this->request->data['Country']['import_separator']; $separator = Country::separators($separator, true); } # separate list into single records $countries = CommonComponent::parseList($list, $separator, false, false); if (empty($countries)) { $this->Countries->invalidate('import_separator', 'falscher Separator'); } elseif (!empty($this->request->data['Country']['import_pattern'])) { $pattern = str_replace(['{SPACE}', '{TAB}'], [Country::separators(SEPARATOR_SPACE, true), Country::separators(SEPARATOR_TAB, true)], $this->request->data['Country']['import_pattern']); # select part that matches %name foreach ($countries as $key => $danceStep) { $tmp = sscanf($danceStep, $pattern); # returns array # write back into $countries array if (!empty($tmp[2])) { $this->request->data['Form'][$key] = ['name' => $tmp[2], 'confirm' => 1]; if (!empty($tmp[1])) { $this->request->data['Form'][$key]['iso2'] = $tmp[1]; } if (!empty($tmp[0])) { $this->request->data['Form'][$key]['iso3'] = $tmp[0]; } } $countries[$key] = $tmp; } if (empty($this->request->data['Form'])) { $this->Countries->invalidate('import_pattern', 'falsches Muster'); } } else { foreach ($countries as $key => $country) { $this->request->data['Form'][$key] = ['name' => $country, 'confirm' => 1]; } } } $this->set(compact('countries')); } }