/
Post.php
807 lines (694 loc) · 20.1 KB
/
Post.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
<?php defined('SYSPATH') OR die('No direct access allowed.');
/**
* Ushahidi Post Repository
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Application
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\FormAttributeRepository;
use Ushahidi\Core\Entity\FormStageRepository;
use Ushahidi\Core\Entity\Post;
use Ushahidi\Core\Entity\PostValueContainer;
use Ushahidi\Core\Entity\PostRepository;
use Ushahidi\Core\Entity\PostSearchData;
use Ushahidi\Core\Entity\UserRepository;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Usecase\Post\StatsPostRepository;
use Ushahidi\Core\Usecase\Post\UpdatePostRepository;
use Ushahidi\Core\Usecase\Post\UpdatePostTagRepository;
use Ushahidi\Core\Tool\JsonTranscode;
use Ushahidi\Core\Traits\UserContext;
use Aura\DI\InstanceFactory;
class Ushahidi_Repository_Post extends Ushahidi_Repository implements
PostRepository,
UpdatePostRepository
{
use UserContext;
protected $form_attribute_repo;
protected $form_stage_repo;
protected $post_value_factory;
protected $bounding_box_factory;
protected $tag_repo;
protected $include_value_types = [];
protected $include_attributes = [];
protected $json_transcoder;
protected $json_properties = ['published_to'];
public function setTranscoder(JsonTranscode $transcoder)
{
$this->json_transcoder = $transcoder;
}
/**
* Construct
* @param Database $db
* @param FormAttributeRepository $form_attribute_repo
* @param FormStageRepository $form_stage_repo
* @param Ushahidi_Repository_PostValueFactory $post_value_factory
* @param Aura\DI\InstanceFactory $bounding_box_factory
*/
public function __construct(
Database $db,
FormAttributeRepository $form_attribute_repo,
FormStageRepository $form_stage_repo,
Ushahidi_Repository_PostValueFactory $post_value_factory,
InstanceFactory $bounding_box_factory,
UpdatePostTagRepository $tag_repo
)
{
parent::__construct($db);
$this->form_attribute_repo = $form_attribute_repo;
$this->form_stage_repo = $form_stage_repo;
$this->post_value_factory = $post_value_factory;
$this->bounding_box_factory = $bounding_box_factory;
$this->tag_repo = $tag_repo;
}
// Ushahidi_Repository
protected function getTable()
{
return 'posts';
}
// Ushahidi_Repository
public function getEntity(Array $data = null)
{
if (!empty($data['id']))
{
$data += [
'values' => $this->getPostValues($data['id']),
'tags' => $this->getTagsForPost($data['id']),
'completed_stages' => $this->getCompletedStagesForPost($data['id']),
];
}
return new Post($data);
}
// Override selectQuery to fetch 'value' from db as text
protected function selectQuery(Array $where = [])
{
$query = parent::selectQuery($where);
// Join to messages and load message id
$query->join('messages', 'LEFT')->on('posts.id', '=', 'messages.post_id')
->select(['messages.id', 'message_id'], ['messages.type', 'source']);
return $query;
}
protected function getPostValues($id)
{
// Get all the values for the post. These are the EAV values.
$values = $this->post_value_factory
->proxy($this->include_value_types)
->getAllForPost($id, $this->include_attributes);
$output = [];
foreach ($values as $value) {
if (empty($output[$value->key])) {
$output[$value->key] = [];
}
if ($value->value !== NULL) {
$output[$value->key][] = $value->value;
}
}
return $output;
}
protected function getCompletedStagesForPost($id)
{
$result = DB::select('form_stage_id', 'completed')
->from('form_stages_posts')
->where('post_id', '=', $id)
->where('completed', '=', 1)
->execute($this->db);
return $result->as_array(NULL, 'form_stage_id');
}
// Ushahidi_Repository
public function getSearchFields()
{
return [
'status', 'type', 'locale', 'slug', 'user',
'parent', 'form', 'set', 'q', /* LIKE title, content */
'created_before', 'created_after',
'updated_before', 'updated_after',
'bbox', 'tags', 'values',
'center_point', 'within_km',
'include_types', 'include_attributes', // Specify values to include
'group_by', 'group_by_tags', 'group_by_attribute_key', // Group results
'timeline', 'timeline_interval', 'timeline_attribute' // Timeline params
];
}
// Ushahidi_Repository
protected function setSearchConditions(SearchData $search)
{
if ($search->include_types)
{
if (is_array($search->include_types))
{
$this->include_value_types = $search->include_types;
}
else
{
$this->include_value_types = explode(',', $search->include_types);
}
}
if ($search->include_attributes)
{
if (is_array($search->include_attributes))
{
$this->include_attributes = $search->include_attributes;
}
else
{
$this->include_attributes = explode(',', $search->include_attributes);
}
}
$query = $this->search_query;
$table = $this->getTable();
$status = $search->getFilter('status', 'published');
if ($status !== 'all')
{
$query->where("$table.status", '=', $status);
}
foreach (['type', 'locale', 'slug'] as $key)
{
if ($search->$key)
{
$query->where("$table.$key", '=', $search->$key);
}
}
// If user = me, replace with current user id
if ($search->user === 'me')
{
$search->user = $this->getUserId();
}
foreach (['user', 'parent', 'form'] as $key)
{
if (isset($search->$key))
{
// Special case: empty search string looks for null
if (empty($search->$key))
{
$query->where("$table.{$key}_id", "IS", NULL);
}
else
{
// Make sure we have an array
if (!is_array($search->$key)) {
$search->$key = explode(',', $search->$key);
}
$query->where("$table.{$key}_id", 'IN', $search->$key);
}
}
}
if ($search->q)
{
// search terms are all wrapped as a series of OR conditions
$query->and_where_open();
if (ctype_digit($search->q)) {
// possibly searching for a specific id
$query->or_where('id', '=', $search->q);
}
// or possible text searching in title / content
$query
->where("$table.title", 'LIKE', "%$search->q%")
->or_where("$table.content", 'LIKE', "%$search->q%");
$query->and_where_close();
}
// date chcks
if ($search->created_after)
{
$created_after = strtotime($search->created_after);
$query->where("$table.created", '>=', $created_after);
}
if ($search->created_before)
{
$created_before = strtotime($search->created_before);
$query->where("$table.created", '<=', $created_before);
}
if ($search->updated_after)
{
$updated_after = strtotime($search->updated_after);
$query->where("$table.updated", '>=', $updated_after);
}
if ($search->updated_before)
{
$updated_before = strtotime($search->updated_before);
$query->where("$table.updated", '<=', $updated_before);
}
// Bounding box search
// Create geometry from bbox (or create bbox from center & radius)
$bounding_box = null;
if ($search->bbox) {
$bounding_box = $this->createBoundingBoxFromCSV($search->bbox);
} else if ($search->center_point && $search->within_km) {
$bounding_box = $this->createBoundingBoxFromCenter(
$search->center_point, $search->within_km
);
}
if ($bounding_box) {
$query
->join([
$this->getBoundingBoxSubquery($bounding_box), 'Filter_BBox'
], 'INNER')
->on('posts.id', '=', 'Filter_BBox.post_id')
;
}
// Filter by tag
if ($search->tags)
{
if (isset($search->tags['any']))
{
$tags = $search->tags['any'];
if (!is_array($tags)) {
$tags = explode(',', $tags);
}
$query
->join('posts_tags')->on('posts.id', '=', 'posts_tags.post_id')
->where('tag_id', 'IN', $tags);
}
elseif (isset($search->tags['all']))
{
$tags = $search->tags['all'];
if (!is_array($tags)) {
$tags = explode(',', $tags);
}
foreach ($tags as $tag)
{
$sub = DB::select('post_id')
->from('posts_tags')
->where('tag_id', '=', $tag);
$query
->where('posts.id', 'IN', $sub);
}
}
else
{
$tags = $search->tags;
if (!is_array($tags)) {
$tags = explode(',', $tags);
}
$query
->join('posts_tags')->on('posts.id', '=', 'posts_tags.post_id')
->where('tag_id', 'IN', $tags);
}
}
// Filter by set
if ($search->set)
{
$query
->join('posts_sets', 'INNER')->on('posts.id', '=', 'posts_sets.post_id')
->where('posts_sets.set_id', '=', $search->set);
}
// Attributes
if ($search->values)
{
foreach ($search->values as $key => $value)
{
$attribute = $this->form_attribute_repo->getByKey($key);
$sub = $this->post_value_factory
->getRepo($attribute->type)
->getValueQuery($attribute->id, $value);
$query
->join([$sub, 'Filter_'.ucfirst($key)], 'INNER')
->on('posts.id', '=', 'Filter_'.ucfirst($key).'.post_id');
}
}
}
// SearchRepository
public function getSearchTotal()
{
// Assume we can simply count the results to get a total
$query = $this->getSearchQuery(true)
->select([DB::expr('COUNT(DISTINCT posts.id)'), 'total']);
// Fetch the result and...
$result = $query->execute($this->db);
// ... return the total.
return (int) $result->get('total', 0);
}
// StatsPostRepository
public function getGroupedTotals(SearchData $search)
{
// Create a new query to select posts count
$this->search_query = DB::select([DB::expr('COUNT(DISTINCT posts.id)'), 'total'])
->from($this->getTable());
// Quick hack to ensure all posts are available to
// group_by=status
if ($search->group_by === 'status' && ! $search->status) {
$search->status = 'all';
}
// Set filters
// Note: we're calling setSearchConditions, not setSearchParams
// because we don't want to set sorting params
$this->setSearchConditions($search);
// Group by time-intervals
if ($search->timeline)
{
// Default to posts created
$time_field = 'posts.created';
if ($search->timeline_attribute === 'created' || $search->timeline_attribute == 'updated')
{
// Assumed created / updated means the builtin posts created/updated times
$time_field = 'posts.' . $search->timeline_attribute;
}
elseif ($search->timeline_attribute)
{
// Find the attribute
$key = $search->timeline_attribute;
$attribute = $this->form_attribute_repo->getByKey($key);
if ($attribute)
{
// Get the post_TYPE table.
$sub = $this->post_value_factory
->getRepo($attribute->type)
->getValueTable();
// Join to attribute
$this->search_query
->join([$sub, 'Time_'.ucfirst($key)], 'INNER')
->on('form_attribute_id', '=', DB::expr($attribute->id))
->on('posts.id', '=', 'Time_'.ucfirst($key).'.post_id');
// Use the attribute `value` as our time
$time_field = 'Time_'.ucfirst($key).'.value';
}
}
$this->search_query
->select([
DB::expr('FLOOR('.$time_field.'/:interval)*:interval', [':interval' => (int)$search->getFilter('timeline_interval', 86400)]),
'time_label'
])
->group_by('time_label');
}
// Group by attribute
if ($search->group_by === 'attribute' AND $search->group_by_attribute_key)
{
$key = $search->group_by_attribute_key;
$attribute = $this->form_attribute_repo->getByKey($key);
if ($attribute)
{
$sub = $this->post_value_factory
->getRepo($attribute->type)
->getValueTable();
$this->search_query
->join([$sub, 'Group_'.ucfirst($key)], 'INNER')
->on('form_attribute_id', '=', DB::expr($attribute->id))
->on('posts.id', '=', 'Group_'.ucfirst($key).'.post_id')
->select(['Group_'.ucfirst($key).'.value', 'label'])
->group_by('label');
}
}
// Group by status
elseif ($search->group_by === 'status')
{
$this->search_query
->select(['posts.status', 'label'])
->group_by('label');
}
// Group by form
elseif ($search->group_by === 'form')
{
$this->search_query
->join('forms')->on('posts.form_id', '=', 'forms.id')
->select(['forms.name', 'label'])
->group_by('posts.form_id');
}
// Group by tags
elseif ($search->group_by === 'tags')
{
/**
* The output query looks something like
* SELECT
* `parents`.`tag` AS `label`,
* COUNT(DISTINCT posts.id) AS `total`
* FROM `posts`
* JOIN `posts_tags` ON (`posts`.`id` = `posts_tags`.`post_id`)
* JOIN `tags` ON (`posts_tags`.`tag_id` = `tags`.`id`)
* JOIN `tags` as `parents` ON (`parents`.`id` = `tags`.`parent_id` OR `parents`.`id` = `posts_tags`.`tag_id`)
* WHERE `status` = 'published' AND `posts`.`type` = 'report'
* AND `parents`.`parent_id` IS NULL
* GROUP BY `parents`.`id`
*/
// Count by tag but also include child counts in the parent count
$this->search_query
->join('posts_tags')->on('posts.id', '=', 'posts_tags.post_id')
->join('tags')->on('posts_tags.tag_id', '=', 'tags.id')
->join(['tags', 'parents'])
// Slight hack to avoid kohana db forcing multiple ON clauses to use AND not OR.
->on(DB::expr("`parents`.`id` = `tags`.`parent_id` OR `parents`.`id` = `posts_tags`.`tag_id`"), '', DB::expr(""))
->select(['parents.tag', 'label'])
->group_by('parents.id');
// Limit tags to a top level, or a specific parent.
if ($search->group_by_tags !== 'all') {
$this->search_query
->where('parents.parent_id', '=', $search->getFilter('group_by_tags', null));
}
}
// If no group_by just count all posts
else {
$this->search_query
->select([DB::expr('"all"'), 'label']);
}
// .. Add orderby time *after* order by groups
if ($search->timeline)
{
// Order by label, then time
$this->search_query->order_by('label');
$this->search_query->order_by('time_label');
}
else {
// Order by count, then label
$this->search_query->order_by('total', 'DESC');
$this->search_query->order_by('label');
}
// Fetch the results and...
$results = $this->search_query->execute($this->db);
// ... return them as an array
return $results->as_array();
}
// PostRepository
public function getByIdAndParent($id, $parent_id, $type)
{
return $this->getEntity($this->selectOne([
'posts.id' => $id,
'posts.parent_id' => $parent_id,
'posts.type' => $type
]));
}
// PostRepository
public function getByLocale($locale, $parent_id, $type)
{
return $this->getEntity($this->selectOne([
'posts.locale' => $locale,
'posts.parent_id' => $parent_id,
'posts.type' => $type
]));
}
/**
* Return a Bounding Box given a CSV of west,north,east,south points
*
* @param string $csv 'west,north,east,south'
* @return Util_BoundingBox
*/
private function createBoundingBoxFromCSV($csv)
{
list($bb_west, $bb_north, $bb_east, $bb_south)
= array_map('floatval', explode(',', $csv))
;
$bounding_box_factory = $this->bounding_box_factory;
return $bounding_box_factory($bb_west, $bb_north, $bb_east, $bb_south);
}
private function createBoundingBoxFromCenter($center, $within_km = 0)
{
// if a $center point and $within_km distance was given,
// create a bounding box that matches those conditions.
$center_point = explode(',', $center);
$center_lat = $center_point[0];
$center_lon = $center_point[1];
$bounding_box_factory = $this->bounding_box_factory;
$bounding_box = $bounding_box_factory(
$center_lon, $center_lat, $center_lon, $center_lat
);
if ($within_km) {
$bounding_box->expandByKilometers($within_km);
}
return $bounding_box;
}
/**
* Get a subquery to return post_point entries within a bounding box
* @param string $bbox Bounding box
* @return Database_Query
*/
private function getBoundingBoxSubquery(Util_BoundingBox $bounding_box)
{
return DB::select('post_id')
->from('post_point')
->where(
DB::expr(
'CONTAINS(GeomFromText(:bounds), value)',
[':bounds' => $bounding_box->toWKT()]
),
'=',
1
);
}
/**
* Get tags for a post
* @param int $id post id
* @return array tag ids for post
*/
private function getTagsForPost($id)
{
$result = DB::select('tag_id')->from('posts_tags')
->where('post_id', '=', $id)
->execute($this->db);
return $result->as_array(NULL, 'tag_id');
}
// UpdatePostRepository
public function isSlugAvailable($slug)
{
return $this->selectCount(compact('slug')) === 0;
}
// UpdatePostRepository
public function doesTranslationExist($locale, $parent_id, $type)
{
// If this isn't a translation of an existing post, skip
if ($type != 'translation')
{
return TRUE;
}
// Is locale the same as parent?
$parent = $this->get($parent_id);
if ($parent->locale === $locale)
{
return FALSE;
}
// Check for other translations
return $this->selectCount([
'posts.type' => 'translation',
'posts.parent_id' => $parent_id,
'posts.locale' => $locale
]) === 0;
}
// UpdateRepository
public function create(Entity $entity)
{
$post = array_filter($this->json_transcoder->encode(
$entity->asArray(),
$this->json_properties
));
$post['created'] = time();
// Remove attribute values and tags
unset($post['values'], $post['tags'], $post['completed_stages']);
// Create the post
$id = $this->executeInsert($this->removeNullValues($post));
if ($entity->tags)
{
// Update post-tags
$this->updatePostTags($id, $entity->tags);
}
if ($entity->values)
{
// Update post-values
$this->updatePostValues($id, $entity->values);
}
if ($entity->completed_stages)
{
// Update post-stages
$this->updatePostStages($id, $entity->form_id, $entity->completed_stages);
}
return $id;
}
// UpdateRepository
public function update(Entity $entity)
{
$post = $this->json_transcoder->encode(
$entity->getChanged(),
$this->json_properties
);
$post['updated'] = time();
// Remove attribute values and tags
unset($post['values'], $post['tags'], $post['completed_stages']);
// Update the post
$count = $this->executeUpdate(['id' => $entity->id], $post);
if ($entity->tags)
{
// Update post-tags
$this->updatePostTags($entity->id, $entity->tags);
}
if ($entity->values)
{
// Update post-values
$this->updatePostValues($entity->id, $entity->values);
}
if ($entity->completed_stages)
{
// Update post-stages
$this->updatePostStages($id, $entity->form_id, $entity->completed_stages);
}
return $count;
}
protected function updatePostValues($post_id, $attributes)
{
$this->post_value_factory->proxy()->deleteAllForPost($post_id);
foreach ($attributes as $key => $values)
{
$attribute = $this->form_attribute_repo->getByKey($key);
$repo = $this->post_value_factory->getRepo($attribute->type);
foreach ($values as $val)
{
$repo->createValue($val, $attribute->id, $post_id);
}
}
}
protected function updatePostTags($post_id, $tags)
{
// Load existing tags
$existing = $this->getTagsForPost($post_id);
$insert = DB::insert('posts_tags', ['post_id', 'tag_id']);
$tag_ids = [];
$new_tags = FALSE;
foreach ($tags as $tag)
{
// Find the tag by id or name
// @todo this should happen before we even get here
$tag_entity = $this->tag_repo->getByTag($tag);
if (! $tag_entity->id)
{
$tag_entity = $this->tag_repo->get($tag);
}
// Does the post already have this tag?
if (! in_array($tag_entity->id, $existing))
{
// Add to insert query
$insert->values([$post_id, $tag_entity->id]);
$new_tags = TRUE;
}
$tag_ids[] = $tag_entity->id;
}
// Save
if ($new_tags)
{
$insert->execute($this->db);
}
// Remove any other tags
if (! empty($tag_ids))
{
DB::delete('posts_tags')
->where('tag_id', 'NOT IN', $tag_ids)
->execute($this->db);
}
}
protected function updatePostStages($post_id, $form_id, $completed_stages)
{
// Remove any existing entries
DB::delete('form_stages_posts')
->where('post_id', '=', $post_id)
->execute($this->db);
$insert = DB::insert('form_stages_posts', ['form_stage_id', 'post_id', 'completed']);
// Get all stages for form
$form_stages = $this->form_stage_repo->getByForm($form_id);
foreach ($form_stages as $stage)
{
$insert->values([
$stage->id,
$post_id,
in_array($stage->id, $completed_stages) ? 1 : 0
]);
}
// Execute the insert
$insert->execute($this->db);
}
}