]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Consolidate group creation into static function in User_group
authorEric Helgeson <erichelgeson@gmail.com>
Wed, 18 Nov 2009 20:53:33 +0000 (15:53 -0500)
committerEric Helgeson <erichelgeson@gmail.com>
Wed, 18 Nov 2009 20:54:24 +0000 (15:54 -0500)
actions/apigroupcreate.php
actions/newgroup.php
classes/User_group.php

index 895dfb7abae5f2074217a47c9e25be65fd49d20f..8827d1c5ce0774da30de682dd4a9fd0bb080ec25 100644 (file)
@@ -117,61 +117,13 @@ class ApiGroupCreateAction extends ApiAuthAction
             return;
         }
 
-        $group = new User_group();
-
-        $group->query('BEGIN');
-
-        $group->nickname    = $this->nickname;
-        $group->fullname    = $this->fullname;
-        $group->homepage    = $this->homepage;
-        $group->description = $this->description;
-        $group->location    = $this->location;
-        $group->created     = common_sql_now();
-
-        $result = $group->insert();
-
-        if (!$result) {
-            common_log_db_error($group, 'INSERT', __FILE__);
-            $this->serverError(
-                _('Could not create group.'),
-                500,
-                $this->format
-            );
-            return;
-        }
-
-        $result = $group->setAliases($this->aliases);
-
-        if (!$result) {
-            $this->serverError(
-                _('Could not create aliases.'),
-                500,
-                $this->format
-            );
-            return;
-        }
-
-        $member = new Group_member();
-
-        $member->group_id   = $group->id;
-        $member->profile_id = $this->user->id;
-        $member->is_admin   = 1;
-        $member->created    = $group->created;
-
-        $result = $member->insert();
-
-        if (!$result) {
-            common_log_db_error($member, 'INSERT', __FILE__);
-            $this->serverError(
-                _('Could not set group membership.'),
-                500,
-                $this->format
-            );
-            return;
-        }
-
-        $group->query('COMMIT');
-
+        $group = User_group::register(array('nickname' => $this->nickname,
+                                            'fullname' => $this->fullname,
+                                            'homepage' => $this->homepage,
+                                            'description' => $this->description,
+                                            'location' => $this->location,
+                                            'aliases'  => $this->aliases,
+                                            'userid'   => $this->user->id));
         switch($this->format) {
         case 'xml':
             $this->showSingleXmlGroup($group);
index 80da9861a0482a7b9b5c1308872c9e9ca9e5a254..25da7f8fc75ebcf679c88bb83bd4d8e9f94571fe 100644 (file)
@@ -186,45 +186,13 @@ class NewgroupAction extends Action
 
         assert(!is_null($cur));
 
-        $group = new User_group();
-
-        $group->query('BEGIN');
-
-        $group->nickname    = $nickname;
-        $group->fullname    = $fullname;
-        $group->homepage    = $homepage;
-        $group->description = $description;
-        $group->location    = $location;
-        $group->created     = common_sql_now();
-
-        $result = $group->insert();
-
-        if (!$result) {
-            common_log_db_error($group, 'INSERT', __FILE__);
-            $this->serverError(_('Could not create group.'));
-        }
-
-        $result = $group->setAliases($aliases);
-
-        if (!$result) {
-            $this->serverError(_('Could not create aliases.'));
-        }
-
-        $member = new Group_member();
-
-        $member->group_id   = $group->id;
-        $member->profile_id = $cur->id;
-        $member->is_admin   = 1;
-        $member->created    = $group->created;
-
-        $result = $member->insert();
-
-        if (!$result) {
-            common_log_db_error($member, 'INSERT', __FILE__);
-            $this->serverError(_('Could not set group membership.'));
-        }
-
-        $group->query('COMMIT');
+        $group = User_group::register(array('nickname' => $nickname,
+                                            'fullname' => $fullname,
+                                            'homepage' => $homepage,
+                                            'description' => $description,
+                                            'location' => $location,
+                                            'aliases'  => $aliases,
+                                            'userid'   => $cur->id));
 
         common_redirect($group->homeUrl(), 303);
     }
index b92638f7aa4615668a2f368b359432c2d96b8893..c86eadf8fa7fff1e21ca9cb2f496d4aa4ce9acb4 100644 (file)
@@ -354,4 +354,66 @@ class User_group extends Memcached_DataObject
 
         return $xs->getString();
     }
+
+    static function register($fields) {
+
+        // MAGICALLY put fields into current scope
+
+        extract($fields);
+
+        $group = new User_group();
+
+        $group->query('BEGIN');
+
+        $group->nickname    = $nickname;
+        $group->fullname    = $fullname;
+        $group->homepage    = $homepage;
+        $group->description = $description;
+        $group->location    = $location;
+        $group->created     = common_sql_now();
+
+        $result = $group->insert();
+
+        if (!$result) {
+            common_log_db_error($group, 'INSERT', __FILE__);
+            $this->serverError(
+                _('Could not create group.'),
+                500,
+                $this->format
+            );
+            return;
+        }
+        $result = $group->setAliases($aliases);
+
+        if (!$result) {
+            $this->serverError(
+                _('Could not create aliases.'),
+                500,
+                $this->format
+            );
+            return;
+        }
+
+        $member = new Group_member();
+
+        $member->group_id   = $group->id;
+        $member->profile_id = $userid;
+        $member->is_admin   = 1;
+        $member->created    = $group->created;
+
+        $result = $member->insert();
+
+        if (!$result) {
+            common_log_db_error($member, 'INSERT', __FILE__);
+            $this->serverError(
+                _('Could not set group membership.'),
+                500,
+                $this->format
+            );
+            return;
+        }
+
+        $group->query('COMMIT');
+        return $group;
+    }
 }