From: David Thompson Date: Wed, 1 Oct 2014 20:48:34 +0000 (-0400) Subject: Factor out a helper class from the member dashboard. X-Git-Tag: v0.1~55 X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7dc977e9e1ba85de237e99b3160e42063431cefd;p=org.fsf.memberdashboard.git Factor out a helper class from the member dashboard. * 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. --- diff --git a/CRM/Memberdashboard/Page/ComponentHelper.php b/CRM/Memberdashboard/Page/ComponentHelper.php new file mode 100644 index 0000000..2767977 --- /dev/null +++ b/CRM/Memberdashboard/Page/ComponentHelper.php @@ -0,0 +1,71 @@ +. + */ + +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(); + } + } +} diff --git a/CRM/Memberdashboard/Page/MemberDashboard.php b/CRM/Memberdashboard/Page/MemberDashboard.php index 3b83927..8164dc9 100644 --- a/CRM/Memberdashboard/Page/MemberDashboard.php +++ b/CRM/Memberdashboard/Page/MemberDashboard.php @@ -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();