public function action_remove($id) { $sc = ORM::factory('supplychain', $id); if ($sc->loaded()) { $sc->flags = $sc->flags & ~Sourcemap::FEATURED; $sc->save(); if (Sourcemap_Search_Index::should_index($sc->id)) { Sourcemap_Search_Index::update($sc->id); } Message::instance()->set('Unfeatured map.', Message::SUCCESS); $this->request->redirect('admin/featured'); } else { Message::instance()->set('That supplychain does not exist.'); $this->request->redirect('admin/featured'); } }
public function action_delete($scid) { if (!Auth::instance()->get_user()) { return $this->_forbidden('You\'re not signed in.'); } $user = Auth::instance()->get_user(); $sc = ORM::factory('supplychain', $scid); if ($sc->loaded()) { $user->remove('favorites', $sc); if (Sourcemap_Search_Index::should_index($scid)) { Sourcemap_Search_Index::update($scid); } } else { return $this->_bad_request('Invalid supplychain id.'); } $this->response = true; }
public function action_refresh_supplychain($id) { $supplychain = ORM::factory('supplychain', $id); if ($supplychain->loaded()) { // pass } else { Message::instance()->set('Invalid supplychain.'); $this->request->redirect('admin/supplychains/'); } try { // Delete and recreate cache entry Cache::instance()->delete('supplychain-' . $id); Message::instance()->set('Primary cache entry for supplychain ' . $id . ' deleted.', Message::INFO); if (Sourcemap_Search_Index::should_index($id)) { Message::instance()->set('Supplychain ' . $id . ' re-indexed.', Message::INFO); Sourcemap_Search_Index::update($id); } else { Message::instance()->set('Supplychain ' . $id . ' de-indexed.', Message::INFO); Sourcemap_Search_Index::delete($id); } // Recreate static image $szs = Sourcemap_Map_Static::$image_sizes; foreach ($szs as $snm => $sz) { $ckey = Sourcemap_Map_Static::cache_key($id, $snm); Cache::instance()->delete($ckey); } Message::instance()->set('Removed cached static map images for map ' . $id . '.', Message::INFO); Sourcemap::enqueue(Sourcemap_Job::STATICMAPGEN, array('baseurl' => Kohana_URL::site('/', true), 'environment' => Sourcemap::$env, 'supplychain_id' => (int) $id, 'sizes' => Sourcemap_Map_Static::$image_sizes, 'thumbs' => Sourcemap_Map_Static::$image_thumbs)); Message::instance()->set('Queued job for new static maps. Should regenerate within 30-60 seconds.', Message::INFO); // I don't know that lotus, et al. want to update // the modified time. I think we just want to trigger // a reload/redraw for cache and static maps, respectively. //$sc = ORM::factory('supplychain', $id); //$sc->modified = time(); //$sc->save(); $this->request->redirect("admin/supplychains/{$id}"); } catch (Exception $e) { Message::instance()->set('Could not refresh supplychain ' . $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; }