Health.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage Health
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Gdata
  24. */
  25. require_once 'Zend/Gdata.php';
  26. /**
  27. * @see Zend_Gdata_Health_ProfileFeed
  28. */
  29. require_once 'Zend/Gdata/Health/ProfileFeed.php';
  30. /**
  31. * @see Zend_Gdata_Health_ProfileListFeed
  32. */
  33. require_once 'Zend/Gdata/Health/ProfileListFeed.php';
  34. /**
  35. * @see Zend_Gdata_Health_ProfileListEntry
  36. */
  37. require_once 'Zend/Gdata/Health/ProfileListEntry.php';
  38. /**
  39. * @see Zend_Gdata_Health_ProfileEntry
  40. */
  41. require_once 'Zend/Gdata/Health/ProfileEntry.php';
  42. /**
  43. * Service class for interacting with the Google Health Data API
  44. *
  45. * @link http://code.google.com/apis/health
  46. *
  47. * @category Zend
  48. * @package Zend_Gdata
  49. * @subpackage Health
  50. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. class Zend_Gdata_Health extends Zend_Gdata
  54. {
  55. /**
  56. * URIs of the AuthSub/OAuth feeds.
  57. */
  58. const AUTHSUB_PROFILE_FEED_URI =
  59. 'https://www.google.com/health/feeds/profile/default';
  60. const AUTHSUB_REGISTER_FEED_URI =
  61. 'https://www.google.com/health/feeds/register/default';
  62. /**
  63. * URIs of the ClientLogin feeds.
  64. */
  65. const CLIENTLOGIN_PROFILELIST_FEED_URI =
  66. 'https://www.google.com/health/feeds/profile/list';
  67. const CLIENTLOGIN_PROFILE_FEED_URI =
  68. 'https://www.google.com/health/feeds/profile/ui';
  69. const CLIENTLOGIN_REGISTER_FEED_URI =
  70. 'https://www.google.com/health/feeds/register/ui';
  71. /**
  72. * Authentication service names for Google Health and the H9 Sandbox.
  73. */
  74. const HEALTH_SERVICE_NAME = 'health';
  75. const H9_SANDBOX_SERVICE_NAME = 'weaver';
  76. /**
  77. * Profile ID used for all API interactions. This can only be set when
  78. * using ClientLogin for authentication.
  79. *
  80. * @var string
  81. */
  82. private $_profileID = null;
  83. /**
  84. * True if API calls should be made to the H9 developer sandbox at /h9
  85. * rather than /health
  86. *
  87. * @var bool
  88. */
  89. private $_useH9Sandbox = false;
  90. public static $namespaces =
  91. array('ccr' => 'urn:astm-org:CCR',
  92. 'batch' => 'http://schemas.google.com/gdata/batch',
  93. 'h9m' => 'http://schemas.google.com/health/metadata',
  94. 'gAcl' => 'http://schemas.google.com/acl/2007',
  95. 'gd' => 'http://schemas.google.com/g/2005');
  96. /**
  97. * Create Zend_Gdata_Health object
  98. *
  99. * @param Zend_Http_Client $client (optional) The HTTP client to use when
  100. * when communicating with the Google Health servers.
  101. * @param string $applicationId The identity of the application in the form
  102. * of Company-AppName-Version
  103. * @param bool $useH9Sandbox True if the H9 Developer's Sandbox should be
  104. * used instead of production Google Health.
  105. */
  106. public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0', $useH9Sandbox = false)
  107. {
  108. $this->registerPackage('Zend_Gdata_Health');
  109. $this->registerPackage('Zend_Gdata_Health_Extension_Ccr');
  110. parent::__construct($client, $applicationId);
  111. $this->_useH9Sandbox = $useH9Sandbox;
  112. }
  113. /**
  114. * Gets the id of the user's profile
  115. *
  116. * @return string The profile id
  117. */
  118. public function getProfileID()
  119. {
  120. return $this->_profileID;
  121. }
  122. /**
  123. * Sets which of the user's profiles will be used
  124. *
  125. * @param string $id The profile ID
  126. * @return Zend_Gdata_Health Provides a fluent interface
  127. */
  128. public function setProfileID($id) {
  129. $this->_profileID = $id;
  130. return $this;
  131. }
  132. /**
  133. * Retrieves the list of profiles associated with the user's ClientLogin
  134. * credentials.
  135. *
  136. * @param string $query The query of the feed as a URL or Query object
  137. * @return Zend_Gdata_Feed
  138. */
  139. public function getHealthProfileListFeed($query = null)
  140. {
  141. if ($this->_httpClient->getClientLoginToken() === null) {
  142. require_once 'Zend/Gdata/App/AuthException.php';
  143. throw new Zend_Gdata_App_AuthException(
  144. 'Profiles list feed is only available when using ClientLogin');
  145. }
  146. if($query === null) {
  147. $uri = self::CLIENTLOGIN_PROFILELIST_FEED_URI;
  148. } else if ($query instanceof Zend_Gdata_Query) {
  149. $uri = $query->getQueryUrl();
  150. } else {
  151. $uri = $query;
  152. }
  153. // use correct feed for /h9 or /health
  154. if ($this->_useH9Sandbox) {
  155. $uri = preg_replace('/\/health\//', '/h9/', $uri);
  156. }
  157. return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileListFeed');
  158. }
  159. /**
  160. * Retrieve a user's profile as a feed object. If ClientLogin is used, the
  161. * profile associated with $this->_profileID is returned, otherwise
  162. * the profile associated with the AuthSub token is read.
  163. *
  164. * @param mixed $query The query for the feed, as a URL or Query
  165. * @return Zend_Gdata_Health_ProfileFeed
  166. */
  167. public function getHealthProfileFeed($query = null)
  168. {
  169. if ($this->_httpClient->getClientLoginToken() !== null &&
  170. $this->getProfileID() == null) {
  171. require_once 'Zend/Gdata/App/AuthException.php';
  172. throw new Zend_Gdata_App_AuthException(
  173. 'Profile ID must not be null. Did you call setProfileID()?');
  174. }
  175. if ($query instanceof Zend_Gdata_Query) {
  176. $uri = $query->getQueryUrl();
  177. } else if ($this->_httpClient->getClientLoginToken() !== null &&
  178. $query == null) {
  179. $uri = self::CLIENTLOGIN_PROFILE_FEED_URI . '/' . $this->getProfileID();
  180. } else if ($query === null) {
  181. $uri = self::AUTHSUB_PROFILE_FEED_URI;
  182. } else {
  183. $uri = $query;
  184. }
  185. // use correct feed for /h9 or /health
  186. if ($this->_useH9Sandbox) {
  187. $uri = preg_replace('/\/health\//', '/h9/', $uri);
  188. }
  189. return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileFeed');
  190. }
  191. /**
  192. * Retrieve a profile entry object
  193. *
  194. * @param mixed $query The query for the feed, as a URL or Query
  195. * @return Zend_Gdata_Health_ProfileEntry
  196. */
  197. public function getHealthProfileEntry($query = null)
  198. {
  199. if ($query === null) {
  200. require_once 'Zend/Gdata/App/InvalidArgumentException.php';
  201. throw new Zend_Gdata_App_InvalidArgumentException(
  202. 'Query must not be null');
  203. } else if ($query instanceof Zend_Gdata_Query) {
  204. $uri = $query->getQueryUrl();
  205. } else {
  206. $uri = $query;
  207. }
  208. return parent::getEntry($uri, 'Zend_Gdata_Health_ProfileEntry');
  209. }
  210. /**
  211. * Posts a new notice using the register feed. This function constructs
  212. * the atom profile entry.
  213. *
  214. * @param string $subject The subject line of the notice
  215. * @param string $body The message body of the notice
  216. * @param string $bodyType The (optional) type of message body
  217. * (text, xhtml, html, etc.)
  218. * @param string $ccrXML The (optional) CCR to add to the user's profile
  219. * @return Zend_Gdata_Health_ProfileEntry
  220. */
  221. public function sendHealthNotice($subject, $body, $bodyType = null, $ccrXML = null)
  222. {
  223. if ($this->_httpClient->getClientLoginToken()) {
  224. $profileID = $this->getProfileID();
  225. if ($profileID !== null) {
  226. $uri = self::CLIENTLOGIN_REGISTER_FEED_URI . '/' . $profileID;
  227. } else {
  228. require_once 'Zend/Gdata/App/AuthException.php';
  229. throw new Zend_Gdata_App_AuthException(
  230. 'Profile ID must not be null. Did you call setProfileID()?');
  231. }
  232. } else {
  233. $uri = self::AUTHSUB_REGISTER_FEED_URI;
  234. }
  235. $entry = new Zend_Gdata_Health_ProfileEntry();
  236. $entry->title = $this->newTitle($subject);
  237. $entry->content = $this->newContent($body);
  238. $entry->content->type = $bodyType ? $bodyType : 'text';
  239. $entry->setCcr($ccrXML);
  240. // use correct feed for /h9 or /health
  241. if ($this->_useH9Sandbox) {
  242. $uri = preg_replace('/\/health\//', '/h9/', $uri);
  243. }
  244. return $this->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');
  245. }
  246. }