Merge pull request #227 from deepak-srivastava/crm
[civicrm-core.git] / api / v2 / Tag.php
CommitLineData
6a488035
TO
1<?php
2// $Id: Tag.php 45502 2013-02-08 13:32:55Z kurund $
3
4
5/*
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29*/
30
31/**
32 * File for the CiviCRM APIv2 tag functions
33 *
34 * @package CiviCRM_APIv2
35 * @subpackage API_Tag
36 *
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: Tag.php 45502 2013-02-08 13:32:55Z kurund $
39 * @todo Erik Hommel 15/12/2010 version to be implemented
40 */
41
42/**
43 * Include utility functions
44 */
45require_once 'api/v2/utils.php';
46
47/**
48 * Add a Tag. Tags are used to classify CRM entities (including Contacts, Groups and Actions).
49 *
50 * @param array $params an associative array used in
51 * construction / retrieval of the
52 * object
53 *
54 * @return array of newly created tag property values.
55 * @access public
56 * @todo Erik Hommel 15/12/2010 : check if function is ok for update
57 */
58function civicrm_tag_create(&$params) {
59 _civicrm_initialize();
60 $errorScope = CRM_Core_TemporaryErrorScope::useException();
61 try {
62
63 civicrm_verify_mandatory($params, 'CRM_Core_DAO_Tag', array('name'));
64
65 if (!array_key_exists('used_for', $params)) {
66 $params['used_for'] = "civicrm_contact";
67 }
68
69 require_once 'CRM/Core/BAO/Tag.php';
70 $ids = array('tag' => CRM_Utils_Array::value('tag', $params));
71 if (CRM_Utils_Array::value('tag', $params)) {
72 $ids['tag'] = $params['tag'];
73 }
74
75 $tagBAO = CRM_Core_BAO_Tag::add($params, $ids);
76
77 if (is_a($tagBAO, 'CRM_Core_Error')) {
78 return civicrm_create_error("Tag is not created");
79 }
80 else {
81 $values = array();
82 _civicrm_object_to_array($tagBAO, $values);
83 $tag = array();
84 $tag['tag_id'] = $values['id'];
85 $tag['name'] = $values['name'];
86 $tag['is_error'] = 0;
87 }
88 return $tag;
89 }
90 catch(PEAR_Exception$e) {
91 return civicrm_create_error($e->getMessage());
92 }
93 catch(Exception$e) {
94 return civicrm_create_error($e->getMessage());
95 }
96}
97
98/**
99 * Deletes an existing Tag
100 *
101 * @param array $params
102 *
103 * @return boolean | error true if successfull, error otherwise
104 * @access public
105 */
106function civicrm_tag_delete(&$params) {
107 _civicrm_initialize();
108 $errorScope = CRM_Core_TemporaryErrorScope::useException();
109 try {
110 civicrm_verify_mandatory($params, NULL, array('tag_id'));
111 $tagID = CRM_Utils_Array::value('tag_id', $params);
112
113 require_once 'CRM/Core/BAO/Tag.php';
114 return CRM_Core_BAO_Tag::del($tagID) ? civicrm_create_success() : civicrm_create_error(ts('Could not delete tag'));
115 }
116 catch(Exception$e) {
117 if (CRM_Core_Error::$modeException) {
118 throw $e;
119 }
120 return civicrm_create_error($e->getMessage());
121 }
122}
123
124/**
125 * Get a Tag.
126 *
127 * This api is used for finding an existing tag.
128 * Either id or name of tag are required parameters for this api.
129 *
130 * @param array $params an associative array of name/value pairs.
131 *
132 * @return array details of found tag else error
133 * @access public
134 */
135function civicrm_tag_get($params) {
136 _civicrm_initialize();
137 require_once 'CRM/Core/BAO/Tag.php';
138 $tagBAO = new CRM_Core_BAO_Tag();
139
140 if (!is_array($params)) {
141 return civicrm_create_error('Params is not an array.');
142 }
143 if (!isset($params['id']) && !isset($params['name'])) {
144 return civicrm_create_error('Required parameters missing.');
145 }
146
147 $properties = array(
148 'id', 'name', 'description', 'parent_id', 'is_selectable', 'is_hidden',
149 'is_reserved', 'used_for',
150 );
151 foreach ($properties as $name) {
152 if (array_key_exists($name, $params)) {
153 $tagBAO->$name = $params[$name];
154 }
155 }
156
157 if (!$tagBAO->find(TRUE)) {
158 return civicrm_create_error('Exact match not found.');
159 }
160
161 _civicrm_object_to_array($tagBAO, $tag);
162 $tag['is_error'] = 0;
163 return $tag;
164}
165