From 633df00027b0c9b98e83a4dd13dad59484a2b6ef Mon Sep 17 00:00:00 2001
From: David Thompson <davet@gnu.org>
Date: Thu, 23 Oct 2014 12:10:51 -0400
Subject: [PATCH] Extract base page class.

* CRM/Memberdashboard/Page.php: New file.
* CRM/Memberdashboard/Page/Communications.php
  (CRM_Memberdashboard_Page_Communications): Inherit from base page.
  [contactId, loadContact, isPOST]: Remove methods.
  [run]: Use pre-loaded contact object.
* CRM/Memberdashboard/Page/MemberDashboard.php
  (CRM_Memberdashboard_Page_MemberDashboard): Inherit from base page.
  [$contact]: Remove variable.
  [__construct, loadContact]: Remove methods.
---
 CRM/Memberdashboard/Page.php                 | 52 ++++++++++++++++++++
 CRM/Memberdashboard/Page/Communications.php  | 20 ++------
 CRM/Memberdashboard/Page/MemberDashboard.php | 21 +-------
 3 files changed, 56 insertions(+), 37 deletions(-)
 create mode 100644 CRM/Memberdashboard/Page.php

diff --git a/CRM/Memberdashboard/Page.php b/CRM/Memberdashboard/Page.php
new file mode 100644
index 0000000..b8ba059
--- /dev/null
+++ b/CRM/Memberdashboard/Page.php
@@ -0,0 +1,52 @@
+<?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/>.
+ */
+
+require_once 'CRM/Core/Page.php';
+
+class CRM_Memberdashboard_Page extends CRM_Core_Page {
+  public $contact = NULL;
+
+  function __construct() {
+    parent::__construct();
+
+    $this->contact = $this->loadContact();
+  }
+
+  function contactId() {
+    return CRM_Core_Session::singleton()->get('userID');
+  }
+
+  /**
+   * Return a contact object for the current user.
+   *
+   * @return CRM_Contact
+   */
+  function loadContact() {
+    return civicrm_api3('contact', 'getsingle', array(
+      'id' => $this->contactId()
+    ));
+  }
+
+  function isPOST() {
+    return $_SERVER['REQUEST_METHOD'] == 'POST';
+  }
+}
diff --git a/CRM/Memberdashboard/Page/Communications.php b/CRM/Memberdashboard/Page/Communications.php
index 9febb95..36a5439 100644
--- a/CRM/Memberdashboard/Page/Communications.php
+++ b/CRM/Memberdashboard/Page/Communications.php
@@ -22,28 +22,14 @@
 
 require_once 'CRM/Core/Page.php';
 
-class CRM_Memberdashboard_Page_Communications extends CRM_Core_Page {
-  function contactId() {
-    return CRM_Core_Session::singleton()->get('userID');
-  }
-
-  function loadContact() {
-    return civicrm_api3('contact', 'getsingle', array(
-      'id' => $this->contactId()
-    ));
-  }
-
-  function isPOST() {
-    return $_SERVER['REQUEST_METHOD'] == 'POST';
-  }
-
+class CRM_Memberdashboard_Page_Communications extends CRM_Memberdashboard_Page {
   function postProcess($params) {
     // Update member's preferred mail format (plain text, HTML)
     $key = 'preferred_mail_format';
     $value = $params[$key];
 
     civicrm_api3('contact', 'create', array(
-      'id' => $this->contactId(),
+      'id' => $this->contact['id'],
       $key => $value
     ));
   }
@@ -61,7 +47,7 @@ class CRM_Memberdashboard_Page_Communications extends CRM_Core_Page {
     $gContact = new CRM_Contact_Page_View_UserDashBoard_GroupContact();
     $gContact->browse();
 
-    $this->assign('contact', $this->loadContact());
+    $this->assign('contact', $this->contact);
     $this->assign('mailFormats', array('Both', 'HTML', 'Text'));
 
     CRM_Utils_System::setTitle('Communications');
diff --git a/CRM/Memberdashboard/Page/MemberDashboard.php b/CRM/Memberdashboard/Page/MemberDashboard.php
index 5029510..2d2185e 100644
--- a/CRM/Memberdashboard/Page/MemberDashboard.php
+++ b/CRM/Memberdashboard/Page/MemberDashboard.php
@@ -22,26 +22,7 @@
 
 require_once 'CRM/Core/Page.php';
 
-class CRM_Memberdashboard_Page_MemberDashboard extends CRM_Core_Page {
-  public $contact = NULL;
-
-  function __construct() {
-    parent::__construct();
-
-    $this->contact = $this->loadContact();
-  }
-
-  /**
-   * Return a contact object for the current user.
-   *
-   * @return CRM_Contact
-   */
-  function loadContact() {
-    return civicrm_api3('contact', 'getsingle', array(
-      'id' => CRM_Core_Session::singleton()->get('userID')
-    ));
-  }
-
+class CRM_Memberdashboard_Page_MemberDashboard extends CRM_Memberdashboard_Page {
   /**
    * Return the personalized title for the page.
    *
-- 
2.25.1