Example #1
0
 public function action_index()
 {
     $cache_key = 'sourcemap-sitemap';
     $ttl = 60 * 60 * 24;
     if ($cached = Cache::instance()->get($cache_key)) {
         $xml = $cached;
     } else {
         // Sitemap instance.
         $sitemap = new Sitemap();
         // basics
         $urls = array('home' => array('', 0.9, 'daily', time()), 'register' => array('register/', 0.6, 'yearly'), 'browse' => array('browse/', 0.7, 'daily', time()), 'login' => array('auth/login', 0.5, 'yearly'), 'about' => array('info/', 0.7, 'monthly'), 'api' => array('info/api', 0.7, 'monthly'), 'contact' => array('info/contact', 0.8, 'monthly'));
         // categories
         $cats = Sourcemap_Taxonomy::arr();
         $nms = array();
         foreach ($cats as $i => $cat) {
             $slug = Sourcemap_Taxonomy::slugify($cat->name);
             $urls['browse-' . $cat->name] = array('browse/' . $slug . '/', 0.7);
         }
         // public maps
         $o = 0;
         $l = 100;
         while (($results = Sourcemap_Search::find(array('o' => $o, 'l' => $l))) && $results->hits_ret) {
             foreach ($results->results as $i => $r) {
                 $urls['sc-' . $r->id] = array('view/' . $r->id, 0.5, 'daily', $r->modified);
             }
             $o += $l;
         }
         $defaults = array(false, 0.5, 'daily', false);
         foreach ($urls as $k => $urld) {
             foreach ($defaults as $i => $d) {
                 if (!isset($urld[$i])) {
                     $urld[$i] = $d;
                 }
             }
             list($loc, $priority, $freq, $lastmod) = $urld;
             $new_url = new Sitemap_URL();
             $new_url->set_loc(URL::site($loc, true))->set_priority($priority)->set_change_frequency($freq);
             if ($lastmod) {
                 $new_url->set_last_mod($lastmod);
             }
             $sitemap->add($new_url);
         }
         $xml = $sitemap->render();
         Cache::instance()->set($cache_key, $xml, $ttl);
     }
     header('Content-Type: application/xml');
     $this->response = $xml;
     die($this->response);
 }
Example #2
0
 public function kitchen_sink($scid)
 {
     $scid = (int) $scid;
     if (($sc = ORM::factory('supplychain', $scid)) && $sc->loaded()) {
         $rows = $this->_db->query(Database::SELECT, sprintf("select s.id as stop_id, ST_AsText(s.geometry) as geometry, \n                        sa.key as attr_k, sa.value as attr_v, s.local_stop_id as local_stop_id\n                    from stop as s left outer join stop_attribute as sa on\n                        (s.supplychain_id=sa.supplychain_id and s.local_stop_id=sa.local_stop_id)\n                    where s.supplychain_id = %d\n                    order by sa.id desc", $scid), true)->as_array();
         $stops = array();
         foreach ($rows as $i => $row) {
             if (!isset($stops[$row->local_stop_id])) {
                 $stops[$row->local_stop_id] = (object) array('local_stop_id' => $row->local_stop_id, 'id' => $row->local_stop_id, 'geometry' => $row->geometry, 'attributes' => (object) array());
             }
             if ($row->attr_k) {
                 $stops[$row->local_stop_id]->attributes->{$row->attr_k} = $row->attr_v;
             }
         }
         $hops = array();
         $sql = sprintf("select h.id as hop_id, h.from_stop_id, h.to_stop_id,\n                ST_AsText(h.geometry) as geometry, ha.key as attr_k, ha.value as attr_v\n                from hop as h \n                    left outer join hop_attribute as ha on (\n                        h.supplychain_id=ha.supplychain_id and\n                        h.from_stop_id=ha.from_stop_id and\n                        h.to_stop_id=ha.to_stop_id\n                    )\n                where h.supplychain_id = %d order by h.id asc", $scid);
         $rows = $this->_db->query(Database::SELECT, $sql, true);
         foreach ($rows as $i => $row) {
             $hkey = sprintf("%d-%d", $row->from_stop_id, $row->to_stop_id);
             if (!isset($hops[$hkey])) {
                 $hops[$hkey] = (object) array('from_stop_id' => $row->from_stop_id, 'to_stop_id' => $row->to_stop_id, 'geometry' => $row->geometry, 'attributes' => (object) array());
             }
             if ($row->attr_k) {
                 $hops[$hkey]->attributes->{$row->attr_k} = $row->attr_v;
             }
         }
         $sql = sprintf("select sca.key as attr_k, sca.value as attr_v\n                from supplychain_attribute as sca\n                where sca.supplychain_id=%d", $scid);
         $owner = $sc->owner;
         $cat = $sc->taxonomy;
         $sc = (object) $sc->as_array();
         $sc->attributes = new stdClass();
         $rows = $this->_db->query(Database::SELECT, $sql, true);
         foreach ($rows as $i => $row) {
             $sc->attributes->{$row->attr_k} = $row->attr_v;
         }
         $sc->stops = array_values($stops);
         $sc->hops = array_values($hops);
         $sc->owner = (object) array('id' => $owner->id, 'name' => $owner->username, 'avatar' => Gravatar::avatar($owner->email));
         $sc->user_id = $owner->id;
         $sc->taxonomy = $cat && $cat->loaded() ? Sourcemap_Taxonomy::load_ancestors($cat->id) : null;
     } else {
         throw new Exception('Supplychain not found.');
     }
     return $sc;
 }
Example #3
0
 public function action_index($category = false)
 {
     $this->layout->scripts = array('sourcemap-core');
     $this->layout->page_title = 'Browsing supply chains';
     $cats = Sourcemap_Taxonomy::arr();
     $nms = array();
     foreach ($cats as $i => $cat) {
         $nms[Sourcemap_Taxonomy::slugify($cat->name)] = $cat;
     }
     $this->template->taxonomy = Sourcemap_Taxonomy::load_tree();
     $defaults = array('q' => false, 'p' => 1, 'l' => 20);
     $params = $_GET;
     if (strtolower(Request::$method) == 'post') {
         $params = $_POST;
     }
     $params = array_merge($defaults, $params);
     $params['recent'] = 'yes';
     $params['l'] = 20;
     if ($category && isset($nms[$category])) {
         $slug = $category;
         $category = $nms[$category];
         $this->template->category = $category;
         $params['c'] = $category->name;
         $this->layout->page_title .= ' - ' . $category->title;
     } elseif ($category) {
         Message::instance()->set('"' . $category . '" is not a valid category slug.');
         return $this->request->redirect('browse');
     } else {
         $this->template->category = false;
     }
     $r = Sourcemap_Search::find($params);
     $p = Pagination::factory(array('current_page' => array('source' => 'query_string', 'key' => 'p'), 'total_items' => $r->hits_tot, 'items_per_page' => $r->limit, 'view' => 'pagination/basic'));
     $this->template->primary = $r;
     $this->template->pager = $p;
     $params['l'] = 1;
     $this->template->favorited = Sourcemap_Search_Simple::find($params + array('favorited' => 'yes'));
     $this->template->discussed = Sourcemap_Search_Simple::find($params + array('comments' => 'yes'));
     $this->template->interesting = Sourcemap_Search_Simple::find($params + array('favorited' => 'yes', 'comments' => 'yes'));
     $this->template->recent = Sourcemap_Search_Simple::find($params + array('recent' => 'yes'));
 }
Example #4
0
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with this
 * program. If not, see <http://www.gnu.org/licenses/>.*/
// category options
$taxonomy = Sourcemap_Taxonomy::load_tree();
$cat_opts = array();
$cat_opts[] = array(0, 'None');
$valid_cats = array(0);
$flat_cats = Sourcemap_Taxonomy::flatten();
$_p = array();
foreach ($flat_cats as $i => $cat) {
    list($id, $nm, $title, $depth) = $cat;
    if ($depth < count($_p)) {
        while (count($_p) > $depth) {
            array_pop($_p);
        }
    }
    $_p[] = $title;
    $valid_cats[] = $id;
    if ($id) {
        $cat_opts[] = array($id, join(' / ', array_slice($_p, 1)));
    }
}
return array('fields' => array('title' => array('type' => 'text', 'label' => 'Title', 'attributes' => array('maxlength' => 55, 'placeholder' => 'Maximum 55 characters.')), 'description' => array('type' => 'textarea', 'label' => 'Description', 'css_class' => array('preview'), 'attributes' => array('maxlength' => 140, 'placeholder' => 'Maximum 140 characters.')), 'tags' => array('type' => 'text', 'label' => 'Tags', 'attributes' => array('placeholder' => 'Separated by spaces.')), 'category' => array('type' => 'select', 'label' => 'Category', 'options' => $cat_opts, 'default' => 0), 'publish' => array('type' => 'checkbox', 'label' => 'Public', 'default' => 0), 'create' => array('type' => 'submit', 'label' => 'Create')), 'messages_file' => 'forms/create', 'rules' => array('title' => array(array('not_empty')), 'category' => array(array('in_array', array($valid_cats))), 'description' => array(array('max_length', array(140)))), 'filters' => array());
Example #5
0
 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');
     }
 }
Example #6
0
 public function fetch()
 {
     parent::fetch();
     if (!isset($this->parameters['q'])) {
         $this->parameters['q'] = '';
     }
     $search = ORM::factory('supplychain_search');
     // category filter
     $clause_category = false;
     if (isset($this->parameters['c'])) {
         $cat = $this->parameters['c'];
         $csql = 'select id from category where name ilike \'%\'||:catq||\'%\'';
         $query = DB::query(Database::SELECT, $csql);
         $query->param(':catq', $cat);
         $rows = $query->execute();
         $cat_ids = array();
         foreach ($rows as $i => $row) {
             $cat_ids[] = $row['id'];
             $children = Sourcemap_Taxonomy::load_children($row['id']);
             foreach ($children as $j => $child) {
                 if (!in_array($child->id, $cat_ids)) {
                     $cat_ids[] = $child->id;
                 }
             }
         }
         if ($cat_ids) {
             $search->and_where('category', 'in', $cat_ids);
         } else {
             // pass
         }
     }
     if (isset($this->parameters['q']) && $this->parameters['q']) {
         $qts = preg_split('/\\s+/', $this->parameters['q'], null, PREG_SPLIT_NO_EMPTY);
         $q = array();
         foreach ($qts as $i => $qt) {
             $q[] = $qt;
         }
         if ($q) {
             $search->and_where(DB::expr('to_tsvector(body)'), '@@', DB::expr('plainto_tsquery(\'english\',' . Database::instance()->quote(join(' ', $q)) . ')'));
         }
     }
     // by userid
     if (isset($this->parameters['user']) && (int) $this->parameters['user']) {
         $search->and_where('user_id', '=', $this->parameters['user']);
     }
     $search->reset(false);
     $ct = $search->count_all();
     // featured filter
     if (isset($this->parameters['featured']) && strtolower($this->parameters['featured']) == 'yes') {
         $search->and_where(DB::expr('featured'), 'and', DB::expr('TRUE'));
     }
     // recent filter
     if (isset($this->parameters['recent']) && strtolower($this->parameters['recent']) == 'yes') {
         $search->order_by('created', 'desc');
     }
     // most commented
     if (isset($this->parameters['comments']) && strtolower($this->parameters['comments']) == 'yes') {
         $search->order_by('comments', 'desc');
     }
     // most favorited
     if (isset($this->parameters['favorited']) && strtolower($this->parameters['favorited']) == 'yes') {
         $search->order_by('favorited', 'desc');
     }
     $search->limit($this->limit);
     $search->offset($this->offset);
     $raw = $search->find_all();
     $results = self::prep_rows($raw);
     $this->results->hits_tot = $ct;
     $this->results->results = $results;
     $this->results->limit = $this->limit;
     $this->results->offset = $this->offset;
     $this->results->hits_ret = count($results);
     $this->results->parameters = $this->parameters;
     return $this->results;
 }
Example #7
0
 public function action_index()
 {
     $this->template->tree = Sourcemap_Taxonomy::load_tree(null);
 }
Example #8
0
<?php 
if ($taxonomy) {
    ?>
<div id="category-list">
    <div class="container">
            <div id="category-list-content">
                <a href="browse/">Everything &nbsp;</a>
                <?php 
    for ($i = 0; $i < count($taxonomy->children); $i++) {
        ?>
                <?php 
        $t = $taxonomy->children[$i];
        ?>
                <a href="browse/<?php 
        echo Sourcemap_Taxonomy::slugify($t->data->name);
        ?>
"<?php 
        if (count($taxonomy->children) - 1 == $i) {
            ?>
 class="last"<?php 
        }
        ?>
><?php 
        echo HTML::chars($t->data->title);
        ?>
</a>
                <?php 
    }
    ?>
            </div>