-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SearchKit - Add display of type
entity
An entity display does not produce user-facing output, instead it writes to a SQL table which can then be queried from SearchKit, the API, or other SQL-based tools like Drupal Views.
- Loading branch information
Showing
17 changed files
with
840 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Civi\Api4\Action\SKEntity; | ||
|
||
use Civi\API\Request; | ||
use Civi\Api4\Generic\AbstractAction; | ||
use Civi\Api4\Generic\Result; | ||
use Civi\Api4\Query\Api4SelectQuery; | ||
|
||
/** | ||
* Store the results of a SearchDisplay as a SQL table. | ||
* | ||
* For displays of type `entity` which save to a DB table | ||
* rather than outputting anything to the user. | ||
* | ||
* @package Civi\Api4\Action\SKEntity | ||
*/ | ||
class Refresh extends AbstractAction { | ||
|
||
/** | ||
* @param \Civi\Api4\Generic\Result $result | ||
* @throws \CRM_Core_Exception | ||
*/ | ||
public function _run(Result $result) { | ||
[, $displayName] = explode('_', $this->getEntityName(), 2); | ||
$display = \Civi\Api4\SearchDisplay::get(FALSE) | ||
->setSelect(['settings', 'saved_search_id.api_entity', 'saved_search_id.api_params']) | ||
->addWhere('type', '=', 'entity') | ||
->addWhere('name', '=', $displayName) | ||
->execute()->single(); | ||
|
||
$apiParams = $display['saved_search_id.api_params']; | ||
foreach ($display['settings']['sort'] ?? [] as $item) { | ||
$apiParams['orderBy'][$item[0]] = $item[1]; | ||
} | ||
$api = Request::create($display['saved_search_id.api_entity'], 'get', $apiParams); | ||
$query = new Api4SelectQuery($api); | ||
$query->forceSelectId = FALSE; | ||
$select = $query->getSql(); | ||
$tableName = 'civicrm_sk_' . \CRM_Utils_String::convertStringToSnakeCase($displayName); | ||
$columnSpecs = array_column($display['settings']['columns'], 'spec'); | ||
$columns = implode(', ', array_column($columnSpecs, 'name')); | ||
\CRM_Core_DAO::executeQuery("TRUNCATE TABLE `$tableName`"); | ||
\CRM_Core_DAO::executeQuery("INSERT INTO `$tableName` ($columns) $select"); | ||
} | ||
|
||
} |
Oops, something went wrong.