PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/linkedin/groups.php

https://github.com/H04S3128/joomla-portal
PHP | 1098 lines | 479 code | 207 blank | 412 comment | 47 complexity | fa11ace7b2fb2187350449707247403d MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Linkedin
  5. *
  6. * @copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die();
  10. /**
  11. * Linkedin API Groups class for the Joomla Platform.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage Linkedin
  15. * @since 13.1
  16. */
  17. class JLinkedinGroups extends JLinkedinObject
  18. {
  19. /**
  20. * Method to get a group.
  21. *
  22. * @param string $id The unique identifier for a group.
  23. * @param string $fields Request fields beyond the default ones.
  24. * @param integer $start Starting location within the result set for paginated returns.
  25. * @param integer $count The number of results returned.
  26. *
  27. * @return array The decoded JSON response
  28. *
  29. * @since 13.1
  30. */
  31. public function getGroup($id, $fields = null, $start = 0, $count = 5)
  32. {
  33. $token = $this->oauth->getToken();
  34. // Set parameters.
  35. $parameters = array(
  36. 'oauth_token' => $token['key']
  37. );
  38. // Set the API base
  39. $base = '/v1/groups/' . $id;
  40. $data['format'] = 'json';
  41. // Check if fields is specified.
  42. if ($fields)
  43. {
  44. $base .= ':' . $fields;
  45. }
  46. // Check if start is specified.
  47. if ($start > 0)
  48. {
  49. $data['start'] = $start;
  50. }
  51. // Check if count is specified.
  52. if ($count != 5)
  53. {
  54. $data['count'] = $count;
  55. }
  56. // Build the request path.
  57. $path = $this->getOption('api.url') . $base;
  58. // Send the request.
  59. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  60. return json_decode($response->body);
  61. }
  62. /**
  63. * Method to find the groups a member belongs to.
  64. *
  65. * @param string $id The unique identifier for a user.
  66. * @param string $fields Request fields beyond the default ones.
  67. * @param integer $start Starting location within the result set for paginated returns.
  68. * @param integer $count The number of results returned.
  69. * @param string $membership_state The state of the caller’s membership to the specified group.
  70. * Values are: non-member, awaiting-confirmation, awaiting-parent-group-confirmation, member, moderator, manager, owner.
  71. *
  72. * @return array The decoded JSON response
  73. *
  74. * @since 13.1
  75. */
  76. public function getMemberships($id = null, $fields = null, $start = 0, $count = 5, $membership_state = null)
  77. {
  78. $token = $this->oauth->getToken();
  79. // Set parameters.
  80. $parameters = array(
  81. 'oauth_token' => $token['key']
  82. );
  83. // Set the API base
  84. $base = '/v1/people/';
  85. // Check if id is specified.
  86. if ($id)
  87. {
  88. $base .= $id . '/group-memberships';
  89. }
  90. else
  91. {
  92. $base .= '~/group-memberships';
  93. }
  94. $data['format'] = 'json';
  95. // Check if fields is specified.
  96. if ($fields)
  97. {
  98. $base .= ':' . $fields;
  99. }
  100. // Check if start is specified.
  101. if ($start > 0)
  102. {
  103. $data['start'] = $start;
  104. }
  105. // Check if count is specified.
  106. if ($count != 5)
  107. {
  108. $data['count'] = $count;
  109. }
  110. // Check if membership_state is specified.
  111. if ($membership_state)
  112. {
  113. $data['membership-state'] = $membership_state;
  114. }
  115. // Build the request path.
  116. $path = $this->getOption('api.url') . $base;
  117. // Send the request.
  118. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  119. return json_decode($response->body);
  120. }
  121. /**
  122. * Method to find the groups a member belongs to.
  123. *
  124. * @param string $person_id The unique identifier for a user.
  125. * @param string $group_id The unique identifier for a group.
  126. * @param string $fields Request fields beyond the default ones.
  127. * @param integer $start Starting location within the result set for paginated returns.
  128. * @param integer $count The number of results returned.
  129. *
  130. * @return array The decoded JSON response
  131. *
  132. * @since 13.1
  133. */
  134. public function getSettings($person_id = null, $group_id = null, $fields = null, $start = 0, $count = 5)
  135. {
  136. $token = $this->oauth->getToken();
  137. // Set parameters.
  138. $parameters = array(
  139. 'oauth_token' => $token['key']
  140. );
  141. // Set the API base
  142. $base = '/v1/people/';
  143. // Check if person_id is specified.
  144. if ($person_id)
  145. {
  146. $base .= $person_id . '/group-memberships';
  147. }
  148. else
  149. {
  150. $base .= '~/group-memberships';
  151. }
  152. // Check if group_id is specified.
  153. if ($group_id)
  154. {
  155. $base .= '/' . $group_id;
  156. }
  157. $data['format'] = 'json';
  158. // Check if fields is specified.
  159. if ($fields)
  160. {
  161. $base .= ':' . $fields;
  162. }
  163. // Check if start is specified.
  164. if ($start > 0)
  165. {
  166. $data['start'] = $start;
  167. }
  168. // Check if count is specified.
  169. if ($count != 5)
  170. {
  171. $data['count'] = $count;
  172. }
  173. // Build the request path.
  174. $path = $this->getOption('api.url') . $base;
  175. // Send the request.
  176. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  177. return json_decode($response->body);
  178. }
  179. /**
  180. * Method to change a groups settings.
  181. *
  182. * @param string $group_id The unique identifier for a group.
  183. * @param boolean $show_logo Show group logo in profile.
  184. * @param string $digest_frequency E-mail digest frequency.
  185. * @param boolean $announcements E-mail announcements from managers.
  186. * @param boolean $allow_messages Allow messages from members.
  187. * @param boolean $new_post E-mail for every new post.
  188. *
  189. * @return array The decoded JSON response
  190. *
  191. * @since 13.1
  192. */
  193. public function changeSettings($group_id, $show_logo = null, $digest_frequency = null, $announcements = null,
  194. $allow_messages = null, $new_post = null)
  195. {
  196. $token = $this->oauth->getToken();
  197. // Set parameters.
  198. $parameters = array(
  199. 'oauth_token' => $token['key']
  200. );
  201. // Set the API base
  202. $base = '/v1/people/~/group-memberships/' . $group_id;
  203. // Build xml.
  204. $xml = '<group-membership>';
  205. if (!is_null($show_logo))
  206. {
  207. $xml .= '<show-group-logo-in-profile>' . $this->booleanToString($show_logo) . '</show-group-logo-in-profile>';
  208. }
  209. if ($digest_frequency)
  210. {
  211. $xml .= '<email-digest-frequency><code>' . $digest_frequency . '</code></email-digest-frequency>';
  212. }
  213. if (!is_null($announcements))
  214. {
  215. $xml .= '<email-announcements-from-managers>' . $this->booleanToString($announcements) . '</email-announcements-from-managers>';
  216. }
  217. if (!is_null($allow_messages))
  218. {
  219. $xml .= '<allow-messages-from-members>' . $this->booleanToString($allow_messages) . '</allow-messages-from-members>';
  220. }
  221. if (!is_null($new_post))
  222. {
  223. $xml .= '<email-for-every-new-post>' . $this->booleanToString($new_post) . '</email-for-every-new-post>';
  224. }
  225. $xml .= '</group-membership>';
  226. // Build the request path.
  227. $path = $this->getOption('api.url') . $base;
  228. $header['Content-Type'] = 'text/xml';
  229. // Send the request.
  230. $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
  231. return $response;
  232. }
  233. /**
  234. * Method to join a group.
  235. *
  236. * @param string $group_id The unique identifier for a group.
  237. * @param boolean $show_logo Show group logo in profile.
  238. * @param string $digest_frequency E-mail digest frequency.
  239. * @param boolean $announcements E-mail announcements from managers.
  240. * @param boolean $allow_messages Allow messages from members.
  241. * @param boolean $new_post E-mail for every new post.
  242. *
  243. * @return array The decoded JSON response
  244. *
  245. * @since 13.1
  246. */
  247. public function joinGroup($group_id, $show_logo = null, $digest_frequency = null, $announcements = null,
  248. $allow_messages = null, $new_post = null)
  249. {
  250. $token = $this->oauth->getToken();
  251. // Set parameters.
  252. $parameters = array(
  253. 'oauth_token' => $token['key']
  254. );
  255. // Set the success response code.
  256. $this->oauth->setOption('success_code', 201);
  257. // Set the API base
  258. $base = '/v1/people/~/group-memberships';
  259. // Build xml.
  260. $xml = '<group-membership><group><id>' . $group_id . '</id></group>';
  261. if (!is_null($show_logo))
  262. {
  263. $xml .= '<show-group-logo-in-profile>' . $this->booleanToString($show_logo) . '</show-group-logo-in-profile>';
  264. }
  265. if ($digest_frequency)
  266. {
  267. $xml .= '<email-digest-frequency><code>' . $digest_frequency . '</code></email-digest-frequency>';
  268. }
  269. if (!is_null($announcements))
  270. {
  271. $xml .= '<email-announcements-from-managers>' . $this->booleanToString($announcements) . '</email-announcements-from-managers>';
  272. }
  273. if (!is_null($allow_messages))
  274. {
  275. $xml .= '<allow-messages-from-members>' . $this->booleanToString($allow_messages) . '</allow-messages-from-members>';
  276. }
  277. if (!is_null($new_post))
  278. {
  279. $xml .= '<email-for-every-new-post>' . $this->booleanToString($new_post) . '</email-for-every-new-post>';
  280. }
  281. $xml .= '<membership-state><code>member</code></membership-state></group-membership>';
  282. // Build the request path.
  283. $path = $this->getOption('api.url') . $base;
  284. $header['Content-Type'] = 'text/xml';
  285. // Send the request.
  286. $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
  287. return $response;
  288. }
  289. /**
  290. * Method to leave a group.
  291. *
  292. * @param string $group_id The unique identifier for a group.
  293. *
  294. * @return array The decoded JSON response
  295. *
  296. * @since 13.1
  297. */
  298. public function leaveGroup($group_id)
  299. {
  300. $token = $this->oauth->getToken();
  301. // Set parameters.
  302. $parameters = array(
  303. 'oauth_token' => $token['key']
  304. );
  305. // Set the success response code.
  306. $this->oauth->setOption('success_code', 204);
  307. // Set the API base
  308. $base = '/v1/people/~/group-memberships/' . $group_id;
  309. // Build the request path.
  310. $path = $this->getOption('api.url') . $base;
  311. // Send the request.
  312. $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters);
  313. return $response;
  314. }
  315. /**
  316. * Method to get dicussions for a group.
  317. *
  318. * @param string $id The unique identifier for a group.
  319. * @param string $fields Request fields beyond the default ones.
  320. * @param integer $start Starting location within the result set for paginated returns.
  321. * @param integer $count The number of results returned.
  322. * @param string $order Sort order for posts. Valid for: recency, popularity.
  323. * @param string $category Category of posts. Valid for: discussion
  324. * @param string $modified_since Timestamp filter for posts created after the specified value.
  325. *
  326. * @return array The decoded JSON response
  327. *
  328. * @since 13.1
  329. */
  330. public function getDiscussions($id, $fields = null, $start = 0, $count = 0, $order = null, $category = 'discussion', $modified_since = null)
  331. {
  332. $token = $this->oauth->getToken();
  333. // Set parameters.
  334. $parameters = array(
  335. 'oauth_token' => $token['key']
  336. );
  337. // Set the API base
  338. $base = '/v1/groups/' . $id . '/posts';
  339. $data['format'] = 'json';
  340. // Check if fields is specified.
  341. if ($fields)
  342. {
  343. $base .= ':' . $fields;
  344. }
  345. // Check if start is specified.
  346. if ($start > 0)
  347. {
  348. $data['start'] = $start;
  349. }
  350. // Check if count is specified.
  351. if ($count > 0)
  352. {
  353. $data['count'] = $count;
  354. }
  355. // Check if order is specified.
  356. if ($order)
  357. {
  358. $data['order'] = $order;
  359. }
  360. // Check if category is specified.
  361. if ($category)
  362. {
  363. $data['category'] = $category;
  364. }
  365. // Check if modified_since is specified.
  366. if ($modified_since)
  367. {
  368. $data['modified-since'] = $modified_since;
  369. }
  370. // Build the request path.
  371. $path = $this->getOption('api.url') . $base;
  372. // Send the request.
  373. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  374. return json_decode($response->body);
  375. }
  376. /**
  377. * Method to get posts a user started / participated in / follows for a group.
  378. *
  379. * @param string $group_id The unique identifier for a group.
  380. * @param string $role Filter for posts related to the caller. Valid for: creator, commenter, follower.
  381. * @param string $person_id The unique identifier for a user.
  382. * @param string $fields Request fields beyond the default ones.
  383. * @param integer $start Starting location within the result set for paginated returns.
  384. * @param integer $count The number of results returned.
  385. * @param string $order Sort order for posts. Valid for: recency, popularity.
  386. * @param string $category Category of posts. Valid for: discussion
  387. * @param string $modified_since Timestamp filter for posts created after the specified value.
  388. *
  389. * @return array The decoded JSON response
  390. *
  391. * @since 13.1
  392. */
  393. public function getUserPosts($group_id, $role, $person_id = null, $fields = null, $start = 0, $count = 0,
  394. $order = null, $category = 'discussion', $modified_since = null)
  395. {
  396. $token = $this->oauth->getToken();
  397. // Set parameters.
  398. $parameters = array(
  399. 'oauth_token' => $token['key']
  400. );
  401. // Set the API base
  402. $base = '/v1/people/';
  403. // Check if person_id is specified.
  404. if ($person_id)
  405. {
  406. $base .= $person_id;
  407. }
  408. else
  409. {
  410. $base .= '~';
  411. }
  412. $base .= '/group-memberships/' . $group_id . '/posts';
  413. $data['format'] = 'json';
  414. $data['role'] = $role;
  415. // Check if fields is specified.
  416. if ($fields)
  417. {
  418. $base .= ':' . $fields;
  419. }
  420. // Check if start is specified.
  421. if ($start > 0)
  422. {
  423. $data['start'] = $start;
  424. }
  425. // Check if count is specified.
  426. if ($count > 0)
  427. {
  428. $data['count'] = $count;
  429. }
  430. // Check if order is specified.
  431. if ($order)
  432. {
  433. $data['order'] = $order;
  434. }
  435. // Check if category is specified.
  436. if ($category)
  437. {
  438. $data['category'] = $category;
  439. }
  440. // Check if modified_since is specified.
  441. if ($modified_since)
  442. {
  443. $data['modified-since'] = $modified_since;
  444. }
  445. // Build the request path.
  446. $path = $this->getOption('api.url') . $base;
  447. // Send the request.
  448. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  449. return json_decode($response->body);
  450. }
  451. /**
  452. * Method to retrieve details about a post.
  453. *
  454. * @param string $post_id The unique identifier for a post.
  455. * @param string $fields Request fields beyond the default ones.
  456. *
  457. * @return array The decoded JSON response
  458. *
  459. * @since 13.1
  460. */
  461. public function getPost($post_id, $fields = null)
  462. {
  463. $token = $this->oauth->getToken();
  464. // Set parameters.
  465. $parameters = array(
  466. 'oauth_token' => $token['key']
  467. );
  468. // Set the API base
  469. $base = '/v1/posts/' . $post_id;
  470. $data['format'] = 'json';
  471. // Check if fields is specified.
  472. if ($fields)
  473. {
  474. $base .= ':' . $fields;
  475. }
  476. // Build the request path.
  477. $path = $this->getOption('api.url') . $base;
  478. // Send the request.
  479. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  480. return json_decode($response->body);
  481. }
  482. /**
  483. * Method to retrieve all comments of a post.
  484. *
  485. * @param string $post_id The unique identifier for a post.
  486. * @param string $fields Request fields beyond the default ones.
  487. * @param integer $start Starting location within the result set for paginated returns.
  488. * @param integer $count The number of results returned.
  489. *
  490. * @return array The decoded JSON response
  491. *
  492. * @since 13.1
  493. */
  494. public function getPostComments($post_id, $fields = null, $start = 0, $count = 0)
  495. {
  496. $token = $this->oauth->getToken();
  497. // Set parameters.
  498. $parameters = array(
  499. 'oauth_token' => $token['key']
  500. );
  501. // Set the API base
  502. $base = '/v1/posts/' . $post_id . '/comments';
  503. $data['format'] = 'json';
  504. // Check if fields is specified.
  505. if ($fields)
  506. {
  507. $base .= ':' . $fields;
  508. }
  509. // Check if start is specified.
  510. if ($start > 0)
  511. {
  512. $data['start'] = $start;
  513. }
  514. // Check if count is specified.
  515. if ($count > 0)
  516. {
  517. $data['count'] = $count;
  518. }
  519. // Build the request path.
  520. $path = $this->getOption('api.url') . $base;
  521. // Send the request.
  522. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  523. return json_decode($response->body);
  524. }
  525. /**
  526. * Method to retrieve all comments of a post.
  527. *
  528. * @param string $group_id The unique identifier for a group.
  529. * @param string $title Post title.
  530. * @param string $summary Post summary.
  531. *
  532. * @return string The created post's id.
  533. *
  534. * @since 13.1
  535. */
  536. public function createPost($group_id, $title, $summary)
  537. {
  538. $token = $this->oauth->getToken();
  539. // Set parameters.
  540. $parameters = array(
  541. 'oauth_token' => $token['key']
  542. );
  543. // Set the success response code.
  544. $this->oauth->setOption('success_code', 201);
  545. // Set the API base
  546. $base = '/v1/groups/' . $group_id . '/posts';
  547. // Build xml.
  548. $xml = '<post><title>' . $title . '</title><summary>' . $summary . '</summary></post>';
  549. // Build the request path.
  550. $path = $this->getOption('api.url') . $base;
  551. $header['Content-Type'] = 'text/xml';
  552. // Send the request.
  553. $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
  554. // Return the post id.
  555. $response = explode('posts/', $response->headers['Location']);
  556. return $response[1];
  557. }
  558. /**
  559. * Method to like or unlike a post.
  560. *
  561. * @param string $post_id The unique identifier for a group.
  562. * @param boolean $like True to like post, false otherwise.
  563. *
  564. * @return array The decoded JSON response
  565. *
  566. * @since 13.1
  567. */
  568. private function _likeUnlike($post_id, $like)
  569. {
  570. $token = $this->oauth->getToken();
  571. // Set parameters.
  572. $parameters = array(
  573. 'oauth_token' => $token['key']
  574. );
  575. // Set the success response code.
  576. $this->oauth->setOption('success_code', 204);
  577. // Set the API base
  578. $base = '/v1/posts/' . $post_id . '/relation-to-viewer/is-liked';
  579. // Build xml.
  580. $xml = '<is-liked>' . $this->booleanToString($like) . '</is-liked>';
  581. // Build the request path.
  582. $path = $this->getOption('api.url') . $base;
  583. $header['Content-Type'] = 'text/xml';
  584. // Send the request.
  585. $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
  586. return $response;
  587. }
  588. /**
  589. * Method used to like a post.
  590. *
  591. * @param string $post_id The unique identifier for a group.
  592. *
  593. * @return array The decoded JSON response
  594. *
  595. * @since 13.1
  596. */
  597. public function likePost($post_id)
  598. {
  599. return $this->_likeUnlike($post_id, true);
  600. }
  601. /**
  602. * Method used to unlike a post.
  603. *
  604. * @param string $post_id The unique identifier for a group.
  605. *
  606. * @return array The decoded JSON response
  607. *
  608. * @since 13.1
  609. */
  610. public function unlikePost($post_id)
  611. {
  612. return $this->_likeUnlike($post_id, false);
  613. }
  614. /**
  615. * Method to follow or unfollow a post.
  616. *
  617. * @param string $post_id The unique identifier for a group.
  618. * @param boolean $follow True to like post, false otherwise.
  619. *
  620. * @return array The decoded JSON response
  621. *
  622. * @since 13.1
  623. */
  624. private function _followUnfollow($post_id, $follow)
  625. {
  626. $token = $this->oauth->getToken();
  627. // Set parameters.
  628. $parameters = array(
  629. 'oauth_token' => $token['key']
  630. );
  631. // Set the success response code.
  632. $this->oauth->setOption('success_code', 204);
  633. // Set the API base
  634. $base = '/v1/posts/' . $post_id . '/relation-to-viewer/is-following';
  635. // Build xml.
  636. $xml = '<is-following>' . $this->booleanToString($follow) . '</is-following>';
  637. // Build the request path.
  638. $path = $this->getOption('api.url') . $base;
  639. $header['Content-Type'] = 'text/xml';
  640. // Send the request.
  641. $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
  642. return $response;
  643. }
  644. /**
  645. * Method used to follow a post.
  646. *
  647. * @param string $post_id The unique identifier for a group.
  648. *
  649. * @return array The decoded JSON response
  650. *
  651. * @since 13.1
  652. */
  653. public function followPost($post_id)
  654. {
  655. return $this->_followUnfollow($post_id, true);
  656. }
  657. /**
  658. * Method used to unfollow a post.
  659. *
  660. * @param string $post_id The unique identifier for a group.
  661. *
  662. * @return array The decoded JSON response
  663. *
  664. * @since 13.1
  665. */
  666. public function unfollowPost($post_id)
  667. {
  668. return $this->_followUnfollow($post_id, false);
  669. }
  670. /**
  671. * Method to flag a post as a Promotion or Job.
  672. *
  673. * @param string $post_id The unique identifier for a group.
  674. * @param string $flag Flag as a 'promotion' or 'job'.
  675. *
  676. * @return array The decoded JSON response
  677. *
  678. * @since 13.1
  679. */
  680. public function flagPost($post_id, $flag)
  681. {
  682. $token = $this->oauth->getToken();
  683. // Set parameters.
  684. $parameters = array(
  685. 'oauth_token' => $token['key']
  686. );
  687. // Set the success response code.
  688. $this->oauth->setOption('success_code', 204);
  689. // Set the API base
  690. $base = '/v1/posts/' . $post_id . '/category/code';
  691. // Build xml.
  692. $xml = '<code>' . $flag . '</code>';
  693. // Build the request path.
  694. $path = $this->getOption('api.url') . $base;
  695. $header['Content-Type'] = 'text/xml';
  696. // Send the request.
  697. $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
  698. return $response;
  699. }
  700. /**
  701. * Method to delete a post if the current user is the creator or flag it as inappropriate otherwise.
  702. *
  703. * @param string $post_id The unique identifier for a group.
  704. *
  705. * @return array The decoded JSON response
  706. *
  707. * @since 13.1
  708. */
  709. public function deletePost($post_id)
  710. {
  711. $token = $this->oauth->getToken();
  712. // Set parameters.
  713. $parameters = array(
  714. 'oauth_token' => $token['key']
  715. );
  716. // Set the success response code.
  717. $this->oauth->setOption('success_code', 204);
  718. // Set the API base
  719. $base = '/v1/posts/' . $post_id;
  720. // Build the request path.
  721. $path = $this->getOption('api.url') . $base;
  722. // Send the request.
  723. $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters);
  724. return $response;
  725. }
  726. /**
  727. * Method to access the comments resource.
  728. *
  729. * @param string $comment_id The unique identifier for a comment.
  730. * @param string $fields Request fields beyond the default ones.
  731. *
  732. * @return array The decoded JSON response
  733. *
  734. * @since 13.1
  735. */
  736. public function getComment($comment_id, $fields = null)
  737. {
  738. $token = $this->oauth->getToken();
  739. // Set parameters.
  740. $parameters = array(
  741. 'oauth_token' => $token['key']
  742. );
  743. // Set the API base
  744. $base = '/v1/comments/' . $comment_id;
  745. $data['format'] = 'json';
  746. // Check if fields is specified.
  747. if ($fields)
  748. {
  749. $base .= ':' . $fields;
  750. }
  751. // Build the request path.
  752. $path = $this->getOption('api.url') . $base;
  753. // Send the request.
  754. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  755. return json_decode($response->body);
  756. }
  757. /**
  758. * Method to add a comment to a post
  759. *
  760. * @param string $post_id The unique identifier for a group.
  761. * @param string $comment The post comment's text.
  762. *
  763. * @return string The created comment's id.
  764. *
  765. * @since 13.1
  766. */
  767. public function addComment($post_id, $comment)
  768. {
  769. $token = $this->oauth->getToken();
  770. // Set parameters.
  771. $parameters = array(
  772. 'oauth_token' => $token['key']
  773. );
  774. // Set the success response code.
  775. $this->oauth->setOption('success_code', 201);
  776. // Set the API base
  777. $base = '/v1/posts/' . $post_id . '/comments';
  778. // Build xml.
  779. $xml = '<comment><text>' . $comment . '</text></comment>';
  780. // Build the request path.
  781. $path = $this->getOption('api.url') . $base;
  782. $header['Content-Type'] = 'text/xml';
  783. // Send the request.
  784. $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
  785. // Return the comment id.
  786. $response = explode('comments/', $response->headers['Location']);
  787. return $response[1];
  788. }
  789. /**
  790. * Method to delete a comment if the current user is the creator or flag it as inappropriate otherwise.
  791. *
  792. * @param string $comment_id The unique identifier for a group.
  793. *
  794. * @return array The decoded JSON response
  795. *
  796. * @since 13.1
  797. */
  798. public function deleteComment($comment_id)
  799. {
  800. $token = $this->oauth->getToken();
  801. // Set parameters.
  802. $parameters = array(
  803. 'oauth_token' => $token['key']
  804. );
  805. // Set the success response code.
  806. $this->oauth->setOption('success_code', 204);
  807. // Set the API base
  808. $base = '/v1/comments/' . $comment_id;
  809. // Build the request path.
  810. $path = $this->getOption('api.url') . $base;
  811. // Send the request.
  812. $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters);
  813. return $response;
  814. }
  815. /**
  816. * Method to get suggested groups for a user.
  817. *
  818. * @param string $person_id The unique identifier for a user.
  819. * @param string $fields Request fields beyond the default ones.
  820. *
  821. * @return array The decoded JSON response
  822. *
  823. * @since 13.1
  824. */
  825. public function getSuggested($person_id = null, $fields = null)
  826. {
  827. $token = $this->oauth->getToken();
  828. // Set parameters.
  829. $parameters = array(
  830. 'oauth_token' => $token['key']
  831. );
  832. // Set the API base
  833. $base = '/v1/people/';
  834. // Check if person_id is specified.
  835. if ($person_id)
  836. {
  837. $base .= $person_id . '/suggestions/groups';
  838. }
  839. else
  840. {
  841. $base .= '~/suggestions/groups';
  842. }
  843. $data['format'] = 'json';
  844. // Check if fields is specified.
  845. if ($fields)
  846. {
  847. $base .= ':' . $fields;
  848. }
  849. // Build the request path.
  850. $path = $this->getOption('api.url') . $base;
  851. // Send the request.
  852. $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
  853. return json_decode($response->body);
  854. }
  855. /**
  856. * Method to delete a group suggestion for a user.
  857. *
  858. * @param string $suggestion_id The unique identifier for a suggestion.
  859. * @param string $person_id The unique identifier for a user.
  860. *
  861. * @return array The decoded JSON response
  862. *
  863. * @since 13.1
  864. */
  865. public function deleteSuggestion($suggestion_id, $person_id = null)
  866. {
  867. $token = $this->oauth->getToken();
  868. // Set parameters.
  869. $parameters = array(
  870. 'oauth_token' => $token['key']
  871. );
  872. // Set the success response code.
  873. $this->oauth->setOption('success_code', 204);
  874. // Set the API base
  875. $base = '/v1/people/';
  876. // Check if person_id is specified.
  877. if ($person_id)
  878. {
  879. $base .= $person_id . '/suggestions/groups/' . $suggestion_id;
  880. }
  881. else
  882. {
  883. $base .= '~/suggestions/groups/' . $suggestion_id;
  884. }
  885. // Build the request path.
  886. $path = $this->getOption('api.url') . $base;
  887. // Send the request.
  888. $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters);
  889. return $response;
  890. }
  891. }