/
ObjectController.php
189 lines (149 loc) · 7.19 KB
/
ObjectController.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php namespace App\Http\Controllers;
use App\Object;
use App\Http\Controllers\Controller;
use App\ObjectMeta;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class ObjectController extends Controller {
private function processObject(&$object) {
if ($featuredImageId = ObjectMeta::getValue($object->id, '_featured_image')) {
$object->featured_image = Url('/uploads/' . getImageSrc($featuredImageId, 'medium'));
}
if ($contentImageId = ObjectMeta::getValue($object->id, '_content_image')) {
$object->content_image = Url('/uploads/' . getImageSrc($contentImageId, 'medium'));
}
$object->excerpt = htmlentities( strlen($object->excerpt) > 50 ? substr($object->excerpt, 1, 50) . '...' : $object->excerpt );
$object->phone = ObjectMeta::getValue($object->id, '_field_phone' );
$object->email = ObjectMeta::getValue($object->id, '_field_email' );
$object->occupation= ObjectMeta::getValue($object->id, '_field_occupation' );
$object->address= ObjectMeta::getValue($object->id, '_field_address' );
$object->address_location_g= ObjectMeta::getValue($object->id, '_field_address-location-g' );
$object->address_location_k= ObjectMeta::getValue($object->id, '_field_address-location-k' );
$object->address_street= ObjectMeta::getValue($object->id, '_field_address-address' );
$object->address_city= ObjectMeta::getValue($object->id, '_field_address-city' );
$object->address_country= ObjectMeta::getValue($object->id, '_field_address-country' );
if ( $french_speakers = ObjectMeta::getValue($object->id, '_field_french_speakers' ) ) {
$object->french_speakers= 'Franchophone';
}
$object->promoted= ObjectMeta::getValue($object->id, '_field_promoted' );
}
public function get($id) {
if ( $object = Object::select( array( 'objects.id', 'objects.name', 'objects.title', 'objects.excerpt' ) )
->where('id', $id)
->first() ) {
$this->processObject($object);
return $object;
}
}
public function getSearch() {
$categoryId = Input::get('categoryid');
$index = Input::get('index') ?: 0;
$search = Input::get('query');
$objects = null;
if ( $categoryId ) {
// Category Image
if ($categoryFeaturedImageId = ObjectMeta::getValue($categoryId, '_featured_image')) {
$featuredImageUrl = getImageSrc($categoryFeaturedImageId, 'thumbnail');
}
// Get objects where in category
$objects = Object::Where('type', 'object_type')
->whereExists(function ( $query ) use ( $categoryId ) {
$query->select(DB::raw(1))
->from('object_meta')
->whereRaw(DB::getTablePrefix() . 'object_meta.object_id = ' . DB::getTablePrefix() . 'objects.id')
->where('meta_key', '_category_id')
->where('meta_value', $categoryId);
})
->select(DB::raw('substr(name, 14) as field_name'))
->get()
->toArray();
$types = array_map(function($v) {
return $v['field_name'];
}, $objects);
$objects = DB::table('objects')
->whereIn('type', $types)
->select( array( 'objects.id', DB::raw( '"/uploads/'. $featuredImageUrl . '"' . ' as featured_image'), 'objects.name', 'objects.title', 'objects.excerpt' ) );
$objects = DB::table('objects')
->whereIn('type', $types)
->select( array( 'objects.id', DB::raw( '"/uploads/'. $featuredImageUrl . '"' . ' as featured_image'), 'objects.name', 'objects.title', 'objects.excerpt' ) )
->whereExists(function ( $query ) {
$query->select(DB::raw(1))
->from('object_meta')
->whereRaw(DB::getTablePrefix() . 'object_meta.object_id = ' . DB::getTablePrefix() . 'objects.id')
->where('meta_key', '_field_promoted')
->where('meta_value', '1');
})->
union($objects);
} else {
if ( $search ) {
$objects = Object::whereNotIn('type', ['object_type', 'image'])
->where(function( $query ) use ( $search ) {
$query->where('title', 'LIKE', '%' . $search . '%')
->orWhere('name', 'LIKE', '%' . $search . '%');
})
->select('id', 'type', 'title')
->get();
return response( $objects );
}
}
if ( $objects ) {
$objects = $objects
->skip($index)
->take(50)
->get();
foreach ($objects as $object) {
$this->processObject($object);
}
}
return $objects;
}
public function getLocations() {
$results = $this->getSearch();
$data = array();
$locations = array(
'type' => 1,
'userMsg' => false
);
if ( !empty( $results ) ) {
foreach ( $results as $result ) {
if ( $lat = ObjectMeta::getValue($result->id, '_field_address-location-g') ) {
if ( $long = ObjectMeta::getValue($result->id, '_field_address-location-k' ) ) {
$location = array(
'geo_latitude' => $lat,
'geo_longitude' => $long,
'location' => '',
'title' => $result->title,
'excerpt' => $result->excerpt
);
if ($contentImageId = ObjectMeta::getValue($result->id, '_content_image')) {
$location['content_image'] = Url('/uploads/' . getImageSrc($contentImageId, 'medium'));
} else {
$location['content_image'] = '';
}
$location['address_street']= ObjectMeta::getValue($result->id, '_field_address-address' );
$location['address_city']= ObjectMeta::getValue($result->id, '_field_address-city' );
$location['address_country']= ObjectMeta::getValue($result->id, '_field_address-country' );
$location['promoted'] = ObjectMeta::getValue($result->id, '_field_promoted' );
$data[] = $location;
}
}
}
}
$locations['data'] = $data;
return $locations;
//return view('partials.map', compact( 'locations' ));
}
public function getContent($id) {
if ($id) {
if ($object = Object::find($id)) {
return $object->content;
}
}
}
public function getMap() {
if ( $queryString = $_SERVER['QUERY_STRING'] ) {
$criteria = '?' . $queryString;
}
return view('partials.map', compact( 'criteria' ));
}
}