Merge pull request #11949 from magnolia61/Hide_waitinglist_for_past_and_registration_...
[civicrm-core.git] / api / v3 / EntityTag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This api exposes CiviCRM EntityTag records.
30 *
31 * Use this api to add/remove tags from a contact/activity/etc.
32 * To create/update/delete the tags themselves, use the Tag api.
33 *
34 * @package CiviCRM_APIv3
35 */
36
37 /**
38 * Get entity tags.
39 *
40 * @param array $params
41 *
42 * @return array
43 */
44 function civicrm_api3_entity_tag_get($params) {
45 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
46 }
47
48 /**
49 * Adjust Metadata for Get action.
50 *
51 * The metadata is used for setting defaults, documentation & validation.
52 *
53 * @param array $params
54 * Array of parameters determined by getfields.
55 */
56 function _civicrm_api3_entity_tag_get_spec(&$params) {
57 $params['entity_id']['api.aliases'] = array('contact_id');
58 $params['entity_table']['api.default'] = 'civicrm_contact';
59 }
60
61 /**
62 * Create an entity tag.
63 *
64 * @param array $params
65 *
66 * @return array
67 */
68 function civicrm_api3_entity_tag_create($params) {
69 return _civicrm_api3_entity_tag_common($params, 'add');
70 }
71
72 /**
73 * Mark entity tag as removed.
74 *
75 * @param array $params
76 *
77 * @return array
78 */
79 function civicrm_api3_entity_tag_delete($params) {
80
81 return _civicrm_api3_entity_tag_common($params, 'remove');
82 }
83
84 /**
85 * Modify metadata.
86 *
87 * @param array $params
88 */
89 function _civicrm_api3_entity_tag_delete_spec(&$params) {
90 // set as not required as tag_id also acceptable & no either/or std yet
91 $params['id']['api.required'] = 0;
92 }
93
94 /**
95 * Helper function for formatting tags (part of api v2 legacy).
96 *
97 * @param array $params
98 * @param string $op
99 *
100 * @return array
101 */
102 function _civicrm_api3_entity_tag_common($params, $op = 'add') {
103
104 $entityIDs = array();
105 $entityTable = 'civicrm_contact';
106 if (is_array($params)) {
107 foreach ($params as $n => $v) {
108 if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) {
109 $entityIDs[] = $v;
110 }
111 elseif (substr($n, 0, 6) == 'tag_id') {
112 $tagIDs[] = $v;
113 }
114 elseif (substr($n, 0, 12) == 'entity_table') {
115 $entityTable = $v;
116 }
117 }
118 }
119
120 if (empty($entityIDs)) {
121 return civicrm_api3_create_error('contact_id is a required field');
122 }
123
124 if (empty($tagIDs)) {
125 if ($op == 'remove') {
126 $tagIDs = array_keys(CRM_Core_BAO_EntityTag::getContactTags($entityIDs[0]));
127 }
128 else {
129 return civicrm_api3_create_error('tag_id is a required field');
130 }
131 }
132
133 $values = array('is_error' => 0);
134 if ($op == 'add') {
135 $values['total_count'] = $values['added'] = $values['not_added'] = 0;
136 foreach ($tagIDs as $tagID) {
137 list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable,
138 CRM_Utils_Array::value('check_permissions', $params));
139 $values['total_count'] += $te;
140 $values['added'] += $a;
141 $values['not_added'] += $na;
142 }
143 }
144 else {
145 $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
146 foreach ($tagIDs as $tagID) {
147 list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable, CRM_Utils_Array::value('check_permissions', $params));
148 $values['total_count'] += $te;
149 $values['removed'] += $r;
150 $values['not_removed'] += $nr;
151 }
152 }
153 if (empty($values['added']) && empty($values['removed'])) {
154 $values['is_error'] = 1;
155 $values['error_message'] = "Unable to $op tags";
156 }
157 return $values;
158 }