lay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZip($Path = "", $Filename = "", $Ext = "csv"){ $zip = new ZipArchive(); $zipPath = $Path.$Filename.".zip"; if ($zip->open($zipPath,ZipArchive::CREATE)) { $zip->addFile($Path.$Filename.".{$Ext}", $Filename.".{$Ext}"); $zip->close(); //Make sure the zip file created and output it. if(is_file($zipPath)){ if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression',ta = array("KommunKod" => $Kod); $Result = cURLRequest($Url, $Data, false); // if(DEBUG === true) return $Kod; if( !is_object($Result) || $Result->HasError !== false || ($Result->HasError === false && sizeof($Result->Kommun->Namn) === 0) ) return false; return $Result->Kommun->Namn; } /* anläggningar -- -----------------*/ function KommunAnlaggningar($Kommunkod){ $Url = sprintf("%s/Anlaggningar/Sok/", KO_API_URI); $Data = array("Kommunkod" => $Kommunkod); $Result = cURLRequest($Url, $Data, false); if( !is_object($Result) || $Result->HasError !== false || ($Result->HasError === false && sizeof($Result->Anlaggningar) === 0) ) return false; return $Result->Anlaggningar; } /* ApN anläggningar -- -----------------*/ function Anlaggningar($LevelId = 0){ global $dbh; $LevelId = (int)$LevelId; $query = " SELECT `section_props` FROM `school_section` INNER JOIN `school_type` ON `section_id` = `type_parent` INNER JOIN `school_level` ON `type_id` = `level_parent` WHERE `level_id` = :LevelId "; $pst = $dbh->prepare($query); $pst->bindParam(":LevelId", $LevelId, PDO::PARAM_INT); $pst->execute(); if($pst->rowCount() !== 0){ $res = $pst->fetch(PDO::FETCH_ASSOC); if(is_null($res['section_props'])) return false; $Props = unserialize($res['section_props']); if(isset($Props['Anlaggningar'])) return $Props['Anlaggningar']; } $pst = null; return false; } /* strip key 'x' from querystring -- -----------------*/ function stripQuerystring($key, $url) { $url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); $url = substr($url, 0, -1); return ($url); } /* font awesome -- -----------------*/ function setFA($filetype){ $filetype = strtolower($filetype); switch($filetype){ case "pdf" : $fileext = "file-pdf-o"; break; case "docx" : case "doc" : $fileext = "file-word-o"; break; case "xlsx" : case "xls" : $fileext = "file-excel-o"; break; case "pptx" : case "ppt" : $fileext = "file-power-point-o"; break; case "jpeg" : case "jpg" : case "png" : case "gif" : case "bmp" : case "tif" : $fileext = "file-image-o"; break; case "txt" : $fileext = "file-text-o"; break; case "zip" : case "tar" : case "rar" : $fileext = "file-archive-o"; break; // case "mp3" : $fileext = "file_mp3.png"; break; default : $fileext = "file-o"; break; } return $fileext; } /* filuppladdning -- -----------------*/ function UploadError($Code){ switch ($Code) { case UPLOAD_ERR_INI_SIZE: $message = sprintf(_("Filstorleken är större än max tillåtna (%s)."), ini_get('upload_max_filesize')); break; case UPLOAD_ERR_FORM_SIZE: $message = sprintf(_("Filstorleken är större än max tillåtna (%s)."), ini_get('upload_max_filesize')); break; case UPLOAD_ERR_PARTIAL: $message = _('Filen kunde inte laddas upp fullständigt.'); break; case UPLOAD_ERR_NO_FILE: $message = _('Ingen fil valdes.'); break; case UPLOAD_ERR_NO_TMP_DIR: $message = _('Ett fel uppstod med den temporära mappen.'); break; case UPLOAD_ERR_CANT_WRITE: $message = _('Kunde inte skriva till disk.'); break; case UPLOAD_ERR_EXTENSION: $message = _('Felaktig/ogiltig filändelse.'); break; default: $message = _('Ett okänt fel inträffade.'); break; } return $message; } /* average -- -----------------*/ function avg($sum=0,$count=0){ return ($count)? $sum / $count: 0; } /* insert white-space --------------------------------------------------------------------*/ function WhiteSpace($value = "", $num = 0){ // $num = ($num - mb_strlen($value, 'utf8')); $num = ($num - strlen($value)); if($num <= 0) return ""; return str_repeat(chr(32), $num); } /* RGB2HTML -- -----------------*/ /* convert RGB color to HEX --------------------------------------------------------------------*/ function rgb2html($r, $g=-1, $b=-1){ if (is_array($r) && sizeof($r) == 3) list($r, $g, $b) = $r; $r = intval($r); $g = intval($g); $b = intval($b); $r = dechex($r<0?0:($r>255?255:$r)); $g = dechex($g<0?0:($g>255?255:$g)); $b = dechex($b<0?0:($b>255?255:$b)); $color = (strlen($r) < 2?'0':'').$r; $color .= (strlen($g) < 2?'0':'').$g; $color .= (strlen($b) < 2?'0':'').$b; return '#'.$color; } /* -- -----------------*/ function BSCols($Data, $MaxCols = 2, $Col = 'md'){ //$MaxCols = $MaxCols; //columns will arrange to any number (as long as it is evenly divisible by 12) $column = 12/$MaxCols; //column number $total_items = sizeof($Data); $remainder = sizeof($Data)%$MaxCols; //how many items are in the last row $first_row_item = ($total_items - $remainder); //first item in the last row $i=0; // counter $Sb = ""; foreach($Data as $S){ if ($i%$MaxCols==0) { // if counter is multiple of 3 $Sb .= "
\n"; } if ($i >= $first_row_item) { //if in last row $Sb .= sprintf("
\n", $Col, (12/$remainder)); } else { $Sb .= sprintf("
\n", $Col, $column); } $Sb .= $S; $Sb .= "
\n"; $i++; if($i%$MaxCols==0) { // if counter is multiple of 3 $Sb .= "

\n"; } } if($i%$MaxCols!=0) { // put closing div if loop is not exactly a multiple of 3 $Sb .= "
\n"; } return $Sb; } /* -- -----------------*/ function format_datetime($dt, $format = 'yyyy-MM-dd HH:mm:ss'){ global $currentLanguage; $formatter = new IntlDateFormatter( $currentLanguage['Code'], IntlDateFormatter::FULL, IntlDateFormatter::FULL, KO_DEFAULT_TZ, IntlDateFormatter::GREGORIAN, $format ); return $formatter->format($dt); } /* -- -----------------*/ function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => _('år'), 'm' => _('månader'), 'w' => _('veckor'), 'd' => _('dagar'), 'h' => _('timmar'), 'i' => _('minuter'), 's' => _('sekunder'), ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v/* . ($diff->$k > 1 ? 's' : '')*/; } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 2); return $string ? implode(' och ', $string) . _(' sedan') : _('just nu'); } /* -- -----------------*/ function CreateDistanceTable(){ global $dbh; try { $TempName = "_competition_distance_" . rand(); $query = " CREATE TEMPORARY TABLE `{$TempName}` ( `DistId` smallint(3) NOT NULL, `DistName` varchar(75) COLLATE utf8_unicode_ci NOT NULL, `DistPool` tinyint(2) NOT NULL, `Distance` smallint(6) NOT NULL DEFAULT '0', `Relay` tinyint(1) NOT NULL DEFAULT '0', `Stroke` tinyint(1) NOT NULL DEFAULT '0', UNIQUE KEY `DistId` (`DistId`), KEY `DistPool` (`DistPool`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "; $dbh->query($query); $query = " INSERT INTO `{$TempName}` (`DistId`, `DistName`, `DistPool`, `Distance`, `Relay`, `Stroke`) (SELECT `DistId`, CONVERT(`DistName` USING `utf8`) AS `DistName`, `DistPool`, `Distance`, `Relay`, `Stroke` FROM `competition_distance` WHERE `DistRemoved` = 1) UNION (SELECT `DistId`, CONVERT(`DistName` USING `utf8`) AS `DistName`, 4 AS `DistPool`, 0 `Distance`, 0 `Relay`, 0 `Stroke` FROM `competition_distance_custom` WHERE `DistRemoved` = 1) ORDER BY `DistId` "; $pst = $dbh->query($query); if($pst->rowCount() === 0) return false; return $TempName; } catch(PDOException $e){ echo $e->getMessage(); exit; } catch(Exception $e){ echo $e->getMessage(); exit; } return false; } /* -- -----------------*/ function UserResults($UserId){ global $dbh; $TempName = sprintf("_result%d", rand(1111,9999)); $query = " CREATE TEMPORARY TABLE `{$TempName}` SELECT `ResDate` `Date`, MIN(`ResTime`) `Time`, `DistId`, `DistName` `Name`, `DistPool` `Pool`, CONCAT(`Distance`, `Relay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZip($Path = "", $Filename = "", $Ext = "csv"){ $zip = new ZipArchive(); $zipPath = $Path.$Filename.".zip"; if ($zip->open($zipPath,ZipArchive::CREATE)) { $zip->addFile($Path.$Filename.".{$Ext}", $Filename.".{$Ext}"); $zip->close(); //Make sure the zip file created and output it. if(is_file($zipPath)){ if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header('Content-Description: File Transfer'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename="'.$Filename.'.zip"'); header('Content-Length: ' . filesize($zipPath)); readfile($zipPath); @unlay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZip($Path = "", $Filename = "", $Ext = "csv"){ $zip = new ZipArchive(); $zipPath = $Path.$Filename.".zip"; if ($zip->open($zipPath,ZipArchive::CREATE)) { $zip->addFile($Path.$Filename.".{$Ext}", $Filenamelay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZip($Path = "", $Filename = "", $Ext = "csv"){ $zip = new ZipArchive(); $zipPath = $Path.$Filename.".zip"; if ($zip->open($zipPath,ZipArchive::CREATE)) { $zip->addFile($Path.$Filename.".{$Ext}", $Filename.".{$Ext}"); $zlay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZi$r)); $g = dechex($g<0?0:($g>255?255:$g)); $b = dechex($b<0?0:($b>255?255:$b)); $color = (strlen($r) < 2?'0':'').$r; $color .= (strlen($g) < 2?'0':'').$g; $color .= (strlen($b) < 2?'0':'').$b; return '#'.$color; } /* -- -----------------*/ function BSCols($Data, $MaxCols = 2, $Col = 'md'){ //$MaxCols = $MaxCols; //columns will arrange to any number (as long as it is evenly divisible by 12) $column = 12/$MaxCols; //column number $total_items = sizeof($Data); $remainder = sizeof($Data)%$MaxCols; //how many items are in the last row $first_row_item = ($total_items - $remainder); //first item in the last row $i=0; // counter $Sb = ""; foreach($Data as $S){ if ($i%$MaxCols==0) { // if counter is multiple of 3 $Sb .= "
\n"; } if ($i >= $first_row_item) { //if in last row $Sb .= sprintf("
\n", $Col, (12/$remainder)); } else { $Sb .= sprintf("
\n", $Col, $column); } $Sb .= $S; $Sb .= "
\n"; $i++; if($i%$MaxCols==0) { // if counter is multiple of 3 $Sb .= "

\n"; } } if($i%$MaxCols!=0) { // put closing div if loop is not exactly a multiple of 3 $Sb .= "
\n"; } return $Sb; } /* -- -----------------*/ function format_datetime($dt, $format = 'yyyy-MM-dd HH:mm:ss'){ global $currentLanguage; $formatter = new IntlDateFormatter( $currentLanguage['Code'], IntlDateFormatter::FULL, IntlDateFormatter::FULL, KO_DEFAULT_TZ, IntlDateFormatter::GREGORIAN, $format ); return $formatter->format($dt); } /* -- -----------------*/ function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => _('år'), 'm' => _('månader'), 'w' => _('veckor'), 'd' => _('dagar'), 'h' => _('timmar'), 'i' => _('minuter'), 's' => _('sekunder'), ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v/* . ($diff->$k > 1 ? 's' : '')*/; } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 2); return $string ? implode(' och ', $string) . _(' sedan') : _('just nu'); } /* -- -----------------*/ function CreateDistanceTable(){ global $dbh; try { $TempName = "_competition_distance_" . rand(); $query = " CREATE TEMPORARY TABLE `{$TempName}` ( `DistId` smallint(3) NOT NULL, `DistName` varchar(75) COLLATE utf8_unicode_ci NOT NULL, `DistPool` tinyint(2) NOT NULL, `Distance` smallint(6) NOT NULL DEFAULT '0', `Relay` tinyint(1) NOT NULL DEFAULT '0', `Stroke` tinyint(1) NOT NULL DEFAULT '0', UNIQUE KEY `DistId` (`DistId`), KEY `DistPool` (`DistPool`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; "; $dbh->query($query); $query = " INSERT INTO `{$TempName}` (`DistId`, `DistName`, `DistPool`, `Distance`, `Relay`, `Stroke`) (SELECT `DistId`, CONVERT(`DistName` USING `utf8`) AS `DistName`, `DistPool`, `Distance`, `Relay`, `Stroke` FROM `competition_distance` WHERE `DistRemoved` = 1) UNION (SELECT `DistId`, CONVERT(`DistName` USING `utf8`) AS `DistName`, 4 AS `DistPool`, 0 `Distance`, 0 `Relay`, 0 `Stroke` FROM `competition_distance_custom` WHERE `DistRemoved` = 1) ORDER BY `DistId` "; $pst = $dbh->query($query); if($pst->rowCount() === 0) return false; return $TempName; } catch(PDOException $e){ echo $e->getMessage(); exit; } catch(Exception $e){ echo $e->getMessage(); exit; } return false; } /* -- -----------------*/ function UserResults($UserId){ global $dbh; $TempName = sprintf("_result%d", rand(1111,9999)); $query = " CREATE TEMPORARY TABLE `{$TempName}` SELECT `ResDate` `Date`, MIN(`ResTime`) `Time`, `DistId`, `DistName` `Name`, `DistPool` `Pool`, CONCAT(`Distance`, `Relay`, `Stroke`) `DistCode` FROM `competition_result` INNER JOIN `competition_distance` ON `ResDistance` = `DistId` WHERE `ResDate` >= DATE_SUB(CURRENT_DATE(), INTERVAL 2 YEAR) AND `ResUser` = ? GROUP BY `Relay`, `Stroke`, `Distance` ORDER BY `ResUser`, `Relay`, `Stroke`, `Distance`, `ResTime` "; $pst = $dbh->prepare($query); $pst->execute(array($UserId)); $pst = null; return $TempName; } /* -- -----------------*/ function DownloadAsZip($Path = "", $Filename = "", $Ext = "csv"){ $zip = new ZipArchive(); $zipPath = $Path.$Filename.".zip"; if ($zip->open($zipPath,ZipArchive::CREATE)) { $zip->addFile($Path.$Filename.".{$Ext}", $Filename.".{$Ext}"); $zip->close(); //Make sure the zip file created and output it. if(is_file($zipPath)){ if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); header('Content-Description: File Transfer'); header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename="'.$Filename.'.zip"'); header('Content-Length: ' . filesize($zipPath)); readfile($zipPath); @unlink($Path.$Filename.".{$