Commit a431aacb authored by ErikHommel's avatar ErikHommel

initial load

parents
This diff is collapsed.
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class for ContactType configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_ContactType {
protected $_apiParams = [];
/**
* Method to validate params for create
*
* @param $params
* @throws API_Exception when missing mandatory params
*/
protected function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
throw new Exception(E::ts('Missing mandatory param name in ') . __METHOD__, 9921);
}
$this->_apiParams = $params;
}
/**
* Method to create contact type
*
* @param array $params
* @return mixed
* @throws API_Exception when error from API ContactType Create
*/
public function create($params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
}
if (!isset($this->_apiParams['label']) || empty($this->_apiParams['label'])) {
$this->_apiParams['label'] = CRM_Acqconfigitems_Utils::buildLabelFromName($this->_apiParams['name']);
}
// if parent, retrieve parent_id
if (isset($this->_apiParams['parent'])) {
$this->_apiParams['parent_id'] = $this->getContactTypeIdWithName($this->_apiParams['parent']);
unset($this->_apiParams['parent']);
}
$this->_apiParams['is_active'] = 1;
try {
civicrm_api3('ContactType', 'create', $this->_apiParams);
$this->updateNavigationMenuUrl();
}
catch (CiviCRM_API3_Exception $ex) {
throw new API_Exception(E::ts('Could not create or update membership type with name ') . $this->_apiParams['name']
. E::ts(' in ') . __METHOD__ . E::ts(', error from API ContactType Create: ') . $ex->getMessage(), 9922);
}
}
/**
* Method to check if there is a navigation menu option for the contact type
* and if so, update name and url
*
* @access private
*/
private function updateNavigationMenuUrl() {
$query = "SELECT * FROM civicrm_navigation WHERE label = %1";
$label = "New " . $this->_apiParams['label'];
$dao = CRM_Core_DAO::executeQuery($query, [1 => [$label, 'String']]);
$validParent = ["New Organization", "New Individual", "New Household"];
$contactTypeName = $this->getContactTypeNameWithId($this->_apiParams['parent_id']);
$newUrl = CRM_Utils_System::url('civicrm/contact/add', 'ct=' . $contactTypeName . '&cst=' . $this->_apiParams['name'] . '&reset=1', TRUE);
$newName = "New " . $this->_apiParams['name'];
while ($dao->fetch()) {
// parent should be either New Organization, New Individual or New Household
if (isset($dao->parent_id)) {
$parentQuery = "SELECT name FROM civicrm_navigation WHERE id = %1";
$parentName = CRM_Core_DAO::singleValueQuery($parentQuery, [1 => [$dao->parent_id, 'Integer']]);
if (in_array($parentName, $validParent)) {
$update = "UPDATE civicrm_navigation SET url = %1, name = %2 WHERE id = %3";
$params = [
1 => [$newUrl, 'String'],
2 => [$newName, 'String'],
3 => [$dao->id, 'Integer'],
];
CRM_Core_DAO::executeQuery($update, $params);
}
}
}
}
/**
* Method to get contact type with name
*
* @param string $contactTypeName
* @return array|bool
* @access public
* @static
*/
public function getWithName($contactTypeName) {
try {
return civicrm_api3('ContactType', 'getsingle', ['name' => $contactTypeName]);
}
catch (CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
/**
* Method to disable contact type
*
* @param $contactTypeName
*/
public function disableContactType($contactTypeName) {
if (!empty($contactTypeName)) {
// catch any errors and ignore (disabling can be done manually if problems)
try {
// get contact type with name
$contactTypeId = civicrm_api3('ContactType', 'getvalue', ['name' => $contactTypeName, 'return' => 'id']);
$sqlContactType = "UPDATE civicrm_contact_type SET is_active = %1 WHERE id = %2";
CRM_Core_DAO::executeQuery($sqlContactType, [
1 => [0, 'Integer'],
2 => [$contactTypeId, 'Integer']]);
}
catch (CiviCRM_API3_Exception $ex) {
}
}
}
/**
* Method to enable contact type
*
* @param $contactTypeName
*/
public function enableContactType($contactTypeName) {
if (!empty($contactTypeName)) {
// catch any errors and ignore (disabling can be done manually if problems)
try {
// get contact type with name
$contactTypeId = civicrm_api3('ContactType', 'getvalue', ['name' => $contactTypeName, 'return' => 'id']);
$sqlContactType = "UPDATE civicrm_contact_type SET is_active = %1 WHERE id = %2";
CRM_Core_DAO::executeQuery($sqlContactType, [
1 => [1, 'Integer'],
2 => [$contactTypeId, 'Integer']]);
}
catch (CiviCRM_API3_Exception $ex) {
}
}
}
/**
* Method to uninstall contact type
*
* @param $contactTypeName
*/
public function uninstallContactType($contactTypeName) {
if (!empty($contactTypeName)) {
// catch any errors and ignore (disabling can be done manually if problems)
try {
// get contact type with name
$contactTypeId = civicrm_api3('ContactType', 'getvalue', ['name' => $contactTypeName, 'return' => 'id']);
civicrm_api3('ContactType', 'delete', ['id' => $contactTypeId]);
}
catch (CiviCRM_API3_Exception $ex) {
}
}
}
/**
* Method to get the contact type id with name
*
* @param $contactTypeName
* @return bool|null|string
*/
public function getContactTypeIdWithName($contactTypeName) {
$query = "SELECT id FROM civicrm_contact_type WHERE name = %1";
$contactTypeId = CRM_Core_DAO::singleValueQuery($query, [1 => [$contactTypeName, 'String']]);
if ($contactTypeId) {
return $contactTypeId;
}
return FALSE;
}
/**
* Method to get the contact type name with od
*
* @param $contactTypeId
* @return bool|string
*/
public function getContactTypeNameWithId($contactTypeId) {
$query = "SELECT name FROM civicrm_contact_type WHERE id = %1";
$contactTypeName = CRM_Core_DAO::singleValueQuery($query, [1 => [$contactTypeId, 'Integer']]);
if ($contactTypeName) {
return $contactTypeName;
}
return FALSE;
}
}
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class to build Custom Data Query
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_CustomDataQuery {
protected $_query = NULL;
protected $_queryParams = [];
private $_sourceParams = [];
private $_requiredParams = [];
private $_validQueryActions = [];
/**
* CRM_Acqconfigitems_CustomDataQuery constructor.
*
* @param array $params
*/
public function __construct($params) {
$this->_sourceParams = $params;
$this->_requiredParams = ['table_name', 'custom_fields', 'entity_id', 'query_action'];
$this->_validQueryActions = ['insert', 'update', 'select', 'delete'];
$this->validateSourceParams();
// params before query so it can be corrected for select
$this->setQueryParams();
$this->setQuery();
}
/**
* Method to get the query
*
* @return null
* @access public
*/
public function getQuery() {
return $this->_query;
}
/**
* Method to get the query parameters
*
* @return array
* @access public
*/
public function getQueryParams() {
return $this->_queryParams;
}
/**
* Method to build the relevant query from the sourceParams
*
* @access private
*/
private function setQuery() {
switch ($this->_sourceParams['query_action']) {
case "insert":
$this->buildInsertQuery();
break;
case "update":
$this->buildUpdateQuery();
break;
case "delete":
$this->buildDeleteQuery();
break;
case "select":
$this->buildSelectQuery();
break;
}
}
/**
* Method to build insert query
*
* @access private
*/
private function buildInsertQuery() {
$queryColumns = ["entity_id"];
$queryValues = ["%1"];
$index = 1;
foreach ($this->_sourceParams['custom_fields'] as $column => $value) {
$queryColumns[] = $column;
$index++;
$queryValues[] = "%".$index;
}
$columns = implode(",", $queryColumns);
$values = implode("," , $queryValues);
$this->_query = "INSERT INTO {$this->_sourceParams['table_name']} ({$columns}) VALUES({$values})";
}
/**
* Method to build update query
*
* @access private
*/
private function buildUpdateQuery() {
$querySets = [];
$index = 1;
foreach ($this->_sourceParams['custom_fields'] as $column => $value) {
$index++;
$querySets[] = $column." = %".$index;
}
$sets = implode(",", $querySets);
$this->_query = "UPDATE {$this->_sourceParams['table_name']} SET {$sets} WHERE entity_id = %1";
}
/**
* Method to build delete query
*
* @access private
*/
private function buildDeleteQuery() {
$this->_query = "DELETE FROM ".$this->_sourceParams['table_name'] . " WHERE entity_id = ". $this->_sourceParams['entity_id'];
}
/**
* Method to build select query
*
* @access private
*/
private function buildSelectQuery() {
$querySelectFields = [];
$this->_queryParams = [1 => [$this->_sourceParams['entity_id'], 'Integer']];
foreach ($this->_sourceParams['custom_fields'] as $column => $value) {
$querySelectFields[] = $column;
}
$this->_query = "SELECT ".implode(",", $querySelectFields)." FROM ".$this->_sourceParams['table_name']
." WHERE entity_id = %1";
}
/**
* Method to build query params
*
* @access private
*/
private function setQueryParams() {
$this->_queryParams[1] = [$this->_sourceParams['entity_id'], 'Integer'];
$index = 1;
foreach ($this->_sourceParams['custom_fields'] as $key => $value) {
$index++;
$this->_queryParams[$index] = [$value['value'], $value['type']];
}
}
/**
* Method to validate the source params before attempting to build the query
*
* @throws API_Exception when error found
* @access private
*/
private function validateSourceParams() {
foreach ($this->_requiredParams as $required) {
if (!isset($this->_sourceParams[$required])) {
throw new API_Exception(E::ts('Missing mandatory param ') . $required . E::ts(' in ') . __METHOD__, 9915);
}
}
if (empty($this->_sourceParams['custom_fields']) || !is_array($this->_sourceParams['custom_fields'])) {
throw new API_Exception(E::ts('Could not find any data to build a custom data query for in ') . __METHOD__, 9916);
}
if (empty($this->_sourceParams['table_name'])) {
throw new API_Exception(E::ts('Empty table_name in params, could not build query for custom data in ') . __METHOD__, 9917);
}
if (empty($this->_sourceParams['entity_id'])) {
throw new API_Exception(E::ts('Empty entity_id in params, could not build query for custom data in ') . __METHOD__, 9918);
}
if (empty($this->_sourceParams['query_action'])) {
throw new API_Exception(E::ts('Empty query_action in params, could not build query for custom data in ') . __METHOD__, 9919);
}
$this->_sourceParams['query_action'] = strtolower($this->_sourceParams['query_action']);
if (!in_array($this->_sourceParams['query_action'], $this->_validQueryActions)) {
throw new API_Exception(E::ts('Invalid query_action ') . $this->_sourceParams['query_action']
. E::ts(' in params, could not build query for custom data in ') .__METHOD__, 9920);
}
}
}
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class for CustomField configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_CustomField {
protected $_apiParams = [];
/**
* CRM_Acqconfigitems_CustomField constructor.
*/
public function __construct() {
$this->_apiParams = [];
}
/**
* Method to validate params for create
*
* @param array $params
* @throws API_Exception when missing mandatory params
*/
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name']) || !isset($params['custom_group_id'])
|| empty($params['custom_group_id'])) {
throw new API_Exception(ts(E::ts('When trying to create a Custom Field name and custom_group_id are
mandatory parameters and can not be empty in ') .__METHOD__), 9913);
}
$this->_apiParams = $params;
if (isset($this->_apiParams['option_group'])) {
$optionGroup = new CRM_Acqconfigitems_OptionGroup();
$found = $optionGroup->getWithName($this->_apiParams['option_group']);
if (!empty($found)) {
$this->_apiParams['option_group_id'] = $found['id'];
} else {
$created = $optionGroup->create(['name' => $this->_apiParams['option_group']]);
$this->_apiParams['option_group_id'] = $created['id'];
}
unset($this->_apiParams['option_group']);
}
// if no column name, default to name
if (!isset($this->_apiParams['column_name'])) {
$this->_apiParams['column_name'] = $this->_apiParams['name'];
}
}
/**
* Method to create or update custom field
*
* @param array $params
* @throws API_Exception when error from API CustomField Create
*/
public function create($params) {
$this->validateCreateParams($params);
$existing = $this->getWithNameCustomGroupId($this->_apiParams['name'], $this->_apiParams['custom_group_id']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
}
if (!isset($this->_apiParams['label']) || empty($this->_apiParams['label'])) {
$this->_apiParams['label'] = CRM_Acqconfigitems_Utils::buildLabelFromName($this->_apiParams['name']);
}
try {
$customField = civicrm_api3('CustomField', 'create', $this->_apiParams);
if (isset($params['option_group'])) {
$this->fixOptionGroups($customField['values'], $params['option_group']);
}
} catch (CiviCRM_API3_Exception $ex) {
throw new API_Exception(E::ts('Could not create or update custom field with name ') . $this->_apiParams['name']
. E::ts(' in custom group ') . $this->_apiParams['custom_group_id'] . E::ts(' in ') . __METHOD__
. E::ts(', error from API CustomField Create: ') . $ex->getMessage(), 9914);
}
}
/**
* Method to get custom field with name and custom group id
*
* @param string $name
* @param integer $customGroupId
* @return array|bool
*/
public function getWithNameCustomGroupId($name, $customGroupId) {
try {
return civicrm_api3('CustomField', 'getsingle', ['name' => $name, 'custom_group_id' => $customGroupId]);
} catch (CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
/**
* Method to fix option group in custom field because API always creates an option group whatever you do
* so change option group to the one we created and then remove the one api created
*
* @param array $customField
* @param string $optionGroupName
* @throws CiviCRM_API3_Exception
*/
protected function fixOptionGroups($customField, $optionGroupName) {
$optionGroup = new CRM_Acqconfigitems_OptionGroup();
$found = $optionGroup->getWithName($optionGroupName);
// only if found is not equal to created custom field value
if ($found['id'] != $customField[key($customField)]['option_group_id']) {
$qry = 'UPDATE civicrm_custom_field SET option_group_id = %1 WHERE id = %2';
$params = [
1 => [$found['id'], 'Integer'],
2 => [key($customField), 'Integer'],
];
CRM_Core_DAO::executeQuery($qry, $params);
civicrm_api3('OptionGroup', 'delete', ['id' => $customField[key($customField)]['option_group_id']]);
}
}
/**
* Method to remove custom fields that are not in the config custom group data
*
* @param int $customGroupId
* @param array $configCustomGroupData
* @return boolean
* @access public
* @static
*/
public static function removeUnwantedCustomFields($customGroupId, $configCustomGroupData) {
if (empty($customGroupId)) {
return FALSE;
}
// first get all existing custom fields from the custom group
try {
$existingCustomFields = civicrm_api3('CustomField', 'Get', ['custom_group_id' => $customGroupId]);
foreach ($existingCustomFields['values'] as $existingId => $existingField) {
// if existing field not in config custom data, delete custom field
if (!isset($configCustomGroupData['fields'][$existingField['name']])) {
civicrm_api3('CustomField', 'Delete', ['id' => $existingId]);
}
}
} catch (CiviCRM_API3_Exception $ex) {
return FALSE;
}
return TRUE;
}
/**
* Method to get all custom fields for a custom group id
*
* @param int $customGroupId
* @return array
*/
public function getAllWithCustomGroupId($customGroupId) {
try {
$customFields = civicrm_api3('CustomField', 'Get', ['custom_group_id' => $customGroupId]);
return $customFields['values'];
} catch (CiviCRM_API3_Exception $ex) {
return [];
}
}
}
\ No newline at end of file
This diff is collapsed.
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class for Group configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_Group {
protected $_apiParams = [];
/**
* CRM_Acqconfigitems_Group constructor.
*/
public function __construct() {
$this->_apiParams = [];
}
/**
* Method to validate params for create
*
* @param $params
* @throws API_Exception when missing mandatory params
*/
private function validateCreateParams($params) {
if (!isset($params['name']) || empty($params['name'])) {
throw new API_Exception(E::ts('Missing mandatory param name in ') . __METHOD__, 9909);
}
$this->_apiParams = $params;
}
/**
* Method to create or update group
*
* @param $params
* @throws Exception when error in API Group Create or when missing mandatory param name
* @access public
*/
public function create($params) {
$this->validateCreateParams($params);
$existing = $this->getWithName($this->_apiParams['name']);
if (isset($existing['id'])) {
$this->_apiParams['id'] = $existing['id'];
}
$this->sanitizeParams();
try {
civicrm_api3('Group', 'create', $this->_apiParams);
} catch (CiviCRM_API3_Exception $ex) {
throw new API_Exception(E::ts('Could not create or update group type with name ')
. $this->_apiParams['name'] . E::ts(' in ') . __METHOD__ . E::ts(', error from API Group Create: ') . $ex->getMessage(), 9910);
}
}
/**
* Method to get the group with a name
*
* @param string $groupName
* @return array|bool
* @access public
*/
public function getWithName($groupName) {
try {
return civicrm_api3('Group', 'getsingle', ['name' => $groupName]);
} catch (CiviCRM_API3_Exception $ex) {
return FALSE;
}
}
/**
* Method to sanitize params for group create api
*
* @access private
*/
private function sanitizeParams() {
if (!isset($this->_apiParams['is_active'])) {
$this->_apiParams['is_active'] = 1;
}
if (isset($this->_apiParams['group_type'])) {
$this->_apiParams['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR
. $this->_apiParams['group_type'] . CRM_Core_DAO::VALUE_SEPARATOR;
}
if (empty($this->_apiParams['title']) || !isset($this->_apiParams['title'])) {
$this->_apiParams['title'] = CRM_Acqconfigitems_Utils::buildLabelFromName($this->_apiParams['name']);
}
// if parent is set, retrieve parent number with name and set parents
if (isset($this->_apiParams['parent'])) {
$parentGroup = $this->getWithName($this->_apiParams['parent']);
if ($parentGroup) {
$this->_apiParams['parents'] = $parentGroup['id'];
}
unset($this->_apiParams['parent']);
}
}
}
\ No newline at end of file
<?php
use CRM_Acqconfigitems_ExtensionUtil as E;
/**
* Class for OptionGroup configuration
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 12 Jun 2019
* @license AGPL-3.0
*/
class CRM_Acqconfigitems_OptionGroup {
protected $_apiParams = [];
/**
* CRM_Acqconfigitems_OptionGroup constructor.
*/
public function __construct() {
$this->_apiParams = [];
}
/**
* Method to validate params for create