HEX
Server: Apache/2.4.41 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.40
System: Linux ip-172-31-40-18 4.14.146-93.123.amzn1.x86_64 #1 SMP Tue Sep 24 00:45:23 UTC 2019 x86_64
User: apache (48)
PHP: 5.6.40
Disabled: NONE
Upload Files
File: //var/www/html/qcr24/app/application/controllers/admin/User.php.bk
<?php
defined('BASEPATH') or exit('No direct script access allowed');

class User extends MY_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('admin/muser');
		$this->load->model('admin/mproperty');
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$where = array('ma.user_id <>'=> $this->admin_session_data['user_id']);
		if($this->admin_session_data['role_id'] != ROLE_SUPERADMIN && $this->admin_session_data['property_unit_master_id'] > 0){
			$where['ma.property_unit_master_id'] = $this->admin_session_data['property_unit_master_id'];
		}
		$data['users'] = $this->muser->get_user($where);
		$data['content'] = 'admin/user/list';
		$this->load->view('admin/layouts/index', $data);
	}
	public function adduser($pre_data = array())
	{
		$data = array();
		$data = $pre_data;
		$data['roles'] = $this->muser->get_role();
		$data['zilla_parishads'] = $this->mproperty->get_property_unit(array('parent_unit_id' => 0));
		//$data['states'] = $this->muser->get_state();
		$data['parent_user'] = $this->muser->edit_user($this->admin_session_data['user_id']);
		$data['content'] = 'admin/user/add';
		$this->load->view('admin/layouts/index', $data);
	}
	public function edituser($user_id)
	{
		$data['user'] = $this->muser->edit_user($user_id);
	//var_dump($data['user']);
		$data['roles'] = $this->muser->get_role();
		$data['zilla_parishads'] = $this->mproperty->get_property_unit(array('parent_unit_id' => 0));
		$data['user_property'] = $this->muser->get_user_property(array('user_id' => $user_id));
		$data['user_property'] = !empty($data['user_property']) ? array_column($data['user_property'], 'property_id') : array();
		//$data['districts'] = $this->muser->get_district($data['user']['state_id']);
		$data['content'] = 'admin/user/edit';
		$this->load->view('admin/layouts/index', $data);
	}

	public function submitUser()
	{
		$where = array(
			'ma.email' => $this->input->post('email'),
			'ma.status <>' => 2,
		);
		$has_useremail = $this->muser->get_user($where);
		if(!empty($has_useremail)){
			$this->session->set_flashdata('error_msg', 'Email is already used.');
			$response = array(
				'success' => FALSE,
				'message' => 'Email is already used.',
			);
			echo json_encode($response); exit;
		}
		if($this->input->post('password') != $this->input->post('confirm_password')){
			$this->session->set_flashdata('error_msg', 'Password & Confirm Password not matched.');
			$response = array(
				'success' => FALSE,
				'message' => 'Password & Confirm Password not matched.',
			);
			echo json_encode($response); exit;
		}
		/* $where = array(
			'ma.user_name' => $this->input->post('user_name'),
			'ma.status <>' => 2,
		);
		$has_username = $this->muser->get_user($where);
		if(!empty($has_username)){
			$this->session->set_flashdata('error_msg', 'Username is already used.');
			$response = array(
				'success' => FALSE,
				'message' => 'Username is already used.',
			);
			echo json_encode($response); exit;
		} */
		try{
			$data = array(
				'role_id' => $this->input->post('role_id'),
				'full_name' => $this->input->post('full_name'),
				'designation' => $this->input->post('designation'),
				'password' => md5($this->input->post('password')),
				'email' => $this->input->post('email'),
				'gender' => $this->input->post('gender'),
				'contact_no' => $this->input->post('contact_no'),
				/* 
				'alternate_contact_no' => $this->input->post('alternate_contact_no'),
				'address_line_1' => $this->input->post('address_line_1'),
				'address_line_2' => $this->input->post('address_line_2'),
				'state_id' => $this->input->post('state_id'),
				'district_id' => $this->input->post('district_id'),
				'city' => $this->input->post('city'),
				'pincode' => $this->input->post('pincode'),
				'aadhaar_no' => $this->input->post('aadhaar_no'),
				'pan_no' => $this->input->post('pan_no'), 
				*/
				'property_unit_master_id'=> $this->input->post('unit_id'),
				'status' => $this->input->post('status'),
				'created_by' => $this->admin_session_data['user_id'],
				'created_ts' => date('Y-m-d H:i:s')
			);
			$result = $this->muser->submit_user($data);
				
			if ($result) {
				$property_id = $this->input->post('property_id');
				if(!empty($property_id)){
					foreach($property_id as $p_id){
						$user_property = array(
							'user_id' => $result,
							'property_id' => $p_id,
						);
						$this->muser->submit_user_property($user_property);
					}
				}
				$this->session->set_flashdata('success_msg', 'User Added Successfully');
				$response = array(
					'success' => TRUE,
					'message' => 'User Details Added Successfully.',
					'data'=> $result
				);
			}else{
				$response = array(
					'success' => FALSE,
					'message' => 'Unable to save user details.',
					'data'=> $result
				);
			}
		}catch(Exception $ex){
			$response = array(
				'success' => FALSE,
				'message' => 'Something went wrong.',
				'data'=> $result
			);
		}

		echo json_encode($response); exit;
	}

	public function updateuser($user_id = '')
	{
		$where = array(
			'ma.email' => $this->input->post('email'),
			'ma.status <>' => 2,
			'ma.user_id <>' => $user_id
		);
		$has_useremail = $this->muser->get_user($where);
		if(!empty($has_useremail)){
			$this->session->set_flashdata('error_msg', 'Email is already used.');
			$response = array(
				'success' => FALSE,
				'message' => 'Email is already used.',
			);
			echo json_encode($response); exit;
		}

		/* $where = array(
			'ma.user_name' => $this->input->post('user_name'),
			'ma.status <>' => 2,
			'ma.user_id <>' => $user_id
		);
		$has_username = $this->muser->get_user($where);
		if(!empty($has_username)){
			$this->session->set_flashdata('error_msg', 'Username is already used.');
			$response = array(
				'success' => FALSE,
				'message' => 'Username is already used.',
			);
			echo json_encode($response); exit;
		} */
		if(!empty($this->input->post('password')) && empty($this->input->post('confirm_password'))){
			$response = array(
				'success' => FALSE,
				'message' => 'Confirm Password is required when you have used password.',
			);
			echo json_encode($response); exit;
		}
		if(!empty($this->input->post('password')) && !empty($this->input->post('confirm_password'))){
			if($this->input->post('password') != $this->input->post('confirm_password')){
				$response = array(
					'success' => FALSE,
					'message' => 'Password & Confirm Password are not same.',
				);
				echo json_encode($response); exit;
			}
		}
		try{
			$data = array(
				'role_id' => $this->input->post('role_id'),
				'full_name' => $this->input->post('full_name'),
				'designation' => $this->input->post('designation'),
				'email' => $this->input->post('email'),
				'gender' => $this->input->post('gender'),
				'contact_no' => $this->input->post('contact_no'),
				'property_unit_master_id'=> $this->input->post('unit_id'),
				'status' => $this->input->post('status'),
				'updated_by' => $this->admin_session_data['user_id'],
				'updated_ts' => date('Y-m-d H:i:s')
			);

			if(!empty($this->input->post('password'))){
				$data['password'] = md5($this->input->post('password'));
			}

			$condition = array('user_id' => $user_id);
			$result = $this->muser->update_user($condition, $data);
				
			if ($result) {
				$property_id = $this->input->post('property_id');
				if(!empty($property_id)){
					$this->muser->delete_user_property(array('user_id' => $user_id));
					foreach($property_id as $p_id){
						$user_property = array(
							'user_id' => $user_id,
							'property_id' => $p_id,
						);
						$this->muser->submit_user_property($user_property);
					}
				}
				$this->session->set_flashdata('success_msg', 'User Updated Successfully');
				$response = array(
					'success' => TRUE,
					'message' => 'User Details Added Successfully.',
					'data'=> $result
				);
			}else{
				$response = array(
					'success' => FALSE,
					'message' => 'Unable to update user details.',
					'data'=> $result
				);
			}
		}
		catch(Exception $ex){
			$response = array(
				'success' => FALSE,
				'message' => 'Something went wrong.',
				'data'=> $result
			);
		}

		echo json_encode($response); exit;
	}
	
	public function get_district()
	{
		$data = array();
		$state_id=$this->input->post('state_id');
		$data = $this->muser->get_district($state_id);
		echo json_encode($data); 
	}
}