1: <?php
  2: 
  3:   4:   5:   6:   7:   8: 
  9: class UserController extends BaseController {
 10: 
 11:      12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25: 
 26:     public function postInfo() {
 27:         try {
 28:             $user_id = JWTAuth::getPayload(JWTAuth::getToken())->get('sub');
 29:             $user = User::find($user_id);
 30:             $user_data = array();
 31:             if (!empty($user)) {
 32:                 $user_data['id'] = $user->serial_usr;
 33:                 $profile = Profile::join(
 34:                                 'user_profile', function ($join) use ($user) {
 35:                             $join->on('profile.serial_prf', '=', 'user_profile.serial_prf')
 36:                             ->where('user_profile.serial_usr', '=', $user->serial_usr)
 37:                             ->where('user_profile.status_upr', '=', 'ACTIVE');
 38:                         }
 39:                         )
 40:                         ->whereIn('profile.name_prf', User::$allowedProfiles)
 41:                         ->where('profile.status_prf', '=', 'ACTIVE')
 42:                         ->select('profile.serial_prf', 'profile.name_prf')
 43:                         ->first();
 44:                 if (!empty($profile)) {
 45:                     $user_data['profile'] = array(
 46:                         'id' => $profile->serial_prf,
 47:                         'name' => $profile->name_prf
 48:                     );
 49:                     if ($profile->name_prf == 'Representante') {
 50:                         $this->guardianInfo($user, $user_data);
 51:                     } else {
 52:                         if ($profile->name_prf == 'Estudiante') {
 53:                             $this->studentInfo($user, $user_data);
 54:                         }
 55:                     }
 56:                     return ws_response(false, $user_data, '', 200);
 57:                 }
 58:             }
 59:             return ws_response(false, $user_data, '', 200);
 60:         } catch (Exception $ex) {
 61:             return ws_response(true, null, 'ERROR ' . $ex->getCode() . '! ' . $ex->getMessage(), 500);
 62:         }
 63:     }
 64: 
 65:      66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80: 
 81:     public function guardianInfo($user, &$user_data) {
 82:         $guardian = Guardian::where('serial_usr', '=', $user->serial_usr)
 83:                 ->where('status_grd', '=', 'ACTIVE')
 84:                 ->first();
 85:         if (!empty($guardian)) {
 86:             $user_data['name'] = $guardian->first_name_grd . ' ' . $guardian->last_name_grd;
 87:             $user_data['gender'] = $guardian->gender_grd;
 88:             $user_data['image'] = $guardian->user->image();
 89:             $students = array();
 90:             foreach ($guardian->students as $s) {
 91:                 $course = $s->course();
 92:                 $image = $s->user->image();
 93:                 $students[] = array(
 94:                     'id' => $s->serial_std,
 95:                     'name' => $s->first_name_std . ' ' . $s->last_name_std,
 96:                     'gender' => $s->gender_std,
 97:                     'course' => $course,
 98:                     'image' => $image
 99:                 );
100:             }
101:             if (!empty($students)) {
102:                 $user_data['students'] = $students;
103:             }
104:         }
105:         return $user_data;
106:     }
107: 
108:     109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 
125:     public function studentInfo($user, &$user_data) {
126:         $student = Student::where('serial_usr', '=', $user->serial_usr)
127:                 ->where('status_std', '=', 'ACTIVE')
128:                 ->first();
129:         if (!empty($student)) {
130:             $course = $student->course();
131:             $image = $student->user->image();
132:             $user_data['id_std'] = $student->serial_std;
133:             $user_data['name'] = $student->first_name_std . ' ' . $student->last_name_std;
134:             $user_data['gender'] = $student->gender_std;
135:             $user_data['course'] = $course;
136:             $user_data['image'] = $image;
137:         }
138:         return $user_data;
139:     }
140: 
141:     142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 
157:     public function postRegisterNotification() {
158:         try {
159:             $serial_usr = JWTAuth::getPayload(JWTAuth::getToken())->get('sub');
160:             $token_push = Input::get('token_push');
161:             $action = Input::get('action');
162: 
163:             $user = User::find($serial_usr);
164:             
165:             if ($token_push && $action == 'ADD') {
166:                 
167:                 if (!$user->notification_name_key && !$user->notification_key) {
168:                     
169:                     $school = SchoolSetup::first();
170:                     $notification_name_key = 'appUser-' . $user->username_usr . '-' . $school->token_school . '-' . date(
171:                                     'Y-m-d H:i:s'
172:                     );
173:                     $user->notification_name_key = $notification_name_key;
174:                     $result = $this->createService($token_push, $notification_name_key);
175:                 } else {
176:                     
177:                     $result = $this->modifiedServices(
178:                             $token_push, $user->notification_name_key, $user->notification_key, 'add'
179:                     );
180:                 }
181:                 $result = json_decode($result, true);
182:                 
183:                 if (array_key_exists('notification_key', $result)) {
184:                     
185:                     if (!$user->notification_key) {
186:                         $user->notification_key = $result['notification_key'];
187:                         $user->number_ids = 1;
188:                     } else {
189:                         $user->number_ids = $user->number_ids + 1;
190:                     }
191:                     $user->update();
192:                     
193:                     $messsges = self::postVerificationMessagesPending($user->serial_usr, $token_push);
194:                     return ws_response(false, 'Register Notification', 'success, ' . $messsges, 200);
195:                 } else {
196:                     return ws_response(true, 'Register Notification', 'fail: ' . $result['error'], 500);
197:                 }
198:             } elseif ($token_push && $action == 'DELETE') {
199:                 
200:                 $result = $this->modifiedServices(
201:                         $token_push, $user->notification_name_key, $user->notification_key, 'remove'
202:                 );
203:                 $result = json_decode($result, true);
204:                 
205:                 if (array_key_exists('notification_key', $result)) {
206:                     
207:                     if ($user->number_ids == 1) {
208:                         $user->notification_name_key = '';
209:                         $user->notification_key = '';
210:                         $user->number_ids = 0;
211:                     } else {
212:                         $user->number_ids = $user->number_ids - 1;
213:                     }
214:                     $user->update();
215:                     return ws_response(false, 'Register Notification', 'success', 200);
216:                 } else {
217:                     return ws_response(true, 'Register Notification', 'fail: ' . $result['error'], 500);
218:                 }
219:             } else {
220:                 return ws_response(true, 'Register Notification', 'fail: No value token_push or action', 500);
221:             }
222:         } catch (Exception $ex) {
223:             return ws_response(true, null, 'ERROR ' . $ex->getCode() . '! ' . $ex->getMessage(), 500);
224:         }
225:     }
226: 
227:     228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 
243:     private function createService($token_push, $notification_key) {
244:         $google_api_key = 'AIzaSyCmPV4IBQLA8QSMf96HrsDiEnLDuNeDzBs'; 
245:         $url = 'https://android.googleapis.com/gcm/notification'; 
246:         $number_project = '1005475082231'; 
247:         
248:         $fields = array(
249:             'operation' => 'create',
250:             'notification_key_name' => $notification_key,
251:             'registration_ids' => [$token_push]
252:         );
253:         
254:         $headers = array(
255:             'Content-Type: application/json',
256:             'Authorization: key=' . $google_api_key,
257:             'project_id: ' . $number_project
258:         );
259: 
260:         
261:         $ch = curl_init();
262:         
263:         curl_setopt($ch, CURLOPT_URL, $url);
264:         curl_setopt($ch, CURLOPT_POST, true);
265:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
266:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
267:         
268:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
269:         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
270: 
271:         $result = curl_exec($ch);
272:         curl_close($ch);
273:         return $result;
274:     }
275: 
276:     
277:     278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 
292:     private function modifiedServices($token_push, $notification_name_key, $notification_key, $action) {
293:         $google_api_key = 'AIzaSyCmPV4IBQLA8QSMf96HrsDiEnLDuNeDzBs'; 
294:         $url = 'https://android.googleapis.com/gcm/notification'; 
295:         $number_project = '1005475082231'; 
296:         
297:         $fields = array(
298:             'operation' => $action,
299:             'notification_key_name' => $notification_name_key,
300:             'notification_key' => $notification_key,
301:             'registration_ids' => [$token_push]
302:         );
303:         
304:         $headers = array(
305:             'Content-Type: application/json',
306:             'Authorization: key=' . $google_api_key,
307:             'project_id: ' . $number_project
308:         );
309: 
310:         
311:         $ch = curl_init();
312:         
313:         curl_setopt($ch, CURLOPT_URL, $url);
314:         curl_setopt($ch, CURLOPT_POST, true);
315:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
316:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
317:         
318:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
319:         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
320: 
321:         $result = curl_exec($ch);
322:         curl_close($ch);
323:         return $result;
324:     }
325: 
326:     327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 
342:     private function postVerificationMessagesPending($user_id, $token_push) {
343: 
344:         $messages = Message::where('serial_usr_receive', $user_id)->where('checked_msg', 'NO')->get();
345:         
346:         if ($messages->count() > 0) {
347: 
348:             $category = 'CATEGORY_MESSAGE';
349:             
350:             if ($messages->count() > 1) {
351: 
352:                 $title = $messages->count() . ' comunicados nuevos';
353:                 $body_expanded = array();
354:                 $body_contracted = '';
355:                 $names = array();
356:                 $priority = 'DEFAULT';
357:                 $multiple = true;
358:                 $notification_id = null;
359:                 
360:                 foreach ($messages as $message) {
361:                     if (!array_key_exists($message['serial_usr_send'], $names)) {
362:                         $body_contracted = $body_contracted . $message->user_from_name_msg . ', ';
363:                     }
364:                     $names[$message->serial_usr_send] = $message->user_from_name_msg;
365:                     $text = substr($message->message_msg, 3, -4);
366:                     $new_line = '<b>' . $message->user_from_name_msg . '</b> ' . substr($text, 0, 30);
367:                     array_push($body_expanded, $new_line);
368:                 }
369: 
370:                 $body_contracted = substr($body_contracted, 0, -2);
371:                 if (strpos($body_contracted, ',')) {
372:                     $body['body_contracted'] = $body_contracted;
373:                 } else {
374:                     $body['body_contracted'] = $title;
375:                     $title = $body_contracted;
376:                 }
377: 
378:                 $body['body_expand'] = $body_expanded;
379:             } else {
380:                 
381:                 $message = Message::find($messages[0]->serial_msg);
382:                 $title = $message->user_from_name_msg;
383:                 $body = $message->subject_msg;
384:                 $priority = $message->priority_msg == 'URGENT' ? 'MAX' : 'DEFAULT';
385:                 $multiple = false;
386:                 $notification_id = $messages[0]->serial_msg;
387:             }
388:             $message_data = array();
389:             $message_data['title'] = $title;
390:             $message_data['body'] = $body;
391:             $message_data['priority'] = $priority;
392:             $message_data['category'] = $category;
393:             $message_data['multiple'] = $multiple;
394:             $message_data['notification_id'] = $notification_id;
395:             $message_data['user_id'] = $user_id;
396:             
397:             $result = self::postSendNotification($user_id, $message_data, $token_push);
398:             return 'messages_send';
399:         } else {
400:             return 'not_messages_pending';
401:         }
402:     }
403: 
404: 
405:     406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 
422:     private function postSendNotification($serial_usr, $message_data, $token_push) {
423: 
424:         $user = User::find($serial_usr);
425:         $notification_key = $user->notification_key;
426: 
427:         $google_api_key = 'AIzaSyCmPV4IBQLA8QSMf96HrsDiEnLDuNeDzBs'; 
428:         $url = 'https://gcm-http.googleapis.com/gcm/send'; 
429:         
430:         $fields = array(
431:             'to' => $token_push,
432:             'data' => $message_data
433:         );
434:         
435:         $headers = array(
436:             'Content-Type: application/json',
437:             'Authorization: key=' . $google_api_key,
438:         );
439: 
440:         
441:         $ch = curl_init();
442:         
443:         curl_setopt($ch, CURLOPT_URL, $url);
444:         curl_setopt($ch, CURLOPT_POST, true);
445:         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
446:         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
447:         
448:         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
449:         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
450:         $result = curl_exec($ch);
451:         curl_close($ch);
452:         return $result;
453:     }
454: 
455: }
456: