@@ -47,6 +47,7 @@ private function _getRequest($reqString, $paramArray = null, $apiUser = 'system'
4747
4848 curl_setopt ($ ch , CURLOPT_URL , $ url );
4949 curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
50+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
5051 $ body = curl_exec ($ ch );
5152 $ rc = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
5253 curl_close ($ ch );
@@ -94,42 +95,40 @@ private function _putpostRequest($reqString, $paramArray, $apiUser = 'system', $
9495 return $ resObj ;
9596 }
9697
97- /**
98+ /**
9899 * group
99100 *
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
102103 *
103104 * @return mixed HTTP return code and API return object
104105 */
105-
106106 function group ($ groupname , $ usernames = array ())
107107 {
108- $ obj = $ this ->_getRequest ( " /admin/groups.json " );
109- if ($ obj -> http_code != 200 ) {
108+ $ groupId = $ this ->getGroupIdByGroupName ( $ groupname );
109+ if ($ groupId ) {
110110 return false ;
111111 }
112-
113- foreach ($ obj ->apiresult as $ group ) {
114- if ($ group ->name === $ groupname ) {
115- $ groupId = $ group ->id ;
116- break ;
117- }
118- $ groupId = false ;
119- }
120-
121112 $ params = array (
122113 'group ' => array (
123114 'name ' => $ groupname ,
124- 'usernames ' => implode (', ' , $ usernames )
115+ 'usernames ' => implode (', ' , $ usernames ),
116+ 'alias_level ' => '0 '
125117 )
126118 );
119+ return $ this ->_postRequest ('/admin/groups ' , $ params );
120+ }
127121
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 " );
133132 }
134133
135134 /**
@@ -276,6 +275,20 @@ function createTopic($topicTitle, $bodyText, $categoryId, $userName, $replyToId
276275 return $ this ->_postRequest ('/posts ' , $ params , $ userName );
277276 }
278277
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+
279292 /**
280293 * createPost
281294 *
@@ -307,5 +320,78 @@ function changeSiteSetting($siteSetting, $value)
307320 $ params = array ($ siteSetting => $ value );
308321 return $ this ->_putRequest ('/admin/site_settings/ ' . $ siteSetting , $ params );
309322 }
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+
310396}
311397
0 commit comments