PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Joomla/Linkedin/Groups.php

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