Skip to content

Commit

Permalink
Merge branch 'develop' into 1550-platform-api-receive
Browse files Browse the repository at this point in the history
  • Loading branch information
willdoran committed Jun 1, 2017
2 parents 07c6be3 + 982e69b commit db4d4a6
Show file tree
Hide file tree
Showing 34 changed files with 834 additions and 359 deletions.
4 changes: 2 additions & 2 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
This pull request makes the following changes:
-

Test these changes by:
-
Test checklist:
- [ ]

Fixes ushahidi/platform# .

Expand Down
14 changes: 12 additions & 2 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,7 @@ public static function init()
'form_stage_repo' => $di->lazyGet('repository.form_stage'),
'form_repo' => $di->lazyGet('repository.form'),
'post_value_factory' => $di->lazyGet('repository.post_value_factory'),
'bounding_box_factory' => $di->newFactory('Util_BoundingBox'),
'tag_repo' => $di->lazyGet('repository.tag')
'bounding_box_factory' => $di->newFactory('Util_BoundingBox')
];

$di->set('repository.post.datetime', $di->lazyNew('Ushahidi_Repository_Post_Datetime'));
Expand All @@ -473,6 +472,11 @@ public static function init()
$di->set('repository.post.markdown', $di->lazyNew('Ushahidi_Repository_Post_Markdown'));
$di->set('repository.post.title', $di->lazyNew('Ushahidi_Repository_Post_Title'));
$di->set('repository.post.media', $di->lazyNew('Ushahidi_Repository_Post_Media'));
$di->set('repository.post.tags', $di->lazyNew('Ushahidi_Repository_Post_Tags'));

$di->params['Ushahidi_Repository_Post_Tags'] = [
'tag_repo' => $di->lazyGet('repository.tag')
];

// The post value repo factory
$di->set('repository.post_value_factory', $di->lazyNew('Ushahidi_Repository_Post_ValueFactory'));
Expand All @@ -491,6 +495,7 @@ public static function init()
'markdown' => $di->lazyGet('repository.post.markdown'),
'title' => $di->lazyGet('repository.post.title'),
'media' => $di->lazyGet('repository.post.media'),
'tags' => $di->lazyGet('repository.post.tags'),
],
];

Expand Down Expand Up @@ -629,6 +634,10 @@ public static function init()
$di->params['Ushahidi_Validator_Post_Media'] = [
'media_repo' => $di->lazyGet('repository.media')
];
$di->set('validator.post.tags', $di->lazyNew('Ushahidi_Validator_Post_Tags'));
$di->params['Ushahidi_Validator_Post_Tags'] = [
'tags_repo' => $di->lazyGet('repository.tag')
];


$di->set('validator.post.value_factory', $di->lazyNew('Ushahidi_Validator_Post_ValueFactory'));
Expand All @@ -647,6 +656,7 @@ public static function init()
'title' => $di->lazyGet('validator.post.title'),
'media' => $di->lazyGet('validator.post.media'),
'video' => $di->lazyGet('validator.post.video'),
'tags' => $di->lazyGet('validator.post.tags'),
],
];

Expand Down
2 changes: 1 addition & 1 deletion application/classes/Ushahidi/Formatter/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function format_updated($value)
protected function get_relation($resource, $id)
{
return !$id ? NULL : [
'id' => $id,
'id' => intval($id),
'url' => URL::site(Ushahidi_Rest::url($resource, $id), Request::current()),
];
}
Expand Down
11 changes: 11 additions & 0 deletions application/classes/Ushahidi/Formatter/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ protected function format_color($value)
$value = ltrim($value, '#');
return $value ? '#' . $value : null;
}

protected function format_tags($tags)
{
$output = [];
foreach ($tags as $tagid)
{
$output[] = $this->get_relation('tags', $tagid);
}

return $output;
}
}
14 changes: 9 additions & 5 deletions application/classes/Ushahidi/Formatter/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ protected function format_color($value)
return $value ? '#' . $value : null;
}

protected function format_forms($forms)
protected function format_children($tags)
{
$output = [];
foreach ($forms as $formid)
{
$output[] = $this->get_relation('forms', $formid);

if (is_array($tags)) {
foreach ($tags as $tagid)
{
$output[] = $this->get_relation('tags', $tagid);
//$output[] = intval($tagid);
}
}

return $output;
}
}
131 changes: 36 additions & 95 deletions application/classes/Ushahidi/FormsTagsTrait.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php defined('SYSPATH') OR die('No direct script access.');
/**

/**
* Ushahidi FormsTags Repo Trait
* Helps Forms and Tags-repository use the same methods
** @author Ushahidi Team <team@ushahidi.com>
Expand All @@ -11,111 +11,52 @@

trait Ushahidi_FormsTagsTrait
{
//returning forms for a specific Tag-id
private function getFormsForTag($id)
{
$result = DB::select('form_id')
->from('forms_tags')
->where('tag_id', '=', $id)
->execute($this->db);
return $result->as_array(NULL, 'form_id');
}
//returning tags for a specific Form-id
private function getTagsForForm($id)
{
$result = DB::select('tag_id')
->from('forms_tags')
$attributes = DB::select('form_attributes.options')
->from('form_attributes')
->join('form_stages')->on('form_stage_id', '=', 'form_stages.id')
->join('forms')->on('form_id', '=', 'forms.id')
->where('form_id', '=', $id)
->execute($this->db);
return $result->as_array(NULL, 'tag_id');
}

// updating/adding tags to a form
private function updateFormsTags($form_id, $tags)
{
if (!$tags) {
DB::delete('forms_tags')
->where('form_id', '=', $form_id)
->execute($this->db);
} else if ($tags) {
$existing = $this->getTagsForForm($form_id);
$insert = DB::insert('forms_tags', ['form_id', 'tag_id']);
$tag_ids = [];
$new_tags = FALSE;
foreach ($tags as $tag) {
if (!in_array($tag, $existing)) {
$insert->values([$form_id, $tag]);
$new_tags = TRUE;
}
$tag_ids[] = $tag;
}
if ($new_tags) {
$insert->execute($this->db);
}
if (!empty($tag_ids)) {
DB::delete('forms_tags')
->where('tag_id', 'NOT IN', $tag_ids)
->and_where('form_id', '=', $form_id)
->execute($this->db);
}
}
}
->where('form_attributes.type', '=', 'tags')
->execute($this->db)
->as_array();

//updating/adding forms to a tag
private function updateTagForms($tag_id, $forms)
{
if (empty($forms)) {
DB::delete('forms_tags')
->where('tag_id', '=', $tag_id)
->execute($this->db);
} else {
$existing = $this->getFormsForTag($tag_id);
$insert = DB::insert('forms_tags', ['form_id', 'tag_id']);
$form_ids = [];
$new_forms = FALSE;
foreach ($forms as $form) {
if (isset($form['id'])) {
$id = $form['id'];
} else {
$id = $form;
}
if (!in_array($form, $existing)) {
$insert->values([$id, $tag_id]);
$new_forms = TRUE;
}
$form_ids[] = $id;
}

if ($new_forms) {
$insert->execute($this->db);
}

if (!empty($form_ids)) {
DB::delete('forms_tags')
->where('form_id', 'NOT IN', $form_ids)
->and_where('tag_id', '=', $tag_id)
->execute($this->db);
$tags = [];
// Combine all tag ids into 1 array
foreach ($attributes as $attr) {
$options = json_decode($attr['options'], TRUE);
if (is_array($options)) {
$tags = array_merge($tags, $options);
}
}

return $tags;
}

private function updateFormAttributes($id)
private function removeTagFromAttributeOptions($id)
{
// Grab all tags attributes
$attr = DB::select('id', 'options')
->from('form_attributes')
->where('input', '=', 'tags')
->execute($this->db)
->as_array('id', 'options');
foreach ($attr as $attr_id => $value) {
$value = json_decode($value);
if (in_array($id, $value)) {
$index = array_search($id, $value);
array_splice($value, $index, 1);
$value = json_encode($value);
->from('form_attributes')
->where('type', '=', 'tags')
->execute($this->db)
->as_array('id', 'options');

foreach ($attr as $attr_id => $options) {
$options = json_decode($options, TRUE);
if (is_array($options) && in_array($id, $options)) {
// Remove $id from options array
$index = array_search($id, $options);
array_splice($options, $index, 1);
$options = json_encode($options);

// Save it
DB::update('form_attributes')
->set(array('options' => $value))
->where('id', '=', $attr_id)
->execute($this->db);
->set(array('options' => $options))
->where('id', '=', $attr_id)
->execute($this->db);
}
}
}
Expand Down
29 changes: 9 additions & 20 deletions application/classes/Ushahidi/Repository/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public function getEntity(Array $data = null)
if (isset($data["id"])) {
$can_create = $this->getRolesThatCanCreatePosts($data['id']);
$data = $data + [
'can_create' => $can_create['roles'],
'can_create' => $can_create['roles'],
'tags' => $this->getTagsForForm($data['id'])
];
$data['tags'] = $this->getTagsForForm($data['id']);
}
}
return new Form($data);
}

Expand All @@ -68,38 +68,27 @@ protected function setSearchConditions(SearchData $search)
// CreateRepository
public function create(Entity $entity)
{

$tags = $entity->tags;
unset($entity->tags);
$id = parent::create($entity->setState(['created' => time()]));
//updating forms_tags-table
if ($tags && $id !== null) {
$this->updateFormsTags($id, $tags);
}
// todo ensure default group is created
return $id;
}

// UpdateRepository
public function update(Entity $entity)
{

// If orignal Form update Intercom if Name changed
if ($entity->id === 1) {
foreach ($entity->getChanged() as $key => $val) {
$user = service('session.user');
$key === 'name' ? $this->emit($this->event, $user->email, ['primary_survey_name' => $val]) : null;
}
}

$tags = $entity->tags;
unset($entity->tags);
unset($entity->children);
$id = parent::update($entity->setState(['updated' => time()]));
// updating forms_tags-table
if ($tags && $entity->id !== null) {
$this->updateFormsTags($entity->id, $tags);
}
$form = $entity->getChanged();
$form['updated'] = time();
// removing tags from form before saving
unset($form['tags']);
// Finally save the form
$id = $this->executeUpdate(['id'=>$entity->id], $form);

return $id;
}
Expand Down
Loading

0 comments on commit db4d4a6

Please sign in to comment.