From 1694b93e548059aeb1f8ab6baa1324dbe81e5327 Mon Sep 17 00:00:00 2001
From: ErikHommel <erik.hommel@civicoop.org>
Date: Wed, 18 Sep 2019 15:38:57 +0200
Subject: [PATCH] add parent mailing group type and remove redundant $options
 from Option Group Type

---
 Civi/FormProcessor/Type/Factory.php           |   1 +
 Civi/FormProcessor/Type/OptionGroupType.php   |   3 -
 .../Type/ParentMailingGroupType.php           | 118 ++++++++++++++++++
 3 files changed, 119 insertions(+), 3 deletions(-)
 create mode 100644 Civi/FormProcessor/Type/ParentMailingGroupType.php

diff --git a/Civi/FormProcessor/Type/Factory.php b/Civi/FormProcessor/Type/Factory.php
index 550bcd9..031e394 100644
--- a/Civi/FormProcessor/Type/Factory.php
+++ b/Civi/FormProcessor/Type/Factory.php
@@ -31,6 +31,7 @@
     $this->addType(new CountryIsoCodeType('CountryIsoCode', E::ts('Country ISO Code')));
     $this->addType(new StateProvinceType('StateProvince', E::ts('State/Province')));
     $this->addType(new MailingGroupType('MailingGroup', E::ts('Mailing Group')));
+    $this->addType(new ParentMailingGroupType('ParentMailingGroup', E::ts('Parent Mailing Group')));
     $this->addType(new ParticipantStatusType('ParticipantStatusType', E::ts('Participant Status')));
     $this->addType(new CampaignType('CampaignType', E::ts('Campaign')));
     $this->addType(new FileType('file', E::ts('File')));
diff --git a/Civi/FormProcessor/Type/OptionGroupType.php b/Civi/FormProcessor/Type/OptionGroupType.php
index c04ab75..1122a50 100644
--- a/Civi/FormProcessor/Type/OptionGroupType.php
+++ b/Civi/FormProcessor/Type/OptionGroupType.php
@@ -164,7 +164,6 @@ class OptionGroupType extends AbstractType implements OptionListInterface {
       'is_active' => 1,
       'options' => ['limit' => 0]
     ]);
-    $options = array();
     foreach($optionsApi['values'] as $optionValue) {
       $value = $optionValue['value'];
       if ($use_label) {
@@ -176,8 +175,6 @@ class OptionGroupType extends AbstractType implements OptionListInterface {
     }
 
     $this->optionsLoaded = true;
-
-    return $options;
   }
 
 }
diff --git a/Civi/FormProcessor/Type/ParentMailingGroupType.php b/Civi/FormProcessor/Type/ParentMailingGroupType.php
new file mode 100644
index 0000000..ecd53c9
--- /dev/null
+++ b/Civi/FormProcessor/Type/ParentMailingGroupType.php
@@ -0,0 +1,118 @@
+<?php
+
+/**
+ * @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
+ * @date 18 Sep 2019
+ * @license http://www.gnu.org/licenses/agpl-3.0.html
+ */
+namespace Civi\FormProcessor\Type;
+
+use \Civi\FormProcessor\Config\Specification;
+use \Civi\FormProcessor\Config\SpecificationBag;
+use \Civi\FormProcessor\Type\AbstractType;
+use \Civi\FormProcessor\Type\OptionListInterface;
+
+use \CRM_FormProcessor_ExtensionUtil as E;
+
+class ParentMailingGroupType extends AbstractType implements OptionListInterface {
+
+  private $_childGroups;
+
+  private $_normalizedOptions;
+
+  private $_denormalizedOptions;
+
+  private $_groupsLoaded = FALSE;
+
+  /**
+   * Get the configuration specification
+   *
+   * @return SpecificationBag
+   */
+  public function getConfigurationSpecification() {
+    $parentGroups = [];
+    try {
+      $parentGroupsApi = civicrm_api3('Group', 'get' ,[
+        'return' => ["title", "description"],
+        'is_active' => 1,
+        'group_type' => "Mailing List",
+        'children' => ['IS NOT NULL' => 1],
+        'options' => ['limit' => 0],
+      ]);
+    }
+    catch (\CiviCRM_API3_Exception $ex) {
+    }
+    foreach ($parentGroupsApi['values'] as $groupdId => $groupData) {
+      $groupText = $groupData['title'];
+      if (isset($groupData['description'])) {
+        if (strlen($groupData['description']) > 80) {
+          $groupText .= " (" . substr($groupData['description'], 0, 80) . "...)";
+        }
+        else {
+          $groupText .= " (" . $groupData['description'] . ")";
+        }
+      }
+      $parentGroups[$groupdId] = $groupText;
+    }
+    return new SpecificationBag([
+      new Specification('parent_group_id', 'Integer', E::ts('Parent Mailing Group'), TRUE, NULL, NULL, $parentGroups, FALSE),
+    ]);
+  }
+
+  /**
+   * Method to validate the input from the api call
+   *
+   * @param mixed $value
+   * @param array $allValues
+   * @return bool
+   */
+  public function validateValue($value, $allValues=[]) {
+    // todo check if group id is actually an active parent group
+    return TRUE;
+  }
+
+  /**
+   * Returns the type number from CRM_Utils_Type
+   */
+  public function getCrmType() {
+    return \CRM_Utils_Type::T_INT;
+  }
+
+  public function getOptions($params) {
+    $this->loadChildGroups();
+    return $this->_childGroups;
+  }
+
+  /**
+   * Method to load the active child mailing groups of the selected parent
+   *
+   * @return array|void
+   * @throws \CiviCRM_API3_Exception
+   */
+  private function loadChildGroups() {
+    if ($this->_groupsLoaded) {
+      return;
+    }
+    $this->_childGroups = [];
+    $parentGroupId = $this->configuration->get('parent_group_id');
+    if (!$parentGroupId) {
+      return;
+    }
+    try {
+      $childrenApi = civicrm_api3('Group', 'get', [
+        'return' => ["title"],
+        'parents' => ['LIKE' => $parentGroupId],
+        'is_active' => 1,
+        'options' => ['limit' => 0]
+      ]);
+      $childGroups = [];
+      foreach($childrenApi['values'] as $childId => $child) {
+        $this->_childGroups[$childId] = $child['title'];
+      }
+    }
+    catch (\CiviCRM_API3_Exception $ex) {
+    }
+    $this->_groupsLoaded = TRUE;
+  }
+
+}
-- 
GitLab