Skip to content

Commit

Permalink
Stop limiting tags by form, just limit tags per attribute
Browse files Browse the repository at this point in the history
- Stop using forms_tags table
- Just use form_attribute.options instead
  • Loading branch information
rjmackay committed May 25, 2017
1 parent 2c5e3af commit 38ea41a
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 192 deletions.
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 <[email protected]>
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
107 changes: 49 additions & 58 deletions application/classes/Ushahidi/Repository/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,17 @@ public function getEntity(Array $data = null)
{
if (!empty($data['id']))
{
$data['forms'] = $this->getFormsForTag($data['id']);

if(empty($data['parent_id'])) {

$data['children'] =
DB::select('id')
->from('tags')
->where('parent_id','=',$data['id'])
->execute($this->db)
->as_array();
}
}
// If this is a top level category
if(empty($data['parent_id'])) {
// Load children
$data['children'] = DB::select('id')
->from('tags')
->where('parent_id','=',$data['id'])
->execute($this->db)
->as_array();
}
}

return new Tag($data);
}

Expand All @@ -70,31 +69,31 @@ public function getSearchFields()
}

// Ushahidi_Repository
protected function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
foreach (['tag', 'type', 'parent_id'] as $key)
{
if ($search->$key) {
$query->where($key, '=', $search->$key);
}
}
if ($search->q) {
// Tag text searching
$query->where('tag', 'LIKE', "%{$search->q}%");
}
if($search->level) {
//searching for top-level-tags
if($search->level === 'parent') {
$query->where('parent_id', '=', null);
}
}
if($search->formId){
$query->join('forms_tags')
->on('tags.id', '=', 'forms_tags.tag_id')
->where('form_id','=', $search->formId);
}
}
protected function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
foreach (['tag', 'type', 'parent_id'] as $key)
{
if ($search->$key) {
$query->where($key, '=', $search->$key);
}
}
if ($search->q) {
// Tag text searching
$query->where('tag', 'LIKE', "%{$search->q}%");
}
if($search->level) {
//searching for top-level-tags
if($search->level === 'parent') {
$query->where('parent_id', '=', null);
}
}
if($search->formId){
$query->join('forms_tags')
->on('tags.id', '=', 'forms_tags.tag_id')
->where('form_id','=', $search->formId);
}
}

// SearchRepository
public function getSearchResults()
Expand All @@ -110,29 +109,16 @@ public function create(Entity $entity)
$record = $entity->asArray();
$record['created'] = time();

unset($record['forms']);

$id = $this->executeInsert($this->removeNullValues($record));

if($entity->forms) {
//updating forms_tags-table
$this->updateTagForms($id, $entity->forms);
}

return $id;
}

public function update(Entity $entity)
{
$tag = $entity->getChanged();
unset($tag['forms']);

$count = $this->executeUpdate(['id' => $entity->id], $tag);
// updating forms_tags-table
if($entity->hasChanged('forms'))
{
$this->updateTagForms($entity->id, $entity->forms);
}

return $count;
}
Expand Down Expand Up @@ -162,18 +148,23 @@ public function isSlugAvailable($slug)
{
return $this->selectCount(compact('slug')) === 0;
}
public function delete(Entity $entity)
{
$this->updateFormAttributes($entity->id);
return $this->executeDelete([
'id' => $entity->id
]);
}

public function delete(Entity $entity)
{
// Remove tag from attribute options
$this->removeTagFromAttributeOptions($entity->id);

return $this->executeDelete([
'id' => $entity->id
]);
}

// DeleteTagRepository
public function deleteTag($id)
{
$this->updateFormAttributes($entity->id);
// Remove tag from attribute options
$this->removeTagFromAttributeOptions($entity->id);

return $this->delete(compact('id'));
}
}
14 changes: 0 additions & 14 deletions migrations/20170328080656_add_forms_tags_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,4 @@ public function change()
])
->create();
}

/**
* Migrate Up.
*/
public function up()
{
}

/**
* Migrate Down.
*/
public function down()
{
}
}
Loading

0 comments on commit 38ea41a

Please sign in to comment.