ClientApiController.php

矢野 宗一郎, 2016/02/15 11:37

ダウンロード (7.223 KB)

 
1
<?php
2
class ClientApiController extends AppController {
3
        var $name = 'client_api';
4
        var $uses = array('Shop','ShopType','Prefecture','City','Banner');
5
        var $autoLayout = false;
6
        var $autoRender = false;
7
        var $helpers = array('Html','UploadPack.Upload');
8

    
9
        /**
10
         *
11
         * 更新情報確認API
12
         *
13
         */
14
        function check_updated() {
15

    
16
                //前回更新日時取得(ない場合は、エラー終了)
17
                if($this->request->is('get')){
18
                        $date = $this->request->query['date'];
19
                        if(empty($date)){
20
                                $data['rtn_code'] = 0;
21
                                $json_value        = json_encode( $data );
22
                                header( 'Content-Type: text/javascript; charset=utf-8' );
23
                                echo $json_value;
24
                                exit;
25
                        }
26
                }
27
                $cond_shop = array('conditions'=>array('Shop.updated > '.$date ));
28
                $cond_shoptype = array('conditions'=>array('ShopType.updated > '.$date ));
29
                $cond_city = array('conditions'=>array('City.updated > '.$date ));
30

    
31
                $shop_count = $this->Shop->find('count',$cond_shop);
32
                $shoptype_count = $this->Shop->find('count',$cond_shoptype);
33
                $city_count = $this->Shop->find('count',$cond_city);
34

    
35
                $data['rtn_code'] = 1;
36
                if($shop_count > 0){
37
                        $data['shop_updated'] = 1;
38
                }else{
39
                        $data['shop_updated'] = 0;
40
                }
41
                if($shoptype_count > 0){
42
                        $data['shoptype_updated'] = 1;
43
                }else{
44
                        $data['shoptype_updated'] = 0;
45
                }
46
                if($city_count > 0){
47
                        $data['city_updated'] = 1;
48
                }else{
49
                        $data['city_updated'] = 0;
50
                }
51

    
52
                $json_value        = json_encode( $data );
53
                header( 'Content-Type: text/javascript; charset=utf-8' );
54
                echo $json_value;
55
        }
56

    
57
        /**
58
         *
59
         * 店舗データ取得用API
60
         *
61
         */
62
        function get_shop_data() {
63

    
64
                $this->Shop->recursive = 0;
65
                $this->Shop->unbindModel(array('belongsTo'=>array('ShopType','Prefecture','City')),false);
66
                $fields = array('Shop.id', 'Shop.shopcode', 'Shop.shop_type_id','Shop.name','Shop.kana', 'Shop.postcode', 'Shop.prefecture_id', 'Shop.city_id', 'Shop.address1', 'Shop.address2','(X(latlng)) as Shop__lat', '(Y(latlng)) as Shop__lng', 'Shop.tel', 'Shop.status');
67

    
68
                //日付取得(ない場合は、全件送信)
69
                if($this->request->is('get') || $this->request->is('post')){
70
                        $date = $this->request->query['date'];
71
                }
72

    
73
                if(empty($date)){
74
                        $shop_data = $this->Shop->find('all', array('fields'=>$fields));
75
                }else{
76
                        $shop_data = $this->Shop->find('all', array('fields'=>$fields,'conditions'=>array('Shop.updated > '.$date)));
77
                }
78

    
79
                if(count($shop_data) > 0){
80
                        $result['rtn_code'] = 1;
81
                        $result['hit_num'] = count($shop_data);
82
                        $shop_data = array_merge($result,$shop_data);
83
                }else {
84
                        $shop_data['rtn_code'] = 0;
85
                }
86
                $json_value        = json_encode( $shop_data );
87
                header( 'Content-Type: text/javascript; charset=utf-8' );
88
                echo $json_value;
89
        }
90

    
91
        /**
92
         *
93
         * 業態データ取得用API
94
         *
95
         */
96
        function get_shoptype_data() {
97

    
98
                $this->ShopType->recursive = 0;
99
                $fields = array('ShopType.id', 'ShopType.name', 'ShopType.logo_file_name','ShopType.status');
100

    
101

    
102
                //日付取得(ない場合は、全件送信)
103
                if($this->request->is('get')){
104
                        $date = $this->request->query['date'];
105
                }
106

    
107
                if(empty($date)){
108
                        $data = $this->ShopType->find('all', array('fields'=>$fields));
109
                }else{
110
                        $data = $this->ShopType->find('all', array('fileds'=>$fields,'conditions'=>array('ShopType.updated > '.$date)));
111
                }
112

    
113
                if(count($data) > 0){
114
                        $this->set('datas',$data);
115
                        $this->render();
116
                }else {
117
                        $data['rtn_code'] = 0;
118
                        $json_value        = json_encode( $data );
119
                        header( 'Content-Type: text/javascript; charset=utf-8' );
120
                        echo $json_value;
121
                }
122
        }
123

    
124
        /**
125
         *
126
         * 県コードデータ取得用API
127
         *
128
         */
129
        function get_prefecture_data() {
130

    
131
                $this->Prefecture->recursive = 0;
132
                $fields = array('Prefecture.id', 'Prefecture.name');
133

    
134
                $data = $this->Prefecture->find('all', array('fields'=>$fields));
135

    
136
                if(count($data) > 0){
137
                        $result['rtn_code'] = 1;
138
                        $result['hit_num'] = count($data);
139
                        $data = array_merge($result,$data);
140
                }else {
141
                        $data['rtn_code'] = 0;
142
                }
143
                $json_value        = json_encode( $data );
144
                header( 'Content-Type: text/javascript; charset=utf-8' );
145
                echo $json_value;
146
        }
147

    
148
        /**
149
         *
150
         * 都市コードデータ取得用API
151
         *
152
         */
153
        function get_city_data() {
154

    
155
                $this->City->recursive = 0;
156
                $fields = array('City.id','City.prefecture_id','City.name');
157

    
158

    
159
                //日付取得(ない場合は、全件送信)
160
                if($this->request->is('get')){
161
                        $date = $this->request->query['date'];
162
                }
163

    
164
                if(empty($date)){
165
                        $data = $this->City->find('all', array('fields'=>$fields));
166
                }else{
167
                        $data = $this->City->find('all', array('fileds'=>$fields,'conditions'=>array('City.updated > '.$date)));
168
                }
169

    
170
                if(count($data) > 0){
171
                        $result['rtn_code'] = 1;
172
                        $result['hit_num'] = count($data);
173
                        $data = array_merge($result,$data);
174
                }else {
175
                        $data['rtn_code'] = 0;
176
                }
177
                $json_value        = json_encode( $data );
178
                header( 'Content-Type: text/javascript; charset=utf-8' );
179
                echo $json_value;
180
        }
181

    
182
        /**
183
         *
184
         * 広告データ取得用API
185
         *
186
         */
187
        function get_banner_data() {
188

    
189
                $this->Banner->recursive = 0;
190
                $fields = array('Banner.id','Banner.name','Banner.image_file_name','Banner.url');
191

    
192
                $data = $this->Banner->find('all', array('fileds'=>$fields,'conditions'=>array('Banner.status = 1')));
193

    
194
                if(count($data) > 0){
195
                        $keyArray = array_rand($data,1);
196
                        $result['rtn_code'] = 1;
197
                        $data = $data[$keyArray];
198
                        $data = array_merge($result,$data);
199
                }else{
200
                        $data['rtn_code'] = 0;
201
                }
202

    
203
                $this->set('data',$data);
204
                $this->render();
205
        }
206

    
207
        /**
208
         *
209
         * イメージデータ取得用API
210
         *
211
         */
212
        function get_image() {
213

    
214
                if($this->request->is('get')){
215
                        $image = $this->request->query['image'];
216
                        if(empty($image)){
217
                                $data['rtn_code'] = 0;
218
                                $json_value        = json_encode( $data );
219
                                header( 'Content-Type: text/javascript; charset=utf-8' );
220
                                echo $json_value;
221
                        }
222
                }
223

    
224
                $image = APP . WEBROOT_DIR . $image;
225

    
226
                if(!file_exists($image)){
227
                        $data['rtn_code'] = 0;
228
                        $json_value        = json_encode( $data );
229
                        header( 'Content-Type: text/javascript; charset=utf-8' );
230
                        echo $json_value;
231
                        exit;
232
                }
233

    
234
                $extension = $this->get_file_extention($image);
235

    
236
                if($extension == "gif"){
237
                        header('Content-type: image/gif');
238
                }else if($extension == "jpg"){
239
                        header('Content-type: image/jpeg');
240
                }else if($extension == "png"){
241
                        header('Content-type: image/png');
242
                }
243

    
244
                readfile($image);
245
        }
246

    
247

    
248
        function show_json_data(){
249

    
250
                //更新チェック
251
                //$url = "http://localhost/client_api/check_updated?date=20121205";
252
                //店舗
253
                $url = "http://localhost/client_api/get_shop_data?date=20121204";
254
                //業態
255
                //$url = "http://localhost/client_api/get_shoptype_data?date=20121204";
256
                //県コード
257
                //$url = "http://localhost/client_api/get_prefecture_data?date=";
258
                //都市コード
259
                //$url = "http://localhost/client_api/get_city_data?date=";
260
                //広告
261
                //$url = "http://localhost/client_api/get_banner_data";
262

    
263
                $json = file_get_contents($url,true);
264
                $obj = json_decode($json,true);
265

    
266
                header( 'Content-Type: text/javascript; charset=utf-8' );
267
                print($url . "\n");
268
                print_r($obj);
269
        }
270

    
271
        /**
272
         *
273
         * イメージデータ拡張子取得
274
         *
275
         */
276
        function get_file_extention($file){
277
                $pos = strrpos($file,".");
278
                if($pos)
279
                $ext = substr($file,$pos+1);
280

    
281
                return $ext;
282
        }
283

    
284
}
285
?>