選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

centerController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. class centerController extends mainController {
  3. function __construct() {
  4. parent::__construct();
  5. //To change for every Controllers
  6. $this->viewDir = 'Center';
  7. //$this->allow = [];
  8. }
  9. public function index(){
  10. if(!isset($this->user->getRoles()[ADMIN_ROLE_ID]))
  11. return $this->setRawJsonResponse('err', _('Access denied.'), [], ['button'=>'goto', 'destination'=>'dashboard']);
  12. $this->actionTitle = _("Clinical center");
  13. $user_id = $this->user->getUserId();
  14. $center_id = $this->getPost('center_id', 0);
  15. $this->view->centerId = $center_id;
  16. $this->view->isEditing = $center_id == 0 ? false : true;
  17. $center_data = null;
  18. if($this->view->isEditing){
  19. $center_data = $this->db->where('id', $center_id)->getOne('clinical_centers');
  20. if($center_data == null)
  21. return $this->setRawJsonResponse('err', 'Access denied.', [], ['button'=>'goto', 'destination'=>'dashboard']);
  22. // centerData['medical_specialties'] => [[index] => specialty_id]
  23. $center_data['medical_specialties'] = $this->db->where('center_id', $center_id)->getValue('clinical_center_medical_specialties_to', 'specialty_id', null);
  24. }
  25. $this->view->centerData = $center_data;
  26. $this->view->continents = array_merge([0=>['code'=>null, 'name'=>'...']], $this->db->get('continents'));
  27. if($this->view->isEditing){
  28. $this->view->countries = $this->db
  29. ->orderBy('country_name', 'ASC')
  30. ->where('continent_iso2_code', $center_data['continent_code'])
  31. ->get('countries', null, ['country_iso2_code as code', 'country_name as name']);
  32. }
  33. $this->view->allSpecialties = $this->db
  34. ->orderBy('description', 'ASC')
  35. ->get('users_medical_specialties'); // [index] => [id, description, status]
  36. //$this->view->DEBUG = 'NULL';
  37. return $this->setJsonView('index');
  38. }
  39. // Ajax function: dynamic loading of the Countries select by Continent
  40. public function filterCountries() {
  41. $continent_code = $this->getPost('continent_code', null);
  42. if($this->db->where('code', $continent_code)->getOne('continents') == null){
  43. return $this->setRawJsonResponse('err', $continent_code . ': ' . _('invalid continent code.'), [], []);
  44. }
  45. $countries = [];
  46. $countries_db = $this->db
  47. ->orderBy('country_name', 'ASC')
  48. ->where('continent_iso2_code', $continent_code)
  49. ->get('countries', null, ['country_iso2_code as code', 'country_name as name']);
  50. foreach ($countries_db as $country) {
  51. $countries[$country['code']] = $country['name'];
  52. }
  53. return $this->setRawJsonResponse('ok', '', ['countries'=>$countries]);
  54. }
  55. // Main function for Clinical Center saving and updating
  56. public function centerSave(){
  57. // Check: only Administrator can save
  58. if(!isset($this->user->getRoles()[ADMIN_ROLE_ID]))
  59. return $this->setRawJsonResponse('err', _('Save denied.'), [], ['button'=>'goto', 'destination'=>'dashboard']);
  60. $now = date('Y-m-d H:i:s');
  61. $user_id = $this->user->getUserId();
  62. $data = $this->getPost('data', null);
  63. $center_id = $data['center_id']['value'];
  64. // Check: modify a clinical center only if exists
  65. if($center_id != 0 && $this->db->where('id', $center_id)->getOne('clinical_centers') == null)
  66. return $this->setRawJsonResponse('err', _('Save denied.'), [], ['button'=>'goto', 'destination'=>'dashboard']);
  67. $center_validate_data = [];
  68. $center_validate_data = $this->centerValidate($data);
  69. $specialties_validate_data = [];
  70. $devices_validate_data = [];
  71. $this->view->DEBUG = $center_validate_data;
  72. return $this->setJsonView('centerSave');
  73. if(isset($center_validate_data['ok'])){
  74. //unset($center_validate_data['ok']['center_id']);
  75. $specialties_validate_data = $center_validate_data['ok']['medical_specialties'];
  76. unset($center_validate_data['ok']['medical_specialties']);
  77. $center_validate_data['ok']['updated_by'] = $user_id;
  78. $center_validate_data['ok']['updated_at'] = $now;
  79. //$this->view->DEBUG = $center_validate_data;
  80. //return $this->setJsonView('centerSave');
  81. if($center_id == 0){ // INSERT
  82. $center_validate_data['ok']['created_by'] = $user_id;
  83. $center_validate_data['ok']['created_at'] = $now;
  84. $center_id = $this->db->insert('clinical_centers', $center_validate_data['ok']);
  85. if($center_id) return $this->setRawJsonResponse('ok', _('Clinical center created successfully.'), [], ['button'=>'goto', 'destination'=>'centers/'.time()]);
  86. else return $this->setRawJsonResponse('err', _('Clinical center insert error.'), [], ['button'=>'goto', 'destination'=>'centers/'.time()]);
  87. } else { // UPDATE
  88. if($this->db->where('id', $center_id)->update('clinical_centers', $center_validate_data['ok']))
  89. return $this->setRawJsonResponse('ok', _('Clinical center updated successfully.'), [], ['button'=>'goto', 'destination'=>'centers/'.time()]);
  90. else return $this->setRawJsonResponse('err', _('Clinical center update error.'), [], ['button'=>'goto', 'destination'=>'centers/'.time()]);
  91. }
  92. }
  93. if(isset($center_validate_data['err'])){
  94. $err_class = isset($center_validate_data['err']['field-class']) ? ['class'=>$center_validate_data['err']['field-class']] : [];
  95. return $this->setRawJsonResponse('err', $center_validate_data['err']['err_msg'], $err_class);
  96. }
  97. // For debugging...
  98. // $this->view->DEBUG = 'DEBUG CONTENT';
  99. // return $this->setJsonView('centerSave');
  100. // return $this->setRawJsonResponse('ok', _('MESSAGGIO DI CONFERMA'), [], ['button'=>'goto', 'destination'=>'centers/'.time()]);
  101. // return $this->setRawJsonResponse('err', 'MESSAGGIO DI ERRORE del tipo <b>label</b>: required field', ['class'=>'field-CAMPO1']);
  102. }
  103. // Return an array
  104. // [ok] => [field => value]
  105. // [err] => [
  106. // err_msg => error message formatted
  107. // field-class => filed-FieldName to highlight the error on the page
  108. // ]
  109. private function centerValidate($form_data){
  110. $result = [];
  111. if($form_data['description']['value'] == null){
  112. $result['err']['err_msg'] = '<b>' . $form_data['description']['label'] . '</b>:' . _(' required field.');
  113. $result['err']['field-class'] = 'field-description';
  114. return $result;
  115. }
  116. if((int)$form_data['anonymize']['value'] != 0 && (int)$form_data['anonymize']['value'] != 1){
  117. $result['err']['err_msg'] = '<b>' . $form_data['anonymize']['label'] . '</b>:' . _(' invalid value.');
  118. $result['err']['field-class'] = 'field-anonymize';
  119. return $result;
  120. }
  121. if($form_data['continent_code']['value'] == null){
  122. $result['err']['err_msg'] = '<b>' . $form_data['continent_code']['label'] . '</b>:' . _(' required field.');
  123. $result['err']['field-class'] = 'field-continent_code';
  124. return $result;
  125. }
  126. if($this->db->where('code', $form_data['continent_code']['value'])->getOne('continents') == null){
  127. $result['err']['err_msg'] = _('Save denied. ') . $form_data['continent_code']['value'] . _(': invalid value.');
  128. $result['err']['field-class'] = 'field-continent_code';
  129. return $result;
  130. }
  131. if($form_data['country_code']['value'] == null){
  132. $result['err']['err_msg'] = '<b>' . $form_data['continent_code']['label'] . '</b>:' . _(' required field.');
  133. $result['err']['field-class'] = 'field-country_code';
  134. return $result;
  135. }
  136. if($this->db->where('country_iso2_code', $form_data['country_code']['value'])->getOne('countries') == null){
  137. $result['err']['err_msg'] = _('Save denied. ') . $form_data['country_code']['value'] . _(': invalid value.');
  138. $result['err']['field-class'] = 'field-country_code';
  139. return $result;
  140. }
  141. if($this->db->where('country_iso2_code', $form_data['country_code']['value'])->getOne('countries')['continent_iso2_code'] != $form_data['continent_code']['value']){
  142. $result['err']['err_msg'] = _('Save denied.');
  143. $result['err']['field-class'] = 'field-country_code';
  144. return $result;
  145. }
  146. if($form_data['lat']['value']!=null && !preg_match('/^[-]?[0-9]{1,2}\.[0-9]{1,8}$/', $form_data['lat']['value'])){
  147. $result['err']['err_msg'] = '<b>' . $form_data['lat']['label'] . '</b>:' . _(' invalid value.');
  148. $result['err']['field-class'] = 'field-lat';
  149. return $result;
  150. }
  151. if($form_data['lng']['value']!=null && !preg_match('/^[-]?[0-9]{1,2}\.[0-9]{1,8}$/', $form_data['lng']['value'])){
  152. $result['err']['err_msg'] = '<b>' . $form_data['lng']['label'] . '</b>:' . _(' invalid value.');
  153. $result['err']['field-class'] = 'field-lng';
  154. return $result;
  155. }
  156. if($form_data['lat']['value']!=null && $form_data['lng']['value']==null){
  157. $result['err']['err_msg'] = '<b>' . $form_data['lng']['label'] . '</b>:' . _(' required field.');
  158. $result['err']['field-class'] = 'field-lng';
  159. return $result;
  160. }
  161. if($form_data['lat']['value']==null && $form_data['lng']['value']!=null){
  162. $result['err']['err_msg'] = '<b>' . $form_data['lat']['label'] . '</b>:' . _(' required field.');
  163. $result['err']['field-class'] = 'field-lat';
  164. return $result;
  165. }
  166. // Medical specialties validation
  167. if(isset($form_data['medical_specialties']['value']) && is_array($form_data['medical_specialties']['value'])){
  168. $all_active_specialties = $this->db->where('status', 1)->getValue('users_medical_specialties', 'id', null);
  169. $center_specialties = $this->db->where('center_id', $form_data['center_id']['value'])->getValue('clinical_center_medical_specialties_to', 'specialty_id', null);
  170. foreach ($form_data['medical_specialties']['value'] as $specialty_id) {
  171. if(!in_array($specialty_id, $all_active_specialties)){
  172. $result['err']['err_msg'] = '<b>' . $form_data['medical_specialties']['label'] . '</b>:' . _(' invalid value.');
  173. $result['err']['field-class'] = 'field-medical_specialties';
  174. return $result;
  175. }
  176. }
  177. } else $form_data['medical_specialties']['value'] = [];
  178. // List of editable fields of the clinical center
  179. $center_writable_fields = [];
  180. foreach($this->db->rawQuery('describe clinical_centers') as $attribute) $center_writable_fields[] = $attribute['Field'];
  181. $center_writable_fields[] = 'medical_specialties'; // Field not in clinical_centers table
  182. // The following fields are editable only from the controller
  183. $center_writable_fields = array_diff($center_writable_fields, ['id', 'group_id', 'country_id', 'created_by', 'created_at', 'updated_by', 'updated_at', 'deleted_by', 'deleted_at']);
  184. foreach ($center_writable_fields as $field) {
  185. $result['ok'][$field] = $this->utility->deepTrim($form_data[$field]['value']);
  186. }
  187. // foreach ($form_data as $form_field => $values) {
  188. // if(in_array($form_field, $center_writable_fields)){
  189. // $result['ok'][$form_field] = $values['value'];
  190. // } else {
  191. //
  192. // }
  193. // }
  194. return $result;
  195. }
  196. //List all Clinical Centers in the Group
  197. function centersList() {
  198. if(!$this->checkPermissions([ADMIN_ROLE_ID])) {
  199. return $this->redirect('login', 'permissionDenied');
  200. }
  201. $results = $this->db
  202. ->where('cc.group_id', $this->userGroupId)
  203. ->join('continents cnts', 'cnts.code=cc.continent_code', 'INNER')
  204. ->join('countries cntrs', 'cntrs.country_iso2_code=cc.country_code', 'INNER')
  205. ->orderBy('cnts.name', 'asc')
  206. ->get('clinical_centers cc', null, ['cc.id center_id', 'cc.description', 'cnts.name continent_name', 'cntrs.country_name', 'cc.anonymize', 'cc.notes']);
  207. $activeSpecialties = $this->getActiveMedicalSpecialtiesIdByGroupId($this->userGroupId);
  208. foreach($results as $index => $item) {
  209. $specialties = $this->db
  210. ->where('ccmst.center_id', $item['center_id'])
  211. ->join('users_medical_specialties ums', 'ums.id=ccmst.specialty_id', 'INNER')
  212. ->get('clinical_center_medical_specialties_to ccmst', null, ['ums.id', 'ums.description']);
  213. $translated = [];
  214. if (!empty($specialties)) {
  215. foreach($specialties as $indecSpec => $specialty) {
  216. $translated[$specialty['id']]['description'] = _($specialty['description']);
  217. $translated[$specialty['id']]['active'] = in_array($specialty['id'], $activeSpecialties) ? true : false;
  218. }
  219. }
  220. $results[$index]['specialties'] = $translated;
  221. }
  222. $this->view->centers = $results;
  223. $this->actionTitle = _('Clinical Centers');
  224. $this->breadcrumbs = [['hash'=>null, 'label'=>$this->actionTitle]];
  225. return $this->setJsonView('centersList');
  226. }
  227. }