PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/api/v2/CustomGroup.php

https://github.com/michaelmcandrew/vaw
PHP | 248 lines | 122 code | 36 blank | 90 comment | 32 complexity | 85ca3ab605efc899e2e56c0e3a91701e MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.4 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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. * File for the CiviCRM APIv2 custom group functions
  29. *
  30. * @package CiviCRM_APIv2
  31. * @subpackage API_CustomGroup
  32. *
  33. * @copyright CiviCRM LLC (c) 2004-2011
  34. * @version $Id: CustomGroup.php 32998 2011-03-14 22:00:35Z kurund $
  35. */
  36. /**
  37. * Files required for this package
  38. */
  39. require_once 'api/v2/utils.php';
  40. /**
  41. * Most API functions take in associative arrays ( name => value pairs
  42. * as parameters. Some of the most commonly used parameters are
  43. * described below
  44. *
  45. * @param array $params an associative array used in construction
  46. * retrieval of the object
  47. *
  48. *
  49. */
  50. /**
  51. * Use this API to create a new group. See the CRM Data Model for custom_group property definitions
  52. * $params['class_name'] is a required field, class being extended.
  53. *
  54. * @param $params array Associative array of property name/value pairs to insert in group.
  55. *
  56. *
  57. * @return Newly create custom_group object
  58. *
  59. * @access public
  60. */
  61. function civicrm_custom_group_create( $params )
  62. {
  63. _civicrm_initialize( );
  64. if(! is_array($params) ) {
  65. return civicrm_create_error( "params is not an array");
  66. }
  67. // Require either param['class_name'] (string) - for backwards compatibility - OR parm['extends'] (array)
  68. // If passing extends array - set class_name (e.g. 'Contact', 'Participant'...) as extends[0]. You may optionally
  69. // pass an extends_entity_column_value as extends[1] (e.g. an Activity Type ID).
  70. if( isset( $params['class_name'] ) && trim( $params['class_name'] ) ) {
  71. $params['extends'][0] = trim( $params['class_name'] );
  72. } else {
  73. if ( ! isset( $params['extends'] ) || ! is_array( $params['extends'] ) ) {
  74. return civicrm_create_error( "Params must include either 'class_name' (string) or 'extends' (array)." );
  75. } else {
  76. if ( ! isset( $params['extends'][0] ) || ! trim( $params['extends'][0] ) ) {
  77. return civicrm_create_error( "First item in params['extends'] must be a class name (e.g. 'Contact')." );
  78. }
  79. }
  80. }
  81. $error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomGroup');
  82. require_once 'CRM/Utils/String.php';
  83. if (! isset( $params['title'] ) ||
  84. ! trim($params['title'] ) ) {
  85. return civicrm_create_error( "Title parameter is required." );
  86. }
  87. if ( ! isset( $params['style'] ) || ! trim( $params['style'] ) ) {
  88. $params['style'] = 'Inline';
  89. }
  90. if (is_a($error, 'CRM_Core_Error')) {
  91. return civicrm_create_error( $error->_errors[0]['message'] );
  92. }
  93. require_once 'CRM/Core/BAO/CustomGroup.php';
  94. $customGroup = CRM_Core_BAO_CustomGroup::create($params);
  95. _civicrm_object_to_array( $customGroup, $values );
  96. if ( is_a( $customGroup, 'CRM_Core_Error' ) ) {
  97. return civicrm_create_error( $customGroup->_errors[0]['message'] );
  98. } else {
  99. $values['is_error'] = 0;
  100. }
  101. if ( CRM_Utils_Array::value( 'html_type', $params ) ){
  102. $params['custom_group_id'] = $customGroup->id;
  103. $fieldValues = civicrm_custom_field_create( $params );
  104. $values = array_merge( $values, $fieldValues['result'] );
  105. }
  106. return $values;
  107. }
  108. /**
  109. * Use this API to delete an existing group.
  110. *
  111. * @param array id of the group to be deleted
  112. *
  113. * @return Null if success
  114. * @access public
  115. **/
  116. function civicrm_custom_group_delete($params)
  117. {
  118. _civicrm_initialize( );
  119. if ( !is_array( $params ) ) {
  120. return civicrm_create_error( 'Params is not an array' );
  121. }
  122. if ( ! CRM_Utils_Array::value( 'id', $params ) ) {
  123. return civicrm_create_error( 'Invalid or no value for Custom group ID' );
  124. }
  125. // convert params array into Object
  126. require_once 'CRM/Core/DAO/CustomGroup.php';
  127. $values = new CRM_Core_DAO_CustomGroup( );
  128. $values->id = $params['id'];
  129. $values->find(true);
  130. require_once 'CRM/Core/BAO/CustomGroup.php';
  131. $result = CRM_Core_BAO_CustomGroup::deleteGroup($values);
  132. return $result ? civicrm_create_success( ): civicrm_error('Error while deleting custom group');
  133. }
  134. /**
  135. * Defines 'custom field' within a group.
  136. *
  137. *
  138. * @param $params array Associative array of property name/value pairs to create new custom field.
  139. *
  140. * @return Newly created custom_field id array
  141. *
  142. * @access public
  143. *
  144. */
  145. function civicrm_custom_field_create( $params )
  146. {
  147. _civicrm_initialize( );
  148. if (! is_array($params) ) {
  149. return civicrm_create_error("params is not an array ");
  150. }
  151. if ( ! CRM_Utils_Array::value( 'custom_group_id', $params ) ) {
  152. return civicrm_create_error("Missing Required field :custom_group_id");
  153. }
  154. if ( !( CRM_Utils_Array::value( 'label', $params ) ) ) {
  155. return civicrm_create_error("Missing Required field :label");
  156. }
  157. if ( !( CRM_Utils_Array::value('option_type', $params ) ) ) {
  158. if( CRM_Utils_Array::value('id', $params ) ){
  159. $params['option_type'] = 2;
  160. } else {
  161. $params['option_type'] = 1;
  162. }
  163. }
  164. $error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomField');
  165. if (is_a($error, 'CRM_Core_Error')) {
  166. return civicrm_create_error( $error->_errors[0]['message'] );
  167. }
  168. // Array created for passing options in params
  169. if ( isset( $params['option_values'] ) && is_array( $params['option_values'] ) ) {
  170. foreach ( $params['option_values'] as $key => $value ){
  171. $params['option_label'][$value['weight']] = $value['label'];
  172. $params['option_value'][$value['weight']] = $value['value'];
  173. $params['option_status'][$value['weight']] = $value['is_active'];
  174. $params['option_weight'][$value['weight']] = $value['weight'];
  175. }
  176. }
  177. require_once 'CRM/Core/BAO/CustomField.php';
  178. $customField = CRM_Core_BAO_CustomField::create($params);
  179. $values['customFieldId'] = $customField->id;
  180. if ( is_a( $customField, 'CRM_Core_Error' ) && is_a( $column, 'CRM_Core_Error' ) ) {
  181. return civicrm_create_error( $customField->_errors[0]['message'] );
  182. } else {
  183. return civicrm_create_success($values);
  184. }
  185. }
  186. /**
  187. * Use this API to delete an existing custom group field.
  188. *
  189. * @param $params Array id of the field to be deleted
  190. *
  191. *
  192. * @access public
  193. **/
  194. function civicrm_custom_field_delete( $params )
  195. {
  196. _civicrm_initialize( );
  197. if ( !is_array( $params ) ) {
  198. return civicrm_create_error( 'Params is not an array' );
  199. }
  200. if ( ! CRM_Utils_Array::value( 'id', $params ) ) {
  201. return civicrm_create_error( 'Invalid or no value for Custom Field ID' );
  202. }
  203. require_once 'CRM/Core/DAO/CustomField.php';
  204. $field = new CRM_Core_DAO_CustomField( );
  205. $field->id = $params['id'];
  206. $field->find(true);
  207. require_once 'CRM/Core/BAO/CustomField.php';
  208. $customFieldDelete = CRM_Core_BAO_CustomField::deleteField( $field );
  209. return $customFieldDelete ?
  210. civicrm_create_error('Error while deleting custom field') :
  211. civicrm_create_success( );
  212. }