/** * creates an ad from a row of import * @param class adsimport $adi * @return boolean */ private function create_ad($adi) { //new advertisement $ad = new Model_Ad(); //create user? if ($adi->id_user == NULL or !is_numeric($adi->id_user)) { //create the user $user = Model_User::create_user($adi->user_email, $adi->user_name); //check if in the table other users with same email set the id_user, then gets faster ;) try { DB::update('adsimport')->set(array('id_user' => $user->id_user))->where('user_email', '=', $adi->user_email)->execute(); } catch (Exception $e) { } //set id user to the new ad $ad->id_user = $user->id_user; } else { $ad->id_user = $adi->id_user; } //create category? if ($adi->id_category == NULL or !is_numeric($adi->id_category)) { //create the category $cat = Model_Category::create_name($adi->category); //check if in the table other cats with same name set the id_category, then gets faster ;) try { DB::update('adsimport')->set(array('id_category' => $cat->id_category))->where('category', '=', $adi->category)->execute(); } catch (Exception $e) { } //set id user to the new ad $ad->id_category = $cat->id_category; } else { $ad->id_category = $adi->id_category; } //create location? if (isset($adi->location) and !empty($adi->location) and ($adi->id_location == NULL or !is_numeric($adi->id_location))) { //create the location $loc = Model_Location::create_name($adi->location); //check if in the table other cats with same name set the id_location, then gets faster ;) try { DB::update('adsimport')->set(array('id_location' => $loc->id_location))->where('location', '=', $adi->location)->execute(); } catch (Exception $e) { } //set id user to the new ad $ad->id_location = $loc->id_location; } elseif (is_numeric($adi->id_location)) { $ad->id_location = $adi->id_location; } $ad->title = $adi->title; $ad->seotitle = $ad->gen_seo_title($adi->title); $ad->description = Text::html2bb($adi->description); $ad->published = $adi->date; $ad->created = $adi->date; $ad->price = $adi->price; $ad->address = $adi->address; $ad->phone = $adi->phone; $ad->website = $adi->website; $ad->status = Model_Ad::STATUS_PUBLISHED; try { $ad->save(); } catch (Exception $e) { return FALSE; } //save images if (($has_images = $this->process_images($ad, $adi)) > 0) { $ad->has_images = $has_images; try { $ad->save(); } catch (Exception $e) { return FALSE; } } //mark it as done try { DB::update('adsimport')->set(array('processed' => 1))->where('id_import', '=', $adi->id_import)->execute(); return TRUE; } catch (Exception $e) { return FALSE; } }
public function action_import_tool() { $this->template->title = __('Import tool for locations and categories'); Breadcrumbs::add(Breadcrumb::factory()->set_title($this->template->title)); //sending a CSV if ($_POST) { foreach ($_FILES as $file => $path) { $csv = $path["tmp_name"]; $csv_2[] = $file; if ($path['size'] > 1048576) { Alert::set(Alert::ERROR, __('1 MB file')); $this->redirect(Route::url('oc-panel', array('controller' => 'tools', 'action' => 'import_tool'))); } if ($file == 'csv_file_categories' and $csv != FALSE) { $expected_header = array('name', 'category_parent', 'price'); $cat_array = Core::csv_to_array($csv, $expected_header); if (count($cat_array) > 10000) { Alert::set(Alert::ERROR, __('limited to 10.000 at a time')); $this->redirect(Route::url('oc-panel', array('controller' => 'tools', 'action' => 'import_tool'))); } if ($cat_array === FALSE) { Alert::set(Alert::ERROR, __('Something went wrong, please check format of the file! Remove single quotes or strange characters, in case you have any.')); } else { foreach ($cat_array as $cat) { //category parent was sent? if ($cat[1]) { $category_parent = new Model_Category(); $category_parent->where('name', '=', $cat[1])->limit(1)->find(); if ($category_parent->loaded()) { $cat[1] = $category_parent->id_category; } else { $cat[1] = 1; } } else { $cat[1] = 1; } Model_Category::create_name($cat[0], 0, $cat[1], 0, $cat[2]); } Core::delete_cache(); Alert::set(Alert::SUCCESS, __('Categories successfully imported.')); } } elseif ($file == 'csv_file_locations' and $csv != FALSE) { $expected_header = array('name', 'location_parent', 'latitude', 'longitude'); $loc_array = Core::csv_to_array($csv, $expected_header); if (count($loc_array) > 10000) { Alert::set(Alert::ERROR, __('limited to 10.000 at a time')); $this->redirect(Route::url('oc-panel', array('controller' => 'tools', 'action' => 'import_tool'))); } if ($loc_array === FALSE) { Alert::set(Alert::ERROR, __('Something went wrong, please check format of the file! Remove single quotes or strange characters, in case you have any.')); } else { foreach ($loc_array as $loc) { //location parent was sent? if ($loc[1]) { $location_parent = new Model_Location(); $location_parent->where('name', '=', $loc[1])->limit(1)->find(); if ($location_parent->loaded()) { $loc[1] = $location_parent->id_location; } else { $loc[1] = 1; } } else { $loc[1] = 1; } Model_Location::create_name($loc[0], 0, $loc[1], 0, $loc[2], $loc[3]); } Core::delete_cache(); Alert::set(Alert::SUCCESS, __('Locations successfully imported.')); } } } } $this->template->content = View::factory('oc-panel/pages/tools/import_tool'); }