Cleanup and move eventcart form components to ext
authorMatthew Wire <mjw@mjwconsult.co.uk>
Sat, 18 Jul 2020 11:42:02 +0000 (12:42 +0100)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Sat, 18 Jul 2020 11:43:49 +0000 (12:43 +0100)
CRM/Event/Form/ManageEvent/TabHeader.php
CRM/Event/Page/EventInfo.php
CRM/Event/Page/List.php
CRM/Event/Page/ManageEvent.php
ext/eventcart/CRM/Event/Cart/PageCallback.php [new file with mode: 0644]
ext/eventcart/eventcart.php
ext/eventcart/templates/CRM/Event/Cart/eventlistpre.tpl [new file with mode: 0644]
templates/CRM/Event/Page/List.tpl

index cae7c1cb96f9bbde1420b114b62b036d6a7cbe23..ebfba8f0dc39c1fdaca1e36f4bb13e64cf30dcd7 100644 (file)
@@ -83,6 +83,7 @@ class CRM_Event_Form_ManageEvent_TabHeader {
       unset($tabs['repeat']['class']);
     }
 
+    // @todo Move to eventcart extension
     // check if we're in shopping cart mode for events
     if (!(bool) Civi::settings()->get('enable_cart')) {
       unset($tabs['conference']);
index 265b3f5dfeb81ab09a04d8351c591a2fb6fcccdc..83671166ab2e879ac65caf04c91c6bbbef4e50c2 100644 (file)
@@ -272,21 +272,11 @@ class CRM_Event_Page_EventInfo extends CRM_Core_Page {
             $registerText = $values['event']['registration_link_text'];
           }
 
-          // check if we're in shopping cart mode for events
-          $enable_cart = Civi::settings()->get('enable_cart');
-          if ($enable_cart) {
-            $link = CRM_Event_Cart_BAO_EventInCart::get_registration_link($this->_id);
-            $registerText = $link['label'];
-
-            $url = CRM_Utils_System::url($link['path'], $link['query'] . $action_query, FALSE, NULL, TRUE, TRUE);
-          }
-
           //Fixed for CRM-4855
           $allowRegistration = CRM_Event_BAO_Event::showHideRegistrationLink($values);
 
           $this->assign('registerText', $registerText);
           $this->assign('registerURL', $url);
-          $this->assign('eventCartEnabled', $enable_cart);
         }
       }
       elseif (CRM_Core_Permission::check('register for events')) {
index e3f4a086180bc22ffd3c032b68cdc18e975f784f..ffeae8ba922a164e48399784ba0b87aede1fb4e6 100644 (file)
@@ -23,13 +23,12 @@ class CRM_Event_Page_List extends CRM_Core_Page {
     $info = CRM_Event_BAO_Event::getCompleteInfo($start, $type, $id, $end);
     $this->assign('events', $info);
 
+    // @todo Move this to eventcart extension
     // check if we're in shopping cart mode for events
-    $enable_cart = (bool) Civi::settings()->get('enable_cart');
-    $this->assign('eventCartEnabled', $enable_cart);
-
-    if ($enable_cart) {
+    if ((bool) Civi::settings()->get('enable_cart')) {
       $this->assign('registration_links', TRUE);
     }
+
     return parent::run();
   }
 
index a5c4150fce7b02f3842c786a9a979c87fc2d9a93..08dc44dfbba45c68d38820beb6b7a10b629fa524 100644 (file)
@@ -123,13 +123,15 @@ class CRM_Event_Page_ManageEvent extends CRM_Core_Page {
   /**
    * Get tab  Links for events.
    *
-   * @param $enableCart
-   *
    * @return array
    *   (reference) of tab links
+   * @throws \CiviCRM_API3_Exception
    */
   public static function &tabs() {
-    $enableCart = Civi::settings()->get('enable_cart');
+    // @todo Move to eventcart extension
+    // check if we're in shopping cart mode for events
+    $enableCart = (bool) Civi::settings()->get('enable_cart');
+
     $cacheKey = $enableCart ? 1 : 0;
     if (!(self::$_tabLinks)) {
       self::$_tabLinks = [];
@@ -340,9 +342,6 @@ ORDER BY start_date desc
     while ($pcpDao->fetch()) {
       $eventPCPS[$pcpDao->entity_id] = $pcpDao->entity_id;
     }
-    // check if we're in shopping cart mode for events
-    $enableCart = Civi::settings()->get('enable_cart');
-    $this->assign('eventCartEnabled', $enableCart);
     $mapping = CRM_Utils_Array::first(CRM_Core_BAO_ActionSchedule::getMappings([
       'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID,
     ]));
@@ -426,7 +425,7 @@ ORDER BY start_date desc
       }
     }
 
-    $manageEvent['tab'] = self::tabs($enableCart);
+    $manageEvent['tab'] = self::tabs();
     $this->assign('rows', $manageEvent);
 
     $statusTypes = CRM_Event_PseudoConstant::participantStatus(NULL, 'is_counted = 1', 'label');
diff --git a/ext/eventcart/CRM/Event/Cart/PageCallback.php b/ext/eventcart/CRM/Event/Cart/PageCallback.php
new file mode 100644 (file)
index 0000000..d6d1e7d
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+class CRM_Event_Cart_PageCallback {
+
+  /**
+   * @param \Civi\Core\Event\GenericHookEvent $event
+   */
+  public static function run($event) {
+    switch ($event->page->getVar('_name')) {
+      case 'CRM_Event_Page_EventInfo':
+        self::alterEventInfo($event);
+        break;
+
+      case 'CRM_Event_Page_List':
+        self::alterEventList($event);
+    }
+  }
+
+  public static function alterEventInfo($event) {
+    $eventID = $event->page->getVar('_id');
+    $link = CRM_Event_Cart_BAO_EventInCart::get_registration_link($eventID);
+    $registerText = $link['label'];
+
+    $action = CRM_Utils_Request::retrieve('action', 'String', $event->page, FALSE);
+    $action_query = ($action === CRM_Core_Action::PREVIEW) ? "&action=$action" : '';
+
+    $url = CRM_Utils_System::url($link['path'], $link['query'] . $action_query, FALSE, NULL, TRUE, TRUE);
+
+    $event->page->assign('registerText', $registerText);
+    $event->page->assign('registerURL', $url);
+    $event->page->assign('eventCartEnabled', TRUE);
+  }
+
+  public static function alterEventList($event) {
+    if ((bool) Civi::settings()->get('enable_cart')) {
+      CRM_Core_Region::instance('crm-event-list-pre')
+        ->add(['template' => 'CRM/Event/Cart/eventlistpre.tpl']);
+    }
+  }
+
+}
index 41a32d244919e814ed7f90722e74f415d5d95359..dfcaf951a8aa474d10d71e88170d56c8741f8b36 100644 (file)
@@ -11,6 +11,12 @@ use CRM_Eventcart_ExtensionUtil as E;
  * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
  */
 function eventcart_civicrm_config(&$config) {
+  if (isset(Civi::$statics[__FUNCTION__])) {
+    return;
+  }
+  Civi::$statics[__FUNCTION__] = 1;
+  Civi::dispatcher()->addListener('hook_civicrm_pageRun', 'CRM_Event_Cart_PageCallback::run');
+
   _eventcart_civix_civicrm_config($config);
 }
 
diff --git a/ext/eventcart/templates/CRM/Event/Cart/eventlistpre.tpl b/ext/eventcart/templates/CRM/Event/Cart/eventlistpre.tpl
new file mode 100644 (file)
index 0000000..a212dc0
--- /dev/null
@@ -0,0 +1,2 @@
+<a href="{crmURL p='civicrm/event/view_cart'}" class="button crm-shoppingcart-button"><i class="crm-i fa-shopping-cart" aria-hidden="true"></i> {ts}View Cart{/ts}</a>
+<a href="{crmURL p='civicrm/event/cart_checkout'}" class="button crm-check-out-button"><i class="crm-i fa-credit-card" aria-hidden="true"></i> {ts}Checkout{/ts}</a>
index e5f5fa182f19c98b3c0ea81d370162e0ef5bc0aa..85b9ff832522540a47575d3e63db2e2aee182ab7 100644 (file)
 {* Displays current and upcoming public Events Listing as an HTML page. *}
 {include file="CRM/common/jsortable.tpl"}
 <div class="crm-section crm-event-list">
-  {if $eventCartEnabled}
-    <a href="{crmURL p='civicrm/event/view_cart' }" class="button crm-shoppingcart-button"><i class="crm-i fa-shopping-cart" aria-hidden="true"></i> {ts}View Cart{/ts}</a>
-    <a href="{crmURL p='civicrm/event/cart_checkout'}" class="button crm-check-out-button"><i class="crm-i fa-credit-card" aria-hidden="true"></i> {ts}Checkout{/ts}</a>
-  {/if}
+  {crmRegion name="crm-event-list-pre"}
+  {/crmRegion}
 
   <table id="options" class="display">
     <thead>
@@ -48,4 +46,7 @@
       </tr>
     {/foreach}
   </table>
+
+  {crmRegion name="crm-event-list-post"}
+  {/crmRegion}
 </div>