Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 4.3 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * File for the CiviCRM APIv3 entity tag functions | |
31 | * | |
32 | * @package CiviCRM_APIv3 | |
33 | * @subpackage API_EntityTag | |
34 | * | |
35 | * @copyright CiviCRM LLC (c) 2004-2013 | |
36 | * @version $Id: EntityTag.php 30879 2010-11-22 15:45:55Z shot $ | |
37 | */ | |
38 | ||
39 | /** | |
40 | * Include utility functions | |
41 | */ | |
42 | ||
43 | /** | |
44 | * {@getfields EntityTag_get} | |
45 | * @example EntityTagGet.php | |
46 | * | |
47 | * @param array $params | |
48 | * | |
49 | * @return array | |
50 | */ | |
51 | function civicrm_api3_entity_tag_get($params) { | |
52 | ||
53 | ||
54 | $values = CRM_Core_BAO_EntityTag::getTag($params['entity_id'], $params['entity_table']); | |
55 | $result = array(); | |
56 | foreach ($values as $v) { | |
57 | $result[$v] = array('tag_id' => $v); | |
58 | } | |
59 | return civicrm_api3_create_success($result, $params); | |
60 | } | |
11e09c59 TO |
61 | |
62 | /** | |
6a488035 TO |
63 | * Adjust Metadata for Get action |
64 | * | |
65 | * The metadata is used for setting defaults, documentation & validation | |
66 | * @param array $params array or parameters determined by getfields | |
67 | */ | |
68 | function _civicrm_api3_entity_tag_get_spec(&$params) { | |
69 | $params['entity_id']['api.required'] = 1; | |
70 | $params['entity_id']['api.aliases'] = array('contact_id'); | |
71 | $params['entity_table']['api.default'] = 'civicrm_contact'; | |
72 | } | |
73 | ||
6a488035 TO |
74 | /** |
75 | * | |
76 | * @param array $params | |
77 | * | |
78 | * @return array | |
79 | * {@getfields EntityTag_create} | |
80 | * @example EntityTagCreate.php | |
81 | */ | |
82 | function civicrm_api3_entity_tag_create($params) { | |
83 | ||
84 | ||
85 | return _civicrm_api3_entity_tag_common($params, 'add'); | |
86 | } | |
87 | ||
88 | /** | |
89 | *{@getfields EntityTag_delete} | |
90 | * @example EntityTagGet.php | |
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 | /** | |
6a488035 TO |
102 | * modify metadata |
103 | */ | |
104 | function _civicrm_api3_entity_tag_delete_spec(&$params) { | |
105 | // set as not required as tag_id also acceptable & no either/or std yet | |
106 | $params['id']['api.required'] = 0; | |
107 | } | |
108 | ||
109 | /** | |
110 | * | |
111 | * @param <type> $params | |
112 | * @param <type> $op | |
113 | * | |
114 | * @return <type> | |
115 | */ | |
116 | function _civicrm_api3_entity_tag_common($params, $op = 'add') { | |
117 | ||
118 | $entityIDs = array(); | |
119 | $tagsIDs = array(); | |
120 | $entityTable = 'civicrm_contact'; | |
121 | if (is_array($params)) { | |
122 | foreach ($params as $n => $v) { | |
123 | if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) { | |
124 | $entityIDs[] = $v; | |
125 | } | |
126 | elseif (substr($n, 0, 6) == 'tag_id') { | |
127 | $tagIDs[] = $v; | |
128 | } | |
129 | elseif (substr($n, 0, 12) == 'entity_table') { | |
130 | $entityTable = $v; | |
131 | } | |
132 | } | |
133 | } | |
134 | if (empty($entityIDs)) { | |
135 | return civicrm_api3_create_error('contact_id is a required field'); | |
136 | } | |
137 | ||
138 | if (empty($tagIDs)) { | |
139 | return civicrm_api3_create_error('tag_id is a required field'); | |
140 | } | |
141 | ||
142 | $values = array('is_error' => 0); | |
143 | if ($op == 'add') { | |
144 | $values['total_count'] = $values['added'] = $values['not_added'] = 0; | |
145 | foreach ($tagIDs as $tagID) { | |
146 | list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable); | |
147 | $values['total_count'] += $te; | |
148 | $values['added'] += $a; | |
149 | $values['not_added'] += $na; | |
150 | } | |
151 | } | |
152 | else { | |
153 | $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; | |
154 | foreach ($tagIDs as $tagID) { | |
155 | list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable); | |
156 | $values['total_count'] += $te; | |
157 | $values['removed'] += $r; | |
158 | $values['not_removed'] += $nr; | |
159 | } | |
160 | } | |
161 | return $values; | |
162 | } | |
163 |