1
|
<?php
|
2
|
/**
|
3
|
* Created by PhpStorm.
|
4
|
* User: Zdenda
|
5
|
* Date: 22.4.2018
|
6
|
* Time: 20:21
|
7
|
*/
|
8
|
|
9
|
namespace App\Model;
|
10
|
|
11
|
use Illuminate\Support\Facades\DB;
|
12
|
|
13
|
/**
|
14
|
* Trida reprezentujici entitu zarizeni.
|
15
|
* @package App\Model
|
16
|
*/
|
17
|
class Zarizeni extends BaseModel
|
18
|
{
|
19
|
|
20
|
/**
|
21
|
* Jmeno tabulky v databazi.
|
22
|
*
|
23
|
* @var string
|
24
|
*/
|
25
|
protected $table = 'zarizeni';
|
26
|
|
27
|
/**
|
28
|
* Vrati vsechna zarizeni.
|
29
|
* Mesto a ulice jsou vraceny spolu se zarizenim.
|
30
|
*
|
31
|
* @return mixed
|
32
|
*/
|
33
|
public static function getAllJoinAddress()
|
34
|
{
|
35
|
return DB::table('zarizeni')
|
36
|
->join('ulice', 'zarizeni.ulice_id', '=', 'ulice.id')
|
37
|
->join('mesto', 'ulice.mesto_id', '=', 'mesto.id')
|
38
|
->select('zarizeni.id as id',
|
39
|
'zarizeni.smer_popis as name',
|
40
|
'ulice.nazev as street',
|
41
|
'ulice.id as street_id',
|
42
|
'ulice.zem_sirka as lat',
|
43
|
'ulice.zem_delka as lng',
|
44
|
'mesto.nazev as town',
|
45
|
'mesto.id as town_id')
|
46
|
->get();
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Vrati zarizeni nalezene podle adresy (mesto+ulice).
|
51
|
*
|
52
|
* @param $address Adresa, jsou vraceny zaznamy u kterych ulice, nebo mesto odpovida adrese.
|
53
|
* @param $showDirection 1 pokud má být rozlišen směr zařízení.
|
54
|
* @return mixed
|
55
|
*/
|
56
|
public static function findByAddressJoinAddress($address, $showDirection)
|
57
|
{
|
58
|
if (empty($address)) {
|
59
|
Zarizeni::getAllJoinAddress();
|
60
|
}
|
61
|
|
62
|
$query = DB::table('zarizeni')
|
63
|
->join('ulice', 'zarizeni.ulice_id', '=', 'ulice.id')
|
64
|
->join('mesto', 'ulice.mesto_id', '=', 'mesto.id')
|
65
|
->select(DB::Raw('zarizeni.id as id,
|
66
|
zarizeni.smer_popis as name,
|
67
|
ulice.nazev as street,
|
68
|
ulice.id as street_id,
|
69
|
ulice.zem_sirka as lat,
|
70
|
ulice.zem_delka as lng,
|
71
|
mesto.nazev as town,
|
72
|
mesto.id as town_id,' .
|
73
|
($showDirection ? 'zaznam_cas.smer as direction' : 'null as direction')));
|
74
|
|
75
|
if (!empty($address)) {
|
76
|
$query = $query->where('ulice.nazev', 'like', '%' . $address . '%')
|
77
|
->orWhere('mesto.nazev', 'like', '%' . $address . '%')
|
78
|
->orWhere('zarizeni.smer_popis', 'like', '%' . $address . '%');
|
79
|
}
|
80
|
|
81
|
if ($showDirection) {
|
82
|
$query = $query->join('zaznam_cas', 'zaznam_cas.zarizeni_id', '=', 'zarizeni.id')
|
83
|
->groupBy('zarizeni.id', 'zaznam_cas.smer');
|
84
|
}
|
85
|
|
86
|
return $query->get();
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Vrati zarizeni se zadanym id.
|
91
|
* Mesto a ulice jsou vraceny spolu se zarizenim.
|
92
|
*
|
93
|
* @param $id Id zarizeni.
|
94
|
* @return mixed
|
95
|
*/
|
96
|
public static function findByIdJoinAddress($id)
|
97
|
{
|
98
|
return DB::table('zarizeni')
|
99
|
->join('ulice', 'zarizeni.ulice_id', '=', 'ulice.id')
|
100
|
->join('mesto', 'ulice.mesto_id', '=', 'mesto.id')
|
101
|
->select('zarizeni.id as id',
|
102
|
'zarizeni.smer_popis as name',
|
103
|
'ulice.nazev as street',
|
104
|
'ulice.id as street_id',
|
105
|
'ulice.zem_sirka as lat',
|
106
|
'ulice.zem_delka as lng',
|
107
|
'mesto.nazev as town',
|
108
|
'mesto.id as town_id')
|
109
|
->where('zarizeni.id', '=', $id)
|
110
|
->orderBy('zarizeni.id')
|
111
|
->first();
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Databazovy klic.
|
116
|
*
|
117
|
* @var long
|
118
|
*/
|
119
|
public $id;
|
120
|
|
121
|
/**
|
122
|
* Popis smeru zarizeni.
|
123
|
*
|
124
|
* @var string
|
125
|
*/
|
126
|
public $smer_popis;
|
127
|
|
128
|
/**
|
129
|
* Stav zarizeni.
|
130
|
*
|
131
|
* @var integer
|
132
|
*/
|
133
|
public $stav;
|
134
|
|
135
|
public function ulice()
|
136
|
{
|
137
|
return $this->belongsTo('App\Model\Ulice');
|
138
|
}
|
139
|
}
|