/** * Create new group on the site * * @since 1.2 * @access public * @param SocialTableStepSession The table mapping for step session * @return */ public function createGroup(SocialTableStepSession &$session) { $config = FD::config(); // Set the basic group details here. // Other group details should be fulfilled by the respective custom fields. FD::import('admin:/includes/group/group'); $group = new SocialGroup(); $group->creator_uid = FD::user()->id; $group->creator_type = SOCIAL_TYPE_USER; $group->category_id = $session->uid; $group->cluster_type = SOCIAL_TYPE_GROUP; $group->hits = 0; $group->created = FD::date()->toSql(); // Generate a unique key for this group which serves as a password $group->key = md5(JFactory::getDate()->toSql() . FD::user()->password . uniqid()); // Load up the values which the user inputs $param = FD::get('Registry'); // Bind the JSON values. $param->bind($session->values); // Convert the data into an array of result. $data = $param->toArray(); $model = FD::model('Fields'); // Get all published fields for the group. // $fields = $model->getCustomFieldsForNode($session->uid, SOCIAL_TYPE_CLUSTERS); $fields = $model->getCustomFields(array('uid' => $session->uid, 'group' => SOCIAL_TYPE_GROUP, 'visible' => SOCIAL_GROUPS_VIEW_REGISTRATION)); // Pass in data and new user object by reference for fields to manipulate $args = array(&$data, &$group); // Perform field validations here. Validation should only trigger apps that are loaded on the form // @trigger onRegisterBeforeSave $lib = FD::getInstance('Fields'); // Get the trigger handler $handler = $lib->getHandler(); // Trigger onRegisterBeforeSave $errors = $lib->trigger('onRegisterBeforeSave', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args, array($handler, 'beforeSave')); // If there are any errors, throw them on screen. if (is_array($errors)) { if (in_array(false, $errors, true)) { $this->setError($errors); return false; } } // If groups required to be moderated, unpublish it first. $my = FD::user(); $group->state = $my->getAccess()->get('groups.moderate') ? SOCIAL_CLUSTER_PENDING : SOCIAL_CLUSTER_PUBLISHED; // If the creator is a super admin, they should not need to be moderated if ($my->isSiteAdmin()) { $group->state = SOCIAL_CLUSTER_PUBLISHED; } // Let's try to save the user now. $state = $group->save(); // If there's a problem saving the user object, set error message. if (!$state) { $this->setError($group->getError()); return false; } // Send e-mail notification to site admin to approve / reject the group. if ($my->getAccess()->get('groups.moderate') && !$my->isSiteAdmin()) { $this->notifyAdminsModeration($group); } else { // If the creator is a site admin, we don't need to notify the admins if (!$my->isSiteAdmin()) { $this->notifyAdmins($group); } } // Once the group is stored, we just re-load it with the proper data $group = FD::group($group->id); // After the group is created, assign the current user as the node item $group->createOwner($my->id); // Reform the args with the binded custom field data in the user object $args = array(&$data, &$group); // Allow fields app to make necessary changes if necessary. At this point, we wouldn't want to allow // the field to stop the registration process already. // @trigger onRegisterAfterSave $lib->trigger('onRegisterAfterSave', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args); // Bind custom fields for this user. $group->bindCustomFields($data); // @trigger onRegisterAfterSaveFields $lib->trigger('onRegisterAfterSaveFields', SOCIAL_FIELDS_GROUP_GROUP, $fields, $args); // We need to set the "data" back to the registration table $newData = FD::json()->encode($data); $session->values = $newData; // If there is still no alias generated, we need to automatically build one for the group if (!$group->alias) { $group->alias = $this->getUniqueAlias($group->getName()); $group->save(); } return $group; }