Unverified Commit 064037b6 authored by Eileen McNaughton's avatar Eileen McNaughton Committed by GitHub

Merge pull request #16867 from colemanw/dashboard

APIv4 - Add Dashboard & DashboardContact entities
parents 0667dc58 756d2540
......@@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Contact/DashboardContact.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:e2e825929c6c5404e7649fdde3162597)
* (GenCodeChecksum:65a1fcc31732f320ae7193e2a111f72b)
*/
/**
......@@ -50,7 +50,7 @@ class CRM_Contact_DAO_DashboardContact extends CRM_Core_DAO {
/**
* column no for this widget
*
* @var bool
* @var int
*/
public $column_no;
......@@ -139,7 +139,7 @@ class CRM_Contact_DAO_DashboardContact extends CRM_Core_DAO {
],
'column_no' => [
'name' => 'column_no',
'type' => CRM_Utils_Type::T_BOOLEAN,
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Column No'),
'description' => ts('column no for this widget'),
'where' => 'civicrm_dashboard_contact.column_no',
......
......@@ -511,7 +511,7 @@ LIMIT {$offset}, {$rowCount}
case 'delete_dashlet':
$dashletID = CRM_Utils_Type::escape($_REQUEST['dashlet_id'], 'Positive');
CRM_Core_BAO_Dashboard::deleteDashlet($dashletID);
CRM_Core_DAO_Dashboard::deleteRecord(['id' => $dashletID]);
}
CRM_Utils_System::civiExit();
......
......@@ -399,33 +399,20 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
$dashlet = new CRM_Core_DAO_Dashboard();
if (!$dashboardID) {
// Assign domain before search to allow identical dashlets in different domains.
if (empty($params['domain_id'])) {
$dashlet->domain_id = CRM_Core_Config::domainID();
}
else {
$dashlet->domain_id = $params['domain_id'] ?? NULL;
}
$dashlet->domain_id = $params['domain_id'] ?? CRM_Core_Config::domainID();
// Try and find an existing dashlet - it will be updated if found.
if (!empty($params['name'])) {
if (!empty($params['name']) || !empty($params['url'])) {
$dashlet->name = $params['name'] ?? NULL;
$dashlet->find(TRUE);
}
else {
$dashlet->url = $params['url'] ?? NULL;
$dashlet->find(TRUE);
}
}
else {
$dashlet->id = $dashboardID;
}
if (isset($params['permission']) && is_array($params['permission'])) {
$params['permission'] = implode(',', $params['permission']);
}
$dashlet->copyValues($params);
$dashlet->save();
......@@ -504,19 +491,18 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard {
}
/**
* Delete Dashlet.
*
* @deprecated
* @param int $dashletID
*
* @return bool
*/
public static function deleteDashlet($dashletID) {
$dashlet = new CRM_Core_DAO_Dashboard();
$dashlet->id = $dashletID;
if (!$dashlet->find(TRUE)) {
CRM_Core_Error::deprecatedFunctionWarning('CRM_Core_DAO_Dashboard::deleteRecord');
try {
CRM_Core_DAO_Dashboard::deleteRecord(['id' => $dashletID]);
}
catch (CRM_Core_Exception $e) {
return FALSE;
}
$dashlet->delete();
return TRUE;
}
......
......@@ -6,7 +6,7 @@
*
* Generated from xml/schema/CRM/Core/Dashboard.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:6b9dee0261c55c757550983c4fbd9aea)
* (GenCodeChecksum:06a6051f8aa495ae244afaa0e75aa7e0)
*/
/**
......@@ -214,6 +214,7 @@ class CRM_Core_DAO_Dashboard extends CRM_Core_DAO {
'entity' => 'Dashboard',
'bao' => 'CRM_Core_BAO_Dashboard',
'localizable' => 0,
'serialize' => self::SERIALIZE_COMMA,
],
'permission_operator' => [
'name' => 'permission_operator',
......
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
* $Id$
*
*/
namespace Civi\Api4;
/**
* Dashboard entity.
*
* A "dashboard" record represents an item that can be displayed on a user's home screen.
* E.g. the "News" or "Getting Started" dashboard items.
*
* Dashboards can also be created from CiviReports, and some extensions provide dashboards as well.
* Displaying an item to a user is done with the `DashboardContact` entity.
*
* @see \Civi\Api4\DashboardContact
* @package Civi\Api4
*/
class Dashboard extends Generic\DAOEntity {
}
......@@ -18,25 +18,16 @@
*
*/
namespace Civi\Api4;
namespace Civi\Api4\Service\Spec\Provider;
use Civi\Api4\Service\Spec\RequestSpec;
class UFMatchCreationSpecProvider implements Generic\SpecProviderInterface {
/**
* @inheritDoc
*/
public function modifySpec(RequestSpec $spec) {
$spec->getFieldByName('domain_id')->setRequired(FALSE);
}
/**
* @inheritDoc
*/
public function applies($entity, $action) {
return $entity === 'UFMatch' && $action === 'create';
}
/**
* DashboardContact entity.
*
* This places a dashboard item on a user's home screen.
*
* @see \Civi\Api4\Dashboard
* @package Civi\Api4
*/
class DashboardContact extends Generic\DAOEntity {
}
......@@ -23,20 +23,23 @@ namespace Civi\Api4\Service\Spec\Provider;
use Civi\Api4\Service\Spec\RequestSpec;
class StatusPreferenceCreationSpecProvider implements Generic\SpecProviderInterface {
class FieldDomainIdSpecProvider implements Generic\SpecProviderInterface {
/**
* @inheritDoc
* Generic create spec function to set sensible defaults for any entity with a "domain_id" field.
*/
public function modifySpec(RequestSpec $spec) {
$spec->getFieldByName('domain_id')->setRequired(FALSE);
$domainIdField = $spec->getFieldByName('domain_id');
if ($domainIdField) {
$domainIdField->setRequired(FALSE)->setDefaultValue('current_domain');;
}
}
/**
* @inheritDoc
*/
public function applies($entity, $action) {
return $entity === 'StatusPreference' && $action === 'create';
return $action === 'create';
}
}
......@@ -28,6 +28,10 @@ class NavigationSpecProvider implements Generic\SpecProviderInterface {
/**
* This runs for both create and get actions
*
* @fixme - for 'create', this is redundant with FieldDomainIdSpecProvider.
* @fixme - for 'get', this is inconsistent with other entities which do not set this default. We should standardize on setting or not setting it.
* @see FieldDomainIdSpecProvider
*
* @inheritDoc
*/
public function modifySpec(RequestSpec $spec) {
......
......@@ -28,7 +28,6 @@ class PaymentProcessorCreationSpecProvider implements Generic\SpecProviderInterf
* @inheritDoc
*/
public function modifySpec(RequestSpec $spec) {
$spec->getFieldByName('domain_id')->setRequired(FALSE)->setDefaultValue('current_domain');
// Billing mode is copied across from the payment processor type field in the BAO::create function.
$spec->getFieldByName('billing_mode')->setRequired(FALSE);
......
......@@ -65,12 +65,8 @@ function civicrm_api3_dashboard_get($params) {
* Array holding 'id' of dashlet to be deleted.
* @return array
* @throws API_Exception
* @throws CiviCRM_API3_Exception
*/
function civicrm_api3_dashboard_delete($params) {
if (CRM_Core_BAO_Dashboard::deleteDashlet($params['id'])) {
return civicrm_api3_create_success(1, $params, 'Dashboard', 'delete');
}
else {
throw new API_Exception('Could not delete dashlet');
}
return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
......@@ -44,9 +44,13 @@ class api_v3_DashboardContactTest extends CiviUnitTestCase {
$this->useTransaction(TRUE);
}
public function testDashboardContactCreate() {
/**
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testDashboardContactCreate($version) {
$this->_apiversion = $version;
$dashParams = [
'version' => 3,
'label' => 'New Dashlet element',
'name' => 'New Dashlet element',
'url' => 'civicrm/report/list&compid=99&reset=1',
......@@ -61,12 +65,11 @@ class api_v3_DashboardContactTest extends CiviUnitTestCase {
]);
$oldCount = CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_dashboard_contact where contact_id = {$contact['id']} AND is_active = 1 AND dashboard_id = {$dashresult['id']}");
$params = [
'version' => 3,
'contact_id' => $contact['id'],
'dashboard_id' => $dashresult['id'],
'is_active' => 1,
];
$dashboradContact = $this->callAPISuccess('dashboard_contact', 'create', $params);
$this->callAPISuccess('dashboard_contact', 'create', $params);
$newCount = CRM_Core_DAO::singleValueQuery("select count(*) from civicrm_dashboard_contact where contact_id = {$contact['id']} AND is_active = 1 AND dashboard_id = {$dashresult['id']}");
$this->assertEquals($oldCount + 1, $newCount);
}
......
......@@ -44,7 +44,12 @@ class api_v3_DashboardTest extends CiviUnitTestCase {
$this->useTransaction(TRUE);
}
public function testDashboardCreate() {
/**
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testDashboardCreate($version) {
$this->_apiversion = $version;
$oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_dashboard');
$params = [
'label' => 'New Dashlet element',
......@@ -62,11 +67,12 @@ class api_v3_DashboardTest extends CiviUnitTestCase {
}
/**
* CRM-19534.
*
* Ensure that Dashboard create works fine for non admins
* CRM-19534. Ensure that Dashboard create works fine for non admins
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testDashboardCreateByNonAdmins() {
public function testDashboardCreateByNonAdmins($version) {
$this->_apiversion = $version;
$loggedInContactID = $this->createLoggedInUser();
CRM_Core_Config::singleton()->userPermissionClass->permissions = [];
$params = [
......@@ -84,11 +90,12 @@ class api_v3_DashboardTest extends CiviUnitTestCase {
}
/**
* CRM-19217.
*
* Ensure that where is_active is specifically set to 0 is_active returns 0.
* CRM-19217. Ensure that where is_active is specifically set to 0 is_active returns 0.
* @param int $version
* @dataProvider versionThreeAndFour
*/
public function testDashboardCreateNotActive() {
public function testDashboardCreateNotActive($version) {
$this->_apiversion = $version;
$params = [
'label' => 'New Dashlet element',
'name' => 'New Dashlet element',
......
......@@ -56,7 +56,7 @@
</index>
<field>
<name>column_no</name>
<type>boolean</type>
<type>int</type>
<comment>column no for this widget</comment>
<default>0</default>
<add>3.1</add>
......
......@@ -76,6 +76,7 @@
<length>255</length>
<comment>Permission for the dashlet</comment>
<add>3.1</add>
<serialize>COMMA</serialize>
</field>
<field>
<name>permission_operator</name>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment