8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
27 require_once 'Zend/Gdata.php';
30 * @see Zend_Gdata_Health_ProfileFeed
32 require_once 'Zend/Gdata/Health/ProfileFeed.php';
35 * @see Zend_Gdata_Health_ProfileListFeed
37 require_once 'Zend/Gdata/Health/ProfileListFeed.php';
40 * @see Zend_Gdata_Health_ProfileListEntry
42 require_once 'Zend/Gdata/Health/ProfileListEntry.php';
45 * @see Zend_Gdata_Health_ProfileEntry
47 require_once 'Zend/Gdata/Health/ProfileEntry.php';
50 * Service class for interacting with the Google Health Data API
52 * @link http://code.google.com/apis/health
57 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
58 * @license http://framework.zend.com/license/new-bsd New BSD License
60 class Zend_Gdata_Health extends Zend_Gdata
63 * URIs of the AuthSub/OAuth feeds.
65 const AUTHSUB_PROFILE_FEED_URI =
66 'https://www.google.com/health/feeds/profile/default';
67 const AUTHSUB_REGISTER_FEED_URI =
68 'https://www.google.com/health/feeds/register/default';
71 * URIs of the ClientLogin feeds.
73 const CLIENTLOGIN_PROFILELIST_FEED_URI =
74 'https://www.google.com/health/feeds/profile/list';
75 const CLIENTLOGIN_PROFILE_FEED_URI =
76 'https://www.google.com/health/feeds/profile/ui';
77 const CLIENTLOGIN_REGISTER_FEED_URI =
78 'https://www.google.com/health/feeds/register/ui';
81 * Authentication service names for Google Health and the H9 Sandbox.
83 const HEALTH_SERVICE_NAME = 'health';
84 const H9_SANDBOX_SERVICE_NAME = 'weaver';
87 * Profile ID used for all API interactions. This can only be set when
88 * using ClientLogin for authentication.
92 private $_profileID = null;
95 * True if API calls should be made to the H9 developer sandbox at /h9
100 private $_useH9Sandbox = false;
102 public static $namespaces =
103 array('ccr' => 'urn:astm-org:CCR',
104 'batch' => 'http://schemas.google.com/gdata/batch',
105 'h9m' => 'http://schemas.google.com/health/metadata',
106 'gAcl' => 'http://schemas.google.com/acl/2007',
107 'gd' => 'http://schemas.google.com/g/2005');
110 * Create Zend_Gdata_Health object
112 * @param Zend_Http_Client $client (optional) The HTTP client to use when
113 * when communicating with the Google Health servers.
114 * @param string $applicationId The identity of the application in the form
115 * of Company-AppName-Version
116 * @param bool $useH9Sandbox True if the H9 Developer's Sandbox should be
117 * used instead of production Google Health.
119 public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0', $useH9Sandbox = false)
121 $this->registerPackage('Zend_Gdata_Health');
122 $this->registerPackage('Zend_Gdata_Health_Extension_Ccr');
123 parent::__construct($client, $applicationId);
124 $this->_useH9Sandbox = $useH9Sandbox;
128 * Gets the id of the user's profile
130 * @return string The profile id
132 public function getProfileID()
134 return $this->_profileID;
138 * Sets which of the user's profiles will be used
140 * @param string $id The profile ID
141 * @return Zend_Gdata_Health Provides a fluent interface
143 public function setProfileID($id) {
144 $this->_profileID = $id;
149 * Retrieves the list of profiles associated with the user's ClientLogin
152 * @param string $query The query of the feed as a URL or Query object
153 * @return Zend_Gdata_Feed
155 public function getHealthProfileListFeed($query = null)
157 if ($this->_httpClient->getClientLoginToken() === null) {
158 require_once 'Zend/Gdata/App/AuthException.php';
159 throw new Zend_Gdata_App_AuthException(
160 'Profiles list feed is only available when using ClientLogin');
163 if($query === null) {
164 $uri = self::CLIENTLOGIN_PROFILELIST_FEED_URI;
165 } else if ($query instanceof Zend_Gdata_Query) {
166 $uri = $query->getQueryUrl();
171 // use correct feed for /h9 or /health
172 if ($this->_useH9Sandbox) {
173 $uri = preg_replace('/\/health\//', '/h9/', $uri);
176 return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileListFeed');
180 * Retrieve a user's profile as a feed object. If ClientLogin is used, the
181 * profile associated with $this->_profileID is returned, otherwise
182 * the profile associated with the AuthSub token is read.
184 * @param mixed $query The query for the feed, as a URL or Query
185 * @return Zend_Gdata_Health_ProfileFeed
187 public function getHealthProfileFeed($query = null)
189 if ($this->_httpClient->getClientLoginToken() !== null &&
190 $this->getProfileID() == null) {
191 require_once 'Zend/Gdata/App/AuthException.php';
192 throw new Zend_Gdata_App_AuthException(
193 'Profile ID must not be null. Did you call setProfileID()?');
196 if ($query instanceof Zend_Gdata_Query) {
197 $uri = $query->getQueryUrl();
198 } else if ($this->_httpClient->getClientLoginToken() !== null &&
200 $uri = self::CLIENTLOGIN_PROFILE_FEED_URI . '/' . $this->getProfileID();
201 } else if ($query === null) {
202 $uri = self::AUTHSUB_PROFILE_FEED_URI;
207 // use correct feed for /h9 or /health
208 if ($this->_useH9Sandbox) {
209 $uri = preg_replace('/\/health\//', '/h9/', $uri);
212 return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileFeed');
216 * Retrieve a profile entry object
218 * @param mixed $query The query for the feed, as a URL or Query
219 * @return Zend_Gdata_Health_ProfileEntry
221 public function getHealthProfileEntry($query = null)
223 if ($query === null) {
224 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
225 throw new Zend_Gdata_App_InvalidArgumentException(
226 'Query must not be null');
227 } else if ($query instanceof Zend_Gdata_Query) {
228 $uri = $query->getQueryUrl();
232 return parent::getEntry($uri, 'Zend_Gdata_Health_ProfileEntry');
236 * Posts a new notice using the register feed. This function constructs
237 * the atom profile entry.
239 * @param string $subject The subject line of the notice
240 * @param string $body The message body of the notice
241 * @param string $bodyType The (optional) type of message body
242 * (text, xhtml, html, etc.)
243 * @param string $ccrXML The (optional) CCR to add to the user's profile
244 * @return Zend_Gdata_Health_ProfileEntry
246 public function sendHealthNotice($subject, $body, $bodyType = null, $ccrXML = null)
248 if ($this->_httpClient->getClientLoginToken()) {
249 $profileID = $this->getProfileID();
250 if ($profileID !== null) {
251 $uri = self::CLIENTLOGIN_REGISTER_FEED_URI . '/' . $profileID;
253 require_once 'Zend/Gdata/App/AuthException.php';
254 throw new Zend_Gdata_App_AuthException(
255 'Profile ID must not be null. Did you call setProfileID()?');
258 $uri = self::AUTHSUB_REGISTER_FEED_URI;
261 $entry = new Zend_Gdata_Health_ProfileEntry();
262 $entry->title = $this->newTitle($subject);
263 $entry->content = $this->newContent($body);
264 $entry->content->type = $bodyType ? $bodyType : 'text';
265 $entry->setCcr($ccrXML);
267 // use correct feed for /h9 or /health
268 if ($this->_useH9Sandbox) {
269 $uri = preg_replace('/\/health\//', '/h9/', $uri);
272 return $this->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');