Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to improved categories #1747

Merged
merged 9 commits into from
May 25, 2017
7 changes: 7 additions & 0 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ 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'));

// The post value repo factory
$di->set('repository.post_value_factory', $di->lazyNew('Ushahidi_Repository_Post_ValueFactory'));
Expand All @@ -483,6 +484,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 @@ -621,6 +623,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'] = [
'media_repo' => $di->lazyGet('repository.tags')
];


$di->set('validator.post.value_factory', $di->lazyNew('Ushahidi_Validator_Post_ValueFactory'));
Expand All @@ -639,6 +645,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
23 changes: 6 additions & 17 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,22 +68,14 @@ 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) {
Expand All @@ -92,14 +84,11 @@ public function update(Entity $entity)
}
}

$tags = $entity->tags;
unset($entity->tags);
// Remove children before saving
unset($entity->children);

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

return $id;
}
Expand Down
Loading