From 09d08af9814a4edfb2050cdb47ad8ae20a944472 Mon Sep 17 00:00:00 2001 From: cash Date: Sat, 18 Dec 2010 22:20:25 +0000 Subject: moved profile edit form into core git-svn-id: http://code.elgg.org/elgg/trunk@7672 36083f99-b078-4883-b0ff-0f9b5a30f544 --- actions/profile/edit.php | 109 ++++++++++++++++++++++++++++++++++ engine/lib/users.php | 83 +++++++++++++++++++++++++- languages/en.php | 3 + mod/profile/actions/edit.php | 110 ----------------------------------- mod/profile/start.php | 5 ++ pages/avatar/edit.php | 2 + pages/profile/edit.php | 32 ++++++++++ views/default/forms/profile/edit.php | 64 ++++++++++++++++++++ 8 files changed, 297 insertions(+), 111 deletions(-) create mode 100644 actions/profile/edit.php delete mode 100644 mod/profile/actions/edit.php create mode 100644 pages/profile/edit.php create mode 100644 views/default/forms/profile/edit.php diff --git a/actions/profile/edit.php b/actions/profile/edit.php new file mode 100644 index 000000000..219474f2c --- /dev/null +++ b/actions/profile/edit.php @@ -0,0 +1,109 @@ +canEdit()) { + register_error(elgg_echo('profile:edit:fail')); + forward(REFERER); +} + +// grab the defined profile field names and their load the values from POST. +// each field can have its own access, so sort that too. +$input = array(); +$accesslevel = get_input('accesslevel'); + +if (!is_array($accesslevel)) { + $accesslevel = array(); +} + +/** + * wrapper for recursive array walk decoding + */ +function profile_array_decoder(&$v) { + $v = html_entity_decode($v, ENT_COMPAT, 'UTF-8'); +} + +$profile_fields = elgg_get_config('profile'); +foreach ($profile_fields as $shortname => $valuetype) { + // the decoding is a stop gag to prevent && showing up in profile fields + // because it is escaped on both input (get_input()) and output (view:output/text). see #561 and #1405. + // must decode in utf8 or string corruption occurs. see #1567. + $value = get_input($shortname); + if (is_array($value)) { + array_walk_recursive($value, 'profile_array_decoder'); + } else { + $value = html_entity_decode($value, ENT_COMPAT, 'UTF-8'); + } + + // limit to reasonable sizes + // @todo - throwing away changes due to this is dumb! + if (!is_array($value) && $valuetype != 'longtext' && elgg_strlen($value) > 250) { + $error = elgg_echo('profile:field_too_long', array(elgg_echo("profile:{$shortname}"))); + register_error($error); + forward(REFERER); + } + + if ($valuetype == 'tags') { + $value = string_to_tag_array($value); + } + + $input[$shortname] = $value; +} + +// display name is handled separately +$name = strip_tags(get_input('name')); +if ($name) { + if (elgg_strlen($name) > 50) { + register_error(elgg_echo('user:name:fail')); + } elseif ($owner->name != $name) { + $owner->name = $name; + // @todo this is weird...giving two notifications? + if ($owner->save()) { + system_message(elgg_echo('user:name:success')); + } else { + register_error(elgg_echo('user:name:fail')); + } + } +} + +// go through custom fields +if (sizeof($input) > 0) { + foreach ($input as $shortname => $value) { + remove_metadata($owner->guid, $shortname); + if (isset($accesslevel[$shortname])) { + $access_id = (int) $accesslevel[$shortname]; + } else { + // this should never be executed since the access level should always be set + $access_id = ACCESS_DEFAULT; + } + if (is_array($value)) { + $i = 0; + foreach ($value as $interval) { + $i++; + $multiple = ($i > 1) ? TRUE : FALSE; + create_metadata($owner->guid, $shortname, $interval, 'text', $owner->guid, $access_id, $multiple); + } + } else { + create_metadata($owner->getGUID(), $shortname, $value, 'text', $owner->getGUID(), $access_id); + } + } + + $owner->save(); + + // Notify of profile update + elgg_trigger_event('profileupdate', $user->type, $user); + + //add to river if edited by self + if (get_loggedin_userid() == $user->guid) { + add_to_river('river/user/default/profileupdate', 'update', get_loggedin_userid(), get_loggedin_userid(), get_default_access(get_loggedin_user())); + } + + system_message(elgg_echo("profile:saved")); +} + +forward($owner->getUrl()); diff --git a/engine/lib/users.php b/engine/lib/users.php index ee3814fd2..0482fd034 100644 --- a/engine/lib/users.php +++ b/engine/lib/users.php @@ -1477,6 +1477,61 @@ function user_avatar_hook($hook, $entity_type, $returnvalue, $params){ return "pg/avatar/view/{$entity->username}?size=$size"; } +/** + * This function loads a set of default fields into the profile, then triggers a hook letting other plugins to edit + * add and delete fields. + * + * Note: This is a secondary system:init call and is run at a super low priority to guarantee that it is called after all + * other plugins have initialised. + */ +function elgg_profile_fields_setup() { + global $CONFIG; + + $profile_defaults = array ( + 'description' => 'longtext', + 'briefdescription' => 'text', + 'location' => 'tags', + 'interests' => 'tags', + 'skills' => 'tags', + 'contactemail' => 'email', + 'phone' => 'text', + 'mobile' => 'text', + 'website' => 'url', + 'twitter' => 'text' + ); + + $loaded_default = array(); + if ($fieldlist = get_plugin_setting('user_defined_fields','profile')) { + if (!empty($fieldlist)) { + $fieldlistarray = explode(',',$fieldlist); + $loaded_defaults = array(); + foreach($fieldlistarray as $listitem) { + if ($translation = get_plugin_setting("admin_defined_profile_{$listitem}", 'profile')) { + $type = get_plugin_setting("admin_defined_profile_type_{$listitem}", 'profile'); + $loaded_defaults["admin_defined_profile_{$listitem}"] = $type; + add_translation(get_current_language(), array("profile:admin_defined_profile_{$listitem}" => $translation)); + } + } + } + } + + if (count($loaded_defaults)) { + $CONFIG->profile_using_custom = true; + $profile_defaults = $loaded_defaults; + } + + $CONFIG->profile = elgg_trigger_plugin_hook('profile:fields', 'profile', NULL, $profile_defaults); + + // register any tag metadata names + foreach ($CONFIG->profile as $name => $type) { + if ($type == 'tags') { + elgg_register_tag_metadata_name($name); + // register a tag name translation + add_translation(get_current_language(), array("tag_names:$name" => elgg_echo("profile:$name"))); + } + } +} + /** * Avatar page handler * @@ -1495,6 +1550,22 @@ function elgg_avatar_page_handler($page) { } } +/** + * Profile page handler + * + * @param array $page + */ +function elgg_profile_page_handler($page) { + global $CONFIG; + + $user = get_user_by_username($page[0]); + elgg_set_page_owner_guid($user->guid); + + if ($page[1] == 'edit') { + require_once("{$CONFIG->path}pages/profile/edit.php"); + } +} + /** * Members page handler * @@ -1560,6 +1631,7 @@ function users_init() { register_page_handler('login', 'elgg_user_login_page_handler'); register_page_handler('members', 'elgg_members_page_handler'); register_page_handler('avatar', 'elgg_avatar_page_handler'); + register_page_handler('profile', 'elgg_profile_page_handler'); //register_page_handler('collections', 'collections_page_handler'); @@ -1572,7 +1644,14 @@ function users_init() { 'name' => 'edit_avatar', 'url' => "pg/avatar/edit/{$user->username}", 'title' => elgg_echo('avatar:edit'), - 'contexts' => array('avatar'), + 'contexts' => array('profile_edit'), + ); + elgg_register_menu_item('page', $params); + $params = array( + 'name' => 'edit_profile', + 'url' => "pg/profile/{$user->username}/edit", + 'title' => elgg_echo('profile:edit'), + 'contexts' => array('profile_edit'), ); elgg_register_menu_item('page', $params); } @@ -1583,6 +1662,7 @@ function users_init() { elgg_register_action("friends/remove"); elgg_register_action('avatar/upload'); elgg_register_action('avatar/crop'); + elgg_register_action('profile/edit'); //elgg_register_action('friends/addcollection'); //elgg_register_action('friends/deletecollection'); @@ -1683,5 +1763,6 @@ function users_test($hook, $type, $value, $params) { } elgg_register_event_handler('init', 'system', 'users_init', 0); +elgg_register_event_handler('init', 'system', 'elgg_profile_fields_setup', 10000); // Ensure this runs after other plugins elgg_register_event_handler('pagesetup', 'system', 'users_pagesetup', 0); elgg_register_plugin_hook_handler('unit_test', 'system', 'users_test'); \ No newline at end of file diff --git a/languages/en.php b/languages/en.php index 9cde25c18..85c3861d7 100644 --- a/languages/en.php +++ b/languages/en.php @@ -379,6 +379,9 @@ $english = array( 'avatar:crop:success' => 'Cropping the avatar succeeded', 'avatar:crop:fail' => 'Avatar cropping failed', + 'profile:edit' => 'Edit profile', + + /** * Feeds */ diff --git a/mod/profile/actions/edit.php b/mod/profile/actions/edit.php deleted file mode 100644 index da7553b6e..000000000 --- a/mod/profile/actions/edit.php +++ /dev/null @@ -1,110 +0,0 @@ -canEdit()) { - system_message(elgg_echo("profile:noaccess")); - forward(REFERER); -} - -// grab the defined profile field names and their load the values from POST. -// each field can have its own access, so sort that too. -$input = array(); -$accesslevel = get_input('accesslevel'); - -if (!is_array($accesslevel)) { - $accesslevel = array(); -} - -/** - * wrapper for recursive array walk decoding - */ -function profile_array_decoder(&$v) { - $v = html_entity_decode($v, ENT_COMPAT, 'UTF-8'); -} - - -foreach($CONFIG->profile as $shortname => $valuetype) { - // the decoding is a stop gag to prevent && showing up in profile fields - // because it is escaped on both input (get_input()) and output (view:output/text). see #561 and #1405. - // must decode in utf8 or string corruption occurs. see #1567. - $value = get_input($shortname); - if (is_array($value)) { - array_walk_recursive($value, 'profile_array_decoder'); - } else { - $value = html_entity_decode($value, ENT_COMPAT, 'UTF-8'); - } - - // limit to reasonable sizes. - if (!is_array($value) && $valuetype != 'longtext' && elgg_strlen($value) > 250) { - $error = elgg_echo('profile:field_too_long', array(elgg_echo("profile:{$shortname}"))); - register_error($error); - forward(REFERER); - } - - if ($valuetype == 'tags') { - $value = string_to_tag_array($value); - } - - $input[$shortname] = $value; -} - -// display name is handled separately -if ($name = strip_tags(get_input('name'))) { - if (elgg_strlen($name) > 50) { - register_error(elgg_echo('user:name:fail')); - } elseif ($profile_owner->name != $name) { - $profile_owner->name = $name; - // @todo this is weird...giving two notifications? - if ($profile_owner->save()) { - system_message(elgg_echo('user:name:success')); - } else { - register_error(elgg_echo('user:name:fail')); - } - } -} - -// go through custom fields -if (sizeof($input) > 0) { - foreach($input as $shortname => $value) { - remove_metadata($profile_owner->guid, $shortname); - if (isset($accesslevel[$shortname])) { - $access_id = (int) $accesslevel[$shortname]; - } else { - // this should never be executed since the access level should always be set - $access_id = ACCESS_DEFAULT; - } - if (is_array($value)) { - $i = 0; - foreach($value as $interval) { - $i++; - $multiple = ($i > 1) ? TRUE : FALSE; - create_metadata($profile_owner->guid, $shortname, $interval, 'text', $profile_owner->guid, $access_id, $multiple); - } - } else { - create_metadata($profile_owner->getGUID(), $shortname, $value, 'text', $profile_owner->getGUID(), $access_id); - } - } - - $profile_owner->save(); - - // Notify of profile update - elgg_trigger_event('profileupdate',$user->type,$user); - - //add to river if edited by self - if (get_loggedin_userid() == $user->guid) { - add_to_river('river/user/default/profileupdate','update',get_loggedin_userid(),get_loggedin_userid(),get_default_access(get_loggedin_user())); - } - - system_message(elgg_echo("profile:saved")); -} - -forward($profile_owner->getUrl() . "/details"); diff --git a/mod/profile/start.php b/mod/profile/start.php index 4c24c2b50..8ae16cfb5 100644 --- a/mod/profile/start.php +++ b/mod/profile/start.php @@ -135,6 +135,9 @@ function profile_page_handler($page) { switch ($action) { case 'edit': + require $CONFIG->path . 'pages/profile/edit.php'; + return; + /* $layout = 'one_column_with_sidebar'; if (!$user || !$user->canEdit()) { @@ -144,6 +147,8 @@ function profile_page_handler($page) { $content = profile_get_user_edit_content($user, $page); $content = elgg_view_layout($layout, array('content' => $content)); + * + */ break; default: diff --git a/pages/avatar/edit.php b/pages/avatar/edit.php index a577170a0..5e44eb4b0 100644 --- a/pages/avatar/edit.php +++ b/pages/avatar/edit.php @@ -6,6 +6,8 @@ // Only logged in users gatekeeper(); +elgg_set_context('profile_edit'); + $title = elgg_echo('avatar:edit'); $content = elgg_view('core/avatar/upload', array('entity' => elgg_get_page_owner())); diff --git a/pages/profile/edit.php b/pages/profile/edit.php new file mode 100644 index 000000000..64ffd7dc5 --- /dev/null +++ b/pages/profile/edit.php @@ -0,0 +1,32 @@ +canEdit()) { + register_error(elgg_echo("profile:noaccess")); + forward(); +} + +elgg_set_context('profile_edit'); + +$title = elgg_echo('profile:edit'); + +$content = elgg_view_form('profile/edit', array(), array('entity' => $user)); + +$params = array( + 'content' => $content, + 'title' => $title, +); +$body = elgg_view_layout('one_sidebar', $params); + +echo elgg_view_page($title, $body); diff --git a/views/default/forms/profile/edit.php b/views/default/forms/profile/edit.php new file mode 100644 index 000000000..c9117c8e7 --- /dev/null +++ b/views/default/forms/profile/edit.php @@ -0,0 +1,64 @@ + + +

+ + 'name', 'value' => $vars['entity']->name)); ?> +

+ 0) { + foreach ($profile_fields as $shortname => $valtype) { + $metadata = get_metadata_byname($vars['entity']->guid, $shortname); + if ($metadata) { + if (is_array($metadata)) { + $value = ''; + foreach ($metadata as $md) { + if (!empty($value)) { + $value .= ', '; + } + $value .= $md->value; + $access_id = $md->access_id; + } + } else { + $value = $metadata->value; + $access_id = $metadata->access_id; + } + } else { + $value = ''; + $access_id = ACCESS_DEFAULT; + } + +?> +

+ + $shortname, + 'value' => $value, + ); + echo elgg_view("input/{$valtype}", $params); + $params = array( + 'internalname' => "accesslevel[$shortname]", + 'value' => $access_id, + ); + echo elgg_view('input/access', $params); + ?> +

+ +

+ 'guid', 'value' => $vars['entity']->guid)); + echo elgg_view('input/submit', array('value' => elgg_echo('save'))); +?> +

-- cgit v1.2.3