[ZF-6295] Generic:
[zend.git] / library / Zend / Gdata / Health.php
bloba07dddbac1db0b27d2c9886da9a29414083667a6
1 <?php
3 /**
4 * Zend Framework
6 * LICENSE
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.
16 * @category Zend
17 * @package Zend_Gdata
18 * @subpackage Health
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
23 /**
24 * @see Zend_Gdata
26 require_once 'Zend/Gdata.php';
28 /**
29 * @see Zend_Gdata_Health_ProfileFeed
31 require_once 'Zend/Gdata/Health/ProfileFeed.php';
33 /**
34 * @see Zend_Gdata_Health_ProfileListFeed
36 require_once 'Zend/Gdata/Health/ProfileListFeed.php';
38 /**
39 * @see Zend_Gdata_Health_ProfileListEntry
41 require_once 'Zend/Gdata/Health/ProfileListEntry.php';
43 /**
44 * @see Zend_Gdata_Health_ProfileEntry
46 require_once 'Zend/Gdata/Health/ProfileEntry.php';
48 /**
49 * Service class for interacting with the Google Health Data API
51 * @link http://code.google.com/apis/health
53 * @category Zend
54 * @package Zend_Gdata
55 * @subpackage Health
56 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
57 * @license http://framework.zend.com/license/new-bsd New BSD License
59 class Zend_Gdata_Health extends Zend_Gdata
61 /**
62 * URIs of the AuthSub/OAuth feeds.
64 const AUTHSUB_PROFILE_FEED_URI =
65 'https://www.google.com/health/feeds/profile/default';
66 const AUTHSUB_REGISTER_FEED_URI =
67 'https://www.google.com/health/feeds/register/default';
69 /**
70 * URIs of the ClientLogin feeds.
72 const CLIENTLOGIN_PROFILELIST_FEED_URI =
73 'https://www.google.com/health/feeds/profile/list';
74 const CLIENTLOGIN_PROFILE_FEED_URI =
75 'https://www.google.com/health/feeds/profile/ui';
76 const CLIENTLOGIN_REGISTER_FEED_URI =
77 'https://www.google.com/health/feeds/register/ui';
79 /**
80 * Authentication service names for Google Health and the H9 Sandbox.
82 const HEALTH_SERVICE_NAME = 'health';
83 const H9_SANDBOX_SERVICE_NAME = 'weaver';
85 /**
86 * Profile ID used for all API interactions. This can only be set when
87 * using ClientLogin for authentication.
89 * @var string
91 private $_profileID = null;
93 /**
94 * True if API calls should be made to the H9 developer sandbox at /h9
95 * rather than /health
97 * @var bool
99 private $_useH9Sandbox = false;
101 public static $namespaces =
102 array('ccr' => 'urn:astm-org:CCR',
103 'batch' => 'http://schemas.google.com/gdata/batch',
104 'h9m' => 'http://schemas.google.com/health/metadata',
105 'gAcl' => 'http://schemas.google.com/acl/2007',
106 'gd' => 'http://schemas.google.com/g/2005');
109 * Create Zend_Gdata_Health object
111 * @param Zend_Http_Client $client (optional) The HTTP client to use when
112 * when communicating with the Google Health servers.
113 * @param string $applicationId The identity of the application in the form
114 * of Company-AppName-Version
115 * @param bool $useH9Sandbox True if the H9 Developer's Sandbox should be
116 * used instead of production Google Health.
118 public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0', $useH9Sandbox = false)
120 $this->registerPackage('Zend_Gdata_Health');
121 $this->registerPackage('Zend_Gdata_Health_Extension_Ccr');
122 parent::__construct($client, $applicationId);
123 $this->_useH9Sandbox = $useH9Sandbox;
127 * Gets the id of the user's profile
129 * @return string The profile id
131 public function getProfileID()
133 return $this->_profileID;
137 * Sets which of the user's profiles will be used
139 * @param string $id The profile ID
140 * @return Zend_Gdata_Health Provides a fluent interface
142 public function setProfileID($id) {
143 $this->_profileID = $id;
144 return $this;
148 * Retrieves the list of profiles associated with the user's ClientLogin
149 * credentials.
151 * @param string $query The query of the feed as a URL or Query object
152 * @return Zend_Gdata_Feed
154 public function getHealthProfileListFeed($query = null)
156 if ($this->_httpClient->getClientLoginToken() === null) {
157 require_once 'Zend/Gdata/App/AuthException.php';
158 throw new Zend_Gdata_App_AuthException(
159 'Profiles list feed is only available when using ClientLogin');
162 if($query === null) {
163 $uri = self::CLIENTLOGIN_PROFILELIST_FEED_URI;
164 } else if ($query instanceof Zend_Gdata_Query) {
165 $uri = $query->getQueryUrl();
166 } else {
167 $uri = $query;
170 // use correct feed for /h9 or /health
171 if ($this->_useH9Sandbox) {
172 $uri = preg_replace('/\/health\//', '/h9/', $uri);
175 return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileListFeed');
179 * Retrieve a user's profile as a feed object. If ClientLogin is used, the
180 * profile associated with $this->_profileID is returned, otherwise
181 * the profile associated with the AuthSub token is read.
183 * @param mixed $query The query for the feed, as a URL or Query
184 * @return Zend_Gdata_Health_ProfileFeed
186 public function getHealthProfileFeed($query = null)
188 if ($this->_httpClient->getClientLoginToken() !== null &&
189 $this->getProfileID() == null) {
190 require_once 'Zend/Gdata/App/AuthException.php';
191 throw new Zend_Gdata_App_AuthException(
192 'Profile ID must not be null. Did you call setProfileID()?');
195 if ($query instanceof Zend_Gdata_Query) {
196 $uri = $query->getQueryUrl();
197 } else if ($this->_httpClient->getClientLoginToken() !== null &&
198 $query == null) {
199 $uri = self::CLIENTLOGIN_PROFILE_FEED_URI . '/' . $this->getProfileID();
200 } else if ($query === null) {
201 $uri = self::AUTHSUB_PROFILE_FEED_URI;
202 } else {
203 $uri = $query;
206 // use correct feed for /h9 or /health
207 if ($this->_useH9Sandbox) {
208 $uri = preg_replace('/\/health\//', '/h9/', $uri);
211 return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileFeed');
215 * Retrieve a profile entry object
217 * @param mixed $query The query for the feed, as a URL or Query
218 * @return Zend_Gdata_Health_ProfileEntry
220 public function getHealthProfileEntry($query = null)
222 if ($query === null) {
223 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
224 throw new Zend_Gdata_App_InvalidArgumentException(
225 'Query must not be null');
226 } else if ($query instanceof Zend_Gdata_Query) {
227 $uri = $query->getQueryUrl();
228 } else {
229 $uri = $query;
231 return parent::getEntry($uri, 'Zend_Gdata_Health_ProfileEntry');
235 * Posts a new notice using the register feed. This function constructs
236 * the atom profile entry.
238 * @param string $subject The subject line of the notice
239 * @param string $body The message body of the notice
240 * @param string $bodyType The (optional) type of message body
241 * (text, xhtml, html, etc.)
242 * @param string $ccrXML The (optional) CCR to add to the user's profile
243 * @return Zend_Gdata_Health_ProfileEntry
245 public function sendHealthNotice($subject, $body, $bodyType = null, $ccrXML = null)
247 if ($this->_httpClient->getClientLoginToken()) {
248 $profileID = $this->getProfileID();
249 if ($profileID !== null) {
250 $uri = self::CLIENTLOGIN_REGISTER_FEED_URI . '/' . $profileID;
251 } else {
252 require_once 'Zend/Gdata/App/AuthException.php';
253 throw new Zend_Gdata_App_AuthException(
254 'Profile ID must not be null. Did you call setProfileID()?');
256 } else {
257 $uri = self::AUTHSUB_REGISTER_FEED_URI;
260 $entry = new Zend_Gdata_Health_ProfileEntry();
261 $entry->title = $this->newTitle($subject);
262 $entry->content = $this->newContent($body);
263 $entry->content->type = $bodyType ? $bodyType : 'text';
264 $entry->setCcr($ccrXML);
266 // use correct feed for /h9 or /health
267 if ($this->_useH9Sandbox) {
268 $uri = preg_replace('/\/health\//', '/h9/', $uri);
271 return $this->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');