Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * File for the CiviCRM APIv3 entity tag functions | |
30 | * | |
31 | * @package CiviCRM_APIv3 | |
32 | * @subpackage API_EntityTag | |
33 | * | |
731a0992 | 34 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
35 | * @version $Id: EntityTag.php 30879 2010-11-22 15:45:55Z shot $ |
36 | */ | |
37 | ||
38 | /** | |
39 | * Include utility functions | |
40 | */ | |
41 | ||
42 | /** | |
0965e988 | 43 | * Get entity tags. |
6a488035 TO |
44 | * |
45 | * @param array $params | |
46 | * | |
47 | * @return array | |
48 | */ | |
49 | function civicrm_api3_entity_tag_get($params) { | |
50 | ||
22e263ad | 51 | if (empty($params['entity_id'])) { |
283c85e9 | 52 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
53 | } | |
92e4c2a5 | 54 | else { |
283c85e9 | 55 | //do legacy non-standard behaviour |
56 | $values = CRM_Core_BAO_EntityTag::getTag($params['entity_id'], $params['entity_table']); | |
57 | $result = array(); | |
58 | foreach ($values as $v) { | |
59 | $result[$v] = array('tag_id' => $v); | |
60 | } | |
61 | return civicrm_api3_create_success($result, $params); | |
6a488035 | 62 | } |
6a488035 | 63 | } |
11e09c59 TO |
64 | |
65 | /** | |
0965e988 EM |
66 | * Adjust Metadata for Get action. |
67 | * | |
68 | * The metadata is used for setting defaults, documentation & validation. | |
6a488035 | 69 | * |
cf470720 TO |
70 | * @param array $params |
71 | * Array or parameters determined by getfields. | |
6a488035 TO |
72 | */ |
73 | function _civicrm_api3_entity_tag_get_spec(&$params) { | |
6a488035 TO |
74 | $params['entity_id']['api.aliases'] = array('contact_id'); |
75 | $params['entity_table']['api.default'] = 'civicrm_contact'; | |
76 | } | |
77 | ||
6a488035 | 78 | /** |
0965e988 | 79 | * Create an entity tag. |
6a488035 TO |
80 | * |
81 | * @param array $params | |
82 | * | |
83 | * @return array | |
6a488035 TO |
84 | */ |
85 | function civicrm_api3_entity_tag_create($params) { | |
6a488035 TO |
86 | return _civicrm_api3_entity_tag_common($params, 'add'); |
87 | } | |
88 | ||
89 | /** | |
0965e988 | 90 | * Mark entity tag as removed. |
6a488035 TO |
91 | * |
92 | * @param array $params | |
93 | * | |
94 | * @return array | |
95 | */ | |
96 | function civicrm_api3_entity_tag_delete($params) { | |
97 | ||
98 | return _civicrm_api3_entity_tag_common($params, 'remove'); | |
99 | } | |
11e09c59 TO |
100 | |
101 | /** | |
0965e988 EM |
102 | * Modify metadata. |
103 | * | |
d0997921 | 104 | * @param array $params |
6a488035 TO |
105 | */ |
106 | function _civicrm_api3_entity_tag_delete_spec(&$params) { | |
107 | // set as not required as tag_id also acceptable & no either/or std yet | |
108 | $params['id']['api.required'] = 0; | |
109 | } | |
110 | ||
111 | /** | |
0965e988 | 112 | * Helper function for formatting tags (part of api v2 legacy). |
6a488035 | 113 | * |
72b3a70c | 114 | * @param array $params |
77b97be7 | 115 | * @param string $op |
6a488035 | 116 | * |
a6c01b45 | 117 | * @return array |
6a488035 TO |
118 | */ |
119 | function _civicrm_api3_entity_tag_common($params, $op = 'add') { | |
120 | ||
121 | $entityIDs = array(); | |
6a488035 TO |
122 | $entityTable = 'civicrm_contact'; |
123 | if (is_array($params)) { | |
124 | foreach ($params as $n => $v) { | |
125 | if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) { | |
126 | $entityIDs[] = $v; | |
127 | } | |
128 | elseif (substr($n, 0, 6) == 'tag_id') { | |
129 | $tagIDs[] = $v; | |
130 | } | |
131 | elseif (substr($n, 0, 12) == 'entity_table') { | |
132 | $entityTable = $v; | |
133 | } | |
134 | } | |
135 | } | |
136 | if (empty($entityIDs)) { | |
137 | return civicrm_api3_create_error('contact_id is a required field'); | |
138 | } | |
139 | ||
140 | if (empty($tagIDs)) { | |
141 | return civicrm_api3_create_error('tag_id is a required field'); | |
142 | } | |
143 | ||
144 | $values = array('is_error' => 0); | |
145 | if ($op == 'add') { | |
146 | $values['total_count'] = $values['added'] = $values['not_added'] = 0; | |
147 | foreach ($tagIDs as $tagID) { | |
148 | list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable); | |
149 | $values['total_count'] += $te; | |
150 | $values['added'] += $a; | |
151 | $values['not_added'] += $na; | |
152 | } | |
153 | } | |
154 | else { | |
155 | $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; | |
156 | foreach ($tagIDs as $tagID) { | |
157 | list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable); | |
158 | $values['total_count'] += $te; | |
159 | $values['removed'] += $r; | |
160 | $values['not_removed'] += $nr; | |
161 | } | |
162 | } | |
163 | return $values; | |
164 | } |