Skip to content

Instantly share code, notes, and snippets.

@martisj
Created March 14, 2012 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martisj/2035927 to your computer and use it in GitHub Desktop.
Save martisj/2035927 to your computer and use it in GitHub Desktop.
fatty bloat contoller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
private $data;
private $user_id;
private $tab_url;
function __construct() {
parent::__construct();
$this->load->library('facebook');
$tab_url = 'http://www.facebook.com/fuostcomp?sk=app_227293504022231';
// $this->output->enable_profiler(TRUE);
}
public function index()
{
// print_r($_REQUEST);
// $data['fbarr'] = $this->facebook;
$user = $this->facebook->getUser();
// $user_id = $signed_req['user_id'];
// $data['userings'] = $user;
// $data['sreq'] = $signed_req;
if ($user)
{
try
{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $this->facebook->api('/me');
$data['main_content']['user_profile'] = $user_profile;
}
catch (FacebookApiException $e)
{
error_log($e);
$user = null;
}
}
//!!!!Strictly for testing!!!
// $like_stat = TRUE;
if ($this->_is_page_liked())
{
$data['header']['title'] = 'Folkeuniversitetet Øst Store Sangkonkurranse';
$data['main_content']['top_five'] = $this->site_model->get_top_five();
$data['view_to_load'] = 'liked';
$this->load->view('includes/template', $data);
}
else
{
$this->_load_unliked_view();
}
}
public function bidrag($id = 'all')
{
$all = $this->site_model->get_submissions();
if ($id === 'all')
{
if ( ! empty($all))
{
$data['main_content']['all'] = $all;
$data['header']['title'] = 'Alle bidrag';
}
else
{
$data['header']['title'] = 'Ingen bidrag funnet';
}
$data['view_to_load'] = 'alle_bidrag';
$this->load->view('includes/template', $data);
}
else
{
if ( ! empty($all))
{
$data['main_content']['all'] = $all;
$result = $this->site_model->get_submission($id);
if ( ! empty($result))
{
$data['header']['styles'] = array(base_url('assets/css/skin/jplayer_fuost.css'));
$data['header']['script_vars'] = array('audio_file' => base_url('uploads/' . $result->audio_file_name));
$data['header']['scripts'] = array('https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js',
base_url('assets/js/libs/jquery.jplayer.min.js'),
base_url('assets/js/load_jplayer.js')
);
$data['main_content']['result'] = $result;
$data['main_content']['vote_count'] = $this->site_model->count_votes($id);
$data['header']['title'] = $result->group_name . ' - ' . $result->song_name;
$video_id = '/' . $result->video_id;
$data['main_content']['like_url'] = current_url();
$this->facebook->getAccessToken();
try
{
$video_array = $this->facebook->api($video_id);
// echo '<pre>';
// print_r($video_array);
// echo '</pre>';
// exit;
$data['main_content']['video_embed'] = $video_array['format'][1]['embed_html'];
}
catch (Exception $e)
{
// echo '<pre>';
// print_r($e);
// echo '</pre>';
}
}
else
{
$data['header']['title'] = 'Fant ingen bidrag med ID: ' . $id;
}
// $data['id_gotten'] = $id;
$data['main_content']['id_gotten'] = $id;
}
else
{
$data['header']['title'] = 'Ingen bidrag funnet';
}
if ($this->input->post('vote'))
{
//!!!STRICTLY FOR TESTING ONLY
$user_id = '893509325098';
// $signed_req = $this->facebook->getSignedRequest();
// $user_id = $signed_req['user_id'];
if ($this->site_model->add_vote($user_id, $id))
{
// $data['vote_message'] = 'Takk for din stemme';
$data['main_content']['vote_message'] = 'Takk for din stemme';
}
else
{
// $data['vote_message'] = 'Du har allerede stemt';
$data['main_content']['vote_message'] = 'Du har allerede stemt';
}
}
$data['view_to_load'] = 'bidrag';
$this->load->view('includes/template', $data);
}
}
public function pamelding()
{
$data['header']['scripts'] = array('https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js');
$this->load->library('form_validation');
// $this->form_validation->set_rules('navn', 'navn', 'trim|required|max_length[99]|xss_clean');
$this->form_validation->set_rules('sangnavn', 'sangnavn', 'trim|required|max_length[99]|xss_clean');
// $this->form_validation->set_rules('epost', 'epost', 'trim|required|valid_email|max_length[99]|xss_clean');
$this->form_validation->set_rules('gruppenavn', 'gruppenavn', 'trim|required|max_length[99]|xss_clean');
if ($this->form_validation->run() === FALSE)
{
$data['header']['title'] = 'Send inn bidrag';
$data['view_to_load'] = 'pamelding';
$this->load->view('includes/template', $data);
}
else
{
//TODO: handle responses from facebook video-graph api call
// form validation passed - time to upload file
$this->load->library('upload');
if ( ! $this->upload->do_upload())
{
$data['main_content']['upload_error'] = $this->upload->display_errors();
// upload error - redisplay form
$data['header']['title'] = 'Send inn bidrag';
$data['view_to_load'] = 'pamelding';
$this->load->view('includes/template', $data);
}
else
{
$upload_data = $this->upload->data();
//DEBUGGING
echo '<pre>';
print_r($upload_data);
echo '</pre>';
if ($upload_data['file_ext'] === '.mp3')
{
$insert_id = $this->_do_mp3_upload($upload_data);
}
else
{
$insert_id = $this->_do_facebook_video_upload($upload_data);
}
$this->_show_successmessage($insert_id);
}
}
}
public function vilkar()
{
$header['title'] = 'Vilkår for Folkeuniversitetets Sangkonkurranse';
$data['header'] = $this->load->view('includes/header', $header, TRUE);
$data['view_to_load'] = 'vilkar';
$this->load->view('vilkar', $data);
}
private function _show_successmessage($id)
{
$data['header'] = array('title' => 'Opplastingen var vellykket');
$data['main_content']['id'] = $id;
$data['view_to_load'] = 'kvittering';
$this->load->view('includes/template', $data);
}
private function _do_facebook_video_upload($file_data)
{
$video_path = realpath($file_data['full_path']);
// echo '<pre>' . $video_path . '</pre>';
$this->facebook->getAccessToken();
try
{
$signed_request = $this->facebook->getSignedRequest();
$video = $this->facebook->api('239965599421476/videos',
'POST',
array('video' => '@' . $video_path,
'title' => 'title for the win',
'description' => 'description from inside the controller'
));
//DEBUGGING
// echo '<pre>';
// print_r($video);
// echo '</pre>';
// video_id, by_user_id, email, song_name, group_name, file_name
$insert_data = array('video_id' => $video['id'],
// 'video_id' => '10151326747300453',
'by_user_id' => $signed_request['user_id'],
// 'by_user_id' => '600960452',
'email' => $this->input->post('epost'),
'song_name' => $this->input->post('sangnavn'),
'group_name' => $this->input->post('gruppenavn')
);
$insert_id = $this->site_model->add_submission($insert_data);
//DEBUGGING
// echo '<pre>';
// print_r($insert_data);
// echo '</pre>';
}
catch (FacebookApiException $e)
{
// echo '<pre>';
// print_r($e);
// echo '</pre>';
}
$insert_data = array(//'media_id' => $video['id'],
'video_id' => '10151326747300453',
// 'by_user_id' => $signed_request['user_id'],
'by_user_id' => '600960452',
'name' => $this->input->post('navn'),
'email' => $this->input->post('epost'),
'song_name' => $this->input->post('sangnavn'),
'group_name' => $this->input->post('gruppenavn')
);
// $insert_id = $this->site_model->add_submission($insert_data);
//DEBUGGING
// echo '<pre>';
// print_r($insert_data);
// echo '</pre>';
// form validation passed
// upload to CI ok
// add facebook code to send it here
// $this->_show_successmessage($insert_id);
return $this->site_model->add_submission($insert_data);
}
private function _do_mp3_upload($file_data)
{
$insert_data = array('video_id' => NULL,
'by_user_id' => '600960452', //Make sure this is the user id from facebook.
'name' => 'Martin Sjåstad', //Get from facebook user info function
'email' => 'm.sjastad@gmail.com', //Get from facebook user info function
'song_name' => $this->input->post('sangnavn'),
'group_name' => $this->input->post('gruppenavn'),
'audio_file_name' => $file_data['file_name']
);
return $this->site_model->add_submission($insert_data);
}
private function _test_upload()
{
// Upload a photo to a user’s profile
// Your app needs photo_upload permission for this to work
// $facebook->setFileUploadSupport(true);
$this->facebook->setAccessToken('AAADOuOSRJtcBAGJqecWtsZCu0iPS0oRsBzYXydIlZAePlWQBbnuSUxTMtMZCmPXYqJElW4X9VB9HLxNb9Y6ZB9y4NZB5ykUZA565BphztldAZDZD');
$test = 'incoming';
$vid = realpath('./assets/vid_000.mp4');
$video = $this->facebook->api('239965599421476/videos', 'POST', array('video' => '@' . $vid,
'title' => $test,
'description' => 'what a nice description'
));
echo '<pre>video ID: ' . print_r($video) . '</pre>';
}
private function _is_page_liked()
{
// return TRUE;
$sr = $this->facebook->getSignedRequest();
if ($sr['page']['liked'])
{
return TRUE;
}
else
{
return FALSE;
}
}
private function _load_unliked_view()
{
$data['header'] = array('title' => 'Folkeuniversitetet Øst Store Sangkonkurranse');
$data['footer'] = array('dont_show_vilkar' => TRUE);
$data['main_content'] = array();
$data['view_to_load'] = 'unliked';
$this->load->view('includes/template', $data);
}
private function _handle_fb_login()
{
$user = $this->$facebook->getUser();
if ($user)
{
try
{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e)
{
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
}
}
/* End of file site.php */
/* Location: ./application/controllers/site.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment