Created
April 13, 2013 21:47
-
-
Save everdaniel/5380205 to your computer and use it in GitHub Desktop.
Demo files for a Competition site built for Cisco Saudi Arabia
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 | |
/** | |
* Account Controller | |
* | |
*/ | |
class Account extends MY_Controller | |
{ | |
/** | |
* Controller Constructor | |
*/ | |
function __construct() | |
{ | |
parent::MY_Controller(); | |
// Load this controller lang file | |
$this->lang->load('account'); | |
} | |
/** | |
* Index method | |
*/ | |
function index() | |
{ | |
// Render My Account page | |
$this->my(); | |
} | |
/** | |
* Logs in user | |
*/ | |
function login() | |
{ | |
// If the user is already logged in, redirect the user to his account page | |
if ($this->user_is_logged_in) | |
redirect($this->config->item('auth_url_myaccount')); | |
// Auth error array empty by default | |
$this->local_vars['auth_error'] = array(); | |
// Check if the login form is correct | |
if ($this->form_validation->run('login')) | |
{ | |
// Login the user | |
$valid = $this->auth->login( | |
$this->input->post('username_or_email', TRUE), | |
$this->input->post('password', TRUE) | |
); | |
// If there were any errors, get the messages | |
$this->local_vars['auth_error'] = $this->auth->get_error(); | |
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) | |
{ | |
// JSON array | |
$json = array(); | |
$json['status'] = 'success'; | |
$json['message'] = ''; | |
// If login was valid, render the user info view | |
if ($valid) | |
{ | |
$user = $this->session->userdata('user'); | |
$user_info['user_is_logged_in'] = TRUE; | |
$user_info['user_full_name'] = $user['first_name'] . ' ' . $user['last_name']; | |
$user_info['avatar'] = $this->auth_model->get_avatar(NULL, '34x35'); | |
$json['payload'] = $this->load->view('layout/user_info.php', $user_info, TRUE); | |
} | |
else | |
{ | |
$json['status'] = 'error'; | |
$json['message'] = $this->local_vars['auth_error']; | |
} | |
header('Content-Type: application/json'); | |
echo json_encode($json); | |
exit; | |
} | |
else | |
{ | |
// If login was valid, redirect to the my account page | |
if ($valid) | |
{ | |
redirect($this->config->item('auth_url_myaccount')); | |
} | |
} | |
} | |
// Load validate plugin and related files | |
$this->assets['js'][] = array( | |
'sourced/validate/jquery.validate.min.js', | |
'validations/login.mcy.js' | |
); | |
// Init tag selector | |
$this->dom_ready['tag_selector'] = 'initLoginValidation();'; | |
// Render the whole page | |
$this->page_class = array_merge($this->page_class, array('ciscomcy')); | |
$this->page_titles[] = lang('account_login_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view("account/login.php", $this->local_vars, TRUE); | |
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) | |
{ | |
// JSON array | |
$json = array(); | |
$json['status'] = 'error'; | |
$json['message'] = strip_tags(validation_errors()); | |
header('Content-Type: application/json'); | |
echo json_encode($json); | |
exit; | |
} | |
else | |
$this->render_page(); | |
} | |
/** | |
* Logs out current user | |
*/ | |
function logout() | |
{ | |
$this->auth->logout(); | |
redirect('home'); | |
} | |
/** | |
* User Sign Up | |
*/ | |
function sign_up() | |
{ | |
// If the user is already logged in, redirect the user to his account page | |
if ($this->user_is_logged_in) | |
redirect($this->config->item('auth_url_myaccount')); | |
// Auth error array empty by default | |
$this->local_vars['auth_error'] = array(); | |
// Auth error array empty by default | |
$this->local_vars['auth_error'] = array(); | |
// Load upload library | |
$this->load->library('upload'); | |
// Save avatar uploading status in session | |
if ($this->session->userdata('uploaded_avatar') === FALSE) | |
$this->session->set_userdata('uploaded_avatar', FALSE); | |
// Avatar | |
if ($this->session->userdata('avatar_data') === FALSE) | |
$this->session->set_userdata('avatar_data', array()); | |
// Handle Upload | |
if (($this->input->server('REQUEST_METHOD') == 'POST') && | |
($this->upload->do_upload('avatar'))) | |
{ | |
// Load Image Resizer library | |
$this->load->library('Community_Image'); | |
// Load Image Model | |
$this->load->model('image_model'); | |
// Get the default avatar size | |
$avatar_size = $this->config->item('cisco_site_default_avatar_size'); | |
// Get file data | |
$upload_data = $this->upload->data(); | |
// Save file data in the database | |
$ID_image = $this->image_model->add_image($upload_data); | |
// Merge the image ID and thumb path in the file data array | |
$upload_data['ID_image'] = $ID_image; | |
$upload_data['thumb_path'] = $this->community_image->generate_thumb($upload_data, $avatar_size); | |
// and save this in the session in case the form doesn't validate | |
$this->session->set_userdata('uploaded_avatar', TRUE); | |
$this->session->set_userdata('avatar_data', $upload_data); | |
} | |
// Get avatar from session | |
$this->local_vars['avatar_data'] = $this->session->userdata('avatar_data'); | |
// Check if the login form is correct | |
if ($this->form_validation->run('sign_up')) | |
{ | |
// Everything is correct, add the user | |
$new_user = $this->auth->add_new_user(); | |
// Check if the user uploaded an avatar | |
if ($this->session->userdata('uploaded_avatar')) | |
{ | |
// it did, get the avatar data | |
$avatar = $this->session->userdata('avatar_data'); | |
// and set the user with this avatar | |
$this->auth_model->set_avatar($avatar['ID_image'], $new_user['ID_user']); | |
// Load Image Model, just in case | |
$this->load->model('image_model'); | |
// Set owner of this image to this user | |
$this->image_model->set_owner($avatar['ID_image'], $new_user['ID_user']); | |
} | |
// Now try to log the user in | |
$valid = $this->auth->login( | |
$this->input->post('email', TRUE), | |
$this->input->post('password', TRUE) | |
); | |
// and redirect him to the my account page | |
if ($valid) redirect($this->config->item('auth_url_myaccount')); | |
// if the above didn't work, then redirect the user to login | |
redirect($this->config->item('auth_url_login')); | |
} | |
else | |
{ | |
// Get this only if form was submitted at least ONCE | |
$this->local_vars['avatar_error'] = $this->upload->display_errors('<p style="color: red; font-weight: bold;">', '</p>'); | |
} | |
// Get errors from auth lib, if any | |
$this->local_vars['auth_error'] = $this->auth->get_error(); | |
// Load validate plugin and related files | |
$this->assets['js'][] = array( | |
'sourced/validate/jquery.validate.min.js', | |
'validations/signup.mcy.js' | |
); | |
// Init tag selector | |
$this->dom_ready['tag_selector'] = 'initUserSignupValidation();'; | |
// Set page and content and finally, render this page | |
$this->page_titles[] = lang('account_signup_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view("account/sign_up.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* My account user page | |
*/ | |
function my() | |
{ | |
// If user is not logged in, then redirect to login page | |
if ( ! $this->user_is_logged_in) | |
redirect($this->config->item('auth_url_login')); | |
if (strtolower($this->input->server('REQUEST_METHOD')) == 'post') | |
{ | |
// Load upload library | |
$this->load->library('upload'); | |
// Handle Upload | |
if ($this->upload->do_upload('avatar')) | |
{ | |
// Load Image Resizer library | |
$this->load->library('Community_Image'); | |
// Load Image Model | |
$this->load->model('image_model'); | |
// Get the default avatar size | |
$avatar_size = $this->config->item('cisco_site_default_avatar_size'); | |
// Get file data | |
$upload_data = $this->upload->data(); | |
// Save file data in the database | |
$ID_image = $this->image_model->add_image($upload_data); | |
// and set the user with this avatar | |
$this->auth_model->set_avatar($ID_image, $this->session->userdata('ID_user')); | |
// Set owner of this image to this user | |
$this->image_model->set_owner($ID_image, $this->session->userdata('ID_user')); | |
} | |
$user = $this->session->userdata('user'); | |
$this->session->set_flashdata('messages', sprintf(lang('account_flasmsg_avatarupdate'), $user['first_name'])); | |
redirect('my/account'); | |
} | |
// Get the user data and add it to local vars | |
$user = $this->auth_model->get_row(array('ID_user' => $this->session->userdata('ID_user'))); | |
$this->local_vars['user'] = $user; | |
// Get the user avatar | |
$this->local_vars['avatar'] = $this->auth_model->get_avatar($this->session->userdata('ID_user')); | |
// Load ideas stuff | |
$this->lang->load('idea'); | |
$this->load->helper('text'); | |
$this->load->model('idea_model'); | |
$this->local_vars['ideas'] = $this->idea_model->get_ideas(array('idea.ID_user' => $this->session->userdata('ID_user'))); | |
$this->page_titles[] = sprintf(lang('account_my_page_title'), $user['first_name']); | |
$this->public_vars['content'] = | |
$this->load->view("account/my.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* User profile page | |
*/ | |
function details($ID_user = NULL) | |
{ | |
// If no username was provided, redirect to home | |
if (is_null($ID_user)) | |
redirect('home'); | |
// Load idea model | |
$this->load->model('idea_model'); | |
// Load date helper | |
$this->load->helper(array('date', 'text')); | |
// Load Idea lang | |
$this->lang->load('idea'); | |
// Get user and make sure that the user actually exists | |
if (($user = $this->auth_model->get_row(array('ID_user' => $ID_user))) === FALSE) | |
redirect('home'); | |
// | |
$this->local_vars['user'] = $user; | |
$this->local_vars['ideas'] = $this->idea_model->get_ideas( | |
array('idea.ID_user' => $user['ID_user']), | |
array('idea_posted', 'desc')); | |
// Get the user avatar | |
$this->local_vars['avatar'] = $this->auth_model->get_avatar($user['ID_user']); | |
$this->page_titles[] = sprintf(lang('account_details_page_title'), $user['user_name']); | |
if ((int)$user['display_info'] == 1) | |
$this->page_titles[] = sprintf( | |
lang('account_details_page_title'), | |
$user['first_name'] . ' ' . $user['last_name'] . ' (' . $user['user_name'] . ')'); | |
$this->public_vars['content'] = | |
$this->load->view("account/details.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* Edit my account page | |
*/ | |
function edit() | |
{ | |
// If user is not logged in, then redirect to login page | |
if ( ! $this->user_is_logged_in) | |
redirect($this->config->item('auth_url_login')); | |
// Get the user data and add it to local vars | |
$user = $this->auth_model->get_row(array('ID_user' => $this->session->userdata('ID_user'))); | |
$this->local_vars['user'] = $user; | |
// Get the user avatar | |
$this->local_vars['avatar'] = $this->auth_model->get_avatar(); | |
// Load form validation rules (just in case) | |
$this->config->load('form_validation'); | |
// Get rules for the account edit form | |
$account_edit_rules = $this->config->item('account_edit'); | |
// If the password field is empty, remove password and password_confirm rules | |
if (($this->input->post('password') === FALSE) | |
|| (trim($this->input->post('password')) == '')) | |
{ | |
unset($account_edit_rules[6]); | |
unset($account_edit_rules[7]); | |
} | |
$this->form_validation->set_rules($account_edit_rules); | |
if ($this->form_validation->run('account_edit')) | |
{ | |
// Get the ID from the current user | |
$ID_user = $this->session->userdata('ID_user'); | |
// Initialize user array and then get values from the form | |
$user = array(); | |
/** | |
* Change password only if password was provided | |
*/ | |
if (($this->input->post('password') !== FALSE) | |
&& (trim($this->input->post('password')) != '')) | |
{ | |
$user['salt'] = $this->auth->random_string(); | |
$user['password'] = $this->auth->sha256($this->input->post('password', TRUE) . $user['salt']); | |
} | |
$user['first_name'] = $this->input->post('first_name', TRUE); | |
$user['last_name'] = $this->input->post('last_name', TRUE); | |
$user['email'] = $this->input->post('email', TRUE); | |
$user['school'] = $this->input->post('school', TRUE); | |
$user['phone_number'] = $this->input->post('phone_number', TRUE); | |
$user['address'] = $this->input->post('address', TRUE); | |
$user['display_info'] = 0; | |
if ($this->input->post('display_info', TRUE) !== FALSE) | |
$user['display_info'] = 1; | |
$user['display_name'] = 0; | |
if ($this->input->post('display_name', TRUE) !== FALSE) | |
$user['display_name'] = 1; | |
$user['display_email'] = 0; | |
if ($this->input->post('display_email', TRUE) !== FALSE) | |
$user['display_email'] = 1; | |
$user['display_school'] = 0; | |
if ($this->input->post('display_school', TRUE) !== FALSE) | |
$user['display_school'] = 1; | |
$user['display_phone'] = 0; | |
if ($this->input->post('display_phone', TRUE) !== FALSE) | |
$user['display_phone'] = 1; | |
// Update user with the new data | |
$this->auth_model->update_user($ID_user, $user); | |
// Load upload library | |
$this->load->library('upload'); | |
// Handle Upload | |
if ($this->upload->do_upload('avatar')) | |
{ | |
// Load Image Resizer library | |
$this->load->library('Community_Image'); | |
// Load Image Model | |
$this->load->model('image_model'); | |
// Get the default avatar size | |
$avatar_size = $this->config->item('cisco_site_default_avatar_size'); | |
// Get file data | |
$upload_data = $this->upload->data(); | |
// Save file data in the database | |
$ID_image = $this->image_model->add_image($upload_data); | |
// and set the user with this avatar | |
$this->auth_model->set_avatar($ID_image, $ID_user); | |
// Set owner of this image to this user | |
$this->image_model->set_owner($ID_image, $ID_user); | |
} | |
$this->session->set_flashdata('messages', | |
sprintf(lang('account_flasmsg_accountupdate'), | |
$user['first_name'], | |
'<a href="' . site_url('logout') . '">', | |
'</a>')); | |
redirect('account/my'); | |
} | |
// Load validate plugin and related files | |
$this->assets['js'][] = array( | |
'sourced/validate/jquery.validate.min.js', | |
'validations/account.edit.mcy.js' | |
); | |
// Init validation | |
$this->dom_ready['account_edit_validation'] = 'initAccountEditValidation();'; | |
$this->page_titles[] = lang('account_edit_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view("account/edit.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* Forgot password page | |
*/ | |
function forgot_password() | |
{ | |
// If user is already logged in, redirec him to home | |
if ($this->user_is_logged_in) | |
redirect('home'); | |
$page_view = 'account/forgot_password'; | |
if ($this->form_validation->run('forgot_password')) | |
{ | |
// Get email and user data | |
$email = $this->input->post('email', TRUE); | |
$user = $this->auth->get_user_by_email($email); | |
// Generate new password | |
$new_password = $this->auth->random_string(6, '09az'); | |
$data['salt'] = $this->auth->random_string(); | |
$data['password'] = $this->auth->sha256($new_password . $data['salt']); | |
// Update user | |
$this->auth_model->update_user($user['ID_user'], $data); | |
// Assign new password to array | |
$data['new_password'] = $new_password; | |
// Merge data | |
$data = array_merge($user, $data); | |
// Email user new info | |
$this->_email_forgot_password($data); | |
// For the view | |
$this->local_vars['user'] = $data; | |
// Display success message | |
$page_view = 'account/forgot_password_success'; | |
} | |
else | |
{ | |
// Load validate plugin and related files | |
$this->assets['js'][] = array( | |
'sourced/validate/jquery.validate.min.js', | |
'validations/account.forgot.mcy.js' | |
); | |
$this->dom_ready['forgot_password'] = 'initForgotPasswordValidation();'; | |
} | |
// Render page | |
$this->page_class = array_merge($this->page_class, array('ciscomcy')); | |
$this->page_titles[] = lang('account_forgot_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view($page_view, $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
function _email_forgot_password($data) | |
{ | |
// Load email library | |
$this->load->library('email'); | |
// From field | |
$from_email = $this->config->item('cisco_site_email_from_email'); | |
$from_name = $this->config->item('cisco_site_email_from_name'); | |
$data = array_merge($data, array( | |
'login_url' => site_url('login'), | |
'contact_url' => site_url('contact_us') | |
) | |
); | |
// Load both HTML and Text bodies | |
$html_message = $this->load->view('email/' . $this->current_language . '/account/forgot_html.php', $data, TRUE); | |
$text_message = $this->load->view('email/' . $this->current_language . '/account/forgot_text.php', $data, TRUE); | |
// Send the email | |
$this->email->clear(); | |
$this->email->from($from_email, $from_name); | |
$this->email->to($data['email']); | |
$this->email->subject(lang('account_email_forgot_subject')); | |
$this->email->message($html_message); | |
$this->email->set_alt_message($text_message); | |
return $this->email->send(); | |
} | |
} |
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
$.validator.addMethod("maxwords", function(value, element) { | |
console.log(element); | |
total_words = value.split(/[\s\.\?]+/).length; | |
console.log(total_words); | |
return !(total_words > 300); | |
}), "Your idea in around 300 words or less."; | |
/** | |
* Validations > Submit Idea page ((when user is not logged in) | |
* | |
*/ | |
var initSubmitIdeaValidationFull = function() | |
{ | |
$('#general-validation-error').hide(); | |
$('#form-idea-submit').validate({ | |
errorElement: 'div', | |
errorClass: 'warning-note', | |
errorPlacement: function(error, element) { | |
error.appendTo(element.parent().parent()); | |
}, | |
highlight: function(element, errorClass) { | |
$(element).parent().parent().addClass('error-col'); | |
$('#general-validation-error').show(); | |
}, | |
unhighlight: function(element) { | |
$(element).parent().parent().removeClass('error-col'); | |
$('#general-validation-error').hide(); | |
}, | |
rules: { | |
first_name: { | |
required: true, | |
minlength: 2, | |
maxlength: 130 | |
}, | |
last_name: { | |
required: true, | |
minlength: 2, | |
maxlength: 130 | |
}, | |
email: { | |
required: true, | |
minlength: 4 | |
}, | |
user_name: { | |
required: true, | |
minlength: 4 | |
}, | |
password: { | |
required: true, | |
minlength: 4, | |
maxlength: 32, | |
equalTo: "input[name='password_confirm']" | |
}, | |
password_confirm: { | |
required: true, | |
minlength: 4, | |
maxlength: 32 | |
}, | |
idea_title: { | |
required: true, | |
minlength: 2, | |
maxlength: 130 | |
}, | |
idea_description: { | |
required: true, | |
minlength: 2, | |
maxwords: true | |
} | |
}, | |
messages: messagesIdeaValidationFull | |
}); | |
}; | |
/** | |
* Validations > Submit Idea page (when user is logged in) | |
* | |
*/ | |
var initSubmitIdeaValidationSimple = function() | |
{ | |
$('#general-validation-error').hide(); | |
$('#form-idea-submit').validate({ | |
errorElement: 'div', | |
errorClass: 'warning-note', | |
errorPlacement: function(error, element) { | |
error.appendTo(element.parent().parent()); | |
}, | |
highlight: function(element, errorClass) { | |
$(element).parent().parent().addClass('error-col'); | |
$('#general-validation-error').show(); | |
}, | |
unhighlight: function(element) { | |
$(element).parent().parent().removeClass('error-col'); | |
$('#general-validation-error').hide(); | |
}, | |
rules: { | |
idea_title: { | |
required: true, | |
minlength: 2, | |
maxlength: 130 | |
}, | |
idea_description: { | |
required: true, | |
minlength: 2, | |
maxwords: true | |
} | |
}, | |
messages: messagesIdeaValidationSimple | |
}); | |
}; |
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 | |
/** | |
* Idea Controller | |
* | |
*/ | |
class Idea extends MY_Controller | |
{ | |
/** | |
* Controller Constructor | |
*/ | |
function __construct() | |
{ | |
parent::MY_Controller(); | |
// Load this controller lang file | |
$this->lang->load('idea'); | |
// Load Auth, Idea and Tag models | |
$this->load->model('auth_model'); | |
$this->load->model('idea_model'); | |
$this->load->model('tag_model'); | |
} | |
/** | |
* Index method | |
*/ | |
function index() | |
{ | |
// Redirect to Submit Idea | |
redirect('submit_idea'); | |
} | |
/** | |
* List all ideas | |
* | |
* @param string $sort_by | |
* @param integer $start_index | |
* @param integer $per_page | |
*/ | |
function all($sort_by = 'all', $start_index = 0, $per_page = 10) | |
{ | |
// Load pagination | |
$this->load->library('pagination'); | |
// Load Community Image Library | |
$this->load->library('Community_Image'); | |
// Load Text Helper | |
$this->load->helper('text'); | |
// Load Home lang | |
$this->lang->load('home'); | |
// Make sure Start Index and Per Page are integers | |
$start_index = (int)$start_index; | |
$per_page = (int)$per_page; | |
// Change # of records per page if user requested it | |
if ($this->input->post('ideas_per_page') !== FALSE) | |
$per_page = (int)$this->input->post('ideas_per_page'); | |
// Sorting | |
$order = NULL; | |
$limit = NULL; | |
switch($sort_by) | |
{ | |
case 'recently_added': | |
$page_title = lang('idea_list_recentlyadded_list'); | |
$order = 'idea_posted DESC'; | |
break; | |
case 'most_viewed': | |
$page_title = lang('idea_list_mostviewed_list'); | |
$order = 'idea_hits DESC'; | |
break; | |
case 'random': | |
$page_title = lang('idea_list_random_list'); | |
$order = array('ID_idea', 'random'); | |
break; | |
default: | |
$page_title = lang('idea_list_recentlyadded_list'); | |
$order = 'idea_posted DESC'; | |
} | |
$total_ideas = $this->idea_model->count_ideas(); | |
if ($start_index < 0) $start_index = 0; | |
$max_start_index = floor($total_ideas / $per_page) * $per_page; | |
if ($start_index > $max_start_index) $start_index = $max_start_index; | |
$config = array(); | |
$config['base_url'] = base_url() . 'all_ideas/' . $sort_by . '/'; | |
$config['base_url_end'] = '/' . $per_page; | |
$config['total_rows'] = $total_ideas; | |
$config['per_page'] = $per_page; | |
$config['start_index'] = $start_index; | |
$config['full_tag_open'] = '<div class="paging"><ul>'; | |
$config['full_tag_close'] = '</ul></div> <!-- end paging -->'; | |
$config['prev_tag_open'] = '<li class="prev button">'; | |
$config['prev_tag_close'] = '</li>'; | |
$config['next_tag_open'] = '<li class="next button">'; | |
$config['next_tag_close'] = '</li>'; | |
$this->pagination->initialize($config); | |
if (count($limit) == 0) | |
$limit = array($per_page, $start_index); | |
// Get articles | |
$this->local_vars['ideas'] = $this->idea_model->get_ideas(NULL, $order, $limit); | |
$this->local_vars['sort_by'] = $sort_by; | |
$this->local_vars['start_index'] = $start_index; | |
$this->local_vars['per_page'] = $per_page; | |
$this->local_vars['section_base_url'] = 'all_ideas'; | |
// Set pagination vars and load needed assets | |
$this->assets['js'][] = array('pagination.js'); | |
// Section Title | |
$this->local_vars['section_title'] = $page_title; | |
// Renders the whole page | |
$this->page_titles[] = $this->local_vars['section_title']; | |
$this->public_vars['content'] = | |
$this->load->view("idea/all.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* Submit Idea | |
*/ | |
function submit() | |
{ | |
if ($this->user_is_logged_in) | |
{ | |
if ($this->idea_model->has_idea($this->session->userdata('ID_user'))) | |
{ | |
$this->_reject(); | |
return; | |
} | |
} | |
// Auth error array empty by default | |
$this->local_vars['auth_error'] = array(); | |
// Load account lang file | |
$this->lang->load('account'); | |
// Check if the current user is logged in | |
if ($this->user_is_logged_in) | |
{ | |
// it is, just process the submit idea form | |
if ($this->form_validation->run('submit_idea')) | |
{ | |
$idea = $this->_process_submit_idea($this->session->userdata('ID_user'), TRUE); | |
redirect('idea/confirmation'); | |
} | |
} | |
else | |
{ | |
// Load form validation rules (just in case) | |
$this->config->load('form_validation'); | |
// Get the Sign up Rules | |
$signup_rules = $this->config->item('sign_up'); | |
// Get the Submit Idea rules | |
$submit_idea_rules = $this->config->item('submit_idea'); | |
// Merge both rules | |
$all_rules = array_merge($signup_rules, $submit_idea_rules); | |
// Reset form validation with new rules | |
$this->form_validation->set_rules($all_rules); | |
// Load upload library | |
$this->load->library('upload'); | |
// Save avatar uploading status in session | |
if ($this->session->userdata('uploaded_avatar') === FALSE) | |
$this->session->set_userdata('uploaded_avatar', FALSE); | |
// Avatar | |
if ($this->session->userdata('avatar_data') === FALSE) | |
$this->session->set_userdata('avatar_data', array()); | |
// Handle Upload | |
if (($this->input->server('REQUEST_METHOD') == 'POST') && | |
($this->upload->do_upload('avatar'))) | |
{ | |
// Load Image Resizer library | |
$this->load->library('Community_Image'); | |
// Load Image Model | |
$this->load->model('image_model'); | |
// Get the default avatar size | |
$avatar_size = $this->config->item('cisco_site_default_avatar_size'); | |
// Get file data | |
$upload_data = $this->upload->data(); | |
// Save file data in the database | |
$ID_image = $this->image_model->add_image($upload_data); | |
// Merge the image ID and thumb path in the file data array | |
$upload_data['ID_image'] = $ID_image; | |
$upload_data['thumb_path'] = $this->community_image->generate_thumb($upload_data, $avatar_size); | |
// and save this in the session in case the form doesn't validate | |
$this->session->set_userdata('uploaded_avatar', TRUE); | |
$this->session->set_userdata('avatar_data', $upload_data); | |
} | |
// Get avatar from session | |
$this->local_vars['avatar_data'] = $this->session->userdata('avatar_data'); | |
// Process the form | |
if ($this->form_validation->run()) | |
{ | |
// Add user and DO NOT send the welcome email (it goes with the idea) | |
$user = $this->auth->add_new_user(FALSE); | |
// Check if the user uploaded an avatar | |
if ($this->session->userdata('uploaded_avatar')) | |
{ | |
// it did, get the avatar data | |
$avatar = $this->session->userdata('avatar_data'); | |
// and set the user with this avatar | |
$this->auth_model->set_avatar($avatar['ID_image'], $user['ID_user']); | |
// Load Image Model, just in case | |
$this->load->model('image_model'); | |
// Set owner of this image to this user | |
$this->image_model->set_owner($avatar['ID_image'], $user['ID_user']); | |
} | |
// Log the user in | |
$this->auth->login($user['email'], $user['password_plain']); | |
// Process idea | |
$idea = $this->_process_submit_idea($user['ID_user'], FALSE, $user); | |
// Redirect to the idea details page | |
redirect('idea/confirmation'); | |
} | |
else | |
{ | |
// Get this only if form was submitted at least ONCE | |
$this->local_vars['avatar_error'] = $this->upload->display_errors('<p style="color: red; font-weight: bold;">', '</p>'); | |
} | |
/* | |
* If we got this far, it means something went wrong, | |
* get errors from the auth lib if any | |
*/ | |
$this->local_vars['auth_error'] = $this->auth->get_error(); | |
} | |
// Get session state for the current user | |
$this->local_vars['is_logged_in'] = $this->user_is_logged_in; | |
if ( ! $this->user_is_logged_in) | |
{ | |
$this->local_vars['signup_no_form'] = TRUE; | |
$this->local_vars['signup_form'] = $this->load->view("account/sign_up.php", $this->local_vars, TRUE); | |
$this->dom_ready['full_validation'] = 'initSubmitIdeaValidationFull();'; | |
} | |
else | |
{ | |
$local = array( | |
'user' => $this->auth->get_user(), | |
'avatar' => $this->auth_model->get_avatar(), | |
'quick_mode' => TRUE | |
); | |
$this->local_vars['user_profile'] = $this->load->view('account/my', $local, TRUE); | |
$this->dom_ready['simple_validation'] = 'initSubmitIdeaValidationSimple();'; | |
} | |
// Get word count | |
$this->local_vars['word_count'] = count(str_word_count($this->input->post('idea_description'), 1)); | |
// Word Counter | |
$this->dom_ready['word_counter'] = 'initSubmitIdeaWordCounter();'; | |
// Get popular tags | |
$popular_tags = $this->tag_model->get_popular_tags(); | |
$this->local_vars['popular_tags'] = $popular_tags; | |
if ($popular_tags === FALSE) | |
$popular_tags = array('cisco', 'mcy', 'idea'); | |
$this->js_vars[] = '_suggested_tags = ["' . implode('","', $popular_tags) . '"];'; | |
// Load tagInput jQuery plugin | |
$this->assets['css'][] = array('extensions/ideas.mcy.css'); | |
// Load tagInput jQuery plugin and related tag js files | |
$this->assets['js'][] = array( | |
'sourced/wordCount/jquery.wordcount.js', | |
'sourced/tagInput/jquery.tagInput.js', | |
'sourced/tagInput/jquery.timers.js', | |
'sourced/validate/jquery.validate.min.js', | |
'extensions/ideas.fx.js', | |
'validations/idea.mcy.js', | |
'extensions/tags.fx.js' | |
); | |
// Init tag selector | |
$this->dom_ready['tag_selector'] = 'initTagsSelector();'; | |
// Set page title and render page | |
$this->page_class = array_merge($this->page_class, array('ciscomcy')); | |
$this->page_titles[] = lang('idea_submit_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view("idea/submit.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* Idea details page | |
* | |
* @param integer|NULL $ID_idea | |
* @param string $seo_title | |
*/ | |
function details($ID_idea = NULL, $seo_title = '') | |
{ | |
if (is_null($ID_idea)) | |
redirect('home'); | |
// Get Idea from the Database | |
if (($idea = $this->idea_model->get_idea(array('ID_idea' => $ID_idea))) === FALSE) | |
redirect('home'); | |
// Load home lang for tags | |
$this->lang->load('home'); | |
// Load Agent Library | |
$this->load->library('user_agent'); | |
// Track hit only if current agent is not a robot | |
if ( ! $this->agent->is_robot()) | |
$this->idea_model->track_hit($ID_idea); | |
// Assign the idea to the local view | |
$this->local_vars['idea'] = $idea; | |
$this->local_vars['previous_idea'] = | |
$this->idea_model->get_previous_idea($idea['ID_idea']); | |
$this->local_vars['next_idea'] = | |
$this->idea_model->get_next_idea($idea['ID_idea']); | |
// Get Tags from the Database | |
$this->local_vars['tags'] = $this->tag_model->get_tags_by_idea($ID_idea); | |
// Get User data | |
$this->local_vars['user'] = $this->auth_model->get_row(array('ID_user' => $idea['ID_user'])); | |
// Get the user avatar | |
$this->local_vars['avatar'] = $this->auth_model->get_avatar($idea['ID_user']); | |
// Renders the whole page | |
$this->page_titles[] = $idea['idea_title']; | |
$this->public_vars['content'] = | |
$this->load->view("idea/details.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* Submit Idea Confirmation page | |
* | |
*/ | |
function confirmation() | |
{ | |
// Renders the whole page | |
$this->page_class = array_merge($this->page_class, array('ciscomcy')); | |
$this->page_titles[] = lang('idea_confirmation_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view("idea/confirmation.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* List ideas by tag | |
* | |
* @param string $tag_name | |
* @param string $sort_by | |
* @param integer $start_index | |
* @param integer $per_page | |
*/ | |
function tag($tag_name = NULL, $sort_by = 'recently_added', $start_index = 0, $per_page = 10) | |
{ | |
if (is_null($tag_name)) | |
redirect('home'); | |
// Load pagination | |
$this->load->library('pagination'); | |
// Load Community Image Library | |
$this->load->library('Community_Image'); | |
// Load Text Helper | |
$this->load->helper('text'); | |
// Load Home lang | |
$this->lang->load('home'); | |
// Make sure Start Index and Per Page are integers | |
$start_index = (int)$start_index; | |
$per_page = (int)$per_page; | |
// Change # of records per page if user requested it | |
if ($this->input->post('ideas_per_page') !== FALSE) | |
$per_page = (int)$this->input->post('ideas_per_page'); | |
// Sorting | |
$order = NULL; | |
$limit = NULL; | |
switch($sort_by) | |
{ | |
case 'recently_added': | |
$order = 'idea_posted DESC'; | |
break; | |
case 'most_viewed': | |
$order = 'idea_hits DESC'; | |
break; | |
case 'random': | |
$order = array('ID_idea', 'random'); | |
break; | |
default: | |
$order = 'idea_posted DESC'; | |
} | |
$total_ideas = $this->idea_model->count_ideas(array('idea_tag' => $tag_name)); | |
if ($start_index < 0) $start_index = 0; | |
$max_start_index = floor($total_ideas / $per_page) * $per_page; | |
if ($start_index > $max_start_index) $start_index = $max_start_index; | |
$config = array(); | |
$config['base_url'] = base_url() . 'browse-ideas-by-tags/' . $tag_name . '/' . $sort_by . '/'; | |
$config['base_url_end'] = '/' . $per_page; | |
$config['total_rows'] = $total_ideas; | |
$config['per_page'] = $per_page; | |
$config['start_index'] = $start_index; | |
$config['uri_segment'] = 4; | |
$config['full_tag_open'] = '<div class="paging"><ul>'; | |
$config['full_tag_close'] = '</ul></div> <!-- end paging -->'; | |
$config['prev_tag_open'] = '<li class="prev button">'; | |
$config['prev_tag_close'] = '</li>'; | |
$config['next_tag_open'] = '<li class="next button">'; | |
$config['next_tag_close'] = '</li>'; | |
$this->pagination->initialize($config); | |
if (count($limit) == 0) | |
$limit = array($per_page, $start_index); | |
// Get articles | |
$this->local_vars['ideas'] = $this->idea_model->get_ideas(array('idea_tag' => $tag_name), $order, $limit); | |
$this->local_vars['sort_by'] = $sort_by; | |
$this->local_vars['start_index'] = $start_index; | |
$this->local_vars['per_page'] = $per_page; | |
$this->local_vars['section_base_url'] = 'browse-ideas-by-tags/' . $tag_name; | |
// Set pagination vars and load needed assets | |
$this->assets['js'][] = array('pagination.js'); | |
// Section Title | |
$this->local_vars['section_title'] = sprintf(lang('idea_tags_page_title'), $tag_name); | |
// Renders the whole page | |
$this->page_titles[] = $this->local_vars['section_title']; | |
$this->public_vars['content'] = | |
$this->load->view("idea/all.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
/** | |
* Process submit idea | |
* | |
* @param integer $ID_user | |
* @param boolean $logged_in | |
* @param array $user_data | |
* @return array | |
*/ | |
function _process_submit_idea($ID_user, $logged_in, $user_data = array()) | |
{ | |
// Get data | |
$idea = array(); | |
$idea['ID_user'] = $ID_user; | |
$idea['idea_title'] = $this->input->post('idea_title', TRUE); | |
$idea['idea_description'] = $this->input->post('idea_description', TRUE); | |
// Save idea | |
$ID_idea = $this->idea_model->add_idea($idea); | |
// Get Idea ID | |
$idea['ID_idea'] = $ID_idea; | |
// Save tags | |
$this->tag_model->add_tags($ID_idea, $this->input->post('tags', TRUE)); | |
/** | |
* Send an email to the user | |
*/ | |
// Get Current user data | |
$user = $this->auth->get_user($ID_user); | |
// Load email library | |
$this->load->library('email'); | |
// Load lang file (just in case) | |
$this->lang->load('account'); | |
$this->lang->load('idea'); | |
// From field | |
$from_email = $this->config->item('cisco_site_email_from_email'); | |
$from_name = $this->config->item('cisco_site_email_from_name'); | |
// Include some more data | |
$data = array_merge($user, $idea, $user_data, array( | |
'base_url' => base_url(), | |
'login_url' => site_url($this->config->item('auth_url_login')), | |
'idea_url' => site_url('idea/details/' . $idea['ID_idea'] . '/' . seo($idea['idea_title'])) | |
)); | |
// Load both HTML and Text bodies | |
$current_language = $this->session->userdata('language'); | |
if ($logged_in) | |
{ | |
$html_message = $this->load->view('email/' . $current_language . '/idea/submit_idea_html.php', $data, TRUE); | |
$text_message = $this->load->view('email/' . $current_language . '/idea/submit_idea_text.php', $data, TRUE); | |
} | |
else | |
{ | |
$html_message = $this->load->view('email/' . $current_language . '/idea/first_submit_idea_html.php', $data, TRUE); | |
$text_message = $this->load->view('email/' . $current_language . '/idea/first_submit_idea_text.php', $data, TRUE); | |
} | |
// Send the email | |
$this->email->clear(); | |
$this->email->from($from_email, $from_name); | |
$this->email->to($user['email']); | |
if ($logged_in) | |
$this->email->subject(lang('idea_email_submit_subject')); | |
else | |
$this->email->subject(lang('idea_email_submit_subject')); | |
$this->email->message($html_message); | |
$this->email->set_alt_message($text_message); | |
$this->email->send(); | |
// Finally return the idea | |
return $idea; | |
} | |
/** | |
* Displays idea reject page | |
* | |
*/ | |
function _reject() | |
{ | |
// Renders the whole page | |
$this->page_class = array_merge($this->page_class, array('ciscomcy')); | |
$this->page_titles[] = lang('idea_rejection_page_title'); | |
$this->public_vars['content'] = | |
$this->load->view("idea/reject.php", $this->local_vars, TRUE); | |
$this->render_page(); | |
} | |
} |
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'); | |
/** | |
* Idea Model Class | |
*/ | |
class Idea_Model extends Model | |
{ | |
/** | |
* Idea table name | |
* @var string | |
*/ | |
private $table_idea = 'idea'; | |
/** | |
* Idea Selected Status | |
*/ | |
const IDEA_SELECTED = 1; | |
const IDEA_NOT_SELECTED = 0; | |
/** | |
* Idea Deleted Status | |
*/ | |
const IDEA_DELETED = 1; | |
const IDEA_NOT_DELETED = 0; | |
/** | |
* MySQL Date Time Format | |
*/ | |
const MYSQL_DATETIME = 'Y-m-d H:i:s'; | |
/** | |
* Model Constructor | |
*/ | |
function __construct() | |
{ | |
parent::Model(); | |
} | |
/** | |
* Insert idea in the database | |
* | |
* @param array $data | |
* @return integer | |
*/ | |
function add_idea($data) | |
{ | |
if ( ! isset($data['idea_hits'])) | |
$data['idea_hits'] = 0; | |
if ( ! isset($data['idea_posted'])) | |
$data['idea_posted'] = date(self::MYSQL_DATETIME); | |
if ( ! isset($data['idea_is_selected'])) | |
$data['idea_is_selected'] = date(self::IDEA_NOT_SELECTED); | |
if ( ! isset($data['idea_is_deleted'])) | |
$data['idea_is_deleted'] = date(self::IDEA_NOT_DELETED); | |
$data['idea_is_published'] = 1; // Publish all ideas by default | |
$this->db->insert($this->table_idea, $data); | |
return $this->db->insert_id(); | |
} | |
/** | |
* Gets the previous idea, related to a given idea | |
* | |
* @param integer $current_ID_idea | |
* @return array|boolean | |
*/ | |
function get_previous_idea($current_ID_idea) | |
{ | |
return $this->get_idea( | |
array( | |
'ID_idea <>' => $current_ID_idea, | |
'ID_idea <=' => $current_ID_idea | |
), | |
'ID_idea DESC', 1); | |
} | |
/** | |
* Gets the next idea, related to a given idea | |
* | |
* @param integer $current_ID_idea | |
* @return array|boolean | |
*/ | |
function get_next_idea($current_ID_idea) | |
{ | |
return $this->get_idea( | |
array( | |
'ID_idea <>' => $current_ID_idea, | |
'ID_idea >=' => $current_ID_idea | |
), | |
'ID_idea ASC', 1); | |
} | |
/** | |
* Get an idea from the database | |
* | |
* @param array|string $where | |
* @param array|NULL $order | |
* @param array|NULL $limit | |
* @return array|boolean | |
*/ | |
function get_idea($where, $order = NULL, $limit = NULL) | |
{ | |
$this->db->select('*'); | |
$this->db->from($this->table_idea); | |
$this->db->where('idea_is_published', 1); | |
$this->db->where('idea_is_deleted', 0); | |
$this->db->where($where); | |
if ( ! is_null($order)) | |
$this->db->order_by($order); | |
if ( ! is_null($limit)) | |
$this->db->limit($limit); | |
$query = $this->db->get(); | |
return ($query->num_rows() > 0) ? $query->row_array() : FALSE; | |
} | |
/** | |
* Get Recently Added Ideas | |
* | |
* @param array $limit | |
* @return array | |
*/ | |
function get_recently_added_ideas($limit) | |
{ | |
return $this->get_ideas(NULL, array('idea_posted', 'desc'), $limit); | |
} | |
/** | |
* Get Top 12 Ideas | |
* | |
* @param array $limit | |
* @return array | |
*/ | |
function get_top_12_ideas($limit) | |
{ | |
return $this->get_ideas(NULL, array('idea_hits', 'desc'), $limit); | |
} | |
/** | |
* Get Shuffled Ideas | |
* | |
* @param array $limit | |
* @return array | |
*/ | |
function get_shuffled_ideas($limit) | |
{ | |
return $this->get_ideas(NULL, array('ID_idea', 'random'), $limit); | |
} | |
/** | |
* Get ideas from the database | |
* | |
* @param array|NULL $where | |
* @param array|NULL $order | |
* @param array|NULL $limit | |
* @return array | |
*/ | |
function get_ideas($where = NULL, $order = NULL, $limit = NULL) | |
{ | |
// We will store the results in this array | |
$results = array(); | |
// Build query | |
$this->db->select('*'); | |
$this->db->from($this->table_idea); | |
$this->db->join('user', 'user.ID_user = idea.ID_user', 'left'); | |
$this->db->join('image', 'user.ID_image = image.ID_image', 'left'); | |
$this->db->where('idea_is_published', 1); | |
$this->db->where('idea_is_deleted', 0); | |
if ( ! is_null($where)) | |
{ | |
if (is_array($where)) | |
{ | |
// Iterate through the fields and check if we are filtering by tags | |
foreach($where as $field => $value) | |
{ | |
if ($field == 'idea_tag') | |
$this->db->where('ID_idea IN (SELECT ID_idea FROM tag_map WHERE tag_name = ' . $this->db->escape($value) . ')', NULL, FALSE); | |
else | |
$this->db->where($where, $value); | |
} | |
} | |
else | |
$this->db->where($where); | |
} | |
if ( ! is_null($order)) | |
{ | |
if (is_array($order)) | |
$this->db->order_by($order[0], $order[1]); | |
else | |
$this->db->order_by($order); | |
} | |
if ( ! is_null($limit)) | |
{ | |
if (is_array($limit)) | |
$this->db->limit($limit[0], $limit[1]); | |
else | |
$this->db->limit($limit); | |
} | |
$query = $this->db->get(); | |
if ($query->num_rows() > 0) | |
{ | |
// Load the Image library | |
$this->load->library('Community_Image'); | |
// Load URL helper, just in case | |
$this->load->helper('url'); | |
// Get the default avatar size | |
$avatar_size = $this->config->item('cisco_site_default_avatar_size'); | |
// Get the default avatar | |
$default_avatar = base_url() . $this->config->item('cisco_site_default_avatar'); | |
// Get rows | |
$rows = $query->result_array(); | |
foreach ($rows as $row) | |
{ | |
$row['avatar'] = $default_avatar; | |
if ( ! is_null($row['ID_image'])) | |
{ | |
$file = array( | |
'full_path' => $row['full_path'], | |
'raw_name' => $row['raw_name'], | |
'file_ext' => $row['file_ext'], | |
'image_width' => $row['image_width'], | |
'image_height' => $row['image_height'], | |
); | |
$row['avatar'] = $this->community_image->generate_thumb($file, $avatar_size); | |
} | |
$results[] = $row; | |
} | |
} | |
return $results; | |
} | |
/** | |
* Count ideas | |
* | |
* @param array $where | |
* @return integer | |
*/ | |
function count_ideas($where = NULL) | |
{ | |
if ( ! is_null($where)) | |
{ | |
if (is_array($where)) | |
{ | |
// Iterate through the fields and check if we are filtering by tags | |
foreach($where as $field => $value) | |
{ | |
if ($field == 'idea_tag') | |
$this->db->where('ID_idea IN (SELECT ID_idea FROM tag_map WHERE tag_name = ' . $this->db->escape($value) . ')', NULL, FALSE); | |
else | |
$this->db->where($where, $value); | |
} | |
} | |
else | |
$this->db->where($where); | |
} | |
$this->db->where('idea_is_published', 1); | |
$this->db->where('idea_is_deleted', 0); | |
return $this->db->count_all_results($this->table_idea); | |
} | |
/** | |
* Tracks a hit/view of the idea | |
* | |
* @param integer $ID_idea | |
*/ | |
function track_hit($ID_idea) | |
{ | |
// Check if there's already a valid record for this IP and Browser | |
$this->db->select('ID_idea_hit'); | |
$this->db->from('idea_hit'); | |
$this->db->where('visitor_ip', $this->input->ip_address()); | |
$this->db->where('visitor_browser', $this->input->user_agent()); | |
$this->db->where('DATE_ADD(visitor_date, INTERVAL 4 HOUR) >', date('Y-m-d H:i:s')); | |
$this->db->order_by('visitor_date', 'desc'); | |
$this->db->limit(1); | |
$result = $this->db->get(); | |
if ($result->num_rows() == 0) | |
{ | |
// We can track this hit | |
$hit = array(); | |
$hit['visitor_ip'] = $this->input->ip_address(); | |
$hit['visitor_browser'] = $this->input->user_agent(); | |
$hit['ID_idea'] = $ID_idea; | |
$hit['ID_user'] = (($this->session->userdata('ID_user')) ? | |
$this->session->userdata('ID_user') : NULL); | |
$hit['visitor_hour'] = date('H'); | |
$hit['visitor_minute'] = date('i'); | |
$hit['visitor_date'] = date('Y-m-d H:i:s'); | |
$hit['visitor_day'] = date('d'); | |
$hit['visitor_month'] = date('m'); | |
$hit['visitor_year'] = date('Y'); | |
$hit['visitor_referrer'] = $this->input->server('HTTP_REFERER'); | |
$this->db->insert('idea_hit', $hit); | |
// Get the # of this | |
$this->db->where('ID_idea', $ID_idea); | |
$hits_count = $this->db->count_all_results('idea_hit'); | |
// Finally, update the # of hits | |
$this->db->where('ID_idea', $ID_idea); | |
$this->db->set('idea_hits', $hits_count); | |
$this->db->update($this->table_idea); | |
} | |
} | |
/** | |
* Checks wheter a user already posted an idea | |
* | |
* @param integer $ID_user | |
* @return boolean | |
*/ | |
function has_idea($ID_user) | |
{ | |
$this->db->where('ID_user', $ID_user); | |
$this->db->where('idea_is_deleted', 0); | |
return ($this->db->count_all_results($this->table_idea) > 0); | |
} | |
} |
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
/** | |
* Some vars we need in place to track down links usage | |
*/ | |
var _currentFilter = 'recently.added'; | |
var _currentOffset = 0; | |
var _addMore = false; | |
/** | |
* Forms > Submit Idea Word Counter | |
* | |
* @param var-type none | |
* @return none | |
*/ | |
var initSubmitIdeaWordCounter = function() | |
{ | |
$("textarea[name='idea_description']").wordCount({ | |
counterElement: 'idea_description_word_counter', | |
max_words: 300 | |
}); | |
}; | |
/** | |
* Init Home page JS functions | |
* | |
* @return none | |
*/ | |
var initHomePage = function() | |
{ | |
handleRecentlyAddedLinks(); | |
handleTop25Links(); | |
handleShuffledIdeas(); | |
handleLoadMoreIdeas(); | |
}; | |
/** | |
* Handle the Recently added link | |
* | |
*/ | |
var handleRecentlyAddedLinks = function() | |
{ | |
var _ral_offset = 0; | |
$('.ral').bind('click', function(event) { | |
_addMore = false; | |
$('.multiselect li.active').removeClass('active'); | |
$('.load-more-button').show(); | |
getMoreIdeas('recently.added', _ral_offset); | |
$(this).parent().parent().addClass('active'); | |
event.preventDefault(); | |
}); | |
}; | |
/** | |
* Handle the Top 12 (Top 25) link | |
* | |
*/ | |
var handleTop25Links = function() | |
{ | |
var _t2l_offset = 0; | |
$('.t2l').bind('click', function(event) { | |
_addMore = false; | |
$('.multiselect li.active').removeClass('active'); | |
$('.load-more-button').show(); | |
getMoreIdeas('top.25', _t2l_offset); | |
$(this).parent().parent().addClass('active'); | |
event.preventDefault(); | |
}); | |
}; | |
/** | |
* Handle the Shuffled Ideas link | |
* | |
*/ | |
var handleShuffledIdeas = function() | |
{ | |
var _sil_offset = 0; | |
$('.sil').bind('click', function(event) { | |
_addMore = false; | |
$('.multiselect li.active').removeClass('active'); | |
$('.load-more-button').show(); | |
getMoreIdeas('shuffled.ideas', _sil_offset); | |
$(this).parent().parent().addClass('active'); | |
event.preventDefault(); | |
}); | |
}; | |
/** | |
* Handle the Load More button | |
*/ | |
var handleLoadMoreIdeas = function() | |
{ | |
$('.load-more-button').bind('click', function(event) { | |
_addMore = true; | |
_currentOffset += 12; | |
getMoreIdeas(_currentFilter, _currentOffset); | |
event.preventDefault(); | |
}); | |
} | |
/** | |
* Hides the Load More button | |
*/ | |
var hideLoadMoreButton = function() | |
{ | |
$('.load-more-button').hide(); | |
} | |
/** | |
* Get More Ideas AJAX call | |
* | |
*/ | |
var getMoreIdeas = function(filter, offset) | |
{ | |
_currentFilter = filter; | |
_currentOffset = offset; | |
$.ajax({ | |
type: 'POST', | |
url: 'remote', | |
data: 'remote_method=get.more.ideas&filter=' + filter + '&offset=' + offset, | |
dataType: 'json', | |
success: function(response) { | |
if (response) | |
{ | |
if (response.status == 'success') | |
{ | |
if (_addMore) $('#ideas-container').append(response.payload); | |
else $('#ideas-container').html(response.payload); | |
} | |
else hideLoadMoreButton(); | |
} | |
else hideLoadMoreButton(); | |
} | |
}); | |
}; |
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'); | |
/** | |
* Base Controller | |
* | |
*/ | |
class MY_Controller extends Controller | |
{ | |
/** | |
* Auth Library | |
* | |
* @var Auth | |
*/ | |
public $auth; | |
/** | |
* Site Current Language | |
* default to arabic | |
* @var string | |
*/ | |
public $current_language = 'arabic'; | |
/** | |
* Local View vars | |
* | |
* @var array | |
*/ | |
public $local_vars = array(); | |
/** | |
* Assets (JS/CSS) from the Controllers | |
* | |
* @var array | |
*/ | |
public $assets = array(); | |
/** | |
* JS DOM Ready calls | |
* | |
* @var array | |
*/ | |
public $dom_ready = array(); | |
/** | |
* JS Vars | |
* | |
* @var array | |
*/ | |
public $js_vars = array(); | |
/** | |
* Global View vars | |
* @var array | |
*/ | |
public $public_vars = array(); | |
/** | |
* Page Titles | |
* @var array | |
*/ | |
public $page_titles = array(); | |
/** | |
* Page Id | |
* @var array | |
*/ | |
public $page_id = array(); | |
/** | |
* Page Classes | |
* @var array | |
*/ | |
public $page_class = array(); | |
/** | |
* Current user session status | |
* | |
* @var boolean | |
*/ | |
public $user_is_logged_in = FALSE; | |
/** | |
* Display the leading box | |
* | |
* @var boolean | |
*/ | |
public $display_leading_box = FALSE; | |
/** | |
* Wheter use or not use Facebook Meta Tags | |
* | |
* @var boolean | |
*/ | |
public $use_fb_metatags = FALSE; | |
/** | |
* Facebook Application ID | |
* | |
* @var string | |
*/ | |
private $fb_app_id = '142274265816337'; | |
/** | |
* Facebook Meta Tags | |
* | |
* @var array | |
*/ | |
public $fb_meta_tags = array(); | |
/** | |
* Class Constructor | |
*/ | |
function MY_Controller() | |
{ | |
parent::Controller(); | |
// Check if there's a language set in the session | |
if ($this->session->userdata('language')) | |
// there is, set it as the default language | |
$this->config->set_item('language', $this->session->userdata('language')); | |
else | |
// set the current language in the sessions | |
$this->session->set_userdata('language', $this->config->item('language')); | |
$this->current_language = $this->config->item('language'); | |
// Auto load | |
$this->lang->load('cisco'); | |
$this->lang->load('forms'); | |
// Add Cisco to the page title | |
$this->page_titles[] = 'Cisco Systems, Inc.'; | |
// Add default title to the page title | |
$this->page_titles[] = lang('cisco_site_default_page_title'); | |
// Get the session state for the current user | |
$this->user_is_logged_in = $this->auth->is_logged_in(); | |
// Add inner page class to all pages by default | |
$this->page_class = array('inner-pege'); | |
// Initialize array with assets used site wide | |
$this->assets['css'][] = array( | |
'all.css', | |
'extensions/messages.mcy.css', | |
'extensions/addthis.mcy.css' | |
); | |
$this->assets['js'][] = array( | |
'sourced/jquery/jquery-1.4.2.min.js', // jQuery Framework | |
'popup.js', // p2h Popup Stuff | |
'validations/messages/' . $this->current_language . '.js', // Validation Messages | |
'tab.js' // p2h Tabs JS | |
); | |
} | |
/** | |
* Prepare Javascript for inclusion | |
*/ | |
function prepare_javascript() | |
{ | |
$this->js_vars[] = '_base_url = "' . base_url() . '";'; | |
$this->public_vars['static_js'] = | |
$this->load->view( | |
'layout/_js_domready.php', | |
array( | |
'js_vars' => $this->js_vars, | |
'dom_ready' => $this->dom_ready), | |
TRUE | |
); | |
} | |
/** | |
* Prepares the site page title | |
*/ | |
function prepare_page_title() | |
{ | |
krsort($this->page_titles); | |
$this->public_vars['page_title'] = implode($this->page_titles, | |
$this->config->item('cisco_site_default_title_separator')); | |
// Render the leading box if necessary | |
$this->public_vars['leading_box'] = ''; | |
if ($this->display_leading_box) | |
$this->public_vars['leading_box'] = $this->load->view('layout/leading_box', | |
array(), TRUE); | |
/** | |
* Check if we need to render facebook metatags | |
* @see http://developers.facebook.com/docs/opengraph | |
* For ideas, use "idea" for the "type" property (it will appear as other in Facebook) | |
* For news, use "article" for the "type" property | |
*/ | |
$this->public_vars['facebook_meta_tags'] = ''; | |
if ($this->use_fb_metatags) | |
{ | |
$this->public_vars['facebook_meta_tags'] .= | |
'<meta property="fb:app_id" content="' . $this->fb_app_id . '" />' . "\n"; | |
foreach($this->fb_meta_tags as $property => $content) | |
$this->public_vars['facebook_meta_tags'] .= | |
"<meta property=\"$property\" content=\"$content\" />\n"; | |
} | |
} | |
/** | |
* Prepare assets for inclusion | |
*/ | |
function prepare_assets() | |
{ | |
// Initialize assets vars | |
$this->public_vars['css_assets'] = ''; | |
$this->public_vars['js_assets'] = ''; | |
// Load assets helper | |
$this->load->helper('assets'); | |
// Make sure there are css assets first, then add them | |
if (count($this->assets['css']) > 0) | |
foreach($this->assets['css'] as $asset) | |
$this->public_vars['css_assets'].= asset_link($asset, 'css'); | |
// If current language is arabic, add an aditional css | |
if ($this->current_language == 'arabic') | |
$this->public_vars['css_assets'].= asset_link('arabic.mcy.css', 'css'); | |
// Make sure there are js assets first, then add them | |
if (count($this->assets['js']) > 0) | |
foreach($this->assets['js'] as $asset) | |
$this->public_vars['js_assets'].= asset_link($asset, 'js'); | |
} | |
/** | |
* Renders the User Info, if any | |
*/ | |
function render_user_info() | |
{ | |
$user_info = array(); | |
$user_info['user_is_logged_in'] = $this->user_is_logged_in; | |
if ($this->user_is_logged_in) | |
{ | |
$user = $this->session->userdata('user'); | |
$user_info['user_full_name'] = $user['first_name'] . ' ' . $user['last_name']; | |
$user_info['avatar'] = $this->auth_model->get_avatar(NULL, '34x35'); | |
$this->public_vars['login_form_popup'] = ''; | |
} | |
else | |
{ | |
// Load validate plugin and related files | |
$this->assets['js'][] = array( | |
'sourced/validate/jquery.validate.min.js', | |
'validations/login.mcy.js' | |
); | |
// Init login validation | |
$this->dom_ready['popup_login_form'] = 'initLoginValidation();'; | |
// Load login popup | |
$this->public_vars['login_form_popup'] = $this->load->view('layout/login_popup', array(), TRUE); | |
} | |
$this->public_vars['user_info'] = $this->load->view('layout/user_info.php', | |
$user_info, TRUE); | |
} | |
/** | |
* Renders the language switcher | |
*/ | |
function render_language_switcher() | |
{ | |
$this->public_vars['language_switcher'] = $this->load->view('layout/language_switcher.php', | |
array('current_language' => $this->current_language), TRUE); | |
} | |
/** | |
* Renders main menu | |
*/ | |
function render_main_menu() | |
{ | |
$this->public_vars['main_menu'] = $this->load->view('layout/main_menu.php', | |
array(), TRUE); | |
} | |
/** | |
* Render footer menu | |
*/ | |
function render_footer_menu() | |
{ | |
$this->public_vars['footer_menu'] = $this->load->view('layout/footer_menu.php', | |
array('user_is_logged_in' => $this->user_is_logged_in), TRUE); | |
} | |
/** | |
* Renders the articles section | |
*/ | |
function render_articles() | |
{ | |
// Load article model | |
$this->load->model('article_model'); | |
// Get news from the database | |
$articles = $this->article_model->get_articles($this->current_language, | |
array(), 'article_date DESC', array(3, 0)); | |
$this->public_vars['articles'] = ''; | |
// Only render the view if there are news | |
if ($articles !== FALSE) | |
{ | |
// Load article language | |
$this->lang->load('article'); | |
// Load the seo helper | |
$this->load->helper('seo'); | |
// Load the text helper | |
$this->load->helper('text'); | |
$this->public_vars['articles'] = $this->load->view('article/home_list', | |
array('articles' => $articles), TRUE); | |
} | |
} | |
/** | |
* Render the competition stats box | |
*/ | |
function render_competition_stats() | |
{ | |
// Load stats model | |
$this->load->model('stat_model'); | |
$stats = array(); | |
$stats['current_language'] = $this->current_language; | |
$stats['days_remaining'] = $this->stat_model->get_remaining_days(); | |
$stats['total_ideas'] = $this->stat_model->get_total_ideas(); | |
$stats['total_finalists'] = $this->stat_model->get_total_finalists(); | |
// Load stats | |
$this->public_vars['competition_stats'] = $this->load->view('layout/competition_stats', | |
$stats, TRUE); | |
} | |
/** | |
* Renders page | |
*/ | |
function render_page() | |
{ | |
// Renders the language switcher | |
$this->render_language_switcher(); | |
// Renders the use info section | |
$this->render_user_info(); | |
// Renders the main menu | |
$this->render_main_menu(); | |
// Render competition stats | |
$this->render_competition_stats(); | |
// Renders the articles section | |
$this->render_articles(); | |
// Renders the footer menu | |
$this->render_footer_menu(); | |
$this->public_vars['fb_app_id'] = $this->fb_app_id; | |
$this->public_vars['current_language'] = $this->current_language; | |
$this->public_vars['page_id'] = $this->page_id; | |
$this->public_vars['page_class'] = $this->page_class; | |
// Prepare Assets (JS/CSS) for Inclusion | |
$this->prepare_assets(); | |
// Prepara JS Calls | |
$this->prepare_javascript(); | |
// Generates the page Title | |
$this->prepare_page_title(); | |
// Renders the main layout | |
$this->load->view('layout/main.php', $this->public_vars); | |
} | |
} |
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'); | |
/** | |
* Base Form Validation (Extension to CI Form Validation) | |
* | |
*/ | |
class MY_Form_validation extends CI_Form_validation | |
{ | |
/** | |
* Class Constructor | |
*/ | |
function MY_Form_validation($rules = array()) | |
{ | |
parent::CI_Form_validation(); | |
// Validation rules can be stored in a config file. | |
$this->_config_rules = $rules; | |
$this->set_error_delimiters('<strong class="warning-note" style="display: block;">', '</strong>'); | |
$this->CI->load->library('encrypt'); | |
} | |
/** | |
* Check how many words are in a string and make sure it's less than | |
* a given number | |
* | |
* @param string $value | |
* @param integer $count | |
* @return boolean | |
*/ | |
function word_count($value, $count = 300) | |
{ | |
if (count(str_word_count($value, 1)) > 300) | |
{ | |
$this->set_message(__FUNCTION__, lang('word_count')); | |
return FALSE; | |
} | |
return TRUE; | |
} | |
/** | |
* Check if the current email is in the list of blocked emails | |
* | |
* @param string $email | |
* @return boolean | |
*/ | |
function blocked_email_domains($email) | |
{ | |
$tmp = explode('@', $email); | |
if (count($tmp) == 2) $domain = $tmp[1]; | |
else $domain = 'blocked.domain'; | |
$blocked_domains = $this->CI->config->item('auth_blocked_domains'); | |
if (is_array($blocked_domains)) | |
{ | |
foreach ($blocked_domains as $blocked_domain) | |
{ | |
if (strtolower($domain) == strtolower($blocked_domain)) | |
{ | |
$this->set_message(__FUNCTION__, lang('blocked_email_domains')); | |
return FALSE; | |
} | |
} | |
} | |
return TRUE; | |
} | |
/** | |
* Check if the word is a valid captcha | |
* | |
* @param string $user_word | |
* @return boolean | |
*/ | |
function valid_captcha($user_word) | |
{ | |
if ($this->CI->input->post('captcha_word')) | |
$captcha_word = $this->CI->encrypt->decode($this->CI->input->post('captcha_word')); | |
else | |
$captcha_word = $this->CI->session->userdata('captcha'); | |
if (strtolower($user_word) != strtolower($captcha_word)) | |
{ | |
$user_word = ''; | |
$this->set_message(__FUNCTION__, lang('invalid_captcha')); | |
return FALSE; | |
} | |
return TRUE; | |
} | |
/** | |
* Checks if a given username or email is valid | |
* | |
* @param string $user_name_or_email | |
* @return boolean | |
*/ | |
function valid_username_or_email($user_name_or_email) | |
{ | |
// If there is an @ in the string, look for email, if not, username | |
if (stristr($user_name_or_email, '@') === FALSE) | |
{ | |
$tmp = $this->CI->auth->get_user_by_name($user_name_or_email); | |
if ($tmp !== FALSE) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('user_name_doesnt_exists')); | |
return FALSE; | |
} | |
$tmp = $this->CI->auth->get_user_by_email($user_name_or_email); | |
if ($tmp !== FALSE) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('email_doesnt_exists')); | |
return FALSE; | |
} | |
/** | |
* Checks if a given email address exists in the system | |
* | |
* @param string $email | |
* @return boolean | |
*/ | |
function email_exists($email) | |
{ | |
$tmp = $this->CI->auth->get_user_by_email($email); | |
if ($tmp !== FALSE) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('email_doesnt_exists')); | |
return FALSE; | |
} | |
/** | |
* Check if the given user name is unique | |
* | |
* @param string $user_name | |
* @return boolean | |
*/ | |
function unique_username($user_name) | |
{ | |
$tmp = $this->CI->auth->get_user_by_name($user_name); | |
if ($tmp === FALSE) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('user_name_exists')); | |
return FALSE; | |
} | |
/** | |
* Check if the given email is unique | |
* | |
* @param string $email | |
* @return boolean | |
*/ | |
function unique_email($email) | |
{ | |
$tmp = $this->CI->auth->get_user_by_email($email); | |
if ($tmp === FALSE) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('email_exists')); | |
return FALSE; | |
} | |
/** | |
* Check if the given user name is unique | |
* | |
* @param string $user_name | |
* @return boolean | |
*/ | |
function edit_unique_username($user_name) | |
{ | |
$tmp = $this->CI->auth->get_user_by_name($user_name); | |
if ($tmp === FALSE) return TRUE; | |
$tmp2 = $this->CI->auth->get_user(); | |
if ($tmp['user_name'] == $tmp2['user_name']) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('user_name_exists')); | |
return FALSE; | |
} | |
/** | |
* Check if the given email is unique | |
* | |
* @param string $email | |
* @return boolean | |
*/ | |
function edit_unique_email($email) | |
{ | |
$tmp = $this->CI->auth->get_user_by_email($email); | |
if ($tmp === FALSE) return TRUE; | |
$tmp2 = $this->CI->auth->get_user(); | |
if ($tmp['email'] == $tmp2['email']) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('email_exists')); | |
return FALSE; | |
} | |
/** | |
* Check if the given password is valid | |
* | |
* @param string $pass | |
* @return boolean | |
*/ | |
function valid_password($pass) | |
{ | |
$tmp = $this->CI->auth->get_user(); | |
if ($tmp['password'] == $this->CI->auth->sha256($pass.$tmp['salt'])) | |
return TRUE; | |
$this->set_message(__FUNCTION__, lang('invalid_password')); | |
return FALSE; | |
} | |
/** | |
* Check the given string if is a valid alpha dash string | |
* | |
* @param string $str | |
* @return boolean | |
*/ | |
function alpha_dash($str) | |
{ | |
return ( ! preg_match("/^([-a-z0-9\ _-])+$/i", $str)) ? FALSE : TRUE; | |
} | |
} |
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
/** | |
* Validations > User Signup Page | |
* | |
*/ | |
var initUserSignupValidation = function() | |
{ | |
$('#form-user-signup').validate({ | |
errorElement: 'div', | |
errorClass: 'warning-note', | |
errorPlacement: function(error, element) { | |
error.appendTo(element.parent().parent()); | |
}, | |
highlight: function(element, errorClass) { | |
$(element).parent().parent().addClass('error-col'); | |
}, | |
unhighlight: function(element) { | |
$(element).parent().parent().removeClass('error-col'); | |
}, | |
rules: { | |
first_name: { | |
required: true, | |
minlength: 2, | |
maxlength: 130 | |
}, | |
last_name: { | |
required: true, | |
minlength: 2, | |
maxlength: 130 | |
}, | |
email: { | |
required: true, | |
minlength: 4, | |
email: true | |
}, | |
user_name: { | |
required: true, | |
minlength: 4, | |
maxlength: 65 | |
}, | |
password: { | |
required: true, | |
minlength: 4, | |
maxlength: 32, | |
equalTo: "input[name='password_confirm']" | |
}, | |
password_confirm: { | |
required: true, | |
minlength: 4, | |
maxlength: 32 | |
} | |
}, | |
messages: messagesUserSignup | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment