ProfileInterface.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Models {
  3. require_once 'Models/User.class.php';
  4. require_once 'Tools/Random.class.php';
  5. class ProfileInterface {
  6. //
  7. protected $DataInterface;
  8. /**
  9. *
  10. */
  11. public function __construct($DataInterface) {
  12. $this->DataInterface = $DataInterface;
  13. }
  14. /**
  15. * Get profile data.
  16. */
  17. public function profileGet($User, $lang) {
  18. $ID_user = $User->ID;
  19. $statement = $this->DataInterface->DatabaseConnection->prepare(
  20. "SELECT * FROM user WHERE ID = $ID_user"
  21. );
  22. if(!$statement->execute()) {
  23. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  24. }
  25. $user = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  26. if($user['type']=='investigator') {
  27. $statement = $this->DataInterface->DatabaseConnection->prepare(
  28. "SELECT probe.* FROM probe, ct_center, user WHERE probe.ID = ct_center.fk_probe AND user.fk_center = ct_center.ID AND user.ID = $ID_user"
  29. );
  30. if(!$statement->execute()) {
  31. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  32. }
  33. $probe = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  34. $statement = $this->DataInterface->DatabaseConnection->prepare(
  35. "SELECT organization.* FROM organization, ct_center, user WHERE organization.ID = ct_center.fk_organization AND user.fk_center = ct_center.ID AND user.ID = $ID_user"
  36. );
  37. if(!$statement->execute()) {
  38. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  39. }
  40. $organization = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  41. }
  42. else if($user['type']=='reader') {
  43. }
  44. else if($user['type']=='physician') {
  45. $statement = $this->DataInterface->DatabaseConnection->prepare(
  46. "SELECT * FROM probe WHERE fk_user = $ID_user"
  47. );
  48. if(!$statement->execute()) {
  49. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  50. }
  51. $probe = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  52. $statement = $this->DataInterface->DatabaseConnection->prepare(
  53. "SELECT * FROM organization WHERE fk_user = $ID_user"
  54. );
  55. if(!$statement->execute()) {
  56. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  57. }
  58. $organization = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  59. }
  60. $statement = $this->DataInterface->DatabaseConnection->prepare(
  61. "SELECT *, name_$lang AS name FROM country ORDER BY name_$lang"
  62. );
  63. if(!$statement->execute()) {
  64. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  65. }
  66. $countries = $statement->fetchAll(\PDO::FETCH_ASSOC);
  67. //
  68. return [
  69. 'result' => 'OK',
  70. 'ID' => $User->ID,
  71. 'firstname' => $User->firstname,
  72. 'lastname' => $User->lastname,
  73. 'email' => $User->email,
  74. 'user' => $user,
  75. 'probe' => $probe,
  76. 'organization' => $organization,
  77. 'countries' => $countries
  78. ];
  79. }
  80. /**
  81. * Check data and create new account.
  82. */
  83. public function profilePost($User, $data) {
  84. $ID_user = $User->ID;
  85. // user
  86. if (strlen($data['user']['password']) < 8 ||
  87. !preg_match("#[0-9]+#", $data['user']['password']) ||
  88. !preg_match("#[a-z]+#", $data['user']['password']) ||
  89. !preg_match("#[A-Z]+#", $data['user']['password'])) {
  90. return ['result' => 'ERROR', 'reason' => 'password_strength'];
  91. }
  92. if($data['user']['password'] != $data['user']['password2']) {
  93. return ['result' => 'ERROR', 'reason' => 'password_mismatch'];
  94. }
  95. if(empty($data['user']['firstname'])) {
  96. return ['result' => 'ERROR', 'group' => 'user', 'reason' => 'firstname'];
  97. }
  98. if(empty($data['user']['lastname'])) {
  99. return ['result' => 'ERROR', 'group' => 'user', 'reason' => 'lastname'];
  100. }
  101. if(empty($data['user']['phone'])) {
  102. return ['result' => 'ERROR', 'group' => 'user', 'reason' => 'phone'];
  103. }
  104. // organization
  105. if(empty($data['organization']['name'])) {
  106. return ['result' => 'ERROR', 'group' => 'organization', 'reason' => 'name'];
  107. }
  108. if(empty($data['organization']['country'])) {
  109. return ['result' => 'ERROR', 'group' => 'organization', 'reason' => 'country'];
  110. }
  111. if(empty($data['organization']['zip'])) {
  112. return ['result' => 'ERROR', 'group' => 'organization', 'reason' => 'zip'];
  113. }
  114. if(empty($data['organization']['city'])) {
  115. return ['result' => 'ERROR', 'group' => 'organization', 'reason' => 'city'];
  116. }
  117. if(empty($data['organization']['address'])) {
  118. return ['result' => 'ERROR', 'group' => 'organization', 'reason' => 'address'];
  119. }
  120. if(empty($data['organization']['phone'])) {
  121. return ['result' => 'ERROR', 'group' => 'organization', 'reason' => 'phone'];
  122. }
  123. // probe
  124. if(empty($data['probe']['brand'])) {
  125. return ['result' => 'ERROR', 'group' => 'probe', 'reason' => 'brand'];
  126. }
  127. if(empty($data['probe']['year'])) {
  128. $data['probe']['year'] = null;
  129. }
  130. if(empty($data['probe']['frequency'])) {
  131. return ['result' => 'ERROR', 'group' => 'probe', 'reason' => 'frequency'];
  132. }
  133. // Begin transaction
  134. $this->DataInterface->DatabaseConnection->beginTransaction();
  135. // Insert user
  136. $statement = $this->DataInterface->DatabaseConnection->prepare(
  137. "UPDATE user SET password=:password, firstname=:firstname, lastname=:lastname, phone=:phone WHERE ID=$ID_user"
  138. );
  139. $password = \Tools\Crypto::getHashPassword($data['user']['password']);
  140. $statement->bindParam(':password', $password);
  141. $statement->bindParam(':firstname', $data['user']['firstname']);
  142. $statement->bindParam(':lastname', $data['user']['lastname']);
  143. $statement->bindParam(':phone', $data['user']['phone']);
  144. // Error check
  145. if(!$statement->execute()) {
  146. $this->DataInterface->DatabaseConnection->rollback();
  147. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  148. }
  149. $fk_user = $this->DataInterface->DatabaseConnection->lastInsertId();
  150. // Update organization (does nothing on investigator since fk_user is related to fk_center)
  151. $statement = $this->DataInterface->DatabaseConnection->prepare(
  152. "UPDATE organization SET name=:name, fk_country=:fk_country, zip=:zip, city=:city, address=:address, phone=:phone WHERE fk_user=$ID_user"
  153. );
  154. $statement->bindParam(':name', $data['organization']['name']);
  155. $statement->bindParam(':fk_country', $data['organization']['country']);
  156. $statement->bindParam(':zip', $data['organization']['zip']);
  157. $statement->bindParam(':city', $data['organization']['city']);
  158. $statement->bindParam(':address', $data['organization']['address']);
  159. $statement->bindParam(':phone', $data['organization']['phone']);
  160. // Error check
  161. if(!$statement->execute()) {
  162. $this->DataInterface->DatabaseConnection->rollback();
  163. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  164. }
  165. // Update probe (does nothing on investigator since fk_user is related to fk_center)
  166. $statement = $this->DataInterface->DatabaseConnection->prepare(
  167. "UPDATE probe SET name=:name, brand=:brand, type=:type, year=:year, frequency=:frequency WHERE fk_user=$ID_user"
  168. );
  169. $statement->bindParam(':name', $data['probe']['name']);
  170. $statement->bindParam(':brand', $data['probe']['brand']);
  171. $statement->bindParam(':type', $data['probe']['type']);
  172. $statement->bindParam(':year', $data['probe']['year']);
  173. $statement->bindParam(':frequency', $data['probe']['frequency']);
  174. // Error check
  175. if(!$statement->execute()) {
  176. $this->DataInterface->DatabaseConnection->rollback();
  177. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  178. }
  179. // Commit
  180. $this->DataInterface->DatabaseConnection->commit();
  181. return [
  182. 'result' => 'OK',
  183. 'data' => $data
  184. ];
  185. }
  186. }
  187. }