Commit 901f98d9 authored by mattwire's avatar mattwire
Browse files

Convert Manage Event Templates to datatables

parent 836d3f26
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2019 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM 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. |
| |
| CiviCRM 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 Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2019
*/
trait CRM_AdvancedEvents_OldVersionEntityPageTrait {
// @fixme: This is a copy of CRM_Core_Page_EntityPageTrait included in CiviCRM 5.17+
/**
* Pages MUST declare the following functions:
* public function getDefaultEntity() {
*/
/**
* Page MAY want to override the following functions:
* public function getDefaultContext()
*/
/**
* The id of the contact.
*
* @var int
*/
protected $_id;
/**
* The mode of operation for this page
*
* @var int
*/
protected $_action;
/**
* The context that we are working on.
*
* @var string
*/
protected $_context;
/**
* Contact ID of the contact on the page.
*
* @var int
*/
public $_contactID = NULL;
/**
* Contact ID of the contact on the page.
*
* @var int
* @deprecated Historically pages alternate between $_contactID and $_contactId. We'll standardise on one
*/
public $_contactId = NULL;
/**
* @var int
*/
public $_permission = NULL;
/**
* The action links that we need to display for the browse screen.
*
* @var array
*/
public static $_links = NULL;
/**
* Get the entity id being edited.
*
* @return int|null
*/
public function getEntityId() {
return $this->_id;
}
/**
* Get the context we are working in
*
* @return string
*/
public function getContext() {
return $this->_context;
}
/**
* Get the contact ID
*
* @return int
*/
public function getContactId() {
return $this->_contactID;
}
/**
* Set the contact ID
*
* @param $contactId
*/
public function setContactId($contactId) {
$this->_contactID = $contactId;
$this->_contactId = $contactId;
}
public function getAction() {
return $this->_action;
}
/**
* Explicitly declare the form context.
*
* @return string|null
*/
public function getDefaultContext() {
return NULL;
}
public function preProcessQuickEntityPage() {
$this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
$this->assign('action', $this->getAction());
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$this->setContactId(CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE, CRM_Core_Session::getLoggedInContactID()));
$this->assign('contactId', $this->getContactId());
$this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this, FALSE, $this->getDefaultContext());
$this->assign('context', $this->_context);
// check logged in url permission
CRM_Contact_Page_View::checkUserPermission($this);
$this->assign('entityInClassFormat', strtolower(str_replace('_', '-', $this->getDefaultEntity())));
}
/**
* Is the form being used in the context of a deletion.
*
* (For some reason rather than having separate forms Civi overloads one form).
*
* @return bool
*/
protected function isDeleteContext() {
return ($this->getAction() & CRM_Core_Action::DELETE);
}
/**
* Is the form being used in the context of a view.
*
* @return bool
*/
protected function isViewContext() {
return ($this->getAction() & CRM_Core_Action::VIEW);
}
/**
* Is the form being used in the context of a edit.
*
* @return bool
*/
protected function isEditContext() {
return ($this->getAction() & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD));
}
}
......@@ -13,7 +13,7 @@ trait CRM_AdvancedEvents_OldVersionSupportTrait {
* @throws \CRM_Core_Exception
*/
public function setSelectedChild($default = NULL) {
$selectedChild = CRM_Utils_Request::retrieve('selectedChild', 'Alphanumeric', $form, FALSE, $default);
$selectedChild = CRM_Utils_Request::retrieve('selectedChild', 'Alphanumeric', $this, FALSE, $default);
if (!empty($selectedChild)) {
$this->set('selectedChild', $selectedChild);
$this->assign('selectedChild', $selectedChild);
......
<?php
/* https://civicrm.org/licensing */
use CRM_AdvancedEvents_ExtensionUtil as E;
/**
* This class contains all the function that are called using AJAX (jQuery)
*/
class CRM_AdvancedEvents_Page_AJAX {
public static function getEventTemplates() {
$params = CRM_Core_Page_AJAX::defaultSortAndPagerParams();
$eventParams = [
'return' => array_keys(CRM_AdvancedEvents_Page_ManageEventTemplate::getColumnHeaders()),
'is_template' => TRUE,
'options' => ['sort' => $params['sortBy'], 'limit' => $params['rp'], 'offset' => $params['offset']],
];
$eventTemplates = civicrm_api3('Event', 'get', $eventParams);
if (empty($eventTemplates['values'])) {
CRM_Utils_JSON::output([
'data' => [],
'recordsTotal' => 0,
'recordsFiltered' => 0,
]);
}
$eventTemplatesDT = [];
$columnHeaders = CRM_AdvancedEvents_Page_ManageEventTemplate::getColumnHeaders();
foreach ($eventTemplates['values'] as $eventTemplate) {
foreach ($columnHeaders as $headerKey => $headerDetail) {
$dtEventTemplate[$headerKey] = CRM_AdvancedEvents_Page_ManageEventTemplate::renderField($eventTemplate['id'], $headerKey, CRM_Utils_Array::value($headerKey, $eventTemplate));
}
$dtEventTemplate['id'] = $eventTemplate['id'];
$eventTemplatesDT['data'][] = $dtEventTemplate;
}
$eventTemplatesDT['recordsTotal'] = count($eventTemplates['values']);
$eventTemplatesDT['recordsFiltered'] = count($eventTemplates['values']);
CRM_Utils_JSON::output($eventTemplatesDT);
}
}
......@@ -6,64 +6,43 @@ use CRM_AdvancedEvents_ExtensionUtil as E;
/**
* Page for displaying list of event templates.
*/
class CRM_AdvancedEvents_Page_ManageEventTemplate extends CRM_Core_Page_Basic {
class CRM_AdvancedEvents_Page_ManageEventTemplate extends CRM_Core_Page {
// @fixme: Increment min version to 5.17 and switch to core trait
// https://github.com/civicrm/civicrm-core/commit/6921a8204f541aac41c083f7860036cacd3c4bf4
//use CRM_Core_Page_EntityPageTrait;
use CRM_AdvancedEvents_OldVersionEntityPageTrait;
/**
* The action links that we need to display for the browse screen.
*
* @var array
*/
static $_links = NULL;
private static $_actionLinks;
/**
* Get BAO Name.
* The list of column headers
* @var array
*/
private static $columnHeaders;
/**
* Explicitly declare the entity api name.
*
* @return string
* Classname of BAO.
*/
public function getBAOName() {
return 'CRM_Event_BAO_Event';
public function getDefaultEntity() {
return 'Relationship';
}
/**
* Get action Links.
* Get BAO Name.
*
* @return array
* (reference) of action links
* @return string
* Classname of BAO.
*/
public function &links() {
if (!(self::$_links)) {
// helper variable for nicer formatting
self::$_links = [
CRM_Core_Action::ADD => [
'name' => E::ts('Create Event'),
'title' => ts('Create a new event using this template'),
'url' => 'civicrm/event/add',
'qs' => 'reset=1&action=add&template_id=%%id%%',
],
CRM_Core_Action::UPDATE => [
'name' => E::ts('Edit'),
'title' => E::ts('Edit Event Template'),
'url' => 'civicrm/event/manage/settings',
'qs' => 'action=update&id=%%id%%&reset=1',
],
CRM_Core_Action::DELETE => [
'name' => E::ts('Delete'),
'title' => E::ts('Delete Event Template'),
'url' => 'civicrm/admin/advancedevents/delete',
'qs' => 'action=delete&istemplate=1&id=%%id%%',
],
CRM_Core_Action::COPY => [
'name' => E::ts('Copy'),
'title' => E::ts('Create Template from existing template'),
'url' => 'civicrm/admin/eventTemplate',
'qs' => 'reset=1&action=copy&id=%%id%%',
'extra' => 'onclick = "return confirm(\'Are you sure you want to copy this template?\');"',
],
];
}
return self::$_links;
public function getBAOName() {
return 'CRM_Event_BAO_Event';
}
/**
......@@ -73,74 +52,27 @@ class CRM_AdvancedEvents_Page_ManageEventTemplate extends CRM_Core_Page_Basic {
* type of action and executes that action.
* Finally it calls the parent's run method.
*
* @return void
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function run() {
// get the requested action
$action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
$this->preProcessQuickEntityPage();
if ($action & CRM_Core_Action::COPY) {
if ($this->getAction() & CRM_Core_Action::COPY) {
$this->copy();
}
else {
// parent run
return parent::run();
$this->browse();
}
parent::run();
}
/**
* Browse all event templates.
*/
public function browse() {
//get all event templates.
$allEventTemplates = [];
$eventTemplate = new CRM_Event_DAO_Event();
$eventTypes = CRM_Event_PseudoConstant::eventType();
$participantRoles = CRM_Event_PseudoConstant::participantRole();
$participantListings = CRM_Event_PseudoConstant::participantListing();
//find all event templates.
$eventTemplate->is_template = TRUE;
$eventTemplate->find();
while ($eventTemplate->fetch()) {
CRM_Core_DAO::storeValues($eventTemplate, $allEventTemplates[$eventTemplate->id]);
//get listing types.
if ($eventTemplate->participant_listing_id) {
$allEventTemplates[$eventTemplate->id]['participant_listing'] = $participantListings[$eventTemplate->participant_listing_id];
}
//get participant role
if ($eventTemplate->default_role_id) {
$allEventTemplates[$eventTemplate->id]['participant_role'] = $participantRoles[$eventTemplate->default_role_id];
}
//get event type.
if (isset($eventTypes[$eventTemplate->event_type_id])) {
$allEventTemplates[$eventTemplate->id]['event_type'] = $eventTypes[$eventTemplate->event_type_id];
}
//form all action links
$action = array_sum(array_keys($this->links()));
//add action links.
$allEventTemplates[$eventTemplate->id]['action'] = CRM_Core_Action::formLink(self::links(), $action,
['id' => $eventTemplate->id],
E::ts('more'),
FALSE,
'eventTemplate.manage.action',
'Event',
$eventTemplate->id
);
// Get count of events linked to this template
$allEventTemplates[$eventTemplate->id]['count'] = civicrm_api3('EventTemplate', 'getcount', [
'template_id' => $eventTemplate->id,
]);
}
$this->assign('rows', $allEventTemplates);
$columnHeaders = self::getColumnHeaders();
$this->assign('columnHeaders', $columnHeaders);
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath(),
......@@ -201,4 +133,150 @@ class CRM_AdvancedEvents_Page_ManageEventTemplate extends CRM_Core_Page_Basic {
CRM_Utils_System::redirect(CRM_Utils_System::url($urlString, $urlParams));
}
/**
* Get action Links.
*
* @return array
* action links
*/
public static function actionLinks() {
if (!(self::$_actionLinks)) {
// helper variable for nicer formatting
self::$_actionLinks = [
CRM_Core_Action::ADD => [
'name' => E::ts('Create Event'),
'title' => ts('Create a new event using this template'),
'url' => 'civicrm/event/add',
'qs' => 'reset=1&action=add&template_id=%%id%%',
],
CRM_Core_Action::UPDATE => [
'name' => E::ts('Edit'),
'title' => E::ts('Edit Event Template'),
'url' => 'civicrm/event/manage/settings',
'qs' => 'action=update&id=%%id%%&reset=1',
],
CRM_Core_Action::DELETE => [
'name' => E::ts('Delete'),
'title' => E::ts('Delete Event Template'),
'url' => 'civicrm/admin/advancedevents/delete',
'qs' => 'action=delete&istemplate=1&id=%%id%%',
],
CRM_Core_Action::COPY => [
'name' => E::ts('Copy'),
'title' => E::ts('Create Template from existing template'),
'url' => 'civicrm/admin/eventTemplate',
'qs' => 'reset=1&action=copy&id=%%id%%',
'extra' => 'onclick = "return confirm(\'Are you sure you want to copy this template?\');"',
],
];
}
return self::$_actionLinks;
}
public static function getColumnHeaders() {
if (!isset(self::$columnHeaders)) {
self::$columnHeaders = [
'template_title' => [
'name' => E::ts('Title'),
'sort' => 'template_title',
'direction' => CRM_Utils_Sort::DONTCARE,
],
'event_type_id' => [
'name' => E::ts('Event Type'),
],
'default_role_id' => [
'name' => E::ts('Participant Role'),
],
'participant_listing_id' => [
'name' => E::ts('Participant Listing'),
],
'is_public' => [
'name' => E::ts('Public Event'),
'sort' => 'is_public',
'direction' => CRM_Utils_Sort::DONTCARE,
],
'is_monetary' => [
'name' => E::ts('Paid Event'),
'sort' => 'is_monetary',
'direction' => CRM_Utils_Sort::DONTCARE,
],
'is_online_registration' => [
'name' => E::ts('Allow Online Registration'),
'sort' => 'is_online_registration',
'direction' => CRM_Utils_Sort::DONTCARE,
],
'is_active' => [
'name' => E::ts('Active'),
'sort' => 'is_active',
'direction' => CRM_Utils_Sort::DONTCARE,
],
'count' => [
'name' => E::ts('Events'),
],
'action' => [
'name' => '',
],
];
}
$allEventTemplates = $selector = NULL;
CRM_Utils_Hook::searchColumns('eventTemplate.columns', self::$columnHeaders, $allEventTemplates, $selector);
return self::$columnHeaders;
}
/**
* Render the entity fields for display (eg. convert to links / pseudoconstants etc).
*
* @param int $entityID
* @param string $fieldName
* @param mixed $fieldValue
*
* @return string
* @throws \CiviCRM_API3_Exception
*/
public static function renderField($entityID, $fieldName, $fieldValue) {
switch ($fieldName) {
case 'template_title':
$url = CRM_Utils_System::url('civicrm/event/manage/settings', "action=update&id={$entityID}&reset=1");
return "<a class='action-item crm-hover-button' href='{$url}'>{$fieldValue}</a>";
case 'event_type_id':
return CRM_Event_PseudoConstant::eventType($fieldValue);
case 'participant_listing_id':
return CRM_Event_PseudoConstant::participantListing($fieldValue);
case 'default_role_id':
return CRM_Event_PseudoConstant::participantRole($fieldValue);
case 'is_public':
case 'is_monetary':
case 'is_active':
case 'is_online_registration':
return $fieldValue ? E::ts('Yes') : E::ts('No');
case 'count':
$count = civicrm_api3('EventTemplate', 'getcount', ['template_id' => $entityID]);
$url = CRM_Utils_System::url("civicrm/event/manage/settings", "action=update&id={$entityID}&reset=1&selectedChild=linkedevents");
return "<a class='action-item crm-hover-button' href='{$url}'>{$count}</a>";
case 'action':
$action = array_sum(array_keys(CRM_AdvancedEvents_Page_ManageEventTemplate::actionLinks()));
return CRM_Core_Action::formLink(CRM_AdvancedEvents_Page_ManageEventTemplate::actionLinks(), $action,
['id' => $entityID],
E::ts('more'),
FALSE,
'eventTemplate.manage.action',
'Event',
$entityID
);
default:
return $fieldValue;
}
}
}
......@@ -13,12 +13,17 @@ The extension is licensed under [AGPL-3.0](LICENSE.txt).
* Ability to hide functionality in the UI that is not required (eg. Tell a Friend, Personal Campaigns).
* Replace existing repeat events functionality with new based directly on Event templates.
* Add an "Event Search/Selector" that allows "Copy Participants" and "Delete Event" actions.
* Allow to create templates from existing events.
* Replaces "Manage Event Templates" with a new datatables based page.
## Requirements
* PHP v7.1+
* CiviCRM 5.13+
### CiviCRM 5.13
Recommended: https://github.com/civicrm/civicrm-core/pull/14617 - Fix redirection on manage event tabs
### CiviCRM 5.7
Requires: https://github.com/civicrm/civicrm-core/pull/12769
Recommended: https://github.com/civicrm/civicrm-core/pull/12747 *without this change some page/form redirects may go to the wrong tab on submit (eg. "Info and Settings" instead of "Repeat")*
......@@ -41,3 +46,7 @@ Uninstalling will remove all "Event Template" links. No event data will be lost
## Usage
Navigate to Administer->CiviEvent->Advanced Events Configuration.
## Compatibility
* We have a copy of `CRM_Core_Page_EntityPageTrait` as `CRM_AdvancedEvents_OldVersionEntityPageTrait`. This can be removed once we update the minimum version to 5.17.
* There are a number of functions in `CRM_AdvancedEvents_OldVersionSupportTrait` which can be removed once the minimum version is updated.
......@@ -14,12 +14,16 @@ The extension is licensed under [AGPL-3.0](LICENSE.txt).
* Replace existing repeat events functionality with new based directly on Event templates.
* Add an "Event Search/Selector" that allows "Copy Participants" and "Delete Event" actions.
* Allow to create templates from existing events.
* Replaces "Manage Event Templates" with a new datatables based page.
## Requirements
* PHP v7.1+
* CiviCRM 5.13+
### CiviCRM 5.13
Recommended: https://github.com/civicrm/civicrm-core/pull/14617 - Fix redirection on manage event tabs
### CiviCRM 5.7