Guest User

Untitled

a guest
Sep 23rd, 2016
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Pelapor extends CI_Controller {
  5.  
  6.  
  7. function __construct(){
  8. parent::__construct();
  9. // Add origin header
  10. header('Access-Control-Allow-Origin: *');
  11. header('Access-Control-Allow-Credentials: true');
  12. header('Access-Control-Allow-Method: PUT, GET, POST, DELETE, OPTIONS');
  13. header('Access-Control-Allow-Headers: Content-Type, x-xsrf-token');
  14.  
  15. $this->load->model('pelapor_model');
  16. }
  17.  
  18. private function outputJson($response=array(),$status=200){
  19. $this->output
  20. ->set_status_header($status)
  21. ->set_content_type('application/json', 'utf-8')
  22. ->set_output(json_encode($response, JSON_PRETTY_PRINT))
  23. ->_display();
  24. exit();
  25. }
  26.  
  27. public function index(){
  28. $json=array(
  29. 'message' => 'Naon?',
  30. 'data' => '',
  31. 'code' => 404
  32. );
  33. $this->outputJson($json);
  34. }
  35.  
  36. public function sign_up(){
  37. $postdata = (array)json_decode(file_get_contents('php://input'));
  38. $json=array(
  39. 'message' => '',
  40. 'data' => '',
  41. 'code' => 404
  42. );
  43.  
  44. @$email = $postdata['email'];
  45. @$no_telphone = $postdata['no_telphone'];
  46. @$username = $postdata['username'];
  47. @$password = $postdata['password'];
  48.  
  49. if(!empty($email)){
  50. // validasi
  51. $cekUsername = $this->pelapor_model->cekUsername($username);
  52. $cekEmail = $this->pelapor_model->cekEmail($email);
  53. $cekPhone = $this->pelapor_model->cekPhone($no_telphone);
  54.  
  55. if(!$cekUsername){
  56. if(!$cekEmail){
  57. if(!$cekPhone){
  58. $data = array(
  59. 'email' => $email,
  60. 'no_telphone' => $no_telphone,
  61. 'username' => $username,
  62. 'password' => md5($password),
  63. 'status' => 'f',
  64. 'device_token' => ''
  65. );
  66. // insert data user
  67. $insert = $this->pelapor_model->add($data);
  68. if($insert){
  69. $json = array (
  70. 'message' => 'Anda telah berhasil daftar, Akun anda akan segera diaktifkan',
  71. 'data' => '',
  72. 'code' => 200
  73. );
  74. }else{
  75. $json = array (
  76. 'message' => 'Maaf anda gagal mendaftar, silahkan coba lagi ',
  77. 'data' => '',
  78. 'code' => 500
  79. );
  80. }
  81. }else{
  82. $json = array (
  83. 'message' => 'Maaf nomor telphone sudah digunakan',
  84. 'data' => '',
  85. 'code' => 500
  86. );
  87. }
  88. }else{
  89. $json = array (
  90. 'message' => 'Maaf email sudah digunakan',
  91. 'data' => '',
  92. 'code' => 500
  93. );
  94. }
  95. }else{
  96. $json = array (
  97. 'message' => 'Maaf username sudah digunakan',
  98. 'data' => '',
  99. 'code' => 500
  100. );
  101. }
  102. }
  103.  
  104. $this->outputJson($json);
  105. }
  106.  
  107. public function login(){
  108. $postdata = (array)json_decode(file_get_contents('php://input'));
  109. $json=array(
  110. 'message' => '',
  111. 'data' => '',
  112. 'code' => 404
  113. );
  114.  
  115. @$email = $postdata['email'];
  116. @$password = $postdata['password'];
  117. @$device_token = $postdata['device_token'];
  118.  
  119. if(!empty($email)){
  120. $data = array();
  121. $data['device_token']=$device_token;
  122. $data['email']=$email;
  123. $data['password']=md5($password);
  124.  
  125. $status = $this->pelapor_model->cekLogin($data)->result();
  126. if($status!=null){
  127. //update device token
  128. $this->pelapor_model->updateDeviceToken($data['device_token'],$status[0]->id);
  129. $json = array (
  130. 'message' => 'Login Berhasil',
  131. 'data' => $status,
  132. 'code' => 200
  133. );
  134. }else{
  135. $json = array (
  136. 'message' => 'Maaf email atau password tidak sama',
  137. 'data' => '',
  138. 'code' => 500
  139. );
  140. }
  141. }
  142.  
  143. $this->outputJson($json);
  144. }
  145.  
  146. public function lapor(){
  147.  
  148. $this->load->model('laporan_model');
  149. $this->load->library('upload');
  150.  
  151. //set preferences
  152. $config['upload_path'] = './uploads/';
  153. $config['allowed_types'] = 'png|jpg|jpeg';
  154.  
  155. //load upload class library
  156. $this->load->library('upload', $config);
  157. if (!$this->upload->do_upload('foto_pelapor')) {
  158. $json = array (
  159. 'message' => 'Gagal melaporkan',
  160. 'data' => '',
  161. 'code' => 500
  162.  
  163. );
  164. }else{
  165. $gambar = $this->upload->data('file_name');
  166. $this->laporan_model->add($this->input->post(),$gambar);
  167. $json = array (
  168. 'message' => 'Berhasil Melaporkan',
  169. 'data' => '',
  170. 'code' => 200
  171.  
  172. );
  173. }
  174.  
  175. $this->output
  176. ->set_content_type('application/json')
  177. ->set_output(json_encode($json));
  178. }
  179.  
  180. public function activeAccount(){
  181. $id = $this->input->post('id');
  182. if($this->pelapor_model->updateStatusActive($id)){
  183. $json = array (
  184. 'message' => 'Akun telah diaktifkan',
  185. 'data' => '',
  186. 'code' => 200
  187.  
  188. );
  189. }else{
  190. $json = array (
  191. 'message' => 'Akun gagal diaktifkan',
  192. 'data' => '',
  193. 'code' => 500
  194.  
  195. );
  196. }
  197. $this->output
  198. ->set_content_type('application/json')
  199. ->set_output(json_encode($json));
  200. }
  201.  
  202. public function updatePassword(){
  203. $postdata = (array)json_decode(file_get_contents('php://input'));
  204. $json=array(
  205. 'message' => '',
  206. 'data' => '',
  207. 'code' => 404
  208. );
  209.  
  210. @$password = $postdata['password'];
  211. @$id = $postdata['id'];
  212.  
  213. if(!empty($password)){
  214. $data = array();
  215. $data['password'] = md5($password);
  216. $data['id'] = $id;
  217.  
  218. if($this->pelapor_model->updatePassword($data)){
  219. $json = array (
  220. 'message' => 'Password berhasil diubah',
  221. 'data' => '',
  222. 'code' => 200
  223. );
  224. }else{
  225. $json = array (
  226. 'message' => 'Password gagal diubah',
  227. 'data' => '',
  228. 'code' => 500
  229. );
  230. }
  231. }
  232.  
  233. $this->outputJson($json);
  234. }
  235.  
  236. public function getAllPelapor(){
  237. $offset = 0;
  238. if($this->input->post('offset')!=null){
  239. $offset = $this->input->post('offset');
  240. }
  241.  
  242. $limit = 0;
  243. if($this->input->post('limit')!=null){
  244. $limit = $this->input->post('limit');
  245. }
  246.  
  247. $data = $this->pelapor_model->getAll($offset,$limit);
  248. if(!empty($data)){
  249. $json = array (
  250. 'message' => 'List pelapor berhasil di ambil',
  251. 'data' => $data,
  252. 'code' => 200
  253.  
  254. );
  255. }else{
  256. $json = array (
  257. 'message' => 'List pelapor kosong',
  258. 'data' => '',
  259. 'code' => 500
  260.  
  261. );
  262. }
  263. $this->output
  264. ->set_content_type('application/json')
  265. ->set_output(json_encode($json));
  266. }
  267.  
  268. public function getLaporanByPelapor(){
  269.  
  270. $offset = 0;
  271. if($this->input->post('offset')!=null){
  272. $offset = $this->input->post('offset');
  273. }
  274.  
  275. $limit = 10;
  276.  
  277. if($this->input->post('limit')!=null){
  278. $limit = $this->input->post('limit');
  279. }
  280.  
  281. $data = $this->pelapor_model->getlaporanByid($this->input->post('id'),$limit,$offset);
  282. if(!empty($data)){
  283. $json = array (
  284. 'message' => 'List Laporan berhasil di ambil',
  285. 'data' => $data,
  286. 'code' => 200
  287.  
  288. );
  289. }else{
  290. $json = array (
  291. 'message' => 'List Laporan kosong',
  292. 'data' => '',
  293. 'code' => 500
  294. );
  295. }
  296. $this->output
  297. ->set_content_type('application/json')
  298. ->set_output(json_encode($json));
  299. }
  300. }
RAW Paste Data