public function action_index() { $this->layout->page_title = Auth::instance()->get_user() ? 'Signed in' : 'Sign in'; if (Auth::instance()->get_user()) { $this->template->current_user_id = Auth::instance()->get_user(); $this->template->current_user = ORM::factory('user', Auth::instance()->get_user()); $this->request->redirect('home/'); } else { $this->template->current_user_id = false; $this->template->current_user = false; $f = Sourcemap_Form::load('/auth'); $f->action('auth')->method('post'); $this->template->login_form = $f; } if (strtolower(Request::$method) === 'post') { if ($f->validate($_POST)) { // Login if (Auth::instance()->login($_POST['username'], $_POST['password'])) { } else { Message::instance()->set('Invalid username or password.', Message::ERROR); $this->request->redirect('auth'); } if (!empty($_POST->next)) { $this->request->redirect($_POST->next); } else { $this->request->redirect('home/'); } } else { Message::instance()->set('Correct the errors below.'); } } $this->layout->scripts = array('sourcemap-core'); }
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); $this->layout->page_title = 'Delete a supply chain'; // create the form object and add fields $form = Sourcemap_Form::factory('delete')->method('post')->action('delete/' . $supplychain_id)->add_class('vertical')->select('confirm_once', 'Are you sure?')->select('confirm_twice', 'We can\'t undo this. Are you still sure you want to delete this map?')->select('confirm_thrice', 'Seriously. This is a permanent thing. Are you *sure*?')->submit('delete', 'Delete'); $form->field('confirm_once')->option('no', 'No')->option('yes', 'Yes'); $form->field('confirm_twice')->option('no', 'No')->option('yes', 'Yes'); $form->field('confirm_thrice')->option('no', 'No')->option('yes', 'Yes'); if (strtolower(Request::$method) === 'post') { $post = Validate::factory($_POST); $post->rule('confirm_once', 'in_array', array(array('yes')))->rule('confirm_twice', 'in_array', array(array('yes')))->rule('confirm_thrice', 'in_array', array(array('yes'))); if ($post->check()) { try { ORM::factory('supplychain', $supplychain->id)->delete(); Message::instance()->set('Map deleted.', Message::SUCCESS); return $this->request->redirect('home'); } catch (Exception $e) { $this->request->status = 500; Message::instance()->set('Couldn\'t delete your supplychain. Please contact support.'); } } else { Message::instance()->set('You don\'t seem sure.'); $form->errors($post->errors('forms/create')); } } $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'); } }
public function action_post($p = false) { if ($p === false) { return $this->_bad_request(); } $f = Sourcemap_Form::load($p); if ($f) { if ($f->validate((array) $this->request->posted_data)) { $this->response = true; } else { $this->response = (object) $f->errors(); } } else { return $this->_not_found('Form does not exist.'); } }
public function action_index() { if (Auth::instance()->get_user()) { $this->template->current_user_id = Auth::instance()->get_user(); $this->template->current_user = ORM::factory('user', Auth::instance()->get_user()); $this->request->redirect('home/'); } $this->layout->page_title = 'Register an account'; $this->layout->scripts = array('sourcemap-core', 'sourcemap-template'); $f = Sourcemap_Form::load('/register'); $f->action('register')->method('post'); $this->template->form = $f; if (strtolower(Request::$method) === 'post') { $validate = $f->validate($_POST); if (array_key_exists('recaptcha', Kohana::modules())) { $recap = Recaptcha::instance(); $revalid = (bool) $recap->is_valid($_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); $validate = $validate && $revalid; } if ($validate) { $p = $f->values(); // check for username in use $exists = ORM::factory('user')->where('username', '=', $p['username'])->find()->loaded(); if ($exists) { Message::instance()->set('That username is taken.'); return; } // check for email in use $exists = ORM::factory('user')->where('email', '=', $p['email'])->find()->loaded(); if ($exists) { Message::instance()->set('An account exists for that email address.'); return; } $new_user = ORM::factory('user'); $new_user->username = $p['username']; $new_user->email = $p['email']; $new_user->password = $p['password']; $new_user->save(); if (!$new_user->id) { Message::instance()->set('Could not complete registration. Please contact support.'); return $this->request->redirect('register'); } //send a notification $subj = 'Re: Your New Account on Open Supply Chains'; $h = md5(sprintf('%s-%s', $new_user->username, $new_user->email)); $lid = strrev(base64_encode($new_user->username)); $url = URL::site("register/confirm?t={$lid}-{$h}", true); $msgbody = "Dear {$new_user->username},\n\n"; $msgbody .= 'Welcome to Open Supply Chains! '; $msgbody .= "Go to the url below to activate your account.\n\n"; $msgbody .= $url . "\n\n"; $msgbody .= "If you have any questions, please contact us.\n"; $addlheaders = "From: Open Supply Chains\r\n"; try { $sent = mail($new_user->email, $subj, $msgbody, $addlheaders); Message::instance()->set('Please check your email for further instructions.', Message::INFO); } catch (Exception $e) { Message::instance()->set('Sorry, could not complete registration. Please contact support.'); } return $this->request->redirect('register'); } else { Message::instance()->set('Check the information below and try again.'); } } else { /* pass */ } }
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'); } }