PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/mobile/app.php

https://bitbucket.org/royrutto/climatepal
PHP | 202 lines | 135 code | 46 blank | 21 comment | 7 complexity | 6cfdbbd9ad46db9457672df2827a97d3 MD5 | raw file
  1. <?
  2. /*
  3. * Climate Pal
  4. * Incharge of interfacing between client side and Application layer (class.app.php)
  5. */
  6. session_start();
  7. define('BASE_URL','http://' . $_SERVER['HTTP_HOST'].'/climatepal/');
  8. define('DIR_WEB', dirname(__FILE__).'/..');
  9. define('DIR_SYS', DIR_WEB.'/system');
  10. define('DIR_LIB', DIR_SYS.'/lib');
  11. define('DIR_TEMPLATE', DIR_SYS.'/template');
  12. define('DIR_BUNDLES', DIR_SYS.'/bundles');
  13. date_default_timezone_set('Africa/Nairobi');
  14. define('DATE_FORMAT_DEFAULT', "Y-m-d H:i:s");
  15. define('DATE_FORMAT_DATEPICKER', "d-m-Y");
  16. //load required libraries/models
  17. require_once(DIR_LIB.'/load_models.php');
  18. //container for data to be returned to app.js
  19. $app_data = array();
  20. //if form data is being posted
  21. if(!empty($_POST['form'])){
  22. $posted_form = $_POST['form'];
  23. switch($posted_form){
  24. //handles login
  25. case 'login':
  26. //get login details
  27. if (!empty($_POST['username'])) $username = $_POST['username'];
  28. if (!empty($_POST['password'])) $password = $_POST['password'];
  29. $device_type='mobile';
  30. include(DIR_SYS.'/checklogin.php');
  31. $app_data = $login_details;
  32. header('Vary: Accept-Encoding');
  33. header('Content-Type: application/json');
  34. print json_encode($app_data);
  35. break;
  36. //handle new distribution details
  37. case 'newdistribution':
  38. //New Distribution details
  39. $cpa = $_POST['cpa'];
  40. $serial = $_POST['serial'];
  41. $date = $_POST['date'];
  42. $firstname = $_POST['firstname'];
  43. $middlename = $_POST['middlename'];
  44. $householdname = $_POST['householdname'];
  45. $gender = $_POST['gender'];
  46. $id = $_POST['userid'];
  47. $mobile = $_POST['mobile'];
  48. $location = $_POST['location'];
  49. $latitude = $_POST['latitude'];
  50. $longitude = $_POST['longitude'];
  51. $old = $_POST['oldstove'];
  52. $added_by = $_POST['added_by'];
  53. $device_type='mobile';
  54. include(DIR_BUNDLES.'/sales/validate_add_sale.php');
  55. if($data_saved){
  56. $app_data['dist_status'] = 'success';
  57. }else{
  58. $app_data['dist_status'] = 'fail';
  59. }
  60. header('Vary: Accept-Encoding');
  61. header('Content-Type: application/json');
  62. print json_encode($app_data);
  63. break;
  64. //save survey resuts
  65. case 'savesurvey':
  66. //New Distribution details
  67. $survey_id = $_POST['surveyid'];
  68. $results = $_POST['results'];
  69. $added_by = $_POST['added_by'];
  70. $latitude = $_POST['latitude'];
  71. $longitude = $_POST['longitude'];
  72. $field_values = explode("&", $results);
  73. $fields = array();
  74. foreach($field_values as $field_value){
  75. $getvalues = explode("=", $field_value);
  76. $fields[''.$getvalues[0].''] = $getvalues[1];
  77. }
  78. $device_type='mobile';
  79. include(DIR_BUNDLES.'/surveys/validate_add_response.php');
  80. if($data_saved){
  81. $app_data['dist_status'] = 'success';
  82. }else{
  83. $app_data['dist_status'] = 'fail';
  84. }
  85. header('Vary: Accept-Encoding');
  86. header('Content-Type: application/json');
  87. print json_encode($app_data);
  88. break;
  89. //logout
  90. case 'logout':
  91. $device_type='mobile';
  92. include(DIR_SYS.'/logout.php');
  93. $app_data['logout_status'] = 'logged_out';
  94. header('Vary: Accept-Encoding');
  95. header('Content-Type: application/json');
  96. print json_encode($app_data);
  97. break;
  98. //getaccountinfo
  99. case 'get_account_data':
  100. $device_type='mobile';
  101. $user_id=$_POST['user_id'];
  102. $app_data = $Users->get_upload_info($user_id);
  103. header('Vary: Accept-Encoding');
  104. header('Content-Type: application/json');
  105. print json_encode($app_data);
  106. break;
  107. }//end posted_form switch
  108. }else if(!empty($_GET['cpa'])){
  109. //New Distribution details
  110. $cpa = $_GET['cpa'];
  111. $serial = $_GET['serial'];
  112. $date = $_GET['date'];
  113. $firstname = $_GET['firstname'];
  114. $middlename = $_GET['middlename'];
  115. $householdname = $_GET['householdname'];
  116. $gender = $_GET['gender'];
  117. $id = $_GET['userid'];
  118. $mobile = $_GET['mobile'];
  119. $location = $_GET['location'];
  120. $latitude = $_GET['latitude'];
  121. $longitude = $_GET['longitude'];
  122. $old = $_GET['oldstove'];
  123. /* lhttp://localhost/climatepal/mobile/app.php?cpa=123&serial=1223&date=2012-12-12&user=test&gender=1&userid=23232&mobile=123&householdname=household&latitude=0.0&longitude=0.0&oldstove=1%20%3E%3E%20localhoslocalhost/climatepal/mobile/app.pp?cpa=123&serial=1223&date=2012-12-12&name=test&gender=1&userid=23232&mobile=123&householdname=household&latitude=0.0&longitude=0.0&oldstove=1 */
  124. $device_type='mobile';
  125. include(DIR_BUNDLES.'/sales/validate_add_sale.php');
  126. if($data_saved){
  127. $app_data['dist_status'] = 'success';
  128. }else{
  129. $app_data['dist_status'] = 'fail';
  130. }
  131. header('Vary: Accept-Encoding');
  132. header('Content-Type: application/json');
  133. print json_encode($app_data);
  134. }
  135. else{
  136. //get widget content based on getJSON request and parameters
  137. switch($_GET['a']){
  138. //populate subtype and make dropdowns
  139. case 'initNewDistForm':
  140. $user_id= $_GET['userid'];
  141. $app_data['locations'] = $Sales->getNewDistUIData('locations', $user_id);
  142. $app_data['stovetypes'] = $Sales->getNewDistUIData('stovetypes');
  143. break;
  144. case 'initSurveyForms':
  145. $app_data['surveys']= $Surveys->get_all_surveys();
  146. $app_data['questions']= $Surveys->get_questions_by_survey_id(0, true);
  147. break;
  148. }
  149. //set necessary headers to allow JSONP and storage of compressed and uncompressed formats
  150. header('Access-Control-Allow-Origin:*');
  151. header('Vary: Accept-Encoding');
  152. header('Content-Type: application/json');
  153. //json-encode and return JSONP
  154. print $_GET['callback'].'('.json_encode($app_data).')';
  155. }
  156. ?>