summaryrefslogtreecommitdiff
path: root/app/Libraries/Helper.php
blob: a5556fd6211615daa755e96e79d0e89a26434ab3 (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
<?php
namespace App\Libraries;
use App\Libraries\Helper;
use App\Cocktail;
use App\Ingredient;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;

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

	public static function formatTimestamp($timestamp) {
		return Carbon::createFromTimestamp($timestamp)->format("Y-m-d");
	}

	public static function contains($array, $str) {
		foreach($array as $item) {
			if ( str_contains(strtolower($str), strtolower($item) ) ) {
				return true;
			}
		}
		return false;
	}


    	public static function getAngeboteData() {
		    $views = array("whisky", "wodka", "gin", "rum", "misc");
		    $query = "";

		    foreach($views as $view) {
			if ($query != "") {
				$query .= " UNION ";
			}
			$query .= "
				(SELECT
					name,
					image_url,
					spirit_type,
					spirit_type AS url,
					spirit_type AS angebotsname,
					(SELECT MAX(procent) FROM " . $view . "_view) as procent,
					'' AS linktext FROM ". $view ."_view
				WHERE
					original_price > 29.98
				AND
					spirit_type != 'Verschiedenes'
				AND
					shop != 'Rum & Co'
				AND
					shop = 'Drankdozijn'
				ORDER BY
					created_at DESC,
					procent DESC LIMIT 1)";
		    }

		    $data = DB::select($query);

		    return $data;
        }

	public static function getAngeboteCount() {
		return DB::select("SELECT count(*) as count FROM all_view")[0];
	}

	public static function getCocktailCount() {
		    $cocktails = new Cocktail;
		    return $cocktails->count();
	}

	public static function fillCache(){
		Cache::put('angebote_count', Helper::getAngeboteCount(), 6000); // 100 minutes
		Cache::put('cocktail_count', Helper::getCocktailCount(), 6000); // 100 minutes
		Cache::put('angebote_index', Helper::getAngeboteData(), 6000); // 100 minutes
	}
}