1 <?php
  2 /**
  3  * @package     Joomla.Platform
  4  * @subpackage  Linkedin
  5  *
  6  * @copyright   Copyright (C) 2005 - 2017 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  * Linkedin API Companies class for the Joomla Platform.
 14  *
 15  * @since  13.1
 16  */
 17 class JLinkedinCompanies extends JLinkedinObject
 18 {
 19     /**
 20      * Method to retrieve companies using a company ID, a universal name, or an email domain.
 21      *
 22      * @param   integer  $id      The unique internal numeric company identifier.
 23      * @param   string   $name    The unique string identifier for a company.
 24      * @param   string   $domain  Company email domains.
 25      * @param   string   $fields  Request fields beyond the default ones.
 26      *
 27      * @return  array  The decoded JSON response
 28      *
 29      * @since   13.1
 30      * @throws  RuntimeException
 31      */
 32     public function getCompanies($id = null, $name = null, $domain = null, $fields = null)
 33     {
 34         // At least one value is needed to retrieve data.
 35         if ($id == null && $name == null && $domain == null)
 36         {
 37             // We don't have a valid entry
 38             throw new RuntimeException('You must specify a company ID, a universal name, or an email domain.');
 39         }
 40 
 41         $token = $this->oauth->getToken();
 42 
 43         // Set parameters.
 44         $parameters = array(
 45             'oauth_token' => $token['key'],
 46         );
 47 
 48         // Set the API base
 49         $base = '/v1/companies';
 50 
 51         if ($id && $name)
 52         {
 53             $base .= '::(' . $id . ',universal-name=' . $name . ')';
 54         }
 55         elseif ($id)
 56         {
 57             $base .= '/' . $id;
 58         }
 59         elseif ($name)
 60         {
 61             $base .= '/universal-name=' . $name;
 62         }
 63 
 64         // Set request parameters.
 65         $data['format'] = 'json';
 66 
 67         if ($domain)
 68         {
 69             $data['email-domain'] = $domain;
 70         }
 71 
 72         // Check if fields is specified.
 73         if ($fields)
 74         {
 75             $base .= ':' . $fields;
 76         }
 77 
 78         // Build the request path.
 79         $path = $this->getOption('api.url') . $base;
 80 
 81         // Send the request.
 82         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
 83 
 84         return json_decode($response->body);
 85     }
 86 
 87     /**
 88      * Method to read shares for a particular company .
 89      *
 90      * @param   string   $id     The unique company identifier.
 91      * @param   string   $type   Any valid Company Update Type from the table: https://developer.linkedin.com/reading-company-updates.
 92      * @param   integer  $count  Maximum number of updates to return.
 93      * @param   integer  $start  The offset by which to start Network Update pagination.
 94      *
 95      * @return  array  The decoded JSON response
 96      *
 97      * @since   13.1
 98      */
 99     public function getUpdates($id, $type = null, $count = 0, $start = 0)
100     {
101         $token = $this->oauth->getToken();
102 
103         // Set parameters.
104         $parameters = array(
105             'oauth_token' => $token['key'],
106         );
107 
108         // Set the API base
109         $base = '/v1/companies/' . $id . '/updates';
110 
111         // Set request parameters.
112         $data['format'] = 'json';
113 
114         // Check if type is specified.
115         if ($type)
116         {
117             $data['event-type'] = $type;
118         }
119 
120         // Check if count is specified.
121         if ($count > 0)
122         {
123             $data['count'] = $count;
124         }
125 
126         // Check if start is specified.
127         if ($start > 0)
128         {
129             $data['start'] = $start;
130         }
131 
132         // Build the request path.
133         $path = $this->getOption('api.url') . $base;
134 
135         // Send the request.
136         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
137 
138         return json_decode($response->body);
139     }
140 
141     /**
142      * Method to search across company pages.
143      *
144      * @param   string   $fields    Request fields beyond the default ones.
145      * @param   string   $keywords  Members who have all the keywords anywhere in their profile.
146      * @param   boolean  $hq        Matching companies by the headquarters location. When this is set to "true" and a location facet is used,
147      *                              this restricts returned companies to only those whose headquarters resides in the specified location.
148      * @param   string   $facets    Facet buckets to return, e.g. location.
149      * @param   array    $facet     Array of facet values to search over. Contains values for location, industry, network, company-size,
150      *                              num-followers-range and fortune, in exactly this order, null must be specified for an element if no value.
151      * @param   integer  $start     Starting location within the result set for paginated returns.
152      * @param   integer  $count     The number of results returned.
153      * @param   string   $sort      Controls the search result order. There are four options: relevance, relationship,
154      *                              followers and company-size.
155      *
156      * @return  array  The decoded JSON response
157      *
158      * @since   13.1
159      */
160     public function search($fields = null, $keywords = null, $hq = false, $facets = null, $facet = null, $start = 0, $count = 0, $sort = null)
161     {
162         $token = $this->oauth->getToken();
163 
164         // Set parameters.
165         $parameters = array(
166             'oauth_token' => $token['key'],
167         );
168 
169         // Set the API base
170         $base = '/v1/company-search';
171 
172         $data['format'] = 'json';
173 
174         // Check if fields is specified.
175         if ($fields)
176         {
177             $base .= ':' . $fields;
178         }
179 
180         // Check if keywords is specified.
181         if ($keywords)
182         {
183             $data['keywords'] = $keywords;
184         }
185 
186         // Check if hq is true.
187         if ($hq)
188         {
189             $data['hq-only'] = $hq;
190         }
191 
192         // Check if facets is specified.
193         if ($facets)
194         {
195             $data['facets'] = $facets;
196         }
197 
198         // Check if facet is specified.
199         if ($facet)
200         {
201             $data['facet'] = array();
202 
203             for ($i = 0, $iMax = count($facet); $i < $iMax; $i++)
204             {
205                 if ($facet[$i])
206                 {
207                     if ($i == 0)
208                     {
209                         $data['facet'][] = 'location,' . $facet[$i];
210                     }
211 
212                     if ($i == 1)
213                     {
214                         $data['facet'][] = 'industry,' . $facet[$i];
215                     }
216 
217                     if ($i == 2)
218                     {
219                         $data['facet'][] = 'network,' . $facet[$i];
220                     }
221 
222                     if ($i == 3)
223                     {
224                         $data['facet'][] = 'company-size,' . $facet[$i];
225                     }
226 
227                     if ($i == 4)
228                     {
229                         $data['facet'][] = 'num-followers-range,' . $facet[$i];
230                     }
231 
232                     if ($i == 5)
233                     {
234                         $data['facet'][] = 'fortune,' . $facet[$i];
235                     }
236                 }
237             }
238         }
239 
240         // Check if start is specified.
241         if ($start > 0)
242         {
243             $data['start'] = $start;
244         }
245 
246         // Check if count is specified.
247         if ($count > 0)
248         {
249             $data['count'] = $count;
250         }
251 
252         // Check if sort is specified.
253         if ($sort)
254         {
255             $data['sort'] = $sort;
256         }
257 
258         // Build the request path.
259         $path = $this->getOption('api.url') . $base;
260 
261         // Send the request.
262         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
263 
264         return json_decode($response->body);
265     }
266 
267     /**
268      * Method to get a list of companies the current member is following.
269      *
270      * @param   string  $fields  Request fields beyond the default ones.
271      *
272      * @return  array  The decoded JSON response
273      *
274      * @since   13.1
275      */
276     public function getFollowed($fields = null)
277     {
278         $token = $this->oauth->getToken();
279 
280         // Set parameters.
281         $parameters = array(
282             'oauth_token' => $token['key'],
283         );
284 
285         // Set the API base
286         $base = '/v1/people/~/following/companies';
287 
288         $data['format'] = 'json';
289 
290         // Check if fields is specified.
291         if ($fields)
292         {
293             $base .= ':' . $fields;
294         }
295 
296         // Build the request path.
297         $path = $this->getOption('api.url') . $base;
298 
299         // Send the request.
300         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
301 
302         return json_decode($response->body);
303     }
304 
305     /**
306      * Method to follow a company.
307      *
308      * @param   string  $id  The unique identifier for a company.
309      *
310      * @return  array  The decoded JSON response
311      *
312      * @since   13.1
313      */
314     public function follow($id)
315     {
316         $token = $this->oauth->getToken();
317 
318         // Set parameters.
319         $parameters = array(
320             'oauth_token' => $token['key'],
321         );
322 
323         // Set the success response code.
324         $this->oauth->setOption('success_code', 201);
325 
326         // Set the API base
327         $base = '/v1/people/~/following/companies';
328 
329         // Build xml.
330         $xml = '<company><id>' . $id . '</id></company>';
331 
332         // Build the request path.
333         $path = $this->getOption('api.url') . $base;
334 
335         $header['Content-Type'] = 'text/xml';
336 
337         // Send the request.
338         $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
339 
340         return $response;
341     }
342 
343     /**
344      * Method to unfollow a company.
345      *
346      * @param   string  $id  The unique identifier for a company.
347      *
348      * @return  array  The decoded JSON response
349      *
350      * @since   13.1
351      */
352     public function unfollow($id)
353     {
354         $token = $this->oauth->getToken();
355 
356         // Set parameters.
357         $parameters = array(
358             'oauth_token' => $token['key'],
359         );
360 
361         // Set the success response code.
362         $this->oauth->setOption('success_code', 204);
363 
364         // Set the API base
365         $base = '/v1/people/~/following/companies/id=' . $id;
366 
367         // Build the request path.
368         $path = $this->getOption('api.url') . $base;
369 
370         // Send the request.
371         $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters);
372 
373         return $response;
374     }
375 
376     /**
377      * Method to get a collection of suggested companies for the current user.
378      *
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      *
383      * @return  array  The decoded JSON response
384      *
385      * @since   13.1
386      */
387     public function getSuggested($fields = null, $start = 0, $count = 0)
388     {
389         $token = $this->oauth->getToken();
390 
391         // Set parameters.
392         $parameters = array(
393             'oauth_token' => $token['key'],
394         );
395 
396         // Set the API base
397         $base = '/v1/people/~/suggestions/to-follow/companies';
398 
399         $data['format'] = 'json';
400 
401         // Check if fields is specified.
402         if ($fields)
403         {
404             $base .= ':' . $fields;
405         }
406 
407         // Check if start is specified.
408         if ($start > 0)
409         {
410             $data['start'] = $start;
411         }
412 
413         // Check if count is specified.
414         if ($count > 0)
415         {
416             $data['count'] = $count;
417         }
418 
419         // Build the request path.
420         $path = $this->getOption('api.url') . $base;
421 
422         // Send the request.
423         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
424 
425         return json_decode($response->body);
426     }
427 
428     /**
429      * Method to get a collection of suggested companies for the current user.
430      *
431      * @param   string   $id      The unique identifier for a company.
432      * @param   string   $fields  Request fields beyond the default ones.
433      * @param   integer  $start   Starting location within the result set for paginated returns.
434      * @param   integer  $count   The number of results returned.
435      *
436      * @return  array  The decoded JSON response
437      *
438      * @since   13.1
439      */
440     public function getProducts($id, $fields = null, $start = 0, $count = 0)
441     {
442         $token = $this->oauth->getToken();
443 
444         // Set parameters.
445         $parameters = array(
446             'oauth_token' => $token['key'],
447         );
448 
449         // Set the API base
450         $base = '/v1/companies/' . $id . '/products';
451 
452         $data['format'] = 'json';
453 
454         // Check if fields is specified.
455         if ($fields)
456         {
457             $base .= ':' . $fields;
458         }
459 
460         // Check if start is specified.
461         if ($start > 0)
462         {
463             $data['start'] = $start;
464         }
465 
466         // Check if count is specified.
467         if ($count > 0)
468         {
469             $data['count'] = $count;
470         }
471 
472         // Build the request path.
473         $path = $this->getOption('api.url') . $base;
474 
475         // Send the request.
476         $response = $this->oauth->oauthRequest($path, 'GET', $parameters, $data);
477 
478         return json_decode($response->body);
479     }
480 }
481