public static function make_all($scid, $sizes, $thumbs) { $raw_sc = ORM::factory('supplychain')->kitchen_sink($scid); $sm = new Sourcemap_Map_Static($raw_sc); $oimg = $sm->render(); $szs = $sizes; $szs['o'] = array($sm->w, $sm->h); foreach ($szs as $k => $v) { list($w, $h) = $v; $ckey = sprintf($ckeyfmt, $supplychain_id, $k); $rimg = Sourcemap_Map_Static::resize($oimg, $w, $h); Cache::instance()->set($ckey, $rimgb = Sourcemap_Map_Static::to_binary($rimg), 60 * 60 * 24 * 30 * 12); if (isset($thumbs[$k])) { list($thw, $thh) = $thumbs[$k]; $thx = $w / 2 - $thw / 2; $thy = $h / 2 - $thh / 2; $thumb = imagecreatetruecolor($thw, $thh); imagecopyresampled($thumb, $rimg, 0, 0, $thx, $thy, $thw, $thh, $thw, $thh); $thumbb = Sourcemap_Map_Static::to_binary($thumb); Cache::instance()->set(sprintf($ckeyfmt, $supplychain_id, "th-{$k}"), $thumbb); } } return true; }
public function action_static($supplychain_id, $sz = null) { if (!is_numeric($supplychain_id)) { $supplychain_id = $this->_match_alias($supplychain_id); } $supplychain = ORM::factory('supplychain', $supplychain_id); $szdim = false; $valid_size = false; $image_sizes = Sourcemap_Map_Static::$image_sizes; $image_thumbs = Sourcemap_Map_Static::$image_thumbs; do { if (isset($image_sizes[$sz])) { $valid_size = true; $szdim = $image_sizes[$sz]; } elseif ($sz == 'o') { $valid_size = true; $szdim = array(1024, 780); } else { foreach ($image_thumbs as $tk => $tv) { if ("th-{$tk}" == $sz) { $valid_size = true; $szdim = $tv; break; } } } if (!$valid_size) { $sz = Sourcemap_Map_Static::$default_image_size; } } while (!$valid_size); if ($supplychain->loaded()) { $current_user_id = Auth::instance()->logged_in() ? (int) Auth::instance()->get_user()->id : 0; $owner_id = (int) $supplychain->user_id; if ($supplychain->user_can($current_user_id, Sourcemap::READ)) { header('Content-Type: image/png'); //header('Cache-Control: private,max-age=600'); $ckeyfmt = "static-map-%010d-%s-png"; //$cache_key = sprintf($ckeyfmt, $supplychain_id, $sz); $cache_key = Sourcemap_Map_Static::cache_key($supplychain_id, $sz); $exists = Cache::instance()->get($cache_key); if ($exists) { header('X-Cache-Hit: true'); print $exists; } else { // make blank image and enqueue job to generate $maptic_url = Kohana::config('sourcemap.maptic_baseurl') . sprintf('%s-static-map-sc%06d-%s.png', Sourcemap::$env, $supplychain_id, $sz); $fetched = @file_get_contents($maptic_url); if ($fetched) { print $fetched; Cache::instance()->set($cache_key, $fetched, 300); exit; } elseif ($pimg = imagecreatefrompng(self::placeholder_image())) { // pass $pimgw = imagesx($pimg); $pimgh = imagesy($pimg); if (count($szdim) == 2) { $rpimgw = $szdim[0]; $rpimgh = $szdim[1]; } elseif (count($szdim) == 4) { $rpimgw = $szdim[2] - $szdim[0]; $rpimgh = $szdim[3] - $szdim[1]; } $rpimg = imagecreatetruecolor($rpimgw, $rpimgh); imagecopyresampled($rpimg, $pimg, 0, 0, 0, 0, $rpimgw, $rpimgh, $pimgw, $pimgh); imagedestroy($pimg); $pimg = $rpimg; } else { $pimg = imagecreatetruecolor($szdim[0], $szdim[1]); imagecolorallocate($pimg, 0, 0, 255); } imagepng($pimg); Sourcemap::enqueue(Sourcemap_Job::STATICMAPGEN, array('baseurl' => Kohana_URL::site('/', true), 'environment' => Sourcemap::$env, 'supplychain_id' => $supplychain->id, 'sizes' => Sourcemap_Map_Static::$image_sizes, 'thumbs' => Sourcemap_Map_Static::$image_thumbs)); } exit; } else { $this->request->status = 403; $this->layout = View::factory('layout/error'); $this->template = View::factory('error'); $this->template->error_message = 'This map is private.'; } } else { $this->request->status = 404; $this->layout = View::factory('layout/error'); $this->template = View::factory('error'); $this->template->error_message = 'That map could not be found.'; } }
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; }