forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop limiting tags by form, just limit tags per attribute
- Stop using forms_tags table - Just use form_attribute.options instead
- Loading branch information
Showing
8 changed files
with
148 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]> | ||
|
@@ -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); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.