<commit_before>{% extends "settings/bootstrap2/base_template.html" %}
{% load i18n %}
{% load hq_shared_tags %}

{% block js %}{{ block.super }}
    <script src="{% static 'jquery-ui-1.8.23-legacy/jquery-ui.min.js' %}"></script>
    <script src="{% static 'locations/ko/location_tree.async.js' %}"></script>
    <link rel="stylesheet" href="{% static 'locations/style/manage.css' %}">
{% endblock %}

{% block js-inline %}{{ block.super }}
<script>

var LOAD_LOCS_URL = '{{ api_root }}',
    NEW_LOC_URL = '{% url "create_location" domain %}';

var locs = {{ locations|JSON }};
var can_edit_root = {{ can_edit_root|yesno:"true,false" }};
var selected_loc = {{ selected_id|JSON }};
var hierarchy = {{ hierarchy|JSON }};
var show_inactive = {{ show_inactive|yesno:"true,false" }};

function loc_edit_url(loc_id) {
    var template = '{% url "edit_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_archive_url(loc_id) {
    var template = '{% url "archive_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_unarchive_url(loc_id) {
    var template = '{% url "unarchive_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_delete_url(loc_id) {
    var template = '{% url "delete_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_descendant_url(loc_id) {
    var template = '{% url "location_descendants_count" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

var archive_success_message = _.template(
    '{% trans "You have successfully archived the location <%=name%>" %}'
);

var delete_success_message = _.template(
    '{% trans "You have successfully deleted the location <%=name%> and all of its child locations" %}'
);

var delete_error_message = '{% trans "An error occurred while deleting your location. If the problem persists, please report an issue" %}'

archive_loc = function(button, name, loc_id) {
    var archive_location_modal = $('#archive-location-modal')[0];

    function archive_fn() {
        $(button).disableButton();
        $.ajax({
            type: 'POST',
            url: loc_archive_url(loc_id),
            dataType: 'json',
            error: 'error',
            success: function (response) {
                alert_user(archive_success_message({"name": name}), "success");
                remove_elements_after_action(button);
            }
        });
        $(archive_location_modal).modal('hide');
    }

    var modal_context = {
        "name": name,
        "loc_id": loc_id,
        "archive_fn": archive_fn,
    };
    ko.cleanNode(archive_location_modal);
    $(archive_location_modal).koApplyBindings(modal_context);
    $(archive_location_modal).modal('show');

}

unarchive_loc = function(button, loc_id) {
    $(button).disableButton();
    $.ajax({
        type: 'POST',
        url: loc_unarchive_url(loc_id),
        dataType: 'json',
        error: 'error',
        success: function (response) {
            remove_elements_after_action(button);
        }
    });
}

delete_loc = function(button, name, loc_id) {
    var delete_location_modal = $('#delete-location-modal')[0];
    var modal_context;

    function delete_fn() {
        if (modal_context.count == modal_context.signOff()) {
            $(button).disableButton();
            $.ajax({
                type: 'DELETE',
                url: loc_delete_url(loc_id),
                dataType: 'json',
                error: function (response, status, error) {
                    alert_user(delete_error_message);
                    $(button).enableButton();
                },
                success: function (response) {
                    alert_user(delete_success_message({"name": name}), "success");
                    remove_elements_after_action(button);
                }
            });
            $(delete_location_modal).modal('hide');
        }
    }


    $.ajax({
        type: 'GET',
        url: loc_descendant_url(loc_id),
        dataType: 'json',
        success: function (response) {
            modal_context = {
                "name": name,
                "loc_id": loc_id,
                "delete_fn": delete_fn,
                "count": response.count,
                "signOff": ko.observable(''),
            };
            ko.cleanNode(delete_location_modal);
            ko.applyBindings(modal_context, delete_location_modal);
            $(delete_location_modal).modal('show');
        }
    });
}

remove_elements_after_action = function(button) {
    $(button).closest('.loc_section').remove();
}

$(function() {
    var model = new LocationTreeViewModel(hierarchy);
    $('#location_tree').koApplyBindings(model);
    model.load(locs, selected_loc);
});

</script>
{% endblock %}

{% block main_column %}
<header>
    <div class="row-fluid">
        <div class="span8">
            <h2>{% trans "Manage Organization Structure" %}</h2>
            <p>
                {% blocktrans %}
                    The Organization feature allows you to represent the real-world 
                    structure of your project (districts, facilities, frontline workers, etc.). 
                    Once this structure has been defined, you can use it for reporting and 
                    case sharing. Learn more about Organizations on our <a href="https://confluence.dimagi.com/display/commtrack/Locations">Help Site</a>.
                {% endblocktrans %}
            </p>
            {% if show_inactive %}
            <p class="alert alert-info">
                {% if show_inactive %}{% trans 'Showing the Archived Location List.' %}{% endif %}
            </p>
            {% endif %}
        </div>
        <div class="span4">
            {% if not show_inactive %}
                <a class="btn btn-inverse pull-right" href="?show_inactive=true" style="margin-right: 2.5em;">
                    {% trans "Show Archived Locations" %}
                </a>
            {% else %}
                <a class="btn pull-right" href="?" style="margin-right: 2.5em;">
                    {% trans "Show Active Locations" %}
                </a>
            {% endif %}
        </div>
    </div>
</header>

{% if not show_inactive %}
<div class="row-fluid">
    <div class="span12">
        <div class="btn-toolbar">
            <a class="btn btn-primary"
               href="{% url "location_import" domain %}"
               {% if not has_location_types or not can_edit_any_location %}
                   disabled
               {% endif %}
            >
                <i class="icon-upload"></i> {% trans 'Bulk Import' %}
            </a>
            <a class="btn" href="{% url "location_types" domain %}">
                {% trans "Edit Organization Levels" %}
            </a>
            <a class="btn" href="{% url "location_fields_view" domain %}">
                {% trans "Edit Location Fields" %}
            </a>
        </div>
    </div>
</div>
{% endif %}

<hr /><commit_msg>do not add link to button when disabled<commit_after>{% extends "settings/bootstrap2/base_template.html" %}
{% load i18n %}
{% load hq_shared_tags %}

{% block js %}{{ block.super }}
    <script src="{% static 'jquery-ui-1.8.23-legacy/jquery-ui.min.js' %}"></script>
    <script src="{% static 'locations/ko/location_tree.async.js' %}"></script>
    <link rel="stylesheet" href="{% static 'locations/style/manage.css' %}">
{% endblock %}

{% block js-inline %}{{ block.super }}
<script>

var LOAD_LOCS_URL = '{{ api_root }}',
    NEW_LOC_URL = '{% url "create_location" domain %}';

var locs = {{ locations|JSON }};
var can_edit_root = {{ can_edit_root|yesno:"true,false" }};
var selected_loc = {{ selected_id|JSON }};
var hierarchy = {{ hierarchy|JSON }};
var show_inactive = {{ show_inactive|yesno:"true,false" }};

function loc_edit_url(loc_id) {
    var template = '{% url "edit_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_archive_url(loc_id) {
    var template = '{% url "archive_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_unarchive_url(loc_id) {
    var template = '{% url "unarchive_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_delete_url(loc_id) {
    var template = '{% url "delete_location" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

function loc_descendant_url(loc_id) {
    var template = '{% url "location_descendants_count" domain "-locid-" %}';
    return template.replace('-locid-', loc_id);
}

var archive_success_message = _.template(
    '{% trans "You have successfully archived the location <%=name%>" %}'
);

var delete_success_message = _.template(
    '{% trans "You have successfully deleted the location <%=name%> and all of its child locations" %}'
);

var delete_error_message = '{% trans "An error occurred while deleting your location. If the problem persists, please report an issue" %}'

archive_loc = function(button, name, loc_id) {
    var archive_location_modal = $('#archive-location-modal')[0];

    function archive_fn() {
        $(button).disableButton();
        $.ajax({
            type: 'POST',
            url: loc_archive_url(loc_id),
            dataType: 'json',
            error: 'error',
            success: function (response) {
                alert_user(archive_success_message({"name": name}), "success");
                remove_elements_after_action(button);
            }
        });
        $(archive_location_modal).modal('hide');
    }

    var modal_context = {
        "name": name,
        "loc_id": loc_id,
        "archive_fn": archive_fn,
    };
    ko.cleanNode(archive_location_modal);
    $(archive_location_modal).koApplyBindings(modal_context);
    $(archive_location_modal).modal('show');

}

unarchive_loc = function(button, loc_id) {
    $(button).disableButton();
    $.ajax({
        type: 'POST',
        url: loc_unarchive_url(loc_id),
        dataType: 'json',
        error: 'error',
        success: function (response) {
            remove_elements_after_action(button);
        }
    });
}

delete_loc = function(button, name, loc_id) {
    var delete_location_modal = $('#delete-location-modal')[0];
    var modal_context;

    function delete_fn() {
        if (modal_context.count == modal_context.signOff()) {
            $(button).disableButton();
            $.ajax({
                type: 'DELETE',
                url: loc_delete_url(loc_id),
                dataType: 'json',
                error: function (response, status, error) {
                    alert_user(delete_error_message);
                    $(button).enableButton();
                },
                success: function (response) {
                    alert_user(delete_success_message({"name": name}), "success");
                    remove_elements_after_action(button);
                }
            });
            $(delete_location_modal).modal('hide');
        }
    }


    $.ajax({
        type: 'GET',
        url: loc_descendant_url(loc_id),
        dataType: 'json',
        success: function (response) {
            modal_context = {
                "name": name,
                "loc_id": loc_id,
                "delete_fn": delete_fn,
                "count": response.count,
                "signOff": ko.observable(''),
            };
            ko.cleanNode(delete_location_modal);
            ko.applyBindings(modal_context, delete_location_modal);
            $(delete_location_modal).modal('show');
        }
    });
}

remove_elements_after_action = function(button) {
    $(button).closest('.loc_section').remove();
}

$(function() {
    var model = new LocationTreeViewModel(hierarchy);
    $('#location_tree').koApplyBindings(model);
    model.load(locs, selected_loc);
});

</script>
{% endblock %}

{% block main_column %}
<header>
    <div class="row-fluid">
        <div class="span8">
            <h2>{% trans "Manage Organization Structure" %}</h2>
            <p>
                {% blocktrans %}
                    The Organization feature allows you to represent the real-world 
                    structure of your project (districts, facilities, frontline workers, etc.). 
                    Once this structure has been defined, you can use it for reporting and 
                    case sharing. Learn more about Organizations on our <a href="https://confluence.dimagi.com/display/commtrack/Locations">Help Site</a>.
                {% endblocktrans %}
            </p>
            {% if show_inactive %}
            <p class="alert alert-info">
                {% if show_inactive %}{% trans 'Showing the Archived Location List.' %}{% endif %}
            </p>
            {% endif %}
        </div>
        <div class="span4">
            {% if not show_inactive %}
                <a class="btn btn-inverse pull-right" href="?show_inactive=true" style="margin-right: 2.5em;">
                    {% trans "Show Archived Locations" %}
                </a>
            {% else %}
                <a class="btn pull-right" href="?" style="margin-right: 2.5em;">
                    {% trans "Show Active Locations" %}
                </a>
            {% endif %}
        </div>
    </div>
</header>

{% if not show_inactive %}
<div class="row-fluid">
    <div class="span12">
        <div class="btn-toolbar">
            <a class="btn btn-primary"
               {% if not has_location_types or not can_edit_any_location %}
                   disabled
               {% else %}
                   href="{% url "location_import" domain %}"
               {% endif %}
            >
                <i class="icon-upload"></i> {% trans 'Bulk Import' %}
            </a>
            <a class="btn" href="{% url "location_types" domain %}">
                {% trans "Edit Organization Levels" %}
            </a>
            <a class="btn" href="{% url "location_fields_view" domain %}">
                {% trans "Edit Location Fields" %}
            </a>
        </div>
    </div>
</div>
{% endif %}

<hr />
{'fraction_non_alphanumeric': 0.14853586080067901, 'fraction_numerical': 0.00268779176686943, 'mean_word_length': 2.2207289293849657, 'pattern_counts': {'":': 20, '<': 91, '<?xml version=': 0, '>': 91, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5324593', 'n_tokens_mistral': 4330, 'n_tokens_neox': 4041, 'n_words': 906}