Factor out a helper class from the member dashboard.
authorDavid Thompson <davet@gnu.org>
Wed, 1 Oct 2014 20:48:34 +0000 (16:48 -0400)
committerDavid Thompson <davet@gnu.org>
Wed, 1 Oct 2014 20:48:34 +0000 (16:48 -0400)
* CRM/Memberdashboard/Page/ComponentHelper.php: New file.
* CRM/Memberdashboard/Page/MemberDashboard.php ($components): Delete
  variable.
  (loadComponents, buildDashboardElements): Delete methods.
  (__construct): Don't load dashboard components;
  (run): Use helper class.

CRM/Memberdashboard/Page/ComponentHelper.php [new file with mode: 0644]
CRM/Memberdashboard/Page/MemberDashboard.php

diff --git a/CRM/Memberdashboard/Page/ComponentHelper.php b/CRM/Memberdashboard/Page/ComponentHelper.php
new file mode 100644 (file)
index 0000000..2767977
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+/**
+ * FSF Member Dashboard
+ * Copyright © 2014 Free Software Foundation, Inc.
+ *
+ * This file is a part of FSF Member Dashboard.
+ *
+ * FSF Member Dashboard is free software; you can copy, modify, and
+ * distribute it under the terms of the GNU Affero General Public
+ * License Version 3, 19 November 2007 and the CiviCRM Licensing
+ * Exception.
+ *
+ * FSF Member Dashboard is distributed in the hope that it will be
+ * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with FSF Member Dashboard.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+class CRM_Memberdashboard_Page_ComponentHelper {
+  public $componentNames = array();
+  public $components = array();
+
+  function __construct($componentNames) {
+    $this->componentNames = $componentNames;
+    $this->components = $this->loadComponents();
+  }
+
+  /**
+   * Return an array of components that will be rendered on the dashboard.
+   *
+   * @return Component array
+   */
+  function loadComponents() {
+    $allComponents = CRM_Core_Component::getEnabledComponents();
+    $names = $this->componentNames;
+
+    return array_filter($allComponents, function($component) use($names) {
+      return in_array($component->info['name'], $names);
+    });
+  }
+
+  /**
+   * Build user CiviMember and CiviContribute dashboard elements.
+   *
+   * @return Array of dashboard elements
+   */
+  function buildDashboardElements() {
+    return array_map(function($component) {
+      $elem = $component->getUserDashboardElement();
+      return array(
+        'class' => 'crm-dashboard-' . strtolower($component->name),
+        'sectionTitle' => $elem['title'],
+        'templatePath' => 'CRM/Memberdashboard/Page/Element/' .
+                          $component->name . '.tpl',
+        'weight' => $elem['weight'],
+      );
+    }, $this->components);
+  }
+
+  function run() {
+    // Setup dashboard component state.
+    foreach ($this->components as $name => $component) {
+      $userDashboard = $component->getUserDashboardObject();
+      $userDashboard->run();
+    }
+  }
+}
index 3b839271e4f9b9a35e14e3b0e758cff9ce3c8091..8164dc92bd952d1b61bff2af866dc74ded921490 100644 (file)
@@ -24,13 +24,11 @@ require_once 'CRM/Core/Page.php';
 
 class CRM_Memberdashboard_Page_MemberDashboard extends CRM_Core_Page {
   public $contact = NULL;
-  public $components = array();
 
   function __construct() {
     parent::__construct();
 
     $this->contact = $this->loadContact();
-    $this->components = $this->loadComponents();
   }
 
   /**
@@ -45,35 +43,6 @@ class CRM_Memberdashboard_Page_MemberDashboard extends CRM_Core_Page {
     return CRM_Contact_BAO_Contact::retrieve($params, $defaults);
   }
 
-  /**
-   * Return an array of components that will be rendered on the dashboard.
-   *
-   * @return Component array
-   */
-  function loadComponents() {
-    return array_filter(CRM_Core_Component::getEnabledComponents(), function($component) {
-      return in_array($component->info['name'], array('CiviMember', 'CiviContribute'));
-    });
-  }
-
-  /**
-   * Build user CiviMember and CiviContribute dashboard elements.
-   *
-   * @return Array of dashboard elements
-   */
-  function buildDashboardElements() {
-    return array_map(function($component) {
-      $elem = $component->getUserDashboardElement();
-      return array(
-        'class' => 'crm-dashboard-' . strtolower($component->name),
-        'sectionTitle' => $elem['title'],
-        'templatePath' => 'CRM/Memberdashboard/Page/Element/' .
-                          $component->name . '.tpl',
-        'weight' => $elem['weight'],
-      );
-    }, $this->components);
-  }
-
   /**
    * Return the personalized title for the page.
    *
@@ -84,14 +53,13 @@ class CRM_Memberdashboard_Page_MemberDashboard extends CRM_Core_Page {
   }
 
   function run() {
-    // Setup dashboard component state.
-    foreach ($this->components as $name => $component) {
-      $userDashboard = $component->getUserDashboardObject();
-      $userDashboard->run();
-    }
+    $helper = new CRM_Memberdashboard_Page_ComponentHelper(array(
+      'CiviMember',
+      'CiviContribute'
+    ));
+    $helper->run();
 
-    $this->assign('contact', $this->contact);
-    $this->assign('dashboardElements', $this->buildDashboardElements());
+    $this->assign('dashboardElements', $helper->buildDashboardElements());
 
     parent::run();