Created
November 27, 2017 06:21
-
-
Save jagroop/39850878b4c6e933296b13543b0a03d8 to your computer and use it in GitHub Desktop.
Controller Issues | Bad Formatting
This file contains hidden or 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 | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
use Carbon\Carbon; | |
class User extends Rest_Controller { | |
/** | |
* Uploads Path | |
*/ | |
const UPLOADS = FCPATH . 'uploads/'; | |
/** | |
* User's default avatar. | |
*/ | |
const DEFAULT_AVATAR = 'avatar.png'; | |
/** | |
* To get all users | |
* @return boolean | |
*/ | |
public function all() { | |
$this->validate($this->input->post(),[ | |
'user_type' => 'required' | |
]); | |
$user_type = (string) $this->input->post('user_type'); | |
if($user_type == 'user') { | |
$user_type = 2; | |
} | |
$users = $this->db->get_where('users', ['user_type' => $user_type])->result(); | |
foreach ($users as $key => $user) { | |
$response[$key]['user_id'] = $user->id; | |
$response[$key]['first_name'] = $user->first_name; | |
$response[$key]['last_name'] = $user->last_name; | |
$response[$key]['email'] = $user->email; | |
$response[$key]['profile_pic'] = base_url() . '/uploads/' . $user->profile_pic; | |
} | |
return (count($users)) ? $this->success(msg('profile_details'), $response) : $this->error(msg("users_not_found")); | |
} | |
/** | |
* To edit user profile | |
* @return boolean | |
*/ | |
public function editUserProfile() { | |
$this->validate($this->input->post(),[ | |
'user_id' => 'required|exist,users:id', | |
'first_name' => 'required' | |
]); | |
$config['upload_path'] = self::UPLOADS; | |
$config['allowed_types'] = 'gif|jpg|png'; | |
$user_id = (int) $this->input->post('user_id'); | |
$tmpData = [ | |
'first_name' => $this->input->post('first_name'), | |
'last_name' => $this->input->post('last_name'), | |
'profile_pic' => self::DEFAULT_AVATAR | |
]; | |
$uploaded = false; | |
if (count($_FILES) > 0 && is_uploaded_file($_FILES['profile_pic']['tmp_name'])) { | |
$profile_pic = $_FILES['profile_pic']['name']; | |
$userfile_extn = explode(".", strtolower($_FILES['profile_pic']['name'])); | |
$fileName = pathinfo(basename($_FILES["profile_pic"]["name"]) ,PATHINFO_EXTENSION); | |
$fileName = str_random(30) . '.' . $fileName; | |
$config['file_name'] = $fileName; | |
$this->load->library('upload', $config); | |
if($this->upload->do_upload('profile_pic')){ | |
$uploaded = true; | |
$tmpData['profile_pic'] = $fileName; | |
} | |
} | |
$oldPicName = $this->db->get_where('users', ['id' => $user_id])->row('profile_pic'); | |
$oldPicName = trim($oldPicName); | |
if($oldPicName != "" && $oldPicName != self::DEFAULT_AVATAR){ | |
if($uploaded === true) { | |
@unlink(self::UPLOADS . $oldPicName); | |
} else { | |
$tmpData['profile_pic'] = $oldPicName; | |
} | |
} | |
if($this->input->post('last_name') == NULL){ | |
$user = $this->db->get_where('users', ['id' => $user_id])->row(); | |
$tmpData['last_name'] = $user->last_name; | |
} | |
$update = $this->db->where('id', $user_id)->update('users', $tmpData); | |
$user = $this->db->get_where('users', ['id' => $user_id])->row(); | |
$tmpData['profile_pic'] = base_url() . '/uploads/' . $user->profile_pic; | |
$tmpData['user_id'] = $user->id; | |
$tmpData['email'] = $user->email; | |
return ($update) ? $this->success(msg('profile_updated'), $tmpData) : $this->error(msg("oops")); | |
} | |
/** | |
* To get user profile | |
* @return boolean | |
*/ | |
public function getUserProfile() { | |
$this->validate($this->input->post(),[ | |
'user_id' => 'required|exist,users:id' | |
]); | |
$user_id = (int) $this->input->post('user_id'); | |
$user = $this->db->get_where('users', ['id' => $user_id])->row(); | |
$response = [ | |
'user_id' => $user->id, | |
'first_name' => $user->first_name, | |
'last_name' => $user->last_name, | |
'email' => $user->email, | |
'profile_pic' => base_url() . '/uploads/' . $user->profile_pic | |
]; | |
return ($user) ? $this->success(msg('profile_details'), $response) : $this->error(msg("oops")); | |
} | |
/** | |
* To send messages | |
* @return boolean | |
*/ | |
public function sendMessage() { | |
$this->validate($this->input->post(),[ | |
'from_id' => 'required|exist,users:id', | |
'to_id' => 'required|exist,users:id', | |
'message' => 'required' | |
]); | |
$from_id = $this->input->post('from_id'); | |
$to_id = $this->input->post('to_id'); | |
$message = $this->input->post('message'); | |
$user = $this->db->select('user_type')->get_where('users',['id' => $from_id])->row(); | |
if(($user->user_type) == 2) { | |
$user_id = $this->db->get_where('messages',['from_id' => $from_id])->result(); | |
// check maximum limit. | |
if(count($user_id) == 5){ | |
return $this->error(msg("maximum_limit")); | |
} | |
$tmpData = [ | |
'from_id' => $from_id, | |
'to_id' => $to_id, | |
'message' => $message | |
]; | |
$insert = $this->db->insert('messages', $tmpData); | |
}else { | |
$ids = array_filter(explode( ',', $to_id )); | |
foreach ($ids as $key => $id) { | |
$tmpData[$key]['from_id'] = $from_id; | |
$tmpData[$key]['to_id'] = $id; | |
$tmpData[$key]['message'] = $message; | |
$insert = $this->db->insert('messages', $tmpData[$key]); | |
} | |
$user = $this->db->select('device_token,device_type')->get_where('users',['id' => $to_id])->row(); | |
// if($insert){ | |
// $this->load->library('Notification.php'); | |
// return $this->Notification->send($user); | |
} | |
return ($insert) ? $this->success(msg('message_sent')) : $this->error(msg("oops")); | |
} | |
/** | |
* To get user messages | |
* @return boolean | |
*/ | |
public function getMessage() { | |
$this->validate($this->input->post(),[ | |
'user_id' => 'required|exist,users:id' | |
]); | |
$user_id = $this->input->post('user_id'); | |
$user = $this->db->select('user_type')->get_where('users',['id' => $user_id])->row(); | |
$messages = $this->db->where('from_id ', $user_id) | |
->or_where('to_id', $user_id) | |
->from('messages')->get()->result(); | |
foreach ($messages as $key => $message) { | |
$tmpData[$key]['from_id'] = $message->from_id; | |
$tmpData[$key]['to_id'] = $message->to_id; | |
$tmpData[$key]['message'] = $message->message; | |
$tmpData[$key]['date'] = $message->date; | |
} | |
return ($messages) ? $this->success(msg('all_messages'),$tmpData) : $this->error(msg("no_message")); | |
} | |
/** | |
* To get user messages | |
* @return boolean | |
*/ | |
public function allUsersContacts() { | |
$this->validate($this->input->post(),[ | |
'user_id' => 'required|exist,users:id' | |
]); | |
$user_id = $this->input->post('user_id'); | |
$messages = $this->db->select('from_id')->distinct() | |
->from('messages') | |
->where('from_id ', $user_id) | |
->or_where('to_id', $user_id) | |
->get()->result(); | |
// var_dump($this->db->last_query());die('dmskds'); | |
foreach ($messages as $key => $message) { | |
$tmpData[$key]['from_id'] = $message->from_id; | |
} | |
return ($messages) ? $this->success(msg('all_messages'),$tmpData) : $this->error(msg("no_message")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment