| 1 | <?php |
| 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 | } |
| 61 | |
| 62 | /** |
| 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 | |
| 74 | /** |
| 75 | * |
| 76 | * @param <type> $params |
| 77 | * |
| 78 | * @return <type> |
| 79 | * @todo EM 7 Jan 2011 - believe this should be deleted |
| 80 | * @deprecated |
| 81 | */ |
| 82 | function civicrm_api3_entity_tag_display($params) { |
| 83 | |
| 84 | civicrm_api3_verify_one_mandatory($params, NULL, array('entity_id', 'contact_id')); |
| 85 | |
| 86 | $entityID = NULL; |
| 87 | $entityTable = 'civicrm_contact'; |
| 88 | |
| 89 | if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) { |
| 90 | $entityID = CRM_Utils_Array::value('contact_id', $params); |
| 91 | } |
| 92 | |
| 93 | |
| 94 | if (CRM_Utils_Array::value('entity_table', $params)) { |
| 95 | $entityTable = $params['entity_table']; |
| 96 | } |
| 97 | |
| 98 | $values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable); |
| 99 | $result = array(); |
| 100 | $tags = CRM_Core_PseudoConstant::tag(); |
| 101 | foreach ($values as $v) { |
| 102 | $result[] = $tags[$v]; |
| 103 | } |
| 104 | return implode(',', $result); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * |
| 109 | * @param array $params |
| 110 | * |
| 111 | * @return array |
| 112 | * {@getfields EntityTag_create} |
| 113 | * @example EntityTagCreate.php |
| 114 | */ |
| 115 | function civicrm_api3_entity_tag_create($params) { |
| 116 | |
| 117 | |
| 118 | return _civicrm_api3_entity_tag_common($params, 'add'); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | *{@getfields EntityTag_delete} |
| 123 | * @example EntityTagGet.php |
| 124 | * |
| 125 | * @param array $params |
| 126 | * |
| 127 | * @return array |
| 128 | */ |
| 129 | function civicrm_api3_entity_tag_delete($params) { |
| 130 | |
| 131 | return _civicrm_api3_entity_tag_common($params, 'remove'); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * modify metadata |
| 136 | */ |
| 137 | function _civicrm_api3_entity_tag_delete_spec(&$params) { |
| 138 | // set as not required as tag_id also acceptable & no either/or std yet |
| 139 | $params['id']['api.required'] = 0; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * |
| 144 | * @param <type> $params |
| 145 | * @param <type> $op |
| 146 | * |
| 147 | * @return <type> |
| 148 | */ |
| 149 | function _civicrm_api3_entity_tag_common($params, $op = 'add') { |
| 150 | |
| 151 | $entityIDs = array(); |
| 152 | $tagsIDs = array(); |
| 153 | $entityTable = 'civicrm_contact'; |
| 154 | if (is_array($params)) { |
| 155 | foreach ($params as $n => $v) { |
| 156 | if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) { |
| 157 | $entityIDs[] = $v; |
| 158 | } |
| 159 | elseif (substr($n, 0, 6) == 'tag_id') { |
| 160 | $tagIDs[] = $v; |
| 161 | } |
| 162 | elseif (substr($n, 0, 12) == 'entity_table') { |
| 163 | $entityTable = $v; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | if (empty($entityIDs)) { |
| 168 | return civicrm_api3_create_error('contact_id is a required field'); |
| 169 | } |
| 170 | |
| 171 | if (empty($tagIDs)) { |
| 172 | return civicrm_api3_create_error('tag_id is a required field'); |
| 173 | } |
| 174 | |
| 175 | $values = array('is_error' => 0); |
| 176 | if ($op == 'add') { |
| 177 | $values['total_count'] = $values['added'] = $values['not_added'] = 0; |
| 178 | foreach ($tagIDs as $tagID) { |
| 179 | list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable); |
| 180 | $values['total_count'] += $te; |
| 181 | $values['added'] += $a; |
| 182 | $values['not_added'] += $na; |
| 183 | } |
| 184 | } |
| 185 | else { |
| 186 | $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; |
| 187 | foreach ($tagIDs as $tagID) { |
| 188 | list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable); |
| 189 | $values['total_count'] += $te; |
| 190 | $values['removed'] += $r; |
| 191 | $values['not_removed'] += $nr; |
| 192 | } |
| 193 | } |
| 194 | return $values; |
| 195 | } |
| 196 | |