summaryrefslogtreecommitdiff
path: root/app/helpers.php
blob: c2c6b162fe40f7a6a0b05b8f53ee4f099da4aa46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
use Carbon\Carbon;

function replaceSpecialChars($string) {
	$string = str_replace("&lt;i&gt;", "<i>", $string);
	$string = str_replace("&lt;I&gt;", "<i>", $string);
	$string = str_replace("&lt;/i&gt;", "</i>", $string);
	$string = str_replace("&lt;/I&gt;", "</i>", $string);
	$string = str_replace("&lt;b&gt;", "<b>", $string);
	$string = str_replace("&lt;/b&gt;", "</b>", $string);
	$string = str_replace("&lt;br&gt;", "<br>", $string);
	$string = str_replace("&lt;BR&gt;", "<br>", $string);
	$string = str_replace("%%br%%", "<br>", $string);
	$string = str_replace("%%BR%%", "<br>", $string);

	return $string;
}

function _formatFloat($float, $size = 2) {
	#return number_format($float, $size, ",", ".");
	return number_format($float, $size);
}

function formatFloat($f) {
	if ( 0 == ($f - floor($f))) {
		$f = intval($f);
	} else {
		$f = _formatFloat($f);
	}

	return $f;
}

/**
 * Shorter alias to formatFloat().
 */
function fF($f) {
	return formatFloat($f);
}

function printScoreData($score_array) {
	$return = "[";

	foreach($score_array as $score) {
		try {
			$f_score = formatFloat($score);
		} catch( Exception $e ) {
			error_log("printScoreData: score data seems to be messed up: " . $e->getMessage());
			$f_score = 0;
		}

		if ( 0 != $f_score ) {
			$return .= $f_score;
		} else {
			$return .= "null";
		}
		$return .= ",";
	}

	rtrim($return, ",");

	$return .= "]";

	return $return;
}

function getAiringStatusCode($airing_status) {
	$status = DB::select("SELECT id FROM airing_status WHERE status = ?", array($airing_status));
	if ( empty($status) ) {
		return 4;
	}
	return $status[0]->id;
}

function getSeason( $month = null ){
	if ( is_null($month) ) {
		$month = (int)date("m");
	}
	switch( $month ) {
	case 1:
	case 2:
	case 3: return "Winter";
	case 4:
	case 5:
	case 6: return "Spring";
	case 7:
	case 8:
	case 9: return "Summer";
	case 10:
	case 11:
	case 12: return "Fall";
	}
}

function nextSeason( $month = null ){
	if ( is_null($month) ) {
		$month = (int)date("m");
	}

	$next = $month + 3;
	if ( $next > 12 ) {
		$next = $next - 12;
	}

	return $next;
}

function escapeLike($string){
	$search = array('%', '_');
	$replace   = array('\%', '\_');
	$string = str_replace($search, $replace, $string);
	$string = explode(" ", $string);
	return implode("%", $string);
}


function compare($str1, $str2) {
	$str1 = preg_replace("/[^a-z0-9]/", '', strtolower($str1));
	$str2 = preg_replace("/[^a-z0-9]/", '', strtolower($str2));

	if ( $str1 != $str2 ) {
		return false;
	}
	return true;
}

function getSeasonFromDate($aired_at) {
	if ( is_null($ared_at) ) {
		return array("name" => null, "year" => null);
	}

	$aired_from = Carbon::instance($aired_at);

	return array("name" => getSeason($aired_from->month), "year" => $aired_from->year);
}

function camo($url) {
	if ( "" != env("GOCAMO_HMAC") && "" != env("GOCAMO_URL") ) {
		$phpamo = new \WillWashburn\Phpamo\Phpamo(
			env("GOCAMO_HMAC"),
			env("GOCAMO_URL")
		);
		return $phpamo->camo($url);
	}

	return $url;
}