/** * CRUD controller: CREATE */ public function action_create() { $this->template->title = __('New') . ' ' . __($this->_orm_model); $this->template->scripts['footer'][] = 'js/gmaps.min.js'; $this->template->scripts['footer'][] = 'js/oc-panel/locations-gmap.js'; $this->template->scripts['async_defer'][] = '//maps.google.com/maps/api/js?libraries=geometry&v=3&key=' . core::config("advertisement.gm_api_key") . '&callback=initLocationsGMap'; $location = new Model_Location(); if ($post = $this->request->post()) { //check if the parent is loaded/exists avoiding errors $post['id_location_parent'] = $post['id_location_parent'] != '' ? $post['id_location_parent'] : 1; $parent_loc = new Model_Location($post['id_location_parent']); if (!$parent_loc->loaded()) { Alert::set(Alert::INFO, __('You are assigning a parent location that does not exist')); $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller(), 'action' => 'create'))); } foreach ($post as $name => $value) { //for description we accept the HTML as comes...a bit risky but only admin can if ($name == 'description') { $location->description = Kohana::$_POST_ORIG['description']; } elseif ($name != 'submit') { $location->{$name} = $value; } } if (!isset($post['seoname'])) { $location->seoname = $location->gen_seotitle($post['seoname']); } else { $location->seoname = $post['seoname']; } try { $location->save(); } catch (Exception $e) { throw HTTP_Exception::factory(500, $e->getMessage()); } $this->action_deep(); Core::delete_cache(); Alert::set(Alert::SUCCESS, __('Location created')); $this->redirect(Route::get($this->_route_name)->uri(array('controller' => Request::current()->controller())) . (Core::post('id_location_parent') ? '?id_location=' . Core::post('id_location_parent') : NULL)); } $locations = array('' => ''); foreach (Model_Location::get_as_array() as $location) { $locations[$location['id']] = $location['name']; } return $this->render('oc-panel/pages/locations/create', compact('locations')); }