Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
photo.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Google
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Google Picasa data class for the Joomla Platform.
14  *
15  * @package Joomla.Platform
16  * @subpackage Google
17  * @since 12.3
18  */
20 {
21  /**
22  * @var SimpleXMLElement The photo's XML
23  * @since 12.3
24  */
25  protected $xml;
26 
27  /**
28  * Constructor.
29  *
30  * @param SimpleXMLElement $xml XML from Google
31  * @param JRegistry $options Google options object
32  * @param JGoogleAuth $auth Google data http client object
33  *
34  * @since 12.3
35  */
36  public function __construct(SimpleXMLElement $xml, JRegistry $options = null, JGoogleAuth $auth = null)
37  {
38  $this->xml = $xml;
39 
40  parent::__construct($options, $auth);
41 
42  if (isset($this->auth) && !$this->auth->getOption('scope'))
43  {
44  $this->auth->setOption('scope', 'https://picasaweb.google.com/data/');
45  }
46  }
47 
48  /**
49  * Method to delete a Picasa photo
50  *
51  * @param mixed $match Check for most up to date photo
52  *
53  * @return boolean Success or failure.
54  *
55  * @since 12.3
56  * @throws UnexpectedValueException
57  */
58  public function delete($match = '*')
59  {
60  if ($this->isAuthenticated())
61  {
62  $url = $this->getLink();
63 
64  if ($match === true)
65  {
66  $match = $this->xml->xpath('./@gd:etag');
67  $match = $match[0];
68  }
69 
70  try
71  {
72  $jdata = $this->query($url, null, array('GData-Version' => 2, 'If-Match' => $match), 'delete');
73  }
74  catch (Exception $e)
75  {
76  if (strpos($e->getMessage(), 'Error code 412 received requesting data: Mismatch: etags') === 0)
77  {
78  throw new RuntimeException("Etag match failed: `$match`.");
79  }
80  throw $e;
81  }
82 
83  if ($jdata->body != '')
84  {
85  throw new UnexpectedValueException("Unexpected data received from Google: `{$jdata->body}`.");
86  }
87  $this->xml = null;
88 
89  return true;
90  }
91  else
92  {
93  return false;
94  }
95  }
96 
97  /**
98  * Method to get the photo link
99  *
100  * @param string $type Type of link to return
101  *
102  * @return string Link or false on failure
103  *
104  * @since 12.3
105  */
106  public function getLink($type = 'edit')
107  {
108  $links = $this->xml->link;
109 
110  foreach ($links as $link)
111  {
112  if ($link->attributes()->rel == $type)
113  {
114  return (string) $link->attributes()->href;
115  }
116  }
117  return false;
118  }
119 
120  /**
121  * Method to get the photo's URL
122  *
123  * @return string Link
124  *
125  * @since 12.3
126  */
127  public function getURL()
128  {
129  return (string) $this->xml->children()->content->attributes()->src;
130  }
131 
132  /**
133  * Method to get the photo's thumbnails
134  *
135  * @return array An array of thumbnails
136  *
137  * @since 12.3
138  */
139  public function getThumbnails()
140  {
141  $thumbs = array();
142 
143  foreach ($this->xml->children('media', true)->group->thumbnail as $item)
144  {
145  $url = (string) $item->attributes()->url;
146  $width = (int) $item->attributes()->width;
147  $height = (int) $item->attributes()->height;
148  $thumbs[$width] = array('url' => $url, 'w' => $width, 'h' => $height);
149  }
150  return $thumbs;
151  }
152 
153  /**
154  * Method to get the title of the photo
155  *
156  * @return string Photo title
157  *
158  * @since 12.3
159  */
160  public function getTitle()
161  {
162  return (string) $this->xml->children()->title;
163  }
164 
165  /**
166  * Method to get the summary of the photo
167  *
168  * @return string Photo description
169  *
170  * @since 12.3
171  */
172  public function getSummary()
173  {
174  return (string) $this->xml->children()->summary;
175  }
176 
177  /**
178  * Method to get the access level of the photo
179  *
180  * @return string Photo access level
181  *
182  * @since 12.3
183  */
184  public function getAccess()
185  {
186  return (string) $this->xml->children('gphoto', true)->access;
187  }
188 
189  /**
190  * Method to get the time of the photo
191  *
192  * @return double Photo time
193  *
194  * @since 12.3
195  */
196  public function getTime()
197  {
198  return (double) $this->xml->children('gphoto', true)->timestamp / 1000;
199  }
200 
201  /**
202  * Method to get the size of the photo
203  *
204  * @return int Photo size
205  *
206  * @since 12.3
207  */
208  public function getSize()
209  {
210  return (int) $this->xml->children('gphoto', true)->size;
211  }
212 
213  /**
214  * Method to get the height of the photo
215  *
216  * @return int Photo height
217  *
218  * @since 12.3
219  */
220  public function getHeight()
221  {
222  return (int) $this->xml->children('gphoto', true)->height;
223  }
224 
225  /**
226  * Method to get the width of the photo
227  *
228  * @return int Photo width
229  *
230  * @since 12.3
231  */
232  public function getWidth()
233  {
234  return (int) $this->xml->children('gphoto', true)->width;
235  }
236 
237  /**
238  * Method to set the title of the photo
239  *
240  * @param string $title New photo title
241  *
242  * @return JGoogleDataPicasaPhoto The object for method chaining
243  *
244  * @since 12.3
245  */
246  public function setTitle($title)
247  {
248  $this->xml->children()->title = $title;
249 
250  return $this;
251  }
252 
253  /**
254  * Method to set the summary of the photo
255  *
256  * @param string $summary New photo description
257  *
258  * @return JGoogleDataPicasaPhoto The object for method chaining
259  *
260  * @since 12.3
261  */
262  public function setSummary($summary)
263  {
264  $this->xml->children()->summary = $summary;
265 
266  return $this;
267  }
268 
269  /**
270  * Method to set the access level of the photo
271  *
272  * @param string $access New photo access level
273  *
274  * @return JGoogleDataPicasaPhoto The object for method chaining
275  *
276  * @since 12.3
277  */
278  public function setAccess($access)
279  {
280  $this->xml->children('gphoto', true)->access = $access;
281 
282  return $this;
283  }
284 
285  /**
286  * Method to set the time of the photo
287  *
288  * @param int $time New photo time
289  *
290  * @return JGoogleDataPicasaPhoto The object for method chaining
291  *
292  * @since 12.3
293  */
294  public function setTime($time)
295  {
296  $this->xml->children('gphoto', true)->timestamp = $time * 1000;
297 
298  return $this;
299  }
300 
301  /**
302  * Method to modify a Picasa Photo
303  *
304  * @param string $match Optional eTag matching parameter
305  *
306  * @return mixed Data from Google.
307  *
308  * @since 12.3
309  */
310  public function save($match = '*')
311  {
312  if ($this->isAuthenticated())
313  {
314  $url = $this->getLink();
315 
316  if ($match === true)
317  {
318  $match = $this->xml->xpath('./@gd:etag');
319  $match = $match[0];
320  }
321 
322  try
323  {
324  $headers = array('GData-Version' => 2, 'Content-type' => 'application/atom+xml', 'If-Match' => $match);
325  $jdata = $this->query($url, $this->xml->asXML(), $headers, 'put');
326  }
327  catch (Exception $e)
328  {
329  if (strpos($e->getMessage(), 'Error code 412 received requesting data: Mismatch: etags') === 0)
330  {
331  throw new RuntimeException("Etag match failed: `$match`.");
332  }
333  throw $e;
334  }
335 
336  $this->xml = $this->safeXML($jdata->body);
337 
338  return $this;
339  }
340  else
341  {
342  return false;
343  }
344  }
345 
346  /**
347  * Refresh photo data
348  *
349  * @return mixed Data from Google
350  *
351  * @since 12.3
352  */
353  public function refresh()
354  {
355  if ($this->isAuthenticated())
356  {
357  $url = $this->getLink();
358  $jdata = $this->query($url, null, array('GData-Version' => 2));
359  $this->xml = $this->safeXML($jdata->body);
360 
361  return $this;
362  }
363  else
364  {
365  return false;
366  }
367  }
368 }