public function action_index() { $this->layout->page_title = 'Create a supply chain'; $f = Sourcemap_Form::load('/create'); $f->action('create')->method('post'); if (!Auth::instance()->get_user()) { $this->request->redirect('auth'); } $this->layout->scripts = array('sourcemap-core', 'sourcemap-template'); $import_role = ORM::factory('role')->where('name', '=', 'import')->find(); $admin_role = ORM::factory('role')->where('name', '=', 'admin')->find(); if (Auth::instance()->get_user()->has('roles', $import_role) || Auth::instance()->get_user()->has('roles', $admin_role)) { $this->template->can_import = true; } else { $this->template->can_import = false; } $this->template->create_form = $f; if (strtolower(Request::$method) === 'post') { if ($f->validate($_POST)) { // create! $p = $f->values(); $title = $p['title']; $description = substr($p['description'], 0, 80); $tags = Sourcemap_Tags::join(Sourcemap_Tags::parse($p['tags'])); $category = $p['category']; $public = isset($_POST['publish']) ? Sourcemap::READ : 0; $raw_sc = new stdClass(); if ($category) { $raw_sc->category = $category; } $raw_sc->attributes = new stdClass(); $raw_sc->attributes->title = $title; $raw_sc->attributes->description = $description; $raw_sc->attributes->tags = $tags; $raw_sc->stops = array(); $raw_sc->hops = array(); $raw_sc->user_id = Auth::instance()->get_user()->id; $raw_sc->other_perms = 0; if ($public) { $raw_sc->other_perms |= $public; } else { $raw_sc->other_perms &= ~Sourcemap::READ; } try { $new_scid = ORM::factory('supplychain')->save_raw_supplychain($raw_sc); return $this->request->redirect('view/' . $new_scid); } catch (Exception $e) { $this->request->status = 500; Message::instance()->set('Couldn\\t create your supplychain. Please contact support.'); } } else { Message::instance()->set('Correct the errors below.'); } } }
public function action_index($supplychain_id = false) { if (!$supplychain_id) { $this->request->redirect('home'); } if (!is_numeric($supplychain_id)) { $supplychain_id = $this->_match_alias($supplychain_id); } $supplychain = ORM::factory('supplychain', $supplychain_id); if ($supplychain->loaded()) { $current_user_id = Auth::instance()->logged_in() ? (int) Auth::instance()->get_user()->id : 0; $owner_id = (int) $supplychain->user_id; if ($current_user_id && $supplychain->user_can($current_user_id, Sourcemap::WRITE)) { $supplychain = $supplychain->kitchen_sink($supplychain->id); // Load form template $form = Sourcemap_Form::load('/edit'); $form->action('edit/' . $supplychain->id)->method('post'); // Populate fields $form->field('title')->add_class('required'); if (isset($supplychain->attributes->title)) { $form->field('title')->value($supplychain->attributes->title); } if (isset($supplychain->attributes->description)) { $form->field('description')->value($supplychain->attributes->description); } $form->field('tags')->add_class('tags'); if (isset($supplychain->attributes->tags)) { $form->field('tags')->value($supplychain->attributes->tags); } // fetch the taxonomy tree and use first level $taxonomy = Sourcemap_Taxonomy::load_tree(); $form->field('category')->value($supplychain->category); $form->field('publish')->value($supplychain->other_perms & Sourcemap::READ); if (strtolower(Request::$method) === 'post') { if ($form->validate($_POST)) { $title = $form->get_field('title')->value(); $description = $form->get_field('description')->value(); $tags = Sourcemap_Tags::join(Sourcemap_Tags::parse($form->get_field('tags')->value())); $category = $form->get_field('category')->value(); if ($category) { $supplychain->category = $category; } else { $category = null; } $public = isset($_POST['publish']) ? Sourcemap::READ : 0; $supplychain->attributes->title = $title; $supplychain->attributes->description = $description; $supplychain->attributes->tags = $tags; if ($public) { $supplychain->other_perms |= $public; } else { $supplychain->other_perms &= ~Sourcemap::READ; } try { ORM::factory('supplychain')->save_raw_supplychain($supplychain, $supplychain->id); Message::instance()->set('Map updated.', Message::SUCCESS); return $this->request->redirect('view/' . $supplychain->id); } catch (Exception $e) { $this->request->status = 500; Message::instance()->set('Couldn\\t update your supplychain. Please contact support.'); } } else { Message::instance()->set('Please correct the errors below.'); } } $this->template->supplychain = $supplychain; $this->template->form = $form; } else { Message::instance()->set('You\'re not allowed to edit that map.'); $this->request->redirect('home'); } } else { Message::instance()->set('That map does not exist.'); $this->request->redirect('home'); } }