Skip to content

Instantly share code, notes, and snippets.

@phirschybar
Created March 17, 2012 03:48
Show Gist options
  • Save phirschybar/2054783 to your computer and use it in GitHub Desktop.
Save phirschybar/2054783 to your computer and use it in GitHub Desktop.
Lint error dialog
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Collection extends APP_Controller {
function Collection(){
parent::APP_Controller();
$this->_add_crumb('Collection', '/collection/');
$this->load->library('user_agent');
$this->load->model('Piece');
$this->themes = $this->Piece->themes();
$this->view->set('themes', $this->themes);
$this->geography = $this->Piece->geography();
$this->view->set('geography', $this->geography);
$this->phases = $this->Piece->phases();
$this->view->set('phases', $this->phases);
$this->tags = $this->Piece->tags();
$this->view->set('tags', $this->tags);
$this->load->model('Page');
$this->cats_raw = $this->Page->get(array('parent' => 5));
$this->categories = $this->cats_raw;
unset($this->categories[42]);
$this->view->set('categories', $this->categories);
$this->view->set('curr_section', 5);
$this->view->linkJS(JS.'chosen.jquery.min.js');
}
function add($id){
if ($this->tank_auth->is_logged_in()){
$me = $this->db->query("SELECT * FROM users WHERE id = '".$this->tank_auth->get_user_id()."'")->row_array();
$my = explode(',', $me['pieces']);
if (!in_array($id, $my)){
array_push($my, $id);
}
$this->db->where('id', $me['id']);
$this->db->update('users', array('pieces' => implode(',', $my)));
$this->_set_flash('success', 'This piece has been added to your collection. <a href="/auth/me/">View my collection</a>');
redirect('/collection/detail/'.$id);
} else {
$this->_set_flash('success', 'You need to be logged in to add pieces to your collection. <a href="/auth/login/">Login</a> or <a href="/auth/register/">Register Here</a>');
redirect('/collection/detail/'.$id.'/');
}
}
function remove($id){
if ($this->tank_auth->is_logged_in()){
$me = $this->db->query("SELECT * FROM users WHERE id = '".$this->tank_auth->get_user_id()."'")->row_array();
$my = explode(',', $me['pieces']);
for ($i = 0 ; $i < count($my) ; $i++){
if ($my[$i] == $id){
unset($my[$i]);
break;
}
}
$this->db->where('id', $me['id']);
$this->db->update('users', array('pieces' => implode($my)));
$this->_set_flash('success', 'This piece has been removed from your collection.');
redirect('/auth/me/');
} else {
$this->_set_flash('success', 'You need to be logged in to add pieces to your collection. <a href="/auth/login/">Login</a> or <a href="/auth/register/">Register Here</a>');
redirect('/collection/detail/'.$id.'/');
}
}
function mini_category($cat){
$data = $this->Piece->get(array('category' => $cat, 'order' => 'RAND()', 'has_image' => true, 'limit' => 1));
$data = current($data);
foreach ($this->phases as $phase){
if ($phase['start_date'] < $data['earliest_year'] && $phase['end_date'] > $data['earliest_year']){
$this->view->set('phase_link', '/collection/phase/'.$phase['id'].'/'.url_title($phase['name']));
break;
}
}
$this->view->set('curr_cat', $cat);
$this->view->set('piece', $data);
$this->view->part('content', '_mini_category');
$this->_render_ajax();
}
function category($id){
$this->view->set('page_title', $this->categories[$id]['name'].' : In the Collection');
$this->view->set('curr_cat', $id);
$this->view->set('pieces', $this->Piece->get(array('category' => $id, 'order' => 'name ASC', 'has_image' => true)));
$this->view->set('body_class', 'inner');
$this->view->part('inner', 'collection_list');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function detail($id) {
$data = $this->Piece->get_info($id);
$related = $this->Piece->get(array('tags' => $data['tags'], 'order' => 'RAND()', 'limit' => 3, 'has_image' => true));
$tag = ucwords(current(to_array($data['tags'])));
$this->_add_crumb($tag, '/collection/tag/'.strtolower($tag).'/');
foreach ($this->phases as $phase){
if ($phase['start_date'] < $data['earliest_year'] && $phase['end_date'] > $data['earliest_year']){
$this->view->set('phase_link', '/collection/phase/'.$phase['id'].'/'.url_title($phase['name']));
break;
}
}
$this->view->set('related', $related);
$this->view->set('page_title', $data['name']);
$this->view->set('piece', $data);
$this->view->set('body_class', 'inner');
$this->view->linkJS(JS.'cloud-zoom.1.0.2/cloud-zoom.1.0.2.min.js');
$this->view->part('inner', 'collection_item');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function index(){
if($this->agent->is_mobile() && $this->session->userdata('user_agent') !== 'desktop' ) {
$this->view->set('ref', $this->_referrer());
$alt = $this->Page->get_info(42);
$this->view->set('img', $this->Page->get_info(5));
$this->view->set('namey', 'Collection');
$this->view->part('header','mobile/mobile_header');
$this->view->part('footer','mobile/mobile_footer');
$this->view->load('mobile/mobile_sub');
}
else {
$this->is_collection_home = true;
$this->view->set('is_collection_home', true);
$this->view->set('page_title', 'Collection');
$this->view->set('data', $this->Page->get_info(5));
$themes = array(0 => 6, 1 => 8);
$this->theme($themes[rand(0, count($themes)-1)]);
}
}
function phase($id){
$this->view->set('page_title', 'In the Collection from '.$this->phases[$id]['name']);
$this->view->set('curr_phase', $id);
$this->view->set('pieces', $this->Piece->get(array('between_min' => $this->phases[$id]['start_date'], 'between_max' => $this->phases[$id]['end_date'], 'order' => 'name ASC', 'has_image' => true)));
$this->view->set('body_class', 'inner');
$this->view->part('inner', 'collection_list');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function region($id){
$this->view->set('page_title', 'In the Collection from '.$this->geography[$id]['name']);
$this->view->set('curr_geography', $id);
$this->view->set('pieces', $this->Piece->get(array('region' => $this->geography[$id]['name'], 'order' => 'name ASC', 'has_image' => true)));
$this->view->set('body_class', 'inner');
$this->view->part('inner', 'collection_list');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function search(){
// note: all tables need to have fulltext index for each field being used.
// note: must use same number of select_fields
$this->index = array(
0 => array(
'table' => 'pages',
'select_fields' => 'name AS title, description, image',
'search_fields' => 'name, description',
'where' => 'AND publish = \'1\''
),
1 => array(
'table' => 'events',
'select_fields' => 'name AS title, description, image',
'search_fields' => 'name, description',
'where' => 'AND end_date >= CURDATE() ',
),
2 => array(
'table' => 'exhibitions',
'select_fields' => 'name AS title, description, image',
'search_fields' => 'name, description'
),
3 => array(
'table' => 'geography',
'select_fields' => 'name AS title, description, \'null\' AS image',
'search_fields' => 'name, description',
'link_prefix' => '/collection/region/',
'single' => 'Geographic Region'
),
4 => array(
'table' => 'news',
'select_fields' => 'name AS title, description, image',
'search_fields' => 'name, description',
'where' => 'AND publish = \'1\'',
'single' => 'News Item'
),
5 => array(
'table' => 'themes',
'select_fields' => 'name AS title, blurb_2 AS description, \'null\' AS image',
'search_fields' => 'name, blurb_2',
'link_prefix' => '/collection/theme/'
),
6 => array(
'table' => 'videos',
'select_fields' => 'name AS title, description, image',
'search_fields' => 'name, description',
'where' => 'AND publish = \'1\''
)
);
$this->load->helper('inflector');
$q = $this->input->get('q');
$data = $this->_run_text_query(addslashes($q));
$this->view->set('data', $data);
$this->view->set('index', $this->index);
$this->view->set('page_title', 'Search Results matching "'.$q.'"');
$this->view->set('pieces', $this->Piece->get(array('tags' => $q, 'term' => $q, 'order' => 'name ASC', 'has_image' => true, 'limit' => 20)));
$this->view->set('body_class', 'inner');
$this->view->part('app', '_search');
$this->view->part('inner', 'collection_list');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function tag($tag){
$tag = urldecode($tag);
if ($tag == 'Sculpture Garden'){
redirect('/pages/detail/155/Works-in-the-Garden');
}
$this->view->set('page_title', 'In the Collection tagged "'.$tag.'"');
$this->view->set('curr_tag', $tag);
$this->view->set('pieces', $this->Piece->get(array('tags' => $tag, 'order' => 'name ASC', 'has_image' => true)));
$this->view->set('body_class', 'inner');
$this->view->part('inner', 'collection_list');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function theme($id){
if (!isset($this->is_collection_home)){
$this->view->set('page_title', $this->themes[$id]['name']);
$this->view->set('curr_theme', $id);
}
$this->view->set('children', $this->cats_raw);
$this->view->set('nephews', $this->Page->get(array('parent' => 42)));
$all_pages = $this->Page->get();
$nieces = array();
$child_ids = array_keys($this->categories);
foreach ($all_pages as $item){
if (in_array($item['parent'], $child_ids)){
$nieces[$item['parent']][] = $item;
}
}
$this->view->set('nieces', $nieces);
$this->view->set('pieces', $this->Piece->get(array('tags' => $this->themes[$id]['name'], 'limit' => 12, 'order' => 'RAND()', 'has_image' => true)));
$this->view->set('set_theme', $id);
$this->view->set('body_class', 'collection-theme');
$this->view->part('shell_app', '_theme_intro');
$this->view->part('inner', 'collection_theme');
$this->view->part('content', 'collection_shell');
$this->view->load('shell');
}
function _run_text_query($q) {
$cache_group = 'search';
$cache_name = 'query-'.md5($q);
$this->load->driver('cache', array('adapter' => 'file'));
if (!$data = $this->cache->get($cache_name, $cache_group)) {
$i = 0;
$sql = "SELECT * FROM ( ";
foreach ($this->index as $item){
$sql .= "SELECT id, ".$item['select_fields'].", '".$item['table']."' AS slug, MATCH(".$item['search_fields'].") "
. "AGAINST('".$q."') AS score FROM ".$item['table']." WHERE MATCH(".$item['search_fields'].") AGAINST('".$q."') ";
$sql .= isset($item['where']) ? $item['where']." " : "";
$sql .= $i < count($this->index) -1 ? "UNION " : "";
$i++;
}
$sql .= ") a ORDER BY score DESC LIMIT 30";
$data = $this->db->query($sql)->result_array();
$cached = $this->cache->save($cache_name, $data, $cache_group, 60 * 1); // TTL: 5 minutes
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment