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

https://github.com/michaelmcandrew/vaw · PHP · 258 lines · 139 code · 25 blank · 94 comment · 27 complexity · 681ca9375d33a6542206b23138c933b1 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 membership status functions
  29. *
  30. * @package CiviCRM_APIv2
  31. * @subpackage API_Membership
  32. *
  33. * @copyright CiviCRM LLC (c) 2004-2011
  34. * @version $Id: MembershipStatus.php 32998 2011-03-14 22:00:35Z kurund $
  35. *
  36. */
  37. /**
  38. * Files required for this package
  39. */
  40. require_once 'api/v2/utils.php';
  41. /**
  42. * Create a Membership Status
  43. *
  44. * This API is used for creating a Membership Status
  45. *
  46. * @param array $params an associative array of name/value property values of civicrm_membership_status
  47. * @return array of newly created membership status property values.
  48. * @access public
  49. */
  50. function civicrm_membership_status_create(&$params)
  51. {
  52. _civicrm_initialize();
  53. if ( ! is_array($params) ) {
  54. return civicrm_create_error('Params is not an array.');
  55. }
  56. if ( empty($params) ) {
  57. return civicrm_create_error('Params can not be empty.');
  58. }
  59. $name = CRM_Utils_Array::value( 'name', $params );
  60. if ( !$name ) $name = CRM_Utils_Array::value( 'label', $params );
  61. if ( !$name ) {
  62. return civicrm_create_error('Missing required fields');
  63. }
  64. //don't allow duplicate names.
  65. require_once 'CRM/Member/DAO/MembershipStatus.php';
  66. $status = new CRM_Member_DAO_MembershipStatus( );
  67. $status->name = $name;
  68. if ( $status->find( true ) ) {
  69. return civicrm_create_error( ts( 'A membership status with this name already exists.' ) );
  70. }
  71. require_once 'CRM/Member/BAO/MembershipStatus.php';
  72. $ids = array();
  73. $membershipStatusBAO = CRM_Member_BAO_MembershipStatus::add($params, $ids);
  74. if ( is_a( $membershipStatusBAO, 'CRM_Core_Error' ) ) {
  75. return civicrm_create_error( "Membership is not created" );
  76. } else {
  77. $values = array( );
  78. $values['id'] = $membershipStatusBAO->id;
  79. $values['is_error'] = 0;
  80. return $values;
  81. }
  82. }
  83. /**
  84. * Get a membership status.
  85. *
  86. * This api is used for finding an existing membership status.
  87. *
  88. * @param array $params an associative array of name/value property values of civicrm_membership_status
  89. *
  90. * @return Array of all found membership status property values.
  91. * @access public
  92. */
  93. function civicrm_membership_status_get(&$params)
  94. {
  95. _civicrm_initialize();
  96. if ( ! is_array($params) ) {
  97. return civicrm_create_error('Params is not an array.');
  98. }
  99. require_once 'CRM/Member/BAO/MembershipStatus.php';
  100. $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus();
  101. $properties = array_keys($membershipStatusBAO->fields());
  102. foreach ($properties as $name) {
  103. if (array_key_exists($name, $params)) {
  104. $membershipStatusBAO->$name = $params[$name];
  105. }
  106. }
  107. if ( $membershipStatusBAO->find() ) {
  108. $membershipStatus = array();
  109. while ( $membershipStatusBAO->fetch() ) {
  110. _civicrm_object_to_array( clone($membershipStatusBAO), $membershipStatus );
  111. $membershipStatuses[$membershipStatusBAO->id] = $membershipStatus;
  112. }
  113. } else {
  114. return civicrm_create_error('Exact match not found');
  115. }
  116. return $membershipStatuses;
  117. }
  118. /**
  119. * Update an existing membership status
  120. *
  121. * This api is used for updating an existing membership status.
  122. * Required parrmeters : id of a membership status
  123. *
  124. * @param Array $params an associative array of name/value property values of civicrm_membership_status
  125. *
  126. * @return array of updated membership status property values
  127. * @access public
  128. */
  129. function &civicrm_membership_status_update( &$params )
  130. {
  131. _civicrm_initialize();
  132. if ( !is_array( $params ) ) {
  133. return civicrm_create_error( 'Params is not an array' );
  134. }
  135. if ( !isset($params['id']) ) {
  136. return civicrm_create_error( 'Required parameter missing' );
  137. }
  138. //don't allow duplicate names.
  139. $name = CRM_Utils_Array::value( 'name', $params );
  140. if ( $name ) {
  141. require_once 'CRM/Member/DAO/MembershipStatus.php';
  142. $status = new CRM_Member_DAO_MembershipStatus( );
  143. $status->name = $params['name'];
  144. if ( $status->find( true ) && $status->id != $params['id'] ) {
  145. return civicrm_create_error( ts( 'A membership status with this name already exists.' ) );
  146. }
  147. }
  148. require_once 'CRM/Member/BAO/MembershipStatus.php';
  149. $membershipStatusBAO = new CRM_Member_BAO_MembershipStatus( );
  150. $membershipStatusBAO->id = $params['id'];
  151. if ($membershipStatusBAO->find(true)) {
  152. $fields = $membershipStatusBAO->fields( );
  153. foreach ( $fields as $name => $field) {
  154. if (array_key_exists($name, $params)) {
  155. $membershipStatusBAO->$name = $params[$name];
  156. }
  157. }
  158. $membershipStatusBAO->save();
  159. }
  160. $membershipStatus = array();
  161. _civicrm_object_to_array( clone($membershipStatusBAO), $membershipStatus );
  162. $membershipStatus['is_error'] = 0;
  163. return $membershipStatus;
  164. }
  165. /**
  166. * Deletes an existing membership status
  167. *
  168. * This API is used for deleting a membership status
  169. *
  170. * @param Int $membershipStatusID Id of the membership status to be deleted
  171. *
  172. * @return null if successfull, object of CRM_Core_Error otherwise
  173. * @access public
  174. */
  175. function civicrm_membership_status_delete( &$params ) {
  176. if ( ! is_array( $params ) ) {
  177. return civicrm_create_error( 'Params is not an array' );
  178. }
  179. if ( ! CRM_Utils_Array::value( 'id', $params ) ) {
  180. return civicrm_create_error( 'Invalid or no value for membershipStatusID' );
  181. }
  182. require_once 'CRM/Member/BAO/MembershipStatus.php';
  183. $memberStatusDelete = CRM_Member_BAO_MembershipStatus::del( $params['id'] );
  184. return $memberStatusDelete ?
  185. civicrm_create_error('Error while deleting membership type Status') :
  186. civicrm_create_success( );
  187. }
  188. /**
  189. * Derives the Membership Status of a given Membership Reocrd
  190. *
  191. * This API is used for deriving Membership Status of a given Membership
  192. * record using the rules encoded in the membership_status table.
  193. *
  194. * @param Int $membershipID Id of a membership
  195. * @param String $statusDate
  196. *
  197. * @return Array Array of status id and status name
  198. * @public
  199. */
  200. function civicrm_membership_status_calc( $membershipParams, $excludeIsAdmin = false )
  201. {
  202. if ( ! is_array( $membershipParams ) ) {
  203. return civicrm_create_error( ts( 'membershipParams is not an array' ) );
  204. }
  205. if ( ! ( $membershipID = CRM_Utils_Array::value( 'membership_id', $membershipParams ) ) ) {
  206. return civicrm_create_error( 'membershipParams do not contain membership_id' );
  207. }
  208. $query = "
  209. SELECT start_date, end_date, join_date
  210. FROM civicrm_membership
  211. WHERE id = %1
  212. ";
  213. $params = array( 1 => array( $membershipID, 'Integer' ) );
  214. $dao =& CRM_Core_DAO::executeQuery( $query, $params );
  215. if ( $dao->fetch( ) ) {
  216. require_once 'CRM/Member/BAO/MembershipStatus.php';
  217. // CRM-7248 added $excludeIsAdmin to this function, also 'today' param
  218. $result =&
  219. CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate( $dao->start_date,
  220. $dao->end_date,
  221. $dao->join_date,
  222. 'today',
  223. $excludeIsAdmin );
  224. //make is error zero only when valid status found.
  225. if ( CRM_Utils_Array::value( 'id', $result ) ) {
  226. $result['is_error'] = 0;
  227. }
  228. } else {
  229. $result = civicrm_create_error( 'did not find a membership record' );
  230. }
  231. $dao->free( );
  232. return $result;
  233. }