Ticket #22: m_shop.php

File m_shop.php, 8.3 KB (added by 13243, 10 years ago)
Line 
1<?php
2
3class M_shop extends CI_Model {
4
5
6 function __construct()
7 {
8 // Call the Model constructor
9 parent::__construct();
10 }
11 function getMenu($is_for_head = null)
12 {
13 $q = "select * from tkatalog where katalog_status=1 ";
14 if($is_for_head != null){
15 $q .= " and katalog_header = ".$is_for_head." ";
16 }
17 $q .= " order by katalog_id asc";
18 $rez = $this->db->query($q);
19 return $rez->result_array();
20 }
21 function getMenuItems($is_for_head = null){
22 $q = "select * from tkategorija where kategorija_status=1 ";
23 if($is_for_head != null){
24 $q .= " and kategorija_header = ".$is_for_head." ";
25 }
26 $q .= " order by kategorija_order asc";
27 $rez = $this->db->query($q);
28 return $rez->result_array();
29 }
30
31 function getAllCategories(){
32 $q = "select * from tkategorija order by kategorija_order asc ";
33 $rez = $this->db->query($q);
34 return $rez->result_array();
35 }
36 function saveProduct($kategorija, $naziv, $slika, $cena, $lager, $akcija=0){
37 $q = "insert into tproizvod (kategorija_id, proizvod_ime, proizvod_slika, proizvod_cena, proizvod_lager, proizvod_akcija)
38 values ( ".$kategorija.", '".$naziv."', '".$slika."', ".$cena.", ".$lager.", ".$akcija." )";
39 $this->db->query($q);
40 return true;
41
42 }
43
44 function getHomeProducts(){
45 $q = "select * from tproizvod p left join tkategorija k
46 on k.kategorija_id = p.kategorija_id where proizvod_home=1 order by proizvod_order asc ";
47 $rez = $this->db->query($q);
48 return $rez->result_array();
49 }
50
51 function getNumOfProducts($kat_id){
52 $q = "select count(*) as vkupno from tproizvod where kategorija_id=".$kat_id;
53 $rez = $this->db->query($q);
54 $vk = $rez->row_array();
55 return $vk['vkupno'];
56 }
57
58 function getNumberOfProducts($kat_id){
59 $q = "select count(*) as vkupno from tproizvod where katalog_id=".$kat_id;
60 $rez = $this->db->query($q);
61 $vk = $rez->row_array();
62 return $vk['vkupno'];
63 }
64
65
66function setInvoiceStatus($invoice_id,$status){
67 $q = "update tinvoice set invoice_status=".$status." where invoice_id=".$invoice_id." ";
68 $this->db->query($q);
69 return true;
70 }
71
72 function setStatus($proizvod_id,$status){
73 $q = "update tproizvod set proizvod_status=".$status." where proizvod_id=".$proizvod_id." ";
74 $this->db->query($q);
75 return true;
76 }
77
78function setHome($proizvod_id,$status){
79 $q = "update tproizvod set proizvod_home=".$status." where proizvod_id=".$proizvod_id." ";
80 $this->db->query($q);
81 return true;
82 }
83
84 function getAllP(){
85 $q = "select * from tproizvod p left join tkategorija k
86 on k.kategorija_id = p.kategorija_id ";
87
88
89 $q .= " order by proizvod_order asc ";
90
91 $rez = $this->db->query($q);
92 return $rez->result_array();
93 }
94
95 function getProductsByCategory($kategorija_id){
96 $q = "select * from tproizvod p left join tkategorija k
97 on k.kategorija_id = p.kategorija_id ";
98
99 if($kategorija_id > 0){
100 $q .= "where p.kategorija_id=".$kategorija_id." ";
101 }
102
103 $q .= " order by proizvod_order asc ";
104
105 $rez = $this->db->query($q);
106 return $rez->result_array();
107 }
108
109 function getCategoryName($kategorija_id){
110 if($kategorija_id > 0){
111 $q = "select kategorija_name from tkategorija where kategorija_id=".$kategorija_id;
112 $rez = $this->db->query($q);
113 $vk = $rez->row_array();
114 return $vk['kategorija_name'];
115 }else{
116 return "";
117 }
118 }
119
120 function validniPodatoci($username,$pass){
121 $q = "select * from tuser where user_email='".$username."' AND user_password = '".md5($pass)."' limit 1 ";
122 $rez = $this->db->query($q);
123 $usr = $rez->row_array();
124 if(!empty($usr)){
125 $_SESSION['logiran'] = 1;
126 $_SESSION['info'] = $usr;
127 return true;
128 }else{
129 return false;
130 }
131 }
132
133 function getProizod($proizvod_id){
134 $q = "select * from tproizvod p left join tkategorija k on k.kategorija_id=p.kategorija_id where p.proizvod_id=".$proizvod_id;
135 $rez = $this->db->query($q);
136 return $rez->row_array();
137 }
138
139 function addToCart($proizvod_id){
140 $q = "select * from tproizvod where proizvod_id=".$proizvod_id;
141 $rez = $this->db->query($q);
142 $pr = $rez->row_array();
143 if(!empty($pr)){
144 if(isset($_SESSION['cart'][$pr['proizvod_id']]) ){
145 if( $pr['proizvod_lager'] > $_SESSION['cart'][$pr['proizvod_id']]){
146 $_SESSION['cart'][$pr['proizvod_id']] ++ ;
147 }else{
148 $_SESSION['cart'][$pr['proizvod_id']] = (int)$pr['proizvod_lager'];
149 }
150 }else{
151 if( $pr['proizvod_lager'] > 0){
152 $_SESSION['cart'][$pr['proizvod_id']] = 1;
153 }
154 }
155 return true;
156 }else{
157 return false;
158 }
159 }
160
161 function removeFromCart($proizvod_id){
162 $q = "select * from tproizvod where proizvod_id=".$proizvod_id;
163 $rez = $this->db->query($q);
164 $pr = $rez->row_array();
165 if(!empty($pr)){
166 if(isset($_SESSION['cart'][$pr['proizvod_id']]) ){
167 if( $_SESSION['cart'][$pr['proizvod_id']] > 1){
168 $_SESSION['cart'][$pr['proizvod_id']] -- ;
169 }else{
170 unset($_SESSION['cart'][$pr['proizvod_id']]);
171 }
172 }
173 return true;
174 }else{
175 return false;
176 }
177 }
178
179 function checkout(){
180 $user_id = $_SESSION['info']['user_id'];
181 $total = $_SESSION['total_price'];
182
183 $q = "insert into tinvoice (user_id, invoice_total) values (".$user_id.", ".$total.")";
184 $this->db->query($q);
185
186 $invoice_id = mysql_insert_id();
187
188 foreach ($_SESSION['cart'] as $kluc=>$vrednost){
189 $tmp = array();
190 $pro = $this->getProizod($kluc);
191
192 $qq = "insert into tinvoiceproduct (invoice_id, proizvod_id, ip_quantity, ip_unit_price)
193 values (".$invoice_id.", ".$pro['proizvod_id'].", ".$vrednost.", ".$pro['proizvod_cena'].")";
194 $this->db->query($qq);
195
196
197 }
198 unset($_SESSION['cart']);
199 unset($_SESSION['total_price']);
200 return true;
201 }
202
203 function getAllF(){
204 $q = "select * from tinvoice left join tuser on tuser.user_id = tinvoice.user_id ";
205 $rez = $this->db->query($q);
206 return $rez->result_array();
207 }
208
209
210 function getInvoiceDetali($invoice_id){
211 $q = "select * from tinvoiceproduct ip left join tproizvod p on p.proizvod_id=ip.proizvod_id where ip.invoice_id=".$invoice_id." ";
212 $rez = $this->db->query($q);
213 return $rez->result_array();
214 }
215
216 function getNI($u_id)
217 {
218 $q= "select count(*) as vkupno from tinvoice where user_id=".$u_id;
219 $rez = $this->db->query($q);
220 $p = $rez->row_array();
221
222 return $p['vkupno'];
223 }
224 function getAllUsers()
225 {
226 $q="select * from tuser";
227 $rez = $this->db->query($q);
228 $user=$rez->result_array();
229 for($i=0;$i<count($user);$i++)
230 {
231 $user[$i]['bi']=$this->getNI($user[$i]['user_id']);
232 }
233 return $user;
234 }
235
236 function getUser_id($us_id)
237 {
238 $q="select * from tinvoice i left join tuser u on i.user_id=u.user_id ".$us_id;
239 $rez = $this->db->query($q);
240 return $rez->row_array();
241
242 }
243
244 function getProductsByKatalog($katalog_id)
245 {
246 $q = "select * from tproizvod p left join tkatalog k
247 on k.katalog_id = p.katalog_id ";
248
249 if($katalog_id > 0){
250 $q .= "where p.katalog_id=".$katalog_id." ";
251 }
252
253 $q .= " order by proizvod_order asc ";
254
255 $rez = $this->db->query($q);
256 return $rez->result_array();
257 }
258 function getKatalogName($katalog_id)
259 {
260 if($katalog_id > 0){
261 $q = "select katalog_ime from tkatalog where katalog_id=".$katalog_id;
262 $rez = $this->db->query($q);
263 $vk = $rez->row_array();
264 return $vk['katalog_ime'];
265 }else{
266 return "";
267 }
268 }
269
270
271
272}
273
274?>