/** !!
  * Iterates over $this->profilefields, doing some data parsing
  *  and then saves it back.
  *
  * @param array $request_data POST/GET data of changed information.
  */
 private function handleEntity($request_data)
 {
     $this->err = '';
     // $data = $this->filter($request_data);
     $data = array();
     // use the profile_fields object for some processing
     foreach ($this->profilefields as $i => $d) {
         $k = $d['name'];
         switch ($d['type']) {
             case 'dateselect':
                 $day = @$request_data[$k . '_day'];
                 $month = @$request_data[$k . '_month'];
                 $year = @$request_data[$k . '_year'];
                 if ($day && $month && $year) {
                     $data[$k . '_day'] = $day;
                     $data[$k . '_month'] = $month;
                     $data[$k . '_year'] = $year;
                     $data[$k] = sprintf("%04d-%02d-%02d", $year, $month, $day);
                 }
                 break;
             default:
                 if (!empty($request_data[$k])) {
                     $data[$k] = $request_data[$k];
                 }
                 break;
         }
     }
     $data['type'] = 'family';
     $data['name'] = $request_data['groupname'];
     $data['group_id'] = $this->gid;
     if (empty($data['name'])) {
         $this->err .= __("Please supply a name.") . "<br/>";
     }
     if (empty($this->err)) {
         // sync it
         FamilyTypedGroupEntity::sync($data);
         if (empty($request_data['gid'])) {
             // also add the creator of this family as a parent!
             require_once "api/Entity/TypedGroupEntityRelation.php";
             try {
                 TypedGroupEntityRelation::set_relation(PA::$login_uid, $this->gid, 'parent');
             } catch (PAException $e) {
                 $error_msg = $e->getMessage();
             }
         }
     }
 }
 private function handleGET_join($request_data)
 {
     require_once "api/Activities/Activities.php";
     global $error_msg;
     if (PA::$login_uid && !empty($this->shared_data['group_info'])) {
         $group = $this->shared_data['group_info'];
         if (!Group::member_exists((int) $request_data['gid'], (int) PA::$login_uid)) {
             $user = PA::$login_user;
             $login_name = $user->login_name;
             $group_invitation_id = !empty($request_data['GInvID']) ? $request_data['GInvID'] : null;
             try {
                 $user_joined = $group->join((int) PA::$login_uid, $user->email, $group_invitation_id);
                 // for rivers of people
                 $activity = 'group_joined';
                 //for rivers of people
                 $activity_extra['info'] = $login_name . ' joined a new group';
                 $activity_extra['group_name'] = $group->title;
                 $activity_extra['group_id'] = $request_data['gid'];
                 $extra = serialize($activity_extra);
                 $object = $request_data['gid'];
                 Activities::save(PA::$login_uid, $activity, $object, $extra);
                 if (!empty($group_invitation_id)) {
                     // if group is joined through group invitation
                     $Ginv = Invitation::load($group_invitation_id);
                     $gid = $Ginv->inv_collection_id;
                     $user_obj = new User();
                     $user_obj->load((int) $Ginv->user_id);
                     $group = ContentCollection::load_collection((int) $gid, $Ginv->user_id);
                     $user_type = Group::get_user_type($user_obj->user_id, $gid);
                     if ($group->reg_type == REG_MODERATED && $user_type == OWNER) {
                         $group->collection_id = $gid;
                         $group->approve(PA::$login_uid, 'user');
                     }
                     $user_accepting_ginv_obj = new User();
                     $user_accepting_ginv_obj->load((int) PA::$login_uid);
                     $Ginv->inv_user_id = PA::$login_uid;
                     PANotify::send("invite_accept_group", $user_obj, $user_accepting_ginv_obj, $Ginv);
                 }
             } catch (PAException $e) {
                 if ($e->code == GROUP_NOT_INVITED) {
                     $error_msg = $e->message;
                     //          header("Location: groups_home.php");
                     //          exit;
                 }
                 $error_msg = $e->message;
             }
         } else {
             $error_msg = sprintf(__("You are already a member of \"%s\""), stripslashes($group->title));
         }
         if (@$user_joined) {
             // deal with TypedGroup Relations
             require_once "api/Entity/TypedGroupEntityRelation.php";
             $uid = PA::$login_uid;
             $gid = $group->collection_id;
             $type = @$request_data['relation'];
             try {
                 TypedGroupEntityRelation::set_relation($uid, $gid, $type);
             } catch (PAException $e) {
                 $error_msg = $e->getMessage();
             }
             $gid = (int) $request_data['gid'];
             if (!Group::member_exists($gid, (int) PA::$login_uid) && $group->reg_type == REG_MODERATED) {
                 // if it is a manual join not an invited join
                 $mail_type = 'group_join_request';
                 $error_msg = sprintf(__("Your request to join \"%s\" has been submitted to the owner of the group."), stripslashes($group->title));
             } else {
                 $mail_type = 'group_join';
                 $error_msg = sprintf(__("You have joined \"%s\" successfully."), stripslashes($group->title));
             }
             PANotify::send($mail_type, $group, PA::$login_user, array());
             if ($type == 'child') {
                 // if user type == child remove LoginUser and GroupMember roles
                 PA::$login_user->delete_user_role();
                 // then assign 'Child' role only
                 $_extra = serialize(array('user' => false, 'network' => false, 'groups' => array($gid)));
                 $user_roles[] = array('role_id' => CHILD_MEMBER_ROLE, 'extra' => $_extra);
                 PA::$login_user->set_user_role($user_roles);
             }
         }
     } else {
         // redirect to login
         $msg = urlencode(__("You need to be logged in to join a group."));
         header("Location: " . PA::$url . "/login.php?" . $msg . "&return=" . urlencode($_SERVER['REDIRECT_URL'] . '?' . @$_SERVER['REDIRECT_QUERY_STRING']));
     }
 }
 private function handlePOST_addChild($request_data)
 {
     global $error_msg;
     $error = FALSE;
     $login_name = trim($_POST['login_name']);
     $first_name = stripslashes(trim($_POST['first_name']));
     $last_name = stripslashes(trim($_POST['last_name']));
     $email = trim($_POST['email']);
     $password = trim($_POST['password']);
     $use_parent_email = $_POST['use_parent_email'];
     //echo "<pre>".print_r($_POST, 1)."</pre>"; die();
     if (!isset($_POST['state'])) {
         if (isset($_POST['stateOther'])) {
             $_POST['state'] = $_POST['stateOther'];
         }
     }
     if (isset($_POST['stateOther'])) {
         unset($_POST['stateOther']);
     }
     $msg = NULL;
     if (!Validation::validate_email($email) && !empty($_POST['email'])) {
         $email_invalid = TRUE;
         $error = TRUE;
         $msg .= '<br> Email address is not valid';
     }
     if (User::user_exist($login_name)) {
         $msg = "Username {$login_name} is already taken";
         $error = TRUE;
     }
     if ($error == FALSE) {
         $newuser = new User();
         $newuser->login_name = $login_name;
         $newuser->password = $password;
         $newuser->first_name = $first_name;
         $newuser->last_name = $last_name;
         $newuser->email = $email;
         $newuser->is_active = ACTIVE;
         if (!empty($_FILES['userfile']['name'])) {
             $myUploadobj = new FileUploader();
             //creating instance of file.
             $image_type = 'image';
             $file = $myUploadobj->upload_file(PA::$upload_path, 'userfile', true, true, $image_type);
             if ($file == false) {
                 $msg = $myUploadobj->error;
                 $error = TRUE;
             } else {
                 $newuser->picture = $file;
             }
         }
         if ($error == FALSE) {
             try {
                 if ($use_parent_email) {
                     $newuser->save($check_unique_email = false);
                 } else {
                     $newuser->save($check_unique_email = true);
                 }
                 if (!empty($file)) {
                     Storage::link($file, array("role" => "avatar", "user" => $newuser->user_id));
                 }
                 // creating message basic folders
                 Message::create_basic_folders($newuser->user_id);
                 // adding default relation
                 if ($newuser->user_id != SUPER_USER_ID) {
                     User_Registration::add_default_relation($newuser->user_id, PA::$network_info);
                 }
                 // adding default media as well as album
                 User_Registration::add_default_media($newuser->user_id, '', PA::$network_info);
                 User_Registration::add_default_media($newuser->user_id, '_audio', PA::$network_info);
                 User_Registration::add_default_media($newuser->user_id, '_video', PA::$network_info);
                 User_Registration::add_default_blog($newuser->user_id);
                 //adding default link categories & links
                 User_Registration::add_default_links($newuser->user_id);
                 // code for adding default desktop image for user
                 $desk_img = uihelper_add_default_desktopimage($newuser->user_id);
                 if (empty($desk_img)) {
                     $desktop_images = array('bay.jpg', 'everglade.jpg', 'bay_boat.jpg', 'delhi.jpg');
                     $rand_key = array_rand($desktop_images);
                     $desk_img = $desktop_images[$rand_key];
                 }
                 $states = array_values(PA::getStatesList());
                 $countries = array_values(PA::getCountryList());
                 $profile_keys = array('dob_day', 'dob_month', 'dob_year', 'homeAddress1', 'homeAddress2', 'city', 'state', 'country', 'postal_code', 'phone', 'use_parent_email');
                 $profile_data = array();
                 filter_all_post($_POST);
                 //filters all data of html
                 foreach ($profile_keys as $k => $pkey) {
                     if (!empty($_POST[$pkey])) {
                         if ($pkey == 'state' && $_POST[$pkey] >= 0) {
                             $prof_rec = array('uid' => $newuser->user_id, 'name' => $pkey, 'value' => $states[$_POST[$pkey]], 'type' => GENERAL, 'perm' => 1);
                         } else {
                             if ($pkey == 'country' && $_POST[$pkey] >= 0) {
                                 $prof_rec = array('uid' => $newuser->user_id, 'name' => $pkey, 'value' => $countries[$_POST[$pkey]], 'type' => GENERAL, 'perm' => 1);
                             } else {
                                 $prof_rec = array('uid' => $newuser->user_id, 'name' => $pkey, 'value' => $_POST[$pkey], 'type' => GENERAL, 'perm' => 1);
                             }
                         }
                         $profile_data[] = $prof_rec;
                     }
                 }
                 $profile_data[] = array('uid' => $newuser->user_id, 'name' => 'user_caption_image', 'value' => $desk_img, 'type' => GENERAL, 'perm' => 1);
                 //     echo "<pre>".print_r($profile_data,1)."</pre>";
                 $newuser->save_user_profile($profile_data, GENERAL);
                 //if new user is created in a network then he must set as a joined user
                 if (!empty(PA::$network_info)) {
                     $by_admin = true;
                     Network::join(PA::$network_info->network_id, $newuser->user_id, NETWORK_MEMBER, $by_admin);
                     // $by_admin = true overrides the
                     // user_waiting status if it would get set
                     // this is an admin action, so we want it to happen in any case
                 }
                 $user_joined = $this->family->join((int) $newuser->user_id, $newuser->email, null);
                 if ($user_joined) {
                     // deal with TypedGroup Relations
                     require_once "api/Entity/TypedGroupEntityRelation.php";
                     $type = 'child';
                     TypedGroupEntityRelation::set_relation($newuser->user_id, $this->family->collection_id, $type);
                     if ($type == 'child') {
                         // if user type == child remove LoginUser and GroupMember roles
                         $newuser->delete_user_role();
                         // then assign 'Child' role only
                         $_extra = serialize(array('user' => false, 'network' => false, 'groups' => array($this->family->collection_id)));
                         $user_roles[] = array('role_id' => CHILD_MEMBER_ROLE, 'extra' => $_extra);
                         $newuser->set_user_role($user_roles);
                     }
                 }
                 $msg = __("Child's account was successfully created");
             } catch (PAException $e) {
                 $msg = $e->message;
             }
         }
         // end if
     }
     //end if
     $error_msg = $msg;
 }