Example #1
0
/**
 * returns an array containing the categories and the fields ordered by category and field order
 */
function profile_manager_get_categorized_fields($user = null, $edit = false, $register = false, $profile_type_limit = false, $profile_type_guid = false)
{
    $result = array();
    $profile_type = null;
    if ($register == true) {
        // failsafe for edit
        $edit = true;
    }
    if (!empty($user) && $user instanceof ElggUser) {
        $user_metadata = profile_manager_get_user_profile_data($user);
        $profile_type_guid = profile_manager_get_user_profile_data_value($user_metadata, "custom_profile_type");
        if (!empty($profile_type_guid)) {
            $profile_type = get_entity($profile_type_guid);
            // check if profile type is a REAL profile type
            if (!empty($profile_type) && $profile_type instanceof ProfileManagerCustomProfileType) {
                if ($profile_type->getSubtype() != CUSTOM_PROFILE_FIELDS_PROFILE_TYPE_SUBTYPE) {
                    $profile_type = null;
                }
            }
        }
    } elseif (!empty($profile_type_guid)) {
        $profile_type = get_entity($profile_type_guid);
    }
    $result["categories"] = array();
    $result["categories"][0] = array();
    $result["fields"] = array();
    $ordered_cats = array();
    $options = array("type" => "object", "subtype" => CUSTOM_PROFILE_FIELDS_CATEGORY_SUBTYPE, "limit" => false, "owner_guid" => elgg_get_config("site_guid"), "site_guid" => elgg_get_config("site_guid"));
    // get ordered categories
    if ($cats = elgg_get_entities($options)) {
        foreach ($cats as $cat) {
            $ordered_cats[$cat->order] = $cat;
        }
        ksort($ordered_cats);
    }
    // get filtered categories
    $filtered_ordered_cats = array();
    // default category at index 0
    $filtered_ordered_cats[0] = array();
    if (!empty($ordered_cats)) {
        foreach ($ordered_cats as $key => $cat) {
            if (!$edit || $profile_type_limit) {
                $options = array("type" => "object", "subtype" => CUSTOM_PROFILE_FIELDS_PROFILE_TYPE_SUBTYPE, "count" => true, "owner_guid" => $cat->getOwnerGUID(), "site_guid" => $cat->site_guid, "relationship" => CUSTOM_PROFILE_FIELDS_PROFILE_TYPE_CATEGORY_RELATIONSHIP, "relationship_guid" => $cat->getGUID(), "inverse_relationship" => true);
                $rel_count = elgg_get_entities_from_relationship($options);
                if ($rel_count == 0) {
                    $filtered_ordered_cats[$cat->guid] = array();
                    $result["categories"][$cat->guid] = $cat;
                } elseif (!empty($profile_type) && check_entity_relationship($profile_type->guid, CUSTOM_PROFILE_FIELDS_PROFILE_TYPE_CATEGORY_RELATIONSHIP, $cat->guid)) {
                    $filtered_ordered_cats[$cat->guid] = array();
                    $result["categories"][$cat->guid] = $cat;
                }
            } else {
                $filtered_ordered_cats[$cat->guid] = array();
                $result["categories"][$cat->guid] = $cat;
            }
        }
    }
    $options = array("type" => "object", "subtype" => CUSTOM_PROFILE_FIELDS_PROFILE_SUBTYPE, "limit" => false, "owner_guid" => elgg_get_config("site_guid"), "site_guid" => elgg_get_config("site_guid"));
    // adding fields to categories
    if ($fields = elgg_get_entities($options)) {
        foreach ($fields as $field) {
            if (!($cat_guid = $field->category_guid)) {
                $cat_guid = 0;
                // 0 is default
            }
            $admin_only = $field->admin_only;
            if ($admin_only != "yes" || elgg_is_admin_logged_in()) {
                if ($edit) {
                    if (!$register || $field->show_on_register == "yes") {
                        $filtered_ordered_cats[$cat_guid][$field->order] = $field;
                    }
                } else {
                    // only add if value exists
                    $metadata_name = $field->metadata_name;
                    $user_value = profile_manager_get_user_profile_data_value($user_metadata, $metadata_name);
                    if (!empty($user_value) || $user_value === 0) {
                        $filtered_ordered_cats[$cat_guid][$field->order] = $field;
                    }
                }
            }
        }
    }
    // sorting fields and filtering empty categories
    foreach ($filtered_ordered_cats as $cat_guid => $fields) {
        if (!empty($fields)) {
            ksort($fields);
            $result["fields"][$cat_guid] = $fields;
        } else {
            unset($result["categories"][$cat_guid]);
        }
    }
    //  fire hook to see if other plugins have extra fields
    $hook_params = array("user" => $user, "edit" => $edit, "register" => $register, "profile_type_limit" => $profile_type_limit, "profile_type_guid" => $profile_type_guid);
    $result = elgg_trigger_plugin_hook("categorized_profile_fields", "profile_manager", $hook_params, $result);
    return $result;
}
            ?>
				<script type="text/javascript">
					$(document).ready(function(){
						changeProfileType();
					});
				</script>
				<?php 
            echo "<div>";
            echo "<label>" . elgg_echo("profile_manager:profile:edit:custom_profile_type:label") . "</label>";
            echo elgg_view("input/dropdown", array("name" => "custom_profile_type", "id" => "custom_profile_type", "options_values" => $pulldown_options, "onchange" => "changeProfileType();", "value" => profile_manager_get_user_profile_data_value($user_metadata, "custom_profile_type")));
            echo elgg_view('input/hidden', array('name' => 'accesslevel[custom_profile_type]', 'value' => ACCESS_PUBLIC));
            echo "</div>";
            echo $types_description;
        }
    } else {
        $profile_type = profile_manager_get_user_profile_data_value($user_metadata, "custom_profile_type");
        if (!empty($profile_type)) {
            echo elgg_view("input/hidden", array("name" => custom_profile_type, "value" => $profile_type));
            ?>
				<script type="text/javascript">
					$(document).ready(function(){
						$('.custom_profile_type_<?php 
            echo $profile_type;
            ?>
').show();
					});
				</script>
				<?php 
        }
    }
    $tabs = array();