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

/drupal/sites/all/modules/civicrm/api/class.api.php

https://github.com/michaelmcandrew/th
PHP | 198 lines | 134 code | 13 blank | 51 comment | 15 complexity | f4cdde0cb97f6cb1a0ee3f5dfdb26489 MD5 | raw file
  1. <?php
  2. /**
  3. * This class allows to consume the API, either from within a module that knows civicrm already:
  4. $api = new civicrm_api3();
  5. or from any code on the same server as civicrm
  6. $api = new civicrm_api3 (array('conf_path'=> '/your/path/to/your/civicrm/or/joomla/site)); //the path to civicrm.settings.php
  7. or to query a remote server via the rest api
  8. $api = new civicrm_api3 (array ('server' => 'http://example.org','api_key'=>'theusersecretkey','key'=>'thesitesecretkey'));
  9. no matter how initialised and if civicrm is local or remote, you use the class the same way
  10. $api->{entity}->{action}($params);
  11. so to get the individual contacts
  12. if ($api->Contact->Get(array('contact_type'=>'Individual','return'=>'sort_name,current_employer')) {
  13. echo "\n contacts found " . $api->attr('count');
  14. foreach ($api->values() as $c) {
  15. echo "\n".$c->sort_name. " working for ". $c->current_employer;
  16. }
  17. }else { // in theory, doesn't append
  18. echo $api->errorMsg();
  19. }
  20. or to create an event
  21. if ($api->Event->Create(array('title'=>'Test','event_type_id' => 1,'is_public' => 1,'start_date' => 19430429))) {
  22. echo "created event id:". $api->attr('id');
  23. } else {
  24. echo $api->errorMsg();
  25. }
  26. To make it easier, the get method can either take an associative array $params, or simply an id
  27. $api->Activity->Get (42);
  28. being the same as:
  29. $api->Activity->Get (array('id'=>42));
  30. */
  31. class civicrm_api3 {
  32. function __construct ($config = null) {
  33. $this->local=true;
  34. $this->input = array ();
  35. $this->lastResult = array ();
  36. if (isset ($config) &&isset($config ['server'])) {
  37. // we are calling a remote server via REST
  38. $this->local=false;
  39. $this->uri = $config ['server'];
  40. if (isset($config ['path']))
  41. $this->uri .= "/".$config ['path'];
  42. else
  43. $this->uri .= '/sites/all/modules/civicrm/extern/rest.php';
  44. $this->uri .='?json=1';
  45. if (isset($config ['key'])) {
  46. $this->key = $config['key'];
  47. }else {
  48. die ("\nFATAL:param['key] missing\n");
  49. }
  50. if (isset($config ['api_key'])) {
  51. $this->api_key = $config['api_key'];
  52. }else {
  53. die ("\nFATAL:param['api_key] missing\n");
  54. }
  55. return;
  56. }
  57. if (isset ($config) &&isset($config ['conf_path'] )) {
  58. require_once ($config ['conf_path'] .'/civicrm.settings.php');
  59. require_once 'CRM/Core/Config.php';
  60. require_once 'api/api.php';
  61. require_once "api/v3/utils.php";
  62. $this->cfg= CRM_Core_Config::singleton();
  63. $this->init();
  64. $this->ping();
  65. } else {
  66. $this->cfg= CRM_Core_Config::singleton();
  67. }
  68. }
  69. public function __get($entity) {
  70. //TODO check if it's a valid entity
  71. $this->currentEntity = $entity;
  72. return $this;
  73. }
  74. public function __call($action, $params) {
  75. // TODO : check if it's a valid action
  76. if (isset($params[0])) {
  77. return $this->call ($this->currentEntity,$action,$params[0]);
  78. }else {
  79. return $this->call ($this->currentEntity,$action,$this->input);
  80. }
  81. }
  82. /** As of PHP 5.3.0 */
  83. public static function __callStatic($name, $arguments) {
  84. // Should we implement it ?
  85. echo "Calling static method '$name' "
  86. . implode(', ', $arguments). "\n";
  87. }
  88. function remoteCall ($entity,$action,$params = array()) {
  89. $fields= "key={$this->key}&api_key={$this->api_key}";
  90. $query = $this->uri."&entity=$entity&action=$action";
  91. foreach ($params as $k => $v) {
  92. $fields .= "&$k=".urlencode($v);
  93. }
  94. if (function_exists('curl_init')) {
  95. //set the url, number of POST vars, POST data
  96. $ch = curl_init();
  97. curl_setopt($ch,CURLOPT_URL,$query);
  98. curl_setopt($ch,CURLOPT_POST,count($params)+2);
  99. curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
  100. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  101. //execute post
  102. $result = curl_exec($ch);
  103. curl_close($ch);
  104. return json_decode ($result);
  105. } else { // not good, all in get when should be post
  106. $result = file_get_contents($query.'&'.$fields);
  107. return json_decode ($result);
  108. }
  109. }
  110. function call ($entity,$action='Get',$params = array()) {
  111. if (is_int($params))
  112. $params = array ('id'=> $params);
  113. if (!isset ($params['version']))
  114. $params['version'] = 3;
  115. if (!isset ($params['sequential']))
  116. $params['sequential'] = 1;
  117. if (!$this->local) {
  118. $this->lastResult= $this->remoteCall ($entity,$action,$params);
  119. } else {
  120. $this->ping ();// necessary only when the caller runs a long time (eg a bot)
  121. $this->lastResult= civicrm_api ($entity,$action,$params);
  122. }
  123. /* if ($this->lastResult->count == 1 && count($this->lastResult->values)== 1) {
  124. $this->lastResult->values = array_shift($this->lastResult->values);
  125. }
  126. */
  127. $this->input=array();//reset the input to be ready for a new call
  128. return (!$this->lastResult->is_error);
  129. }
  130. function ping () {
  131. global $_DB_DATAOBJECT;
  132. foreach ($_DB_DATAOBJECT['CONNECTIONS'] as &$c) {
  133. if (!$c->connection->ping()) {
  134. $c->connect($this->cfg->dsn);
  135. if (!$c->connection->ping()) {
  136. die ("we couldn't connect");
  137. }
  138. }
  139. }
  140. }
  141. function errorMsg () {
  142. return $this->lastResult->error_message;
  143. }
  144. function init () {
  145. CRM_Core_DAO::init( $this->cfg->dsn );
  146. }
  147. /*
  148. * $api->attr ('id'); // return the id
  149. * or
  150. * $api->attr ('id',42) //set the id
  151. */
  152. public function attr ($name,$value = null) {
  153. if ($value === null) {
  154. return $this->lastResult->$name;
  155. } else {
  156. $this->input[$name] = $value;
  157. }
  158. }
  159. public function values () {
  160. if (is_array ($this->lastResult))
  161. return $this->lastResult['values'];
  162. else
  163. return $this->lastResult->values;
  164. }
  165. public function result () {
  166. return $this->lastResult;
  167. }
  168. }