load->database(); $this->load->database('Classroom_Model'); } /**************************************** START of Admin Roles - Nandini ************************************/ public function get_bt_admin_roles($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE) { if ($limit) $this->db->limit($limit, $offset); if ($where != '') $this->db->where($where); $this->db->order_by('bt_admin_roles.id', 'DESC'); $query = $this->db->get('bt_admin_roles'); if ($sqlQry != '') $query = $this->db->query($sqlQry); return $query->result_array(); } public function check_role_exists($id, $rname) { $rId = ($this->input->post('id') !== NULL) ? $this->input->post('id') : $id; $rName = ($this->input->post('role_name') !== NULL) ? $this->input->post('role_name') : $rname; $query = $this->db->get_where('bt_admin_roles', array('role_name' => $rName)); if (!empty($query->row_array())) { if ($rId == 0) return false; //Duplicate role_name else { if ($query->row(0)->id != $rId) return false; //Duplicate role_name } } return true; //No duplicate } function add_edit_admin_role($rId = 0, $data = '') { if ($rId == 0) { $this->db->insert('bt_admin_roles', $data); if ($this->db->affected_rows() > 0) { $insertid = $this->db->insert_id(); return $insertid; } else { return 0; } } else { $this->db->where('id', $rId); $this->db->update('bt_admin_roles', $data); if ($this->db->affected_rows() > 0) return true; else { if ($this->db->trans_status() === FALSE) return 0; return true; } } } public function delete_admin_role($idVal) { $delSqlQry = "DELETE FROM bt_admin_roles WHERE id = $idVal"; $this->db->query($delSqlQry); } /**************************************** ENDDD of Admin Roles - Nandini ************************************/ /**************************************** START of Admin Menu - Nandini ************************************/ function recursive_menue($parentId = 0) { $this->db->where('parent_id', $parentId); //$this->db->where('is_active', 'Y'); $this->db->order_by('order_number', 'ASC'); $query = $this->db->get('bt_admin_menu'); $result = $query->result_array(); $adminMenu = array(); if (!empty($result)) { foreach ($result as $res) { array_push($adminMenu, $res); } } return $adminMenu; } function get_admin_menu($parentId = 0) { $zeroMenu = $this->recursive_menue(0); $adminMenu = array(); if (!empty($zeroMenu)) { foreach ($zeroMenu as $zero) { $adminMenu[$zero['id']] = $zero; $oneMenu = $this->recursive_menue($zero['id']); if (!empty($oneMenu)) { foreach ($oneMenu as $one) { $adminMenu[$zero['id']][$zero['id']][$one['id']] = $one; $twoMenu = $this->recursive_menue($one['id']); if (!empty($twoMenu)) { foreach ($twoMenu as $two) { $adminMenu[$zero['id']][$zero['id']][$one['id']][$one['id']][$two['id']] = $two; } } } } } } //echo '
';print_r($adminMenu);
		return $adminMenu;
	}

	public function get_bt_admin_role_permissions($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE)
	{
		if ($limit)
			$this->db->limit($limit, $offset);

		if ($where != '')
			$this->db->where($where);

		$this->db->order_by('bt_admin_role_permissions.id', 'DESC');
		$query = $this->db->get('bt_admin_role_permissions');

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	function add_edit_admin_role_permissions($rpId = 0, $data = '')
	{
		if ($rpId == 0) {
			$this->db->insert('bt_admin_role_permissions', $data);
			if ($this->db->affected_rows() > 0) {
				$insertid = $this->db->insert_id();
				return  $insertid;
			} else {
				return 0;
			}
		} else {
			$this->db->where('id', $rpId);
			$this->db->update('bt_admin_role_permissions', $data);
			if ($this->db->affected_rows() > 0)
				return true;
			else {
				if ($this->db->trans_status() === FALSE)
					return 0;

				return true;
			}
		}
	}
	/**************************************** END of Admin Menu - Nandini ************************************/

	public function check_emailid_exists($id, $email)
	{
		$uId = ($this->input->post('id') !== NULL) ? $this->input->post('id') : $id;
		$eId = ($this->input->post('email') !== NULL) ? $this->input->post('email') : $email;

		$query = $this->db->get_where('bt_admin', array('email' => $eId));
		if (!empty($query->row_array())) {
			if ($uId == 0)
				return false;											//Duplicate email id
			else {
				if ($query->row(0)->id != $uId)
					return false;										//Duplicate email id				
			}
		}
		return true;													//No duplicate
	}

	function add_edit_admin_user($uId = 0, $data = '')
	{
		if ($uId == 0) {
			$this->db->insert('bt_admin', $data);
			if ($this->db->affected_rows() > 0) {
				$insertid = $this->db->insert_id();
				return  $insertid;
			} else {
				return 0;
			}
		} else {
			$this->db->where('id', $uId);
			$this->db->update('bt_admin', $data);
			if ($this->db->affected_rows() > 0)
				return true;
			else {
				if ($this->db->trans_status() === FALSE)
					return 0;

				return true;
			}
		}
	}

	public function delete_admin_user($idVal)
	{
		$delSqlQry = "DELETE FROM bt_admin WHERE id = $idVal";
		$this->db->query($delSqlQry);
		return true;
	}

	public function get_admin_details($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE)
	{
		if ($limit) {
			$this->db->limit($limit, $offset);
		}
		if ($where != '')
			$this->db->where($where);

		$this->db->order_by('bt_admin.id', 'DESC');
		$query = $this->db->get('bt_admin');

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	public function get_admin_menu_details($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE)
	{
		if ($limit) {
			$this->db->limit($limit, $offset);
		}
		if ($where != '')
			$this->db->where($where);

		$this->db->order_by('bt_admin_menu.id', 'DESC');
		$query = $this->db->get('bt_admin_menu');

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	public function check_is_emailid_valid($email)
	{
		$query = $this->db->query("SELECT id FROM bt_admin WHERE email='$email'");
		if ($row = $query->row()) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/*----------------------------- START of Admin User Login Functionality -----------------------------*/
	public function check_login_credentials($email, $encrypt_password)
	{
		$where = 'email="' . $email . '" AND password="' . $encrypt_password . '"';
		$this->db->where($where);
		$result = $this->db->get('bt_admin');

		if ($result->num_rows() == 1) {
			return true;
		} else {
			return false;
		}
	}
	/*----------------------------- ENDDD of Admin User Login Functionality -----------------------------*/

	/*----------------------------- START of Admin User Password Functionality -----------------------------*/
	public function reset_admin_password($uId, $new_password)
	{
		$data = array(
			'password' => md5($new_password)
		);
		$this->db->where('id', $uId);
		return $this->db->update('bt_admin', $data);
	}
	/*----------------------------- ENDDD of Admin User Password Functionality -----------------------------*/

	public function delete_bus($id, $table)
	{

		$query = $this->db->query(
			'
			SELECT ' . $table . '.id
			FROM ' . $table . '
			WHERE ' . $id . ' IN (SELECT bus_id FROM bt_routes where bus_id=' . $id . ')'
		);

		if ($query->num_rows() > 0) {
			echo '0';
		} else {

			$get_data = $this->db->query('select image from ' . $table . ' where id=' . $id);
			$delete_image = $get_data->row_array();

			if ($delete_image) {
				if (file_exists($delete_image['image'])) {
					unlink($delete_image['image']);
					// echo 'File'. $delete_image['image'] . ' deleted';
				} else {
					// echo 'File'. $delete_image['image'] . ' Not deleted';
				}
			}


			$delete = $this->db->delete('bt_buses', array('id' => $id));
			if ($delete) {
				echo '1';
			}
		}
	}

	public function delete_driver($id, $table)
	{

		$query = $this->db->query(
			'
			SELECT ' . $table . '.id
			FROM ' . $table . '
			WHERE ' . $id . ' IN (SELECT driver_id FROM bt_routes where driver_id=' . $id . ')'
		);

		if ($query->num_rows() > 0) {
			echo '0';
		} else {

			$get_data = $this->db->query('select image, document1, document2,document3 from ' . $table . ' where id=' . $id);
			$delete_image = $get_data->row_array();

			if ($delete_image) {
				if (file_exists($delete_image['image'])) {
					unlink($delete_image['image']);
					unlink($delete_image['document1']);
					unlink($delete_image['document2']);
					unlink($delete_image['document3']);
					// echo 'File'. $delete_image['image'] . ' deleted';
				} else {
					// echo 'File'. $delete_image['image'] . ' Not deleted';
				}
			}


			$delete = $this->db->delete('bt_drivers', array('id' => $id));
			if ($delete) {
				echo '1';
			}
		}
	}


	public function check_delete_route($id, $table)
	{
		$query = $this->db->query(
			'
			SELECT ' . $table . '.id
			FROM ' . $table . '
			WHERE ' . $id . ' IN (SELECT route_id FROM bt_students_bus_route_details where route_id=' . $id . ')'
		);
		if ($query->num_rows() > 0) {
			echo '0';
		} else {

			$delete = $this->db->delete('bt_routes_details', array('route_id' => $id));
			if ($delete) {
				$delete_route = $this->db->delete('bt_routes', array('id' => $id));
				if ($delete_route) {
					echo '1';
				}
			} else {
				echo '0';
			}
		}
	}

	public function count_query($table)
	{
		return $this->db->count_all($table);
	}

	public function getDriverPickPoint($id)
	{

		$sql = $this->db->query("SELECT pickup_start as start_time, place_name as stat_point, working_id, br.id as route_id FROM bt_drivers bd,bt_routes br, bt_routes_details brd WHERE br.id='$id' and bd.id = br.driver_id and br.id = brd.route_id  order by route_order asc limit 1")->row_array();

		return $sql;
	}

	public function getDriverDropPoint($id)
	{

		$sql = $this->db->query("SELECT drop_start as start_time, place_name as stat_point, working_id, br.id as route_id FROM bt_drivers bd,bt_routes br, bt_routes_details brd WHERE br.id='$id' and bd.id = br.driver_id and br.id = brd.route_id  order by route_order desc limit 1")->row_array();
		return $sql;
	}

	public function get_query_result_bus_tracking($sqlQry)
	{
		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);
		return $query->result_array();
	}
	public function get_query_row_bus_tracking($sqlQry)
	{
		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);
		return $query->row_array();
	}

	public function get_Count_Student_routes_details($pick_id)
	{

		$query = $this->db->query('select count(id) as student_count from bt_students_bus_route_details where pick_up_id =' . $pick_id);
		return $query->row_array();
	}

	public function get_Count_Student_trip_details($id)
	{

		$query = $this->db->query("select count(bst.id) as student_count from bt_students_trip bst, bt_trips bt, bt_students_bus_route_details bsbrd  where bst.trip_id =$id and bst.trip_id = bt.id and bt.route_id = bsbrd.route_id and bt.route_stop_id = bsbrd.pick_up_id AND bst.student_id = bsbrd.student_id");
		return $query->row_array();
	}

	/************ ENDDD of Batch Route - Shankar *******************/

	function add_edit_batch_route($rId = 0, $data = '')
	{
		if ($rId == 0) {
			$this->db->insert('bt_admin_roles', $data);
			if ($this->db->affected_rows() > 0) {
				$insertid = $this->db->insert_id();
				return  $insertid;
			} else {
				return 0;
			}
		} else {
			$this->db->where('id', $rId);
			$this->db->update('bt_admin_roles', $data);
			if ($this->db->affected_rows() > 0)
				return true;
			else {
				if ($this->db->trans_status() === FALSE)
					return 0;

				return true;
			}
		}
	}

	public function delete_batch_route($idVal)
	{
		$delSqlQry = "DELETE FROM bt_admin_roles WHERE id = $idVal";
		$this->db->query($delSqlQry);
	}

	function add_edit_batch_route_permissions($rpId = 0, $data = '')
	{
		if ($rpId == 0) {
			$this->db->insert('bt_batch_routes', $data);
			if ($this->db->affected_rows() > 0) {
				$insertid = $this->db->insert_id();
				return  $insertid;
			} else {
				return 0;
			}
		} else {
			$this->db->where('id', $rpId);
			$this->db->update('bt_batch_routes', $data);
			if ($this->db->affected_rows() > 0)
				return true;
			else {
				if ($this->db->trans_status() === FALSE)
					return 0;

				return true;
			}
		}
	}

	public function delete_batch_route_role($idVal)
	{
		$delSqlQry = "DELETE FROM bt_batch_routes WHERE id = $idVal";
		$this->db->query($delSqlQry);
	}

	public function get_admin_role_permissions($where = '', $sqlQry = '', $limit = FALSE, $offset = FALSE)
	{
		if ($limit)
			$this->db->limit($limit, $offset);

		if ($where != '')
			$this->db->where($where);

		$this->db->order_by('admin_role_permissions.id', 'DESC');
		$query = $this->db->get('admin_role_permissions');

		if ($sqlQry != '')
			$query = $this->db->query($sqlQry);

		return $query->result_array();
	}

	/************ ENDDD of Batch Route - Shankar *******************/

	public function get_unique_name($field_name, $field_value, $table_name)
	{
		$sql_query = "select * from " . $table_name . " WHERE " . $field_name . " = '" . $field_value . "'";
		$query = $this->db->query($sql_query);
		$results = $query->result_array();
		if (empty($results)) {
			return true;
		} else {
			return false;
		}
	}
}