Skip to content

Instantly share code, notes, and snippets.

@mcabreradev
Created May 6, 2016 00:13
Show Gist options
  • Save mcabreradev/91ab07faeadb020aca1694b6a776c556 to your computer and use it in GitHub Desktop.
Save mcabreradev/91ab07faeadb020aca1694b6a776c556 to your computer and use it in GitHub Desktop.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Users extends MY_Controller {
public function __construct() {
parent::__construct();
$this->load->model('users_model', 'Users');
}
public function create() {
$data = $this->input->post(NULL,TRUE);
if (empty($data)) {
return $this->api_output_error->raise(1);
}
$user_id = $this->Users->insert($data);
if( ! empty($this->Users->validation_errors ) ){
if (isset($this->Users->validation_errors['agree_with_terms'])) {
$this->Users->validation_errors['agree_with_terms'] = "Terms & Conditions are required";
}
return $this->api_output_error->raise(1002, $this->Users->validation_errors);
}
if (!is_numeric($user_id)) {
return $this->api_output_error->raise(1001);
}
$this->load->controller('bios/bios/_create',
array(array(
'user_id'=>$user_id,
'birthday'=> $data['birthday']
))
);
$this->load->controller('educations/educations/_create_default_education',
array(array(
'user_id'=>$user_id
,'university_id'=> $data['university_id']
,'degree_type_id'=> 1
,'user_school_current_year_name_id'=> 1
,'expected_graduation'=>'2014-01-01'
))
);
$this->load->controller('photos_and_videos/photos_and_videos/create_album',
array(array(
'user_id' => $user_id,
'title' => 'Profile Photos & Videos'
))
);
return $this->api_output->send($user_id);
}
public function me() {
return $this->read((int)$this->user['user_id']);
}
public function profile_media($user_id = 0) {
if ( ! is_numeric($user_id) ) {
return $this->api_output_error->raise(5);
}
if (!$user_id) $user_id = $this->user['user_id'];
$data = $this->Users->get($user_id);
if (empty($data)) {
return $this->api_output_error->raise(2);
}
$this->load->model('images/image_model', 'Images');
$this->load->model('videos/video_model', 'Videos');
$this->load->model('photos_and_videos/album_model', 'Album');
$profileImage = $this->Album->get_profile_image($user_id);
$profileCover = $this->Images->get(array('target_table' => 'users_cover', 'target_id' => $user_id, 'deleted' => 0));
$profileVideo = $this->Videos->get(array('target_table' => 'users_profile', 'target_id' => $user_id, 'deleted' => 0, 'zencoder_job_state' => 'finished'));
if ((is_object($profileVideo))) {
$profileVideoThumb = $this->Images->get(array('target_table' => 'videos', 'target_id' => $profileVideo->video_id, 'deleted' => 0));
} else {
$profileVideoThumb = false;
}
$result = array(
'profileImage' => (is_object($profileImage)) ? $this->config->item('router.site.url') . 'image/' . str_replace('/','--',$profileImage->filepath . $profileImage->filename) : false,
'profileCover' => (is_object($profileCover)) ? $this->config->item('router.site.url') . 'image/' . str_replace('/','--',$profileCover->filepath . $profileCover->filename) : false,
'profileVideo' => (is_object($profileVideo)) ? base_url('uploads/' . $profileVideo->filepath . $profileVideo->filename) : false,
'profileVideoId' => (is_object($profileVideo)) ? base_url('uploads/' . $profileVideo->filepath . $profileVideo->video_id) : false,
'profileVideoThumb' => (is_object($profileVideoThumb)) ? $this->config->item('router.site.url') . 'image/' . str_replace('/','--',$profileVideoThumb->filepath . $profileVideoThumb->filename) : false
);
return $this->api_output->send(array('media' => $result, 'data' => $data));
}
public function photos_and_videos($user_id = 0) {
if ( ! is_numeric($user_id) ) {
return $this->api_output_error->raise(5);
}
$user_id or $user_id = $this->user['user_id'];
$data = $this->Users->get($user_id);
if (empty($data)) {
return $this->api_output_error->raise(2);
}
$this->load->model('photos_and_videos/album_model', 'Album');
$albums = $this->Album->get_all(array('user_id' => $user_id, 'deleted' => false));
$result = array(
'albums' => $albums
);
return $this->api_output->send($result);
}
public function read($id) {
if ( ! is_numeric($id) ) {
return $this->api_output_error->raise(5);
}
$data = $this->Users->get($id);
if (empty($data)) {
return $this->api_output_error->raise(2);
}
unset($data->created_at,$data->deleted,$data->password_hash,
$data->password,$data->modified_at,$data->active,$data->reset_token,
$data->reset_token_expires);
return $this->api_output->send($data);
}
public function read_all() {
$data = $this->Users->get_many(array('active'=>1, 'deleted'=>0));
if ( ! is_array($data) ) {
return $this->api_output_error->raise(2);
}
return $this->api_output->send($data);
}
public function search() {
$q = $this->input->post('query');
$users = $this->Users->search($q);
$result = array();
foreach($users as $user) {
if ($user->user_id != $this->user['user_id']) {
$item = new STDClass();
$item->text = $user->first_name . ' ' . $user->last_name;
$item->id = $user->user_id;
$result[] = $item;
}
}
return $this->api_output->send($result);
}
public function update($id, $data = FALSE) {
if ( ! is_numeric($id) ) {
return $this->api_output_error->raise(5);
}
$data = $this->input->post(NULL,TRUE);
if (empty($data)) {
return $this->api_output_error->raise(1);
}
if (!$id) $id = $this->user['user_id'];
$result = $this->Users->update($id, $data);
if( ! empty($this->Users->validation_errors ) ){
return $this->api_output_error->raise(1002, $this->Users->validation_errors);
}
if ($result == '') {
return $this->api_output->send(FALSE);
}
return $this->api_output->send(TRUE);
}
public function delete($id) {
if ( ! is_numeric($id) ) {
return $this->api_output_error->raise(5);
}
$result = $this->Users->update($id, array('deleted'=>1), true);
if ($result == '') {
return $this->api_output->send(FALSE);
}
return $this->api_output->send(TRUE);
}
public function location() {
$data = $this->input->post(NULL,TRUE);
if (empty($data)) {
return $this->api_output_error->raise(1);
}
$result = $this->Users->update($this->user['user_id'], $data);
if( ! empty($this->Users->validation_errors ) ){
return $this->api_output_error->raise(1002, $this->Users->validation_errors);
}
if ($result == '') {
return $this->api_output->send(FALSE);
}
return $this->api_output->send(TRUE);
}
public function filter($page = 1, $search = false, $university_id = false, $major_id = false, $year = false, $group_id = false) {
if (!is_numeric($university_id)) {
$university_id = false;
}
$search === 'null' and $search = false;
$search and $search = urldecode($search);
$major_id === 'null' and $major_id = false;
$year === 'null' and $year = false;
$group_id === 'null' and $group_id = false;
$users = $this->Users->filter($page, $search, $university_id, $major_id, $year, $group_id);
if ( is_array($users) ) {
$users = $this->_populate_students_data($users);
} else {
return $this->api_output_error->raise(2);
}
return $this->api_output->send($users);
}
public function random() {
$disabled_ids = $this->input->post('disabled_ids');
$users = $this->Users->random($disabled_ids);
if ( is_array($users) ) {
$users = $this->_populate_students_data($users);
} else {
return $this->api_output_error->raise(2);
}
return $this->api_output->send($users);
}
public function _populate_students_data($users) {
$this->load->model('groups_model', 'Group');
$this->load->model('group_users_model', 'GroupUsers');
$this->load->model('connections_model', 'Connection');
$this->load->model('educations/user_schools_model', 'School');
$this->load->model('interests/user_interests_model', 'UserInterest');
$this->load->model('photos_and_videos/album_model', 'Album');
$user_id = $this->user['user_id'];
$groups = $this->Group->get_many(array('user_id' => $user_id));
foreach ($users as &$user) {
// Populate user belongs to groups list
$user->belongs_to_groups = array();
if ($groups) {
$belongs_to_groups = $this->GroupUsers->belongs_to_groups($user->user_id, $user_id);
$belongs_to_groups_ids = array();
foreach($belongs_to_groups as $group) {
$belongs_to_groups_ids[$group->group_id] = $group->group_id;
}
foreach($groups as $group) {
$user->belongs_to_groups[] = array(
'group_id' => $group->group_id,
'name' => $group->name,
'isset' => array_key_exists($group->group_id, $belongs_to_groups_ids)
);
}
}
// Current Education
$user->education = $this->School->get_with_year_name(array('user_id' => $user->user_id));
// Interests
$user->interests = $this->UserInterest->get_shared($user->user_id, $user_id);
// Connections
$user->connections = $this->Connection->get_from_user_id($user->user_id, $user_id);
// Profile Image
$profile_image = $this->Album->get_profile_image($user->user_id);
$profile_image = is_object($profile_image) ? $this->config->item('router.site.url').'image/'.str_replace('/','--', $profile_image->filepath.$profile_image->filename) : false;
$user->data = array('profileImage' => $profile_image);
}
return $users;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment