1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
class Charity_model extends CI_Model {
public function _consruct(){
parent::_construct();
}
function addCharity($charityData){
$check_exam_name = $this->db->get_where('tbl_charity',array('title'=>$charityData['title']));
if($check_exam_name->num_rows() > 0){
return false;
}else{
if($this->db->insert('tbl_charity',array('title'=>ucfirst($charityData['title']),'description'=>ucfirst($charityData['description']),'date'=>strtotime(date('Y-m-d'))))) {
$insert_id = $this->db->insert_id();
$res = array('status'=>'success','data'=>$insert_id);
}else{
$res = array('status'=>'Failed','message'=>"Insertion Failed. Something went wrong");
}
return $res;
}
}
function delete_register($id){
$this->db->delete('tbl_charity',array('id'=>$id));
}
function add_immage($id,$data){
if($this->db->update('tbl_charity',array('image'=>$data['image']),array('id'=>$id))){
return $res = array('status'=>'success');
}
}
function get_all_charity(){
$all_charity = $this->db->get('tbl_charity');
if($all_charity->num_rows() > 0){
$result = $all_charity->result_array();
return $result;
}
}
function get_all_clinic(){
$all_clinic = $this->db->get('tbl_clinic');
if($all_clinic->num_rows() > 0){
$result = $all_clinic->result_array();
return $result;
}
}
function charity_delete($id){
if($this->db->delete('tbl_charity',array('id'=>$id))){
return true;
}
}
function get_single_charity($id){
$single_charity= $this->db->get_where('tbl_charity',array('id' => $id));
if($single_charity->num_rows() > 0){
return $single_charity->row_array();
}
}
function update_charity($data,$id){
$this->db->where('title',$data['title']);
$this->db->where('id !=',$id);
$res = $this->db->get('tbl_charity')->row();
if($res){
return false;
}else{
if($this->db->update('tbl_charity',array('title'=>ucfirst($data['title']),'description'=>ucfirst($data['description'])),array('id'=>$id))){
return true;
}
}
}
function addServices($service_data){
$query = $this->db->get_where('tbl_charity_service',array('charity_id'=>$service_data['charity_id'],'clinic_id'=>$service_data['clinic_id']))->row_array();
if($query > 0){
$amount = explode(',',$service_data['amount']);
$db_amount = explode(',', $query['amount']);
foreach ($amount as $value) {
if(in_array($value, $db_amount)){
$return = 1;
}else{
$return = 0;
}
}
if($return == 1){
$res = array('status'=>'failed','message'=>'Sorry Not Inserted. This amount for Clinic Id already Exist in this Charity');
return $res;
}else{
$new_amount = array_merge($amount,$db_amount);
$new = implode(',',$new_amount);
unset($service_data['amount']);
$service_data['amount'] = $new;
$result = $this->insert_section($service_data,$query['id']);
}
}else{
$result = $this->insert_section($service_data,'');
}
return $result;
}
public function insert_section($service_data,$id){
if($id != ''){
if($this->db->update('tbl_charity_service',array('clinic_id'=>$service_data['clinic_id'],'amount'=>$service_data['amount'],'charity_id'=>$service_data['charity_id']),array('id'=>$id))){
$res = array('status'=>'success','message'=>'Inserted Successfully');
}else{
$res = array('status'=>'failed','message'=>'Sorry Not Inserted. Something Went Wrong. Try Again Later');
}
}
else{
if($this->db->insert('tbl_charity_service',array('clinic_id'=>$service_data['clinic_id'],'amount'=>$service_data['amount'],'charity_id'=>$service_data['charity_id']))){
$res = array('status'=>'success','message'=>'Inserted Successfully');
}else{
$res = array('status'=>'failed','message'=>'Sorry Not Inserted. Something Went Wrong. Try Again Later');
}
}
return $res;
}
public function get_all_services(){
$this->db->select('tbl_clinic.name,tbl_charity.title,tbl_charity_service.amount,tbl_charity_service.id');
$this->db->join('tbl_charity','tbl_charity.id = tbl_charity_service.charity_id');
$this->db->join('tbl_clinic','tbl_clinic.id = tbl_charity_service.clinic_id');
return $this->db->get('tbl_charity_service')->result_array();
}
public function service_delete($id){
if($this->db->delete('tbl_charity_service',array('id'=>$id))){
return true;
}
}
public function get_single_service($id){
$single_service= $this->db->get_where('tbl_charity_service',array('id' => $id));
if($single_service->num_rows() > 0){
return $single_service->row_array();
}
}
public function update_service($data,$id){
$this->db->where('id',$id);
$res = $this->db->get('tbl_charity_service')->row_array();
if($res){
$amount = explode(',',$data['amount']);
$db_amount = explode(',', $res['amount']);
foreach ($amount as $value) {
//print_r($value);echo"<br>";
$new = array();
if(in_array($value, $db_amount)){
$return = 1;
}else{
$new[] = $value;
$return = 0;
}
}
$data['clinic_id'] = $res['clinic_id'];
$data['charity_id'] = $res['charity_id'];
if($return == 1 && (count($new) == 0) && (count($amount) == count($db_amount))){
$res = array('status'=>'failed','message'=>'Sorry Not Inserted. This amount for Clinic Id already Exist in this Charity');
return $res;
}elseif((count($amount) != count($db_amount)) && (count($new) == 0)) {
$res = $this->insert_section($data,$id);
}elseif((count($new) > 0)){
$new_amount = array_merge($new,$db_amount);
$new = implode(',',$new_amount);
unset($data['amount']);
$data['amount'] = $new;
$res = $this->insert_section($data,$id);
}
return $res;
}
}
}