@@ -47,6 +47,7 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
47
47
48
48
curl_setopt ($ ch , CURLOPT_URL , $ url );
49
49
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
50
+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
50
51
$ body = curl_exec ($ ch );
51
52
$ rc = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
52
53
curl_close ($ ch );
@@ -94,42 +95,40 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
94
95
return $ resObj ;
95
96
}
96
97
97
- /**
98
+ /**
98
99
* group
99
100
*
100
- * @param string $groupname name of group
101
- * @param string $usernames users to add to group
101
+ * @param string $groupname name of group to be created
102
+ * @param string $usernames users in the group
102
103
*
103
104
* @return mixed HTTP return code and API return object
104
105
*/
105
-
106
106
function group ($ groupname , $ usernames = array ())
107
107
{
108
- $ obj = $ this ->_getRequest ( " /admin/groups.json " );
109
- if ($ obj -> http_code != 200 ) {
108
+ $ groupId = $ this ->getGroupIdByGroupName ( $ groupname );
109
+ if ($ groupId ) {
110
110
return false ;
111
111
}
112
-
113
- foreach ($ obj ->apiresult as $ group ) {
114
- if ($ group ->name === $ groupname ) {
115
- $ groupId = $ group ->id ;
116
- break ;
117
- }
118
- $ groupId = false ;
119
- }
120
-
121
112
$ params = array (
122
113
'group ' => array (
123
114
'name ' => $ groupname ,
124
- 'usernames ' => implode (', ' , $ usernames )
115
+ 'usernames ' => implode (', ' , $ usernames ),
116
+ 'alias_level ' => '0 '
125
117
)
126
118
);
119
+ return $ this ->_postRequest ('/admin/groups ' , $ params );
120
+ }
127
121
128
- if ($ groupId ) {
129
- return $ this ->_putRequest ('/admin/groups/ ' . $ groupId , $ params );
130
- } else {
131
- return $ this ->_postRequest ('/admin/groups ' , $ params );
132
- }
122
+
123
+ /**
124
+ * getCategories
125
+ *
126
+ * @return mixed HTTP return code and API return object
127
+ */
128
+
129
+ function getCategories ()
130
+ {
131
+ return $ this ->_getRequest ("/categories.json " );
133
132
}
134
133
135
134
/**
@@ -276,6 +275,20 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
276
275
return $ this ->_postRequest ('/posts ' , $ params , $ userName );
277
276
}
278
277
278
+
279
+ function getCategory ($ categoryName ) {
280
+ return $ this ->_getRequest ("/c/ {$ categoryName }.json " );
281
+ }
282
+
283
+ /**
284
+ * getTopic
285
+ *
286
+ */
287
+
288
+ function getTopic ($ topicId ) {
289
+ return $ this ->_getRequest ("/t/ {$ topicId }.json " );
290
+ }
291
+
279
292
/**
280
293
* createPost
281
294
*
@@ -307,5 +320,78 @@ function changeSiteSetting($siteSetting, $value)
307
320
$ params = array ($ siteSetting => $ value );
308
321
return $ this ->_putRequest ('/admin/site_settings/ ' . $ siteSetting , $ params );
309
322
}
323
+
324
+ # these are mostly from timolaine
325
+
326
+ /**
327
+ * getUserByEmail
328
+ *
329
+ * @param string $email email of user
330
+ *
331
+ * @return mixed user object
332
+ */
333
+
334
+ function getUserByEmail ($ email )
335
+ {
336
+ $ users = $ this ->_getRequest ("/admin/users/list/active.json " );
337
+ foreach ($ users ->apiresult as $ user ) {
338
+ if (strtolower ($ user ->email ) === strtolower ($ email )) {
339
+ return $ user ;
340
+ }
341
+ }
342
+
343
+ return false ;
344
+ }
345
+
346
+ /*
347
+ * getGroupIdByGroupName
348
+ *
349
+ * @param string $groupname name of group
350
+ *
351
+ * @return mixed id of the group, or false if nonexistent
352
+ */
353
+
354
+ function getGroupIdByGroupName ($ groupname )
355
+ {
356
+ $ obj = $ this ->getGroups ();
357
+ if ($ obj ->http_code != 200 ) {
358
+ return false ;
359
+ }
360
+
361
+ foreach ($ obj ->apiresult as $ group ) {
362
+ if ($ group ->name === $ groupname ) {
363
+ $ groupId = intval ($ group ->id );
364
+ break ;
365
+ }
366
+ $ groupId = false ;
367
+ }
368
+
369
+ return $ groupId ;
370
+ }
371
+
372
+ /**
373
+ * addUserToGroup
374
+ *
375
+ * @param string $groupname name of group
376
+ * @param string $username user to add to the group
377
+ *
378
+ * @return mixed HTTP return code and API return object
379
+ */
380
+
381
+ function addUserToGroup ($ groupname , $ username )
382
+ {
383
+ $ groupId = $ this ->getGroupIdByGroupName ($ groupname );
384
+ if (!$ groupId ) {
385
+ $ this ->group ($ groupname , array ($ username ));
386
+ } else {
387
+ $ user = $ this ->getUserByUserName ($ username )->apiresult ->user ;
388
+ $ params = array (
389
+ 'group_id ' => $ groupId
390
+ );
391
+ return $ this ->_postRequest ('/admin/users/ ' . $ user ->id . '/groups ' , $ params );
392
+ }
393
+ }
394
+
395
+
310
396
}
311
397
0 commit comments