Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Helper.class.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. class Helper {
  3. private $utility;
  4. function __construct($utility=null) {
  5. $this->utility = $utility;
  6. }
  7. public function setHash($destination='') {
  8. return $this->utility->setHash($destination);
  9. }
  10. public function setSortableCol($label='', $destination, $field, $dir) {
  11. $currentField = isset($_POST['orderField']) ? $_POST['orderField'] : null;
  12. $currentDir = isset($_POST['orderDir']) ? $_POST['orderDir'] : null;
  13. $pageNumb = isset($_POST['pageNumb']) ? $_POST['pageNumb'] : 1;
  14. $icon = '';
  15. if ($field == $currentField) {
  16. if ($currentDir == 'desc') $dir = 'asc'; else $dir = 'desc';
  17. if ($dir == 'asc')
  18. $icon = '<span class="icon-menu4 opacize"></span>';
  19. else
  20. $icon = '<span class="icon-menu3 opacize"></span>';
  21. }
  22. return '<a href="'.$this->setHash($destination.'/'.$field.'/'.$dir.'/'.$pageNumb).'">'.$label.'</a> '.$icon;
  23. }
  24. public function wrapVector($vector, $start, $end='', $space=' ') {
  25. $string = '';
  26. if (is_array($vector)) {
  27. foreach($vector as $value) {
  28. $string .= $start.$value.$end.$space;
  29. }
  30. }
  31. return $string;
  32. }
  33. public function setCommaList($vector=[], $sep=', ') {
  34. return is_array($vector) ? implode($sep, $vector) : '';
  35. }
  36. public function dateIntervalString($date) {
  37. $start = date_create($date);
  38. $end = date_create();
  39. $diff = date_diff($start, $end);
  40. if ($diff->y > 0) {
  41. if ($diff->m > 0) {
  42. return vsprintf(_('%s<sup>y</sup> %s<sup>mo</sup>'), [$diff->y, $diff->m]);
  43. } else {
  44. return vsprintf(_('%s<sup>y</sup>'), [$diff->y]);
  45. }
  46. }
  47. if ($diff->y <= 0 && $diff->m > 0) {
  48. if ($diff->d > 0) {
  49. return vsprintf(_('%s<sup>mo</sup> %s<sup>d</sup>'), [$diff->m, $diff->d]);
  50. } else {
  51. return vsprintf(_('%s<sup>mo</sup>'), [$diff->m]);
  52. }
  53. }
  54. if ($diff->y <= 0 && $diff->m <= 0 && $diff->d > 0) {
  55. if ($diff->h > 0) {
  56. return vsprintf(_('%s<sup>d</sup> %s<sup>h</sup>'), [$diff->d, $diff->h]);
  57. } else {
  58. return vsprintf(_('%s<sup>d</sup>'), [$diff->d]);
  59. }
  60. }
  61. if ($diff->y <= 0 && $diff->m <= 0 && $diff->d <= 0) {
  62. if ($diff->h > 0) {
  63. return vsprintf(_('%s<sup>h</sup> %s<sup>mi</sup>'), [$diff->h, $diff->i]);
  64. } else {
  65. if ($diff->i > 0) {
  66. return vsprintf(_('%s<sup>mi</sup>'), [$diff->i]);
  67. }
  68. }
  69. }
  70. return vsprintf(_('%s<sup>s</sup>'), [$diff->s]);
  71. }
  72. public function setYesNo($number=0) {
  73. return $number==1 ? _('Yes') : _('No');
  74. }
  75. public function getDateString($date=null, $withTime=true) {
  76. if ($withTime) {
  77. $dateString = '%d/%b/%Y %H.%M';
  78. } else {
  79. $dateString = '%d/%b/%Y';
  80. }
  81. if (!empty($date))
  82. return strftime($dateString, strtotime($date));
  83. else
  84. return '';
  85. }
  86. public function getTimeString($date=null, $sep='.', $withSecs=false) {
  87. $secs = $withSecs ? $sep.'s' : '';
  88. return date('H'.$sep.'i'.$secs, strtotime($date));
  89. }
  90. public function getJsonTranslation($jsonField, $langCode=null, $default='') {
  91. $data = json_decode($jsonField, true);
  92. return is_array($data) && isset($data[$langCode]) ? $data[$langCode] : $default;
  93. }
  94. public function getRoleNameInList($string='', $roleList=[]) {
  95. $string = '{'.$string.'}';
  96. $array = json_decode($string, true);
  97. $list = [];
  98. if (is_array($array)) {
  99. foreach($array as $roleId => $roleName) { //Role name is in English because of the query in usersController::usersList()
  100. if (isset($roleList[$roleId])) {
  101. $list[] = $roleList[$roleId]['name'];
  102. }
  103. }
  104. }
  105. return !empty($list) && is_array($list) ? implode(', ', $list) : null;
  106. }
  107. public function requestSubject($data=[]) {
  108. $surname = isset($data['patientSurname']) ? $this->cleanText($data['patientSurname']) : '';
  109. $name = isset($data['patientName']) ? $this->cleanText($data['patientName']) : '';
  110. $years = isset($data['ageYears']) ? (int)$data['ageYears'] : 0;
  111. $months = isset($data['ageMonths']) ? (int)$data['ageMonths'] : 0;
  112. $days = isset($data['ageDays']) ? (int)$data['ageDays'] : 0;
  113. $gender = isset($data['patientGender']) ? trim($data['patientGender']) : '';
  114. $anonymous = isset($data['anonymous']) ? $data['anonymous'] : 0;
  115. $age = $months > AGE_MONTH_LIMIT ? $years : $months; //AGE_MONTH_LIMIT is in mainController
  116. //$ageString = $months > AGE_MONTH_LIMIT ? vsprintf(_('%s y.'), [$years]) : vsprintf(_('%s m.'), [$months]);
  117. if ($months > AGE_MONTH_LIMIT) {
  118. $ageString = vsprintf(_('%s y.'), [$years]);
  119. } else {
  120. if ($months > 0) {
  121. //if ($days == 0) {
  122. $ageString = vsprintf(_('%s m.'), [$months]);
  123. //} else {
  124. //$ageString = vsprintf(_('%s m. %s d.'), [$months, $days]);
  125. //}
  126. } else {
  127. $ageString = vsprintf(_('%s d.'), [$days]);
  128. }
  129. }
  130. $genderLabel = $gender == 'm' ? strtolower(_('Mal.'))[0] : strtolower(_('Fem.'))[0];
  131. if ($gender == '') $genderLabel = ''; //Overwrite the value if no gender is passed
  132. if ($anonymous == 0) {
  133. $fullName = '<span class="badge badge-secondary">'.$this->setDottedFullname($name, $surname, false).'</span>';
  134. } else {
  135. $fullName = '<span class="badge badge-light">'.$this->getObfuscateString($name.' '.$surname).'</span>';
  136. }
  137. return $fullName.' <span class="badge badge-secondary">'.$ageString.'</span> <span class="badge badge-dark">'.$genderLabel.'</span>';
  138. }
  139. public function patient_age($data=[]) {
  140. $years = isset($data['ageYears']) ? (int)$data['ageYears'] : 0;
  141. $months = isset($data['ageMonths']) ? (int)$data['ageMonths'] : 0;
  142. $days = isset($data['ageDays']) ? (int)$data['ageDays'] : 0;
  143. $anonymous = isset($data['anonymous']) ? $data['anonymous'] : 0;
  144. $age = $months > AGE_MONTH_LIMIT ? $years : $months; //AGE_MONTH_LIMIT is in mainController
  145. if ($months > AGE_MONTH_LIMIT) {
  146. $ageString = vsprintf(_('%s y.'), [$years]);
  147. } else {
  148. if ($months > 0) {
  149. $ageString = vsprintf(_('%s m.'), [$months]);
  150. } else {
  151. $ageString = vsprintf(_('%s d.'), [$days]);
  152. }
  153. }
  154. return $ageString;
  155. }
  156. public function getObfuscateString($string='') {
  157. //return str_repeat('&#9679;', strlen($string));
  158. return str_repeat('&#9679;', 3);
  159. }
  160. //TODO: some cleaning operations
  161. public function cleanText($text='') {
  162. //return utf8_encode($text);
  163. return $text;
  164. }
  165. public function hashtag($text) {
  166. return preg_replace('/(?<!\S)#([0-9a-zA-Z]+)/', '<a class="badge badge-info" href="/requests-edit/'.time().'/$1/view/hashtag/my/hashtag/created_at/desc/1">#$1</a>', $text);
  167. }
  168. public function linkfy($label='', $href='', $class="", $target='_self', $meta='') {
  169. return '<a href="'.$href.'" class="'.$class.'" target="'.$target.'" '.$meta.'>'.$label.'</a>';
  170. }
  171. public function getExtension($file) {
  172. return pathinfo($file, PATHINFO_EXTENSION);
  173. }
  174. public function getPreviewType($fileName=null) {
  175. $images = ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'tif'];
  176. $videos = ['mp4', 'm4p', 'm4v', 'mov', 'mpg', 'mpeg', 'mp2', 'mpeg', 'mpe', 'mpv', 'webm', 'ogv', 'ogg'];
  177. $audio = ['mp3', 'mpa', 'aac', 'oga'];
  178. $files = ['pdf'];
  179. if (!is_null($fileName)) {
  180. $ext = strtolower($this->getExtension($fileName));
  181. if (in_array($ext, $images)) {
  182. return 'images';
  183. }
  184. if (in_array($ext, $videos)) {
  185. return 'video';
  186. }
  187. if (in_array($ext, $audio)) {
  188. return 'audio';
  189. }
  190. if (in_array($ext, $files)) {
  191. return 'iframe';
  192. }
  193. }
  194. return 'download';
  195. }
  196. public function getPreviewLink($attach=[], $showUser=true, $wrapStart=null, $wrapEnd=null, $linkClass=null) {
  197. $previewType = $this->getPreviewType($attach['file_name']);
  198. $attachId = $attach['id'];
  199. $attachTitle = $attach['file_title'];
  200. $attachExt = $attach['file_ext'];
  201. $attachSize = $attach['file_size'];
  202. $titleLength = 20;
  203. $wrapStart = is_null($wrapStart) ? '<div class="attach-row">' : $wrapStart;
  204. $wrapEnd = is_null($wrapEnd) ? '</div><div class="clearfix"><small class="text-muted float-right mr-1"><i>'.$this->byte_convert($attachSize).'</i></small><small class="text-muted float-right"><i>'.strtoupper($attachExt).'</i> -&nbsp;</small></div></div>' : $wrapEnd;
  205. $user = $showUser ? '<div class="clearfix"><small class="text-muted">'.$attach['name'].' '.$attach['surname'].' <span class="float-right">'.$this->getTimeString($attach['created_at']).'</span></small></div><div class="clearfix">' : '';
  206. if ($previewType != 'download') {
  207. switch ($previewType) {
  208. case 'images':
  209. return $wrapStart.$user.'<a data-options=\'{"hash":false}\' data-toggle="tooltip" data-placement="top" title="'.$attach['file_title'].'.'.$attach['file_ext'].'" data-fancybox="images" data-caption="'.htmlspecialchars($this->setDottedFullname($attach['name'], $attach['surname'], false)).' - '.htmlspecialchars($attachTitle).' - '.$this->getDateString($attach['created_at']).'" data-src="/downlaod/?attach_id='.$attachId.'" href="#" class="'.$linkClass.'"><span class="icon-file-picture"> </span>'.$this->truncate($attachTitle, $titleLength).'</a>'.$wrapEnd;
  210. break;
  211. case 'video':
  212. case 'audio':
  213. return $wrapStart.$user.'<a data-options=\'{"hash":false}\' data-toggle="tooltip" data-placement="top" title="'.$attach['file_title'].'.'.$attach['file_ext'].'" data-fancybox data-type="iframe" data-src="/downlaod/?attach_id='.$attachId.'" href="#" class="'.$linkClass.'"><span class="icon-file-picture"></span> '.$this->truncate($attachTitle, $titleLength).'</a>'.$wrapEnd;
  214. break;
  215. default:
  216. return $wrapStart.$user.'<a data-options=\'{"hash":false}\' data-toggle="tooltip" data-placement="top" title="'.$attach['file_title'].'.'.$attach['file_ext'].'" data-fancybox data-type="iframe" data-src="/downlaod/?attach_id='.$attachId.'" href="#" class="'.$linkClass.'"><span class="icon-download3"></span> '.$this->truncate($attachTitle, $titleLength).'</a>'.$wrapEnd;
  217. }
  218. } else {
  219. return $wrapStart.$user.'<a class="download '.$linkClass.'" data-toggle="tooltip" data-placement="top" title="'.$attach['file_title'].'.'.$attach['file_ext'].'" data-forcedownload="1" href="/downlaod/?attach_id='.$attachId.'"><span class="icon-download3"></span> '.$this->truncate($attachTitle, $titleLength).'</a>'.$wrapEnd;
  220. }
  221. }
  222. public function getDocName($attach=[], $showUser=true, $wrapStart=null, $wrapEnd=null, $linkClass=null) {
  223. $previewType = $this->getPreviewType($attach['file_name']);
  224. $attachId = $attach['id'];
  225. $attachTitle = $attach['file_title'];
  226. $attachExt = $attach['file_ext'];
  227. $attachSize = $attach['file_size'];
  228. // $titleLength = 20;
  229. $wrapStart = is_null($wrapStart) ? '<div class="attach-row">' : $wrapStart;
  230. $wrapEnd = is_null($wrapEnd) ? '<small class="float-right text-muted">'.$this->byte_convert($attachSize).' - '.strtoupper($attachExt).'</small></div>' : $wrapEnd;
  231. $user = $showUser ? '<div class="clearfix"><small class="text-muted">'.$attach['name'].' '.$attach['surname'].' <span class="float-right">'.$this->getTimeString($attach['created_at']).'</span></small></div>' : '';
  232. if ($previewType != 'download') {
  233. switch ($previewType) {
  234. case 'images':
  235. return $wrapStart.$user.'<a data-options=\'{"hash":false}\' data-fancybox="images" data-caption="'.htmlspecialchars($this->setDottedFullname($attach['name'], $attach['surname'], false)).' - '.htmlspecialchars($attachTitle).' - '.$this->getDateString($attach['created_at']).'" data-src="/downlaod/?attach_id='.$attachId.'" href="#" class="'.$linkClass.'"><span class="icon-file-picture"> </span>'.$attachTitle.'.'.$attachExt.'</a>'.$wrapEnd;
  236. break;
  237. case 'video':
  238. case 'audio':
  239. return $wrapStart.$user.'<a data-options=\'{"hash":false}\' data-fancybox data-type="iframe" data-src="/downlaod/?attach_id='.$attachId.'" href="#" class="'.$linkClass.'"><span class="icon-file-picture"></span> '.$attachTitle.'</a>'.$wrapEnd;
  240. break;
  241. default:
  242. return $wrapStart.$user.'<a data-options=\'{"hash":false}\' data-fancybox data-type="iframe" data-src="/downlaod/?attach_id='.$attachId.'" href="#" class="'.$linkClass.'"><span class="icon-download3"></span> '.$attachTitle.'</a>'.$wrapEnd;
  243. }
  244. } else {
  245. return $wrapStart.$user.'<a class="download '.$linkClass.'" data-forcedownload="1" href="/downlaod/?attach_id='.$attachId.'"><span class="icon-download3"></span> '.$attachTitle.'</a>'.$wrapEnd;
  246. }
  247. }
  248. public function truncate($string=null, $len=15, $ell='...') {
  249. if (strlen($string) >= $len) {
  250. return trim(substr($string, 0, $len)).$ell;
  251. } else {
  252. return $string;
  253. }
  254. }
  255. public function formatNumber($float=0, $decimals=0) {
  256. return $this->utility->formatNumber($float, $decimals);
  257. }
  258. public function replaceLinks($string='', $class='') {
  259. return preg_replace("~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~","<a href=\"\\0\" class=\"$class\">\\0</a>", $string);
  260. }
  261. public function setRequestStatus($label='') {
  262. switch(trim($label)) {
  263. case 'pending':
  264. return '<span class="badge badge-light text-muted"><i>'._('Sending').'...</i></span>';
  265. break;
  266. case 'draft':
  267. return '<span class="badge badge-light">'._('Draft').'</span>';
  268. break;
  269. case 'opened':
  270. return '<span class="badge badge-secondary">'._('Opened').'</span>';
  271. break;
  272. case 'referted':
  273. return '<span class="badge badge-success">'._('Answered').'</span>';
  274. break;
  275. case 'reopened':
  276. return '<span class="badge badge-danger">'._('Reopened').'</span>';
  277. break;
  278. case '! referted':
  279. return '<span class="badge badge-warning">'._('Partially answered').'</span>';
  280. break;
  281. default:
  282. return '<span class="badge badge-light">'.$label.'</span>';
  283. }
  284. }
  285. public function getRequestExam($exams=[], $label='', $examType='') {
  286. $html = '';
  287. $examItem = [];
  288. $arrayItem = isset($exams[$label]) ? $exams[$label] : [];
  289. if (!empty($arrayItem)) {
  290. foreach($arrayItem as $examId => $examInfo) {
  291. if ($examInfo['type'] == $examType) {
  292. $examItem = $examInfo;
  293. break;
  294. }
  295. }
  296. if (!empty($examItem)) {
  297. $examLabel = $examItem['description'];
  298. $examId = $examItem['id'];
  299. $examType = $examItem['type'];
  300. $children = $examItem['children'];
  301. $checked = $examItem['checked'] ? 'checked' : '';
  302. $checkbox = '<input type="checkbox" name="data[exam_'.$examType.'][value][]" class="request-exam-checkbox exam-'.$examType.'" data-label="%s" data-checkboxclass="exam-'.$examType.'" data-wizardcontainer="exam-wrapper-'.$examType.'" id="exam-%s" value="%s" %s> ';
  303. $html .= '<ul>';
  304. $html .= '<li class="list-no-type"><label>';
  305. $html .= empty($children) ? vsprintf($checkbox, [$examLabel, $examId, $examId, $checked]) : '';
  306. //$html .= vsprintf($checkbox, [$examLabel, $examId, $examId, $checked]);
  307. $html .= $examLabel;
  308. $html .= '</label>';
  309. if (is_array($children) && !empty($children)) {
  310. $html .= '<ul>';
  311. foreach($children as $childId => $childInfo) {
  312. $checked = $childInfo['checked'] ? 'checked' : '';
  313. $html .= '<li class="list-no-type">'.vsprintf($checkbox, [$examLabel.' / '.$childInfo['description'], $childId, $childId, $checked]).$childInfo['description'].'</li>';
  314. }
  315. $html .= '</ul>';
  316. }
  317. $html .= '</li></ul>';
  318. }
  319. }
  320. return $html;
  321. }
  322. public function getUserFlag($userCountryCode=null, $width=50) {
  323. $imageUrl = '/images/flags/svg/'.strtolower($userCountryCode).'.svg';
  324. $imagePath = PUBLIC_HTML.$imageUrl;
  325. if (file_exists($imagePath)) {
  326. return '<img src='.$imageUrl.' width="'.$width.'" style="vertical-align:middle;">';
  327. }
  328. return null;
  329. }
  330. public function setDottedFullname($name, $surname, $nameFirst=true) {
  331. $nameParts = explode(' ', trim($name));
  332. $chars = [];
  333. foreach($nameParts as $part) {
  334. if (isset($part[0])) $chars[] = strtoupper($part[0]);
  335. }
  336. if ($nameFirst)
  337. return implode('. ', $chars).'. '.ucwords(strtolower($surname));
  338. else
  339. return ucwords(strtolower($surname)).' '.implode('. ', $chars).'.';
  340. }
  341. public function setAvatarView($avatarImagePath, $params=[]) {
  342. global $config;
  343. $timestamp = isset($params['timestamp']) ? $params['timestamp'] : time();
  344. $strTimestamp = "?t=$timestamp";
  345. $cssClass = isset($params['class']) ? $params['class'] : 'list-avatar';
  346. $cssId = isset($params['cssId']) ? $params['cssId'] : null;
  347. $strCssId = !is_null($cssId) ? 'id="'.$cssId.'"' : '';
  348. $status = isset($params['status']) ? (int)$params['status'] : 1;
  349. if ($status == 0) {
  350. $avatarImagePath = $config['settings']['avatar-uri'].$config['settings']['avatar-default'];
  351. $strTimestamp = '';
  352. }
  353. return '<div class="'.$cssClass.'" '.$strCssId.' style="background-image: url('.$avatarImagePath.$strTimestamp.');"></div>';
  354. }
  355. public function byte_convert($number = 0, $precision = 1){
  356. if( !is_numeric($number) ){
  357. return false;
  358. }
  359. $unit = ''; //suffisso dell'unità di misura
  360. //terabyte
  361. if($number >= 1000000000000){
  362. $number = round($number / 1099511627776, $precision);
  363. $unit = 'TB';
  364. //gigabyte
  365. }elseif($number >= 1000000000){
  366. $number = round($number / 1073741824, $precision);
  367. $unit = 'GB';
  368. //megabyte
  369. }elseif($number >= 1000000){
  370. $number = round($number / 1048576, $precision);
  371. $unit = 'MB';
  372. //kilobyte
  373. }elseif($number >= 1000){
  374. $number = round($number / 1024, $precision);
  375. $unit = 'KB';
  376. //byte
  377. }else{
  378. $unit = 'byte';
  379. return number_format($number).' '.$unit;
  380. }
  381. return number_format($number, $precision).' '.$unit;
  382. }
  383. }