Unverified Commit bf43972e authored by awg_github's avatar awg_github Committed by GitHub
Browse files

Merge pull request #33 from agiliway/release_4_1

Release 4.1
parents a8b97f0a f5583d52
<?php
/**
* Class handles CiviMobileCustomFields api
*/
class CRM_CiviMobileAPI_Api_CiviMobileAvailableContactGroup_Get extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Returns results to api
*
* @return array
*/
public function getResult() {
$availableGroups = [];
$groups = $this->getGroups();
$contactGroupIds = $this->getContactGroups($this->validParams['contact_id']);
foreach ($groups as $group) {
if (!in_array($group['id'],$contactGroupIds)) {
$availableGroups[] = $group;
}
}
return $availableGroups;
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws \api_Exception
*/
protected function getValidParams($params) {
$contact = new CRM_Contact_BAO_Contact();
$contact->id = $params['contact_id'];
$contactExistence = $contact->find(TRUE);
if (empty($contactExistence)) {
throw new api_Exception('Contact(id=' . $params['contact_id'] . ') does not exist.', 'contact_does_not_exist');
}
return ['contact_id' => $params['contact_id']];
}
/**
* Gets active simple groups
*/
private function getGroups() {
$groups = [];
try {
$groupsData = civicrm_api3('Group', 'get', [
'sequential' => 1,
'is_active' => 1,
'saved_search_id' => ['IS NULL' => 1],
'options' => ['limit' => 0],
'return' => ['name', 'title', 'id'],
]);
} catch (CiviCRM_API3_Exception $e) {
return $groups;
}
if (!empty($groupsData['values'])) {
$groups = $groupsData['values'];
}
return $groups;
}
/**
* Gets list of group id with contact
*
* @param $contactId
*
* @return array
*/
private function getContactGroups($contactId) {
$groupIds = [];
try {
$groupContacts = civicrm_api3('GroupContact', 'get', [
'sequential' => 1,
'contact_id' => $contactId,
'options' => ['limit' => 0],
'return' => ["group_id"],
]);
} catch (CiviCRM_API3_Exception $e) {
return $groupIds;
}
if (!empty($groupContacts['values'])) {
foreach ($groupContacts['values'] as $groupContact) {
$groupIds[] = (int) $groupContact['group_id'];
}
}
return $groupIds;
}
}
<?php
/**
* Base class for CiviMobile API
*/
abstract class CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Validated params
*/
protected $validParams;
/**
* CRM_CiviMobileAPI_Api_CiviMobileBase constructor.
*
* @param $params
*/
public function __construct($params) {
$this->validParams = $this->getValidParams($params);
}
/**
* Returns results to api
*
* @return array
*/
public function getResult() {
return [];
}
/**
* Returns validated params
*
* @param $params
*
* @return array
*/
protected function getValidParams($params) {
return [];
}
}
<?php
/**
* Class handles CiviMobileCustomFields api
*/
class CRM_CiviMobileAPI_Api_CiviMobileCustomFields_Get extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* This sets if custom field don't have any value
*/
const EMPTY_VALUE_SYMBOL = 'NULL_VALUE';
/**
* Entity map
*/
private static $entityMap = [
'Individual' => [
'find_for' => ['Contact','Individual'],
],
'Organization' => [
'find_for' => ['Contact','Organization'],
],
'Household' => [
'find_for' => ['Contact','Household'],
],
];
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws \api_Exception
*/
protected function getValidParams($params) {
if (!in_array($params['entity'], self::getAvailableEntities())) {
throw new api_Exception('Invalid entity. Available values: (' . implode(', ', self::getAvailableEntities()) . ')', 'used_for_invalid_value');
}
return [
'find_for' => self::$entityMap[$params['entity']]['find_for'],
'entity_id' => $params['entity_id'],
];
}
/**
* Returns results to api
*
* @return array
*/
public function getResult() {
$result = [];
try {
$customGroups = civicrm_api3('CustomGroup', 'get', [
'sequential' => 1,
'extends' => ['IN' => $this->validParams['find_for']],
'is_active' => 1,
'options' => ['limit' => 0],
]);
} catch (CiviCRM_API3_Exception $e) {
return [];
}
if (empty($customGroups['values'])) {
return [];
}
foreach ($customGroups['values'] as $customGroup) {
$result[] = $this->prepareCustomGroup($customGroup);
}
return $result;
}
/**
* Returns prepared CustomGroup
*
* @param $customGroup
*
* @return array
*/
private function prepareCustomGroup($customGroup) {
$customGroupData = [
'id' => $customGroup['id'],
'name' => $customGroup['name'],
'title' => $customGroup['title'],
'style' => $customGroup['style'],
'is_multiple' => $customGroup['is_multiple'],
'custom_fields' => [],
];
try {
$customFields = civicrm_api3('CustomField', 'get', [
'sequential' => 1,
'custom_group_id' => $customGroup['id'],
'options' => ['limit' => 0],
'is_active' => 1,
]);
} catch (CiviCRM_API3_Exception $e) {
return $customGroupData;
}
if (empty($customFields['values'])) {
return $customGroupData;
}
foreach ($customFields['values'] as $customField) {
$customGroupData['custom_fields'][] = $this->prepareCustomField($customField, $customGroup);
}
return $customGroupData;
}
/**
* Returns prepared CustomField
*
* @param $customField
* @param $customGroup
*
* @return array
*/
private function prepareCustomField($customField, $customGroup) {
$customFieldId = CRM_CiviMobileAPI_Utils_CustomField::getId($customGroup['name'], $customField['name']);
$availableValues = [];
if (!empty($customField['option_group_id'])) {
$availableValues = CRM_CiviMobileAPI_Utils_OptionValue::getGroupValues($customField['option_group_id'], ['is_active' => 1]);
}
if ($customField['html_type'] == 'Radio' && $customField['data_type'] == "Boolean") {
$availableValues = ['0','1'];
}
$prepareCustomField = [
"id" => $customField['id'],
"name" => $customField['name'],
"default_value" => $customField['default_value'],
"text_length" => (!empty($customField['text_length'])) ? (int) $customField['text_length'] : "NULL",
"is_view" => $customField['is_view'],
"label" => $customField['label'],
"data_type" => $customField['data_type'],
"html_type" => $customField['html_type'],
"is_required" => $customField['is_required'],
"current_value" => $this->getCurrentValue($customFieldId),
"available_values" => $availableValues
];
if ($prepareCustomField['data_type'] == 'Money'
&& ($prepareCustomField['html_type'] == 'Radio' || $prepareCustomField['html_type'] == 'Select') ) {
$prepareCustomField['current_value'] = preg_replace("/.00$/", "", $prepareCustomField['current_value']);
}
return $prepareCustomField;
}
/**
* Gets available entities for that api
*
* @return array
*/
public static function getAvailableEntities() {
return array_keys(self::$entityMap);
}
/**
* Gets current values
*
* @param $customFieldId
*
* @return string
*/
private function getCurrentValue($customFieldId) {
try {
$dbData = CRM_Core_BAO_CustomField::getTableColumnGroup($customFieldId);
} catch (Exception $e) {
return self::EMPTY_VALUE_SYMBOL;
}
$table = $dbData[0];
$column = $dbData[1];
$query = "SELECT {$table}.{$column} as current_value FROM {$table} WHERE {$table}.entity_id = {$this->validParams['entity_id']}";
$result = CRM_Core_DAO::executeQuery($query);
if ($result->fetch()) {
return ($result->current_value === NULL) ? self::EMPTY_VALUE_SYMBOL : $result->current_value;
}
return self::EMPTY_VALUE_SYMBOL;
}
}
......@@ -3,7 +3,7 @@
/**
* Class handles CiviMobileParticipantPaymentCreate api
*/
class CRM_CiviMobileAPI_Utils_Api_CiviMobileParticipantPayment_Create {
class CRM_CiviMobileAPI_Api_CiviMobileParticipantPayment_Create {
/**
* Api request params
......
<?php
class CRM_CiviMobileAPI_Api_CiviMobileTagStructure_Get extends CRM_CiviMobileAPI_Api_CiviMobileBase {
/**
* Entity Tag items
*/
private $entityTags;
/**
* Api tag map
*/
private $tagMap = [
'Contacts' => 'civicrm_contact',
'Activities' => 'civicrm_activity',
'Cases' => 'civicrm_case',
'Attachements' => 'civicrm_file',
];
/**
* Returns results to api
*
* @return array
*/
public function getResult() {
$this->entityTags = $this->getentityTags($this->tagMap[$this->validParams['entity']], $this->validParams['entity_id']);
$tagSets = $this->getTagSetsItems();
$tagTree = $this->getTagTreeItems();
if ($this->validParams['is_tag_tree_show_in_two_level']) {
$tagTree = $this->transformTagTreeItems($tagTree);
}
return [['tag_tree' => $tagTree, 'tag_sets' => $tagSets]];
}
/**
* Returns validated params
*
* @param $params
*
* @return array
* @throws \api_Exception
*/
public function getValidParams($params) {
$availableEntities = array_keys($this->tagMap);
if (!in_array($params['entity'], $availableEntities)) {
throw new api_Exception('Invalid entity. Available values: (' . implode(', ', $availableEntities) . ')', 'used_for_invalid_value');
}
if (empty($params['entity_id'])) {
throw new api_Exception('"entity_id" is required field.', 'required_filed');
}
return [
'is_tag_tree_show_in_two_level' => (bool) $params['is_tag_tree_show_in_two_level'],
'entity' => $params['entity'],
'entity_id' => $params['entity_id'],
];
}
/**
* @param $parentTag
*
* @return mixed
*/
public function setChildTags($parentTag) {
$parentTag['child_tags'] = [];
try {
$childItems = civicrm_api3('Tag', 'get', [
'sequential' => 1,
'parent_id' => $parentTag['id'],
'used_for' => ['LIKE' => "%" . $this->tagMap[$this->validParams['entity']] . "%"],
'options' => ['limit' => 0],
]);
} catch (CiviCRM_API3_Exception $e) {
return $parentTag;
}
if (!empty($childItems['values'])) {
foreach ($childItems['values'] as $childItem) {
$childItem = $this->setIsTagChecked($childItem);
$parentTag['child_tags'][] = $this->setChildTags($childItem);
}
}
return $parentTag;
}
/**
* Gets entity tags by 'entity table' and 'entity id'
*
* @param $entityTable
* @param $entityId
*
* @return array
*/
private function getEntityTags($entityTable, $entityId) {
$entityTags = [];
try {
$entityTag = civicrm_api3('EntityTag', 'get', [
'sequential' => 1,
'entity_id' => $entityId,
'entity_table' => $entityTable,
'options' => ['limit' => 0],
]);
} catch (CiviCRM_API3_Exception $e) {
return $entityTags;
}
if (!empty($entityTag['values'])) {
foreach ($entityTag['values'] as $entityTagItem) {
$entityTags[$entityTagItem['tag_id']] = $entityTagItem;
}
}
return $entityTags;
}
/**
* Gets tag sets items
*
* @return array
*/
private function getTagSetsItems() {
$tagSets = [];
try {
$tagSetItems = civicrm_api3('Tag', 'get', [
'sequential' => 1,
'is_tagset' => 1,
'options' => ['limit' => 0],
'used_for' => ['LIKE' => "%" . $this->tagMap[$this->validParams['entity']] . "%"],
]);
} catch (CiviCRM_API3_Exception $e) {
$tagSetItems = [];
}
if (!empty($tagSetItems['values'])) {
foreach ($tagSetItems['values'] as $tagSetItem) {
$tagSetItem = $this->setIsTagChecked($tagSetItem);
try {
$childTags = civicrm_api3('Tag', 'get', [
'sequential' => 1,
'used_for' => ['LIKE' => "%" . $this->tagMap[$this->validParams['entity']] . "%"],
'is_tagset' => 0,
'options' => ['limit' => 0],
'parent_id' => $tagSetItem['id'],
]);
} catch (CiviCRM_API3_Exception $e) {
$childTags = [];
}
$tagSetItem['child_tags'] = [];
if (!empty($childTags['values'])) {
foreach ($childTags['values'] as $childTag) {
$tagSetItem['child_tags'][] = $this->setIsTagChecked($childTag);
}
}
$tagSets[] = $tagSetItem;
}
}
return $tagSets;
}
/**
* Gets tag tree items
*
* @return array
*/
private function getTagTreeItems() {
$tagTree = [];
try {
$tagTreeItems = civicrm_api3('Tag', 'get', [
'sequential' => 1,
'parent_id' => ['IS NULL' => 1],
'used_for' => ['LIKE' => "%" . $this->tagMap[$this->validParams['entity']] . "%"],
'is_tagset' => 0,
'options' => ['limit' => 0],
]);
} catch (CiviCRM_API3_Exception $e) {
$tagTreeItems = [];
}
if (!empty($tagTreeItems['values'])) {
foreach ($tagTreeItems['values'] as $tagTreeItem) {
$tagTreeItem = $this->setIsTagChecked($tagTreeItem);
$tagTree[] = $this->setChildTags($tagTreeItem);
}
}
return $tagTree;
}
/**
* Sets 'is_tag_checked' and 'entity_tag_id' filed for tag
*
* @param $tag
*
* @return mixed
*/
private function setIsTagChecked($tag) {
if (empty($tag['id'])) {
return $tag;
}
if (!empty(($this->entityTags[$tag['id']]))) {
$tag['is_tag_checked'] = 1;
$tag['entity_tag_id'] = ($this->entityTags[$tag['id']]['id']);
} else {
$tag['is_tag_checked'] = 0;
$tag['entity_tag_id'] = 'NULL';
}
return $tag;
}
/**
* Transform tag tree items in two level
*
* @param $tagTreeItems
*
* @return mixed
*/
private function transformTagTreeItems($tagTreeItems) {
if (empty($tagTreeItems)) {
return [];
}
$transformedTags = [];
foreach ($tagTreeItems as $key => $tag) {
$childTags = [];
foreach ($tag['child_tags'] as $childTag) {
$collection = $this->collectChildTags($childTag, []);
$childTags = array_merge($childTags, $collection);
}
$tagTreeItems[$key]['child_tags'] = $childTags;
$transformedTags[] = $tagTreeItems[$key];
}
return $transformedTags;
}
/**
* Collect child tags
*
* @param $childTag
* @param $collection
*
* @return array