Last active
May 17, 2016 02:05
-
-
Save vadremix/22b4b6fe84ef7655ef1900ef0bad42ad to your computer and use it in GitHub Desktop.
story controller used on nsinformer.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
// This is the controller for nsinformer.com landing pages. | |
// I wrote this in 2014. These comments were written a couple years later. | |
class Stories extends CI_Controller { | |
public function index($url) | |
{ | |
$this->load->database(); | |
$this->load->model('Storymodel'); | |
// retrieves story | |
$data['story'] = $this->Storymodel->retrieve_story($url); | |
// check if story was retrieved, return 404 otherwise | |
if(isset($data['story']['0']['id'])) { | |
// the following logic is used to determine whether or not to track the landing page's hit. | |
// it would make sense for this to be a helper function instead | |
// ['time'] is a unix timestamp | |
$story_age = time() - $data['story']['0']['time']; | |
// landing page hits should not be counted if story was generated less than 20 seconds ago | |
// now, I would be more inclined to do something more like this... | |
/* | |
$hit_valid['status'] = true; | |
if($story_age <= NEW_STORY) { | |
$hit_valid['status'] = false; | |
$hit_valid['message'] = 'Story generated less than ' . NEW_STORY . ' seconds ago.'; | |
} | |
*/ | |
if($story_age <= 20) { | |
$hit_suppress = true; | |
} else { | |
$hit_suppress = false; | |
} | |
// attempt to retrieve data about whether user has already hit landing page in question | |
$hit_identifier = $data['story']['0']['id']; | |
// returns an empty string if not in client cookie | |
$hit_tracker = $this->session->userdata($hit_identifier); | |
// check conditions that would invalid tracking the hit | |
if($hit_tracker == '' && $hit_suppress == false) { | |
// track the hit | |
$this->Storymodel->hit_story($url, $data['story']['0']['hits']); | |
// redundantly retrieving story... | |
$data['story'] = $this->Storymodel->retrieve_story($url); | |
// add hit to codeigniter session, so that a client refreshing the page does not increment | |
// the hit counter | |
$hit = array( | |
$data['story']['0']['id'] => 1 | |
); | |
$this->session->set_userdata($hit); | |
}; | |
// prepare data to pass to view | |
$data['title'] = $data['story']['0']['name']; | |
// looks like I didn't know how to use ternary logic | |
if($data['story']['0']['hits'] == '1') { $data['people'] = 'person'; } else { $data['people'] = 'people'; }; | |
$data['page_stories'] = 1; | |
// build the page. This could and should be DRYer. If the story is a 'new story' (less than 20s old), | |
// load a template that provides sharing instuctions. I decided on this approach so that I could direct | |
// the user to the URL they would copy and paste after they created a story | |
if($story_age <= 20) { | |
$this->load->view('templates/header', $data); | |
$this->load->view('new-stories'); | |
$this->load->view('templates/footer'); | |
} else { | |
$this->load->view('templates/header', $data); | |
$this->load->view('stories'); | |
$this->load->view('templates/footer'); | |
} | |
} else { | |
$data['title'] = '404'; | |
$this->load->view('templates/header', $data); | |
$this->load->view('fourohfour'); | |
$this->load->view('templates/footer'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment