PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/api/ui/push/onyx_proxy.class.php

https://github.com/patrickdemond/beartooth
PHP | 280 lines | 205 code | 41 blank | 34 comment | 57 complexity | 9cc3cdb87779e8477b9a5c0e0630459e MD5 | raw file
  1. <?php
  2. /**
  3. * onyx_proxy.class.php
  4. *
  5. * @author Patrick Emond <emondpd@mcmaster.ca>
  6. * @filesource
  7. */
  8. namespace beartooth\ui\push;
  9. use cenozo\lib, cenozo\log, beartooth\util;
  10. /**
  11. * push: onyx proxy
  12. *
  13. * Allows Onyx to update proxy and interview details
  14. */
  15. class onyx_proxy extends \cenozo\ui\push
  16. {
  17. /**
  18. * Constructor.
  19. * @author Patrick Emond <emondpd@mcmaster.ca>
  20. * @param array $args Push arguments
  21. * @access public
  22. */
  23. public function __construct( $args )
  24. {
  25. parent::__construct( 'onyx', 'proxy', $args );
  26. }
  27. /**
  28. * This method executes the operation's purpose.
  29. *
  30. * @author Patrick Emond <emondpd@mcmaster.ca>
  31. * @access protected
  32. */
  33. protected function execute()
  34. {
  35. parent::execute();
  36. $participant_class_name = lib::create( 'database\participant' );
  37. $region_class_name = lib::create( 'database\region' );
  38. $onyx_instance_class_name = lib::create( 'database\onyx_instance' );
  39. $db_onyx_user = lib::create( 'business\session' )->get_user();
  40. $db_onyx_instance =
  41. $onyx_instance_class_name::get_unique_record( 'user_id', $db_onyx_user->id );
  42. // get the user who is sending the request
  43. // NOTE: if this is a site instance then there is no interviewer, so use the instance itself
  44. $db_user = $db_onyx_instance->get_interviewer_user();
  45. if( is_null( $db_user ) ) $db_user = $db_onyx_instance->get_user();
  46. // get the body of the request
  47. $data = util::json_decode( http_get_request_body() );
  48. // loop through the proxy array, if everything works then send the data to
  49. // Mastodon as a new alternate, if anything goes wrong then send it into the
  50. // data entry system
  51. foreach( $data->Consent as $proxy_list )
  52. {
  53. foreach( get_object_vars( $proxy_list ) as $uid => $proxy_data )
  54. {
  55. $object_vars = get_object_vars( $proxy_data );
  56. if( 1 >= count( $object_vars ) ) continue;
  57. $noid = array( 'user.name' => $db_user->name );
  58. $entry = array();
  59. $db_participant = $participant_class_name::get_unique_record( 'uid', $uid );
  60. if( is_null( $db_participant ) )
  61. throw lib::create( 'exception\runtime',
  62. sprintf( 'Participant UID "%s" does not exist.', $uid ),
  63. __METHOD__ );
  64. $entry['uid'] = $db_participant->uid;
  65. $db_data_collection = $db_participant->get_data_collection();
  66. if( is_null( $db_data_collection ) )
  67. {
  68. $db_data_collection = lib::create( 'database\data_collection' );
  69. $db_data_collection->participant_id = $db_participant->id;
  70. }
  71. // try timeEnd, if null then try timeStart
  72. $var_name = 'timeEnd';
  73. if( !array_key_exists( $var_name, $object_vars ) ||
  74. 0 == strlen( $proxy_data->$var_name ) )
  75. {
  76. $var_name = 'timeStart';
  77. if( !array_key_exists( $var_name, $object_vars ) ||
  78. 0 == strlen( $proxy_data->$var_name ) )
  79. throw lib::create( 'exception\argument',
  80. $var_name, NULL, __METHOD__ );
  81. }
  82. $entry['date'] = util::get_datetime_object( $proxy_data->$var_name )->format( 'Y-m-d' );
  83. $var_name = 'ICF_IDPROXY_COM';
  84. $entry['proxy'] =
  85. array_key_exists( $var_name, $object_vars ) &&
  86. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  87. $var_name = 'ICF_OKPROXY_COM';
  88. $entry['already_identified'] =
  89. array_key_exists( $var_name, $object_vars ) &&
  90. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  91. $var_name = 'ICF_PXFIRSTNAME_COM';
  92. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  93. $entry['proxy_first_name'] = $proxy_data->$var_name;
  94. $var_name = 'ICF_PXLASTNAME_COM';
  95. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  96. $entry['proxy_last_name'] = $proxy_data->$var_name;
  97. $var_name = 'ICF_PXADD_COM';
  98. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  99. {
  100. $parts = explode( ' ', trim( $proxy_data->$var_name ), 2 );
  101. $entry['proxy_street_number'] = array_key_exists( 0, $parts ) ? $parts[0] : NULL;
  102. $entry['proxy_street_name'] = array_key_exists( 1, $parts ) ? $parts[1] : NULL;
  103. }
  104. $var_name = 'ICF_PXADD2_COM';
  105. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  106. $entry['proxy_address_other'] = $proxy_data->$var_name;
  107. $var_name = 'ICF_PXCITY_COM';
  108. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  109. $entry['proxy_city'] = $proxy_data->$var_name;
  110. $var_name = 'ICF_PXPROVINCE_COM';
  111. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  112. {
  113. $db_region =
  114. $region_class_name::get_unique_record( 'abbreviation', $proxy_data->$var_name );
  115. if( is_null( $db_region ) )
  116. $db_region =
  117. $region_class_name::get_unique_record( 'name', $proxy_data->$var_name );
  118. if( !is_null( $db_region ) )
  119. $noid['proxy_region.abbreviation'] = $db_region->abbreviation;
  120. }
  121. $var_name = 'ICF_PXPOSTALCODE_COM';
  122. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  123. {
  124. $postcode = $proxy_data->$var_name;
  125. $postcode = trim( $postcode );
  126. if( 6 == strlen( $postcode ) )
  127. $postcode = sprintf( '%s %s',
  128. substr( $postcode, 0, 3 ),
  129. substr( $postcode, 3 ) );
  130. $entry['proxy_postcode'] = $postcode;
  131. }
  132. $var_name = 'ICF_PXTEL_COM';
  133. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  134. {
  135. $phone = $proxy_data->$var_name;
  136. $phone = preg_replace( '/[^0-9]/', '', $phone );
  137. $phone = sprintf( '%s-%s-%s',
  138. substr( $phone, 0, 3 ),
  139. substr( $phone, 3, 3 ),
  140. substr( $phone, 6 ) );
  141. $entry['proxy_phone'] = $phone;
  142. }
  143. $var_name = 'ICF_PRXINF_COM';
  144. $entry['informant'] =
  145. array_key_exists( $var_name, $object_vars ) &&
  146. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  147. $var_name = 'ICF_PRXINFSM_COM';
  148. $entry['same_as_proxy'] =
  149. array_key_exists( $var_name, $object_vars ) &&
  150. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  151. $var_name = 'ICF_INFFIRSTNAME_COM';
  152. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  153. $entry['informant_first_name'] = $proxy_data->$var_name;
  154. $var_name = 'ICF_INFLASTNAME_COM';
  155. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  156. $entry['informant_last_name'] = $proxy_data->$var_name;
  157. $var_name = 'ICF_INFADD_COM';
  158. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  159. {
  160. $parts = explode( ' ', trim( $proxy_data->$var_name ), 2 );
  161. $entry['informant_street_number'] = array_key_exists( 0, $parts ) ? $parts[0] : NULL;
  162. $entry['informant_street_name'] = array_key_exists( 1, $parts ) ? $parts[1] : NULL;
  163. }
  164. $var_name = 'ICF_INFADD2_COM';
  165. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  166. $entry['informant_address_other'] = $proxy_data->$var_name;
  167. $var_name = 'ICF_INFCITY_COM';
  168. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  169. $entry['informant_city'] = $proxy_data->$var_name;
  170. $var_name = 'ICF_INFPROVINCE_COM';
  171. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  172. {
  173. $db_region =
  174. $region_class_name::get_unique_record( 'abbreviation', $proxy_data->$var_name );
  175. if( is_null( $db_region ) )
  176. $db_region =
  177. $region_class_name::get_unique_record( 'name', $proxy_data->$var_name );
  178. if( !is_null( $db_region ) )
  179. $noid['informant_region.abbreviation'] = $db_region->abbreviation;
  180. }
  181. $var_name = 'ICF_INFPOSTALCODE_COM';
  182. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  183. {
  184. $postcode = $proxy_data->$var_name;
  185. $postcode = trim( $postcode );
  186. if( 6 == strlen( $postcode ) )
  187. $postcode = sprintf( '%s %s',
  188. substr( $postcode, 0, 3 ),
  189. substr( $postcode, 3 ) );
  190. $entry['informant_postcode'] = $postcode;
  191. }
  192. $var_name = 'ICF_INFTEL_COM';
  193. if( array_key_exists( $var_name, $object_vars ) && 0 < strlen( $proxy_data->$var_name ) )
  194. {
  195. $phone = $proxy_data->$var_name;
  196. $phone = preg_replace( '/[^0-9]/', '', $phone );
  197. $phone = sprintf( '%s-%s-%s',
  198. substr( $phone, 0, 3 ),
  199. substr( $phone, 3, 3 ),
  200. substr( $phone, 6 ) );
  201. $entry['informant_phone'] = $phone;
  202. }
  203. $var_name = 'ICF_ANSW_COM';
  204. $entry['informant_continue'] =
  205. array_key_exists( $var_name, $object_vars ) &&
  206. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  207. $var_name = 'ICF_TEST_COM';
  208. $db_data_collection->physical_tests_continue =
  209. array_key_exists( $var_name, $object_vars ) &&
  210. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  211. $var_name = 'ICF_SAMP_COM';
  212. $db_data_collection->draw_blood_continue =
  213. array_key_exists( $var_name, $object_vars ) &&
  214. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  215. $var_name = 'ICF_HCNUMB_COM';
  216. $entry['health_card'] =
  217. array_key_exists( $var_name, $object_vars ) &&
  218. 1 == preg_match( '/y|yes|true|1/i', $proxy_data->$var_name ) ? 1 : 0;
  219. // now pass on the data to Mastodon
  220. $mastodon_manager = lib::create( 'business\cenozo_manager', MASTODON_URL );
  221. $args = array(
  222. 'columns' => array(
  223. 'from_onyx' => 1,
  224. 'complete' => 0,
  225. 'date' => $entry['date'] ),
  226. 'entry' => $entry,
  227. 'noid' => $noid );
  228. if( array_key_exists( 'pdfForm', $object_vars ) )
  229. $args['form'] = $proxy_data->pdfForm;
  230. $mastodon_manager->push( 'proxy_form', 'new', $args );
  231. // update the participant and data_collection
  232. // NOTE: these calls need to happen AFTER the mastodon push operation above, otherwise
  233. // a database lock will prevent the operation from completing
  234. $db_participant->save();
  235. $db_data_collection->save();
  236. }
  237. }
  238. }
  239. }