Beispiel #1
0
 public function action_index()
 {
     $this->layout->scripts = array('sourcemap-core');
     $admin = ORM::factory('role')->where('name', '=', 'admin')->find();
     if (!($user = Auth::instance()->get_user())) {
         $this->request->redirect('');
     } else {
         if (Auth::instance()->get_user() && Auth::instance()->get_user()->has('roles', $admin)) {
             $this->request->redirect('admin/');
         }
     }
     $p = false;
     $user_arr = $user->as_array();
     unset($user_arr['password']);
     $scs = array();
     // TODO: group ownership?
     foreach ($user->supplychains->order_by('modified', 'desc')->find_all() as $i => $sc) {
         $scs[] = $sc->kitchen_sink($sc->id);
     }
     $this->template->user = (object) $user_arr;
     $this->layout->page_title = "Dashboard for " . $this->template->user->username;
     $this->template->user_event_stream = Sourcemap_User_Event::get_user_stream($user->id, 6);
     $this->template->user_profile = $p;
     $this->template->supplychains = $scs;
 }
Beispiel #2
0
 public function publish_to($scope_id, $scope = self::USER)
 {
     $recent_evts = ORM::factory('user_event')->where('event', '=', Sourcemap_User_Event::UPDATEDSC)->and_where('scope', '=', $scope)->and_where('scope_id', '=', $scope_id)->and_where('timestamp', '>', time() - 3600)->count_all();
     if ($recent_evts > 0) {
         return false;
     } else {
         return parent::publish_to($scope_id, $scope);
     }
 }
 public function action_announce()
 {
     if (Request::$method === 'POST') {
         $post = Validate::factory($_POST);
         $post->rule('announcement_message', 'min_length', array(8))->rule('announcement_message', 'max_length', array(256))->rule('announcement_message', 'not_empty')->rule('confirm1', 'not_empty')->rule('confirm2', 'not_empty')->rule('confirm3', 'not_empty');
         if ($post->check()) {
             $post = (object) $post->as_array();
             Sourcemap_User_Event::factory(Sourcemap_User_Event::ANNOUNCE, $post->announcement_message)->trigger();
             Message::instance()->set('Announced.', Message::SUCCESS);
             $this->request->redirect('admin/announcements');
         } else {
             Message::instance()->set('Try again.');
             $this->request->redirect('admin/announcements');
         }
     } else {
         $this->request->redirect('admin/announcements');
     }
 }
Beispiel #4
0
 public function action_confirm()
 {
     if (Auth::instance()->get_user()) {
         Message::instance()->set('You\'re already signed in. Sign out and click the ' . 'confirmation url again.', Message::INFO);
         return $this->request->redirect('home');
     }
     $get = Validate::factory($_GET);
     $get->rule('t', 'regex', array('/^[A-Za-z0-9\\+\\/=]+-[A-Fa-f0-9]{32}$/'));
     if ($get->check()) {
         list($uh, $h) = explode('-', $get['t']);
         // check token
         $username = base64_decode(strrev($uh));
         $user = ORM::factory('user')->where('username', '=', $username)->find();
         $login = ORM::factory('role')->where('name', '=', 'login')->find();
         if ($user->loaded()) {
             // see if acct is already confirmed
             if ($user->has('roles', $login)) {
                 Message::instance()->set('That token has expired.');
                 return $this->request->redirect('auth');
             }
         } else {
             Message::instance()->set('Invalid confirmation token.');
             return $this->request->redirect('auth');
         }
         // add login role
         $user->add('roles', $login);
         Message::instance()->set('Your account has been confirmed. Please Sign in (and start mapping).', Message::SUCCESS);
         Sourcemap_User_Event::factory(Sourcemap_User_Event::REGISTERED, $user->id)->trigger();
         return $this->request->redirect('auth');
     } else {
         Message::instance()->set('Invalid confirmation token.');
         return $this->request->redirect('auth');
     }
 }
Beispiel #5
0
 public function __construct($message)
 {
     parent::__construct();
     $this->message = $message;
 }
 public function __construct($user_id)
 {
     parent::__construct();
     $this->user_id = $user_id;
 }
 public function save_raw_supplychain($sc, $scid = null)
 {
     $this->_db->query(null, 'BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE', true);
     if (!$scid) {
         # TODO: create here.
         $new_sc = ORM::factory('supplychain');
         $new_sc->user_id = isset($sc->user_id) ? $sc->user_id : null;
         $new_sc->save();
         $scid = $new_sc->id;
     } else {
         $sc->user_id = ORM::factory('supplychain', $scid)->user_id;
         $sql = sprintf('delete from supplychain_attribute where supplychain_id = %d', $scid);
         $this->_db->query(Database::DELETE, $sql, true);
         $sql = sprintf('delete from stop where supplychain_id = %d', $scid);
         $this->_db->query(Database::DELETE, $sql, true);
         $sql = sprintf('delete from hop where supplychain_id = %d', $scid);
         $this->_db->query(Database::DELETE, $sql, true);
     }
     # TODO: concurrency? check last rev?
     try {
         $scattr_sql = 'insert into supplychain_attribute (supplychain_id, "key", "value") ' . 'values (:supplychain_id, :key, :value)';
         $scattr_insert_query = DB::query(Database::INSERT, $scattr_sql);
         foreach ($sc->attributes as $k => $v) {
             list($nothing, $affected) = $scattr_insert_query->param(':supplychain_id', $scid)->param(':key', $k)->param(':value', (string) $v)->execute();
             if (!$affected) {
                 throw new Exception('Could not insert supplychain attribute: "' . $k . '".');
             }
         }
         $sql = sprintf('insert into stop (supplychain_id, local_stop_id, geometry) values ' . '(:supplychain_id, :local_stop_id, ST_SetSRID(ST_GeometryFromText(:geometry), %d))', Sourcemap::PROJ);
         $query = DB::query(Database::INSERT, $sql, true)->param(':supplychain_id', $scid);
         $last_insert_query = DB::query(Database::SELECT, 'select currval(\'stop_id_seq\') as stop_seq');
         $stattr_sql = 'insert into stop_attribute (supplychain_id, local_stop_id, "key", "value") ' . 'values (:supplychain_id, :local_stop_id, :key, :value)';
         $stattr_insert_query = DB::query(Database::INSERT, $stattr_sql);
         foreach ($sc->stops as $sti => $raw_stop) {
             list($nothing, $affected) = $query->param(':local_stop_id', $raw_stop->local_stop_id)->param(':geometry', $raw_stop->geometry)->execute();
             if (!$affected) {
                 throw new Exception('Could not insert stop.');
             }
             foreach ($raw_stop->attributes as $k => $v) {
                 list($nothing, $affected) = $stattr_insert_query->param(':supplychain_id', $scid)->param(':local_stop_id', $raw_stop->local_stop_id)->param(':key', $k)->param(':value', $v)->execute();
                 if (!$affected) {
                     throw new Exception('Could not insert stop attribute: "' . $k . '".');
                 }
             }
         }
         $hop_insert_query = DB::query(Database::INSERT, 'insert into hop (supplychain_id, to_stop_id, from_stop_id,geometry) values ' . '(:supplychain_id, :to_stop_id, :from_stop_id, ST_SetSRID(ST_GeometryFromText(:geometry), ' . Sourcemap::PROJ . '))');
         $last_insert_query = DB::query(Database::SELECT, 'select currval(\'hop_id_seq\') as stop_seq');
         $hattr_sql = 'insert into hop_attribute (supplychain_id, from_stop_id, to_stop_id, "key", "value")' . ' values (:supplychain_id, :from_stop_id, :to_stop_id, :key, :value)';
         $hattr_insert_query = DB::query(Database::INSERT, $hattr_sql);
         foreach ($sc->hops as $hi => $raw_hop) {
             list($nothing, $affected) = $hop_insert_query->param(':supplychain_id', $scid)->param(':to_stop_id', $raw_hop->to_stop_id)->param(':from_stop_id', $raw_hop->from_stop_id)->param(':geometry', $raw_hop->geometry)->execute();
             if (!$affected) {
                 throw new Exception('Could not insert hop.');
             }
             foreach ($raw_hop->attributes as $k => $v) {
                 list($nothing, $affected) = $hattr_insert_query->param(':supplychain_id', $scid)->param(':from_stop_id', $raw_hop->from_stop_id)->param(':to_stop_id', $raw_hop->to_stop_id)->param(':key', $k)->param(':value', $v)->execute();
                 if (!$affected) {
                     throw new Exception('Could not insert hop attribute: "' . $k . '".');
                 }
             }
         }
         if (isset($sc->usergroup_perms)) {
             $sc->usergroup_perms = (int) $sc->usergroup_perms;
             $sql = sprintf('update supplychain set usergroup_perms = %d where id = %d', $sc->usergroup_perms, $scid);
             $this->_db->query(Database::UPDATE, $sql, true);
         }
         if (isset($sc->other_perms)) {
             $sc->other_perms = (int) $sc->other_perms;
             $sql = sprintf('update supplychain set other_perms = %d where id = %d', $sc->other_perms, $scid);
             $this->_db->query(Database::UPDATE, $sql, true);
         }
         if (isset($sc->category)) {
             if (ORM::factory('category', $sc->category)->loaded()) {
                 $sql = sprintf('update supplychain set category = %d where id = %d', $sc->category, $scid);
                 $this->_db->query(Database::UPDATE, $sql, true);
             } else {
                 throw new Exception('Invalid category ' . (int) $sc->category);
             }
         }
     } catch (Exception $e) {
         $this->_db->query(null, 'ROLLBACK', true);
         throw new Exception('Could not save raw supplychain with id "' . $scid . '"(' . $e->getMessage() . ')');
     }
     $this->_db->query(null, 'COMMIT', true);
     $evt = isset($new_sc) ? Sourcemap_User_Event::CREATEDSC : Sourcemap_User_Event::UPDATEDSC;
     try {
         Sourcemap_User_Event::factory($evt, $sc->user_id, $scid)->trigger();
     } catch (Exception $e) {
         // pass
         //die($e);
     }
     Cache::instance()->delete('supplychain-' . $scid);
     if (Sourcemap_Search_Index::should_index($scid)) {
         Sourcemap_Search_Index::update($scid);
     } else {
         Sourcemap_Search_Index::delete($scid);
     }
     $szs = Sourcemap_Map_Static::$image_sizes;
     foreach ($szs as $snm => $sz) {
         $ckey = Sourcemap_Map_Static::cache_key($scid, $snm);
         Cache::instance()->delete($ckey);
     }
     $sc = ORM::factory('supplychain', $scid);
     $sc->modified = time();
     $sc->save();
     return $scid;
 }