Commit 23b4f483 authored by Jaap Jansma's avatar Jaap Jansma
Browse files

Merge pull request #2 from johanv/1-smart_groups

smart group support
parents 351d2c7d 7ed19e00
......@@ -19,7 +19,8 @@ class CRM_Civiconfig_Config {
* CRM_Civiconfig_Config constructor.
*/
function __construct() {
// TODO: If the extensions dir is '[civicrm.files]/ext/' (which is the default)
// the construction below will not work.
$settings = civicrm_api3('Setting', 'Getsingle', array());
$this->resourcesPath = $settings['extensionsDir'].'/org.iida.civiconfig/resources/';
......
......@@ -37,10 +37,21 @@ class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
* @access public
*/
public function create(array $params) {
if (!empty($params['form_values'])) {
// Hack for smart groups.
$formValues = $params['form_values'];
unset($params['form_values']);
}
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
// Some exception handling here would be nice:
$this->handleSavedSearch($formValues, $existing['id']);
}
else {
// Some exception handling here would be nice:
$this->handleSavedSearch($formValues);
}
$this->sanitizeParams();
try {
......@@ -67,6 +78,42 @@ class CRM_Civiconfig_Entity_Group extends CRM_Civiconfig_Entity {
}
}
/**
* Creates or updates a saved search.
*
* A saved search has no name. So I will identify it by the ID of the
* smart group that uses the search. I assume that a saved search is
* used by at most one smart group.
*
* If the saved search is meant for a new smart group, just leave $groupId
* empty.
*
* The saved_search_id will be assigned to the API params.
*
* If $formValues is empty, nothing will happen.
*
* @param array $formValues
* @param int $groupId ID of existing smart group for the saved search.
*
* @throws ApiException
*/
public function handleSavedSearch($formValues, $groupId = NULL) {
if (empty($formValues)) {
return;
}
$params = array();
if (!empty($groupId)) {
$groupResult = civicrm_api3('Group', 'getsingle', array(
'id' => $groupId,
'return' => array('saved_search_id')
));
$params['id'] = $groupResult['saved_search_id'];
}
$params['form_values'] = $formValues;
$savedSearchResult = civicrm_api3('SavedSearch', 'create', $params);
$this->_apiParams['saved_search_id'] = $savedSearchResult['id'];
}
/**
* Method to sanitize params for group create api
*
......
......@@ -7,6 +7,17 @@
"group_type":"2",
"description":"Group for IIDA Donors"
},
"women": {
"name":"women",
"title":"Women",
"is_active":1,
"is_reserved":0,
"group_type":"2",
"description": "Smart group with all women",
"form_values": {
"gender_id":"1"
}
},
"subscriber": {
"name":"iida_subscriber",
"title": "Subscribers",
......
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