web/lib/Zend/Gdata/Health.php
changeset 886 1e110b03ae96
parent 807 877f952ae2bd
child 1230 68c69c656a2c
equal deleted inserted replaced
885:2251fb41dbc7 886:1e110b03ae96
    14  * to license@zend.com so we can send you a copy immediately.
    14  * to license@zend.com so we can send you a copy immediately.
    15  *
    15  *
    16  * @category   Zend
    16  * @category   Zend
    17  * @package    Zend_Gdata
    17  * @package    Zend_Gdata
    18  * @subpackage Health
    18  * @subpackage Health
    19  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    21  * @version    $Id: Health.php 20096 2010-01-06 02:05:09Z bkarwin $
    21  * @version    $Id: Health.php 24779 2012-05-08 19:13:59Z adamlundrigan $
    22  */
    22  */
       
    23 
       
    24 /**
       
    25  * @see Zend_Exception
       
    26  */
       
    27 require_once 'Zend/Exception.php';
    23 
    28 
    24 /**
    29 /**
    25  * @see Zend_Gdata
    30  * @see Zend_Gdata
    26  */
    31  */
    27 require_once 'Zend/Gdata.php';
    32 require_once 'Zend/Gdata.php';
    28 
       
    29 /**
       
    30  * @see Zend_Gdata_Health_ProfileFeed
       
    31  */
       
    32 require_once 'Zend/Gdata/Health/ProfileFeed.php';
       
    33 
       
    34 /**
       
    35  * @see Zend_Gdata_Health_ProfileListFeed
       
    36  */
       
    37 require_once 'Zend/Gdata/Health/ProfileListFeed.php';
       
    38 
       
    39 /**
       
    40  * @see Zend_Gdata_Health_ProfileListEntry
       
    41  */
       
    42 require_once 'Zend/Gdata/Health/ProfileListEntry.php';
       
    43 
       
    44 /**
       
    45  * @see Zend_Gdata_Health_ProfileEntry
       
    46  */
       
    47 require_once 'Zend/Gdata/Health/ProfileEntry.php';
       
    48 
    33 
    49 /**
    34 /**
    50  * Service class for interacting with the Google Health Data API
    35  * Service class for interacting with the Google Health Data API
    51  *
    36  *
    52  * @link http://code.google.com/apis/health
    37  * @link http://code.google.com/apis/health
    53  *
    38  *
    54  * @category   Zend
    39  * @category   Zend
    55  * @package    Zend_Gdata
    40  * @package    Zend_Gdata
    56  * @subpackage Health
    41  * @subpackage Health
    57  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    42  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    58  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    43  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    59  */
    44  */
    60 class Zend_Gdata_Health extends Zend_Gdata
    45 class Zend_Gdata_Health extends Zend_Gdata
    61 {
    46 {
    62     /**
    47     /**
    82      */
    67      */
    83     const HEALTH_SERVICE_NAME = 'health';
    68     const HEALTH_SERVICE_NAME = 'health';
    84     const H9_SANDBOX_SERVICE_NAME = 'weaver';
    69     const H9_SANDBOX_SERVICE_NAME = 'weaver';
    85 
    70 
    86     /**
    71     /**
    87      * Profile ID used for all API interactions.  This can only be set when
       
    88      * using ClientLogin for authentication.
       
    89      *
       
    90      * @var string
       
    91      */
       
    92     private $_profileID = null;
       
    93 
       
    94     /**
       
    95      * True if API calls should be made to the H9 developer sandbox at /h9
       
    96      * rather than /health
       
    97      *
       
    98      * @var bool
       
    99      */
       
   100     private $_useH9Sandbox = false;
       
   101 
       
   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');
       
   108 
       
   109     /**
       
   110      * Create Zend_Gdata_Health object
    72      * Create Zend_Gdata_Health object
   111      *
    73      *
   112      * @param Zend_Http_Client $client (optional) The HTTP client to use when
    74      * @param Zend_Http_Client $client (optional) The HTTP client to use when
   113      *     when communicating with the Google Health servers.
    75      *     when communicating with the Google Health servers.
   114      * @param string $applicationId The identity of the application in the form
    76      * @param string $applicationId The identity of the application in the form
   116      * @param bool $useH9Sandbox True if the H9 Developer's Sandbox should be
    78      * @param bool $useH9Sandbox True if the H9 Developer's Sandbox should be
   117      *     used instead of production Google Health.
    79      *     used instead of production Google Health.
   118      */
    80      */
   119     public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0', $useH9Sandbox = false)
    81     public function __construct($client = null, $applicationId = 'MyCompany-MyApp-1.0', $useH9Sandbox = false)
   120     {
    82     {
   121         $this->registerPackage('Zend_Gdata_Health');
    83         throw new Zend_Exception(
   122         $this->registerPackage('Zend_Gdata_Health_Extension_Ccr');
    84             'Google Health API has been discontinued by Google and was removed'
   123         parent::__construct($client, $applicationId);
    85             . ' from Zend Framework in 1.12.0.  For more information see: '
   124         $this->_useH9Sandbox = $useH9Sandbox;
    86             . 'http://googleblog.blogspot.ca/2011/06/update-on-google-health-and-google.html'
   125     }
    87         );
   126 
       
   127     /**
       
   128      * Gets the id of the user's profile
       
   129      *
       
   130      * @return string The profile id
       
   131      */
       
   132     public function getProfileID()
       
   133     {
       
   134         return $this->_profileID;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Sets which of the user's profiles will be used
       
   139      *
       
   140      * @param string $id The profile ID
       
   141      * @return Zend_Gdata_Health Provides a fluent interface
       
   142      */
       
   143     public function setProfileID($id) {
       
   144         $this->_profileID = $id;
       
   145         return $this;
       
   146     }
       
   147 
       
   148      /**
       
   149      * Retrieves the list of profiles associated with the user's ClientLogin
       
   150      * credentials.
       
   151      *
       
   152      * @param string $query The query of the feed as a URL or Query object
       
   153      * @return Zend_Gdata_Feed
       
   154      */
       
   155     public function getHealthProfileListFeed($query = null)
       
   156     {
       
   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');
       
   161         }
       
   162 
       
   163         if($query === null)  {
       
   164             $uri = self::CLIENTLOGIN_PROFILELIST_FEED_URI;
       
   165         } else if ($query instanceof Zend_Gdata_Query) {
       
   166             $uri = $query->getQueryUrl();
       
   167         } else {
       
   168             $uri = $query;
       
   169         }
       
   170 
       
   171         // use correct feed for /h9 or /health
       
   172         if ($this->_useH9Sandbox) {
       
   173             $uri = preg_replace('/\/health\//', '/h9/', $uri);
       
   174         }
       
   175 
       
   176         return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileListFeed');
       
   177     }
       
   178 
       
   179     /**
       
   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.
       
   183      *
       
   184      * @param mixed $query The query for the feed, as a URL or Query
       
   185      * @return Zend_Gdata_Health_ProfileFeed
       
   186      */
       
   187     public function getHealthProfileFeed($query = null)
       
   188     {
       
   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()?');
       
   194         }
       
   195 
       
   196         if ($query instanceof Zend_Gdata_Query) {
       
   197             $uri = $query->getQueryUrl();
       
   198         } else if ($this->_httpClient->getClientLoginToken() !== null &&
       
   199                    $query == null) {
       
   200             $uri = self::CLIENTLOGIN_PROFILE_FEED_URI . '/' . $this->getProfileID();
       
   201         } else if ($query === null) {
       
   202             $uri = self::AUTHSUB_PROFILE_FEED_URI;
       
   203         } else {
       
   204             $uri = $query;
       
   205         }
       
   206 
       
   207         // use correct feed for /h9 or /health
       
   208         if ($this->_useH9Sandbox) {
       
   209             $uri = preg_replace('/\/health\//', '/h9/', $uri);
       
   210         }
       
   211 
       
   212         return parent::getFeed($uri, 'Zend_Gdata_Health_ProfileFeed');
       
   213     }
       
   214 
       
   215     /**
       
   216      * Retrieve a profile entry object
       
   217      *
       
   218      * @param mixed $query The query for the feed, as a URL or Query
       
   219      * @return Zend_Gdata_Health_ProfileEntry
       
   220      */
       
   221     public function getHealthProfileEntry($query = null)
       
   222     {
       
   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();
       
   229         } else {
       
   230             $uri = $query;
       
   231         }
       
   232         return parent::getEntry($uri, 'Zend_Gdata_Health_ProfileEntry');
       
   233     }
       
   234 
       
   235     /**
       
   236      * Posts a new notice using the register feed.  This function constructs
       
   237      * the atom profile entry.
       
   238      *
       
   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
       
   245      */
       
   246     public function sendHealthNotice($subject, $body, $bodyType = null, $ccrXML = null)
       
   247     {
       
   248         if ($this->_httpClient->getClientLoginToken()) {
       
   249             $profileID = $this->getProfileID();
       
   250             if ($profileID !== null) {
       
   251                 $uri = self::CLIENTLOGIN_REGISTER_FEED_URI . '/' . $profileID;
       
   252             } else {
       
   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()?');
       
   256             }
       
   257         } else {
       
   258             $uri = self::AUTHSUB_REGISTER_FEED_URI;
       
   259         }
       
   260 
       
   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);
       
   266 
       
   267         // use correct feed for /h9 or /health
       
   268         if ($this->_useH9Sandbox) {
       
   269             $uri = preg_replace('/\/health\//', '/h9/', $uri);
       
   270         }
       
   271 
       
   272         return $this->insertEntry($entry, $uri, 'Zend_Gdata_Health_ProfileEntry');
       
   273     }
    88     }
   274 }
    89 }