2
0

Health.php 8.9 KB

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