From 0782a7f8fe92f3e5ae8058a7cc8d9d4aa3c34833 Mon Sep 17 00:00:00 2001
From: EarthlingDavey <15802017+EarthlingDavey@users.noreply.github.com>
Date: Fri, 26 Jan 2024 13:16:40 +0000
Subject: [PATCH] Fix campaign categories being overwritten by users from
 different agencies

Add a function called has_attached_terms_not_in_context to catch if a post has a category attached that does not relate to current Agengy context.

Add a property called run_has_attached_terms_not_in_context to enable this on a taxonomy basis. Currently enabled for Campaign_Category, could also be enabled for other taxonomies that extend Content_Category e.g. News_Category.

Resolves: https://dsdmoj.atlassian.net/jira/software/c/projects/CDPT/boards/1154?selectedIssue=CDPT-1253
---
 .../taxonomies/campaign-category.php          |  3 ++
 .../taxonomies/content-category.php           | 48 +++++++++++++++++--
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/campaign-category.php b/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/campaign-category.php
index c44781eb3..5f87be2de 100644
--- a/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/campaign-category.php
+++ b/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/campaign-category.php
@@ -46,4 +46,7 @@ class Campaign_Category extends Content_Category
             'assign_terms' => 'assign_campaign_categories',
         ),
     );
+
+    protected $run_has_attached_terms_not_in_context = true;
+
 }
diff --git a/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/content-category.php b/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/content-category.php
index bc344bc14..41c0d0d05 100644
--- a/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/content-category.php
+++ b/web/app/themes/clarity/inc/admin/agency_taxonomies/taxonomies/content-category.php
@@ -60,6 +60,38 @@ public function context_has_terms()
         return false;
     }
 
+    /**
+     * For the current post, check if any terms in this category are:
+     * - attached to the post and,
+     * - do not relate to current Agengy context
+     * 
+     * @return bool
+     */
+
+     public function has_attached_terms_not_in_context()
+     {  
+        global $post;
+        
+        if (!is_object($post)) return;
+        
+        $terms = get_the_terms($post->ID, $this->name);
+        $context = Agency_Context::get_agency_context('term_id');
+
+        if (!$terms) return;
+
+        foreach ($terms as $term) {
+            $term_agencies = get_field('term_used_by', $this->name . '_' . $term->term_id);
+            
+            if (is_array($term_agencies) && !in_array($context, $term_agencies)) {
+                return true;
+            }
+        }
+
+        return;
+     }
+
+
+
     /**
      * Remove the default category metabox
      */
@@ -75,11 +107,19 @@ public function remove_default_meta_box()
      */
     public function add_custom_category_meta_box()
     {
-        if ($this->context_has_terms()) {
-            foreach ($this->object_types as $type) {
-                add_meta_box($this->name, $this->args['labels']['name'], array($this, 'show_custom_category_meta_box'), $type, 'side');
-            }
+
+        if (!$this->context_has_terms()) {
+            return;
+        }
+
+        if(!empty($this->run_has_attached_terms_not_in_context) && $this->has_attached_terms_not_in_context()) {
+            return;
+        }
+
+        foreach ($this->object_types as $type) {
+            add_meta_box($this->name, $this->args['labels']['name'], array($this, 'show_custom_category_meta_box'), $type, 'side');
         }
+        
     }
 
     /**