{% extends "admin/change_form.html" %}

{% block extrastyle %}
    {{ block.super }}
    <style>
        .examples pre {
            background-color: #fff;
            border: solid #00bbff 1px;
            padding: 1em;
        }
        .examples pre code {
            color:#004d69;
        }
        .see-more {
            margin-bottom: 2em;
        }

        .examples table {
            border: solid #b4b4b4 1px;
        }
        .examples table th, .examples table td {
            border-bottom: solid #b4b4b4 1px;
            border-right: solid #b4b4b4 1px;
            padding: 0.2em 0.6em;
        }
        .examples table th {
            background-color: #00bbff;
            color: #fff;
        }
        .examples table td:nth-child(2) {
            text-align: right;
        }
    </style>
{% endblock %}

{% block content %}
    {{ block.super }}

<div class="examples">

<h4>Transform format</h4>
<table>
<tr>
    <th>Name</th>
    <th>Type</th>
    <th>Description</th>
</tr>
<tr>
    <td>dest</td>
    <td>string</td>
    <td>Name of key in destination data.</td>
</tr>
<tr>
    <td>value</td>
    <td>string</td>
    <td>Some value (constant).</td>
</tr>
<tr>
    <td>src</td>
    <td>string / array</td>
    <td>
        Query into the JSON source. <a href="https://jqlang.github.io/jq/manual/" target="_top" title="Go to ./jq Manual.">./jq Manual</a>
        </td>
</tr>
<tr>
    <td>fetcher</td>
    <td>string</td>
    <td>
        <div>Fetcher for a value from the query result.</div>
        <div>Default is: <code>first</code>.</div>
        <div>Available types: <code>first, all, text</code>.</div>
        </td>
</tr>
<tr>
    <td>sep</td>
    <td>string</td>
    <td>
        <div>Separator for join multiple parts of query result.</div>
        <div>Default is: <code>" "</code>.</div>
    </td>
</tr>
<tr>
    <td>match</td>
    <td>string / array</td>
    <td>
        <div>Pattern for <code>re.mach(pattern, value).groups()</code>.</div>
        <div>The array type is in the format <code>[pattern, flags, separator]</code></div>
        <div>used as <code>separator.join(re.mach(pattern, value, flags).groups())</code>.</div>
    </td>
</tr>
<tr>
    <td>fnc</td>
    <td>string</td>
    <td>
        <div>A function that processes the data.</div>
        <div>
<pre><code>def remove_identical_value(rule: dataType, src: dataType, dest: dataType) -&gt; None:
    """Remove value if it is identical to another."""
    data = [dest.get(key) for key in rule.get("params", {}).get("fields", [])]
    if len(set(data)) &lt; len(data) and rule["dest"] in dest:
        del dest[rule["dest"]]
</code></pre></div>
    </td>
</tr>
</table>
<div class="see-more">
    You can verify the transformation result via the "Export data via webhook" command in the <a href="{% url "admin:aldryn_forms_formsubmission_changelist" %}">Form submission</a> post administration.
.</div>

<h4>Transform example</h4>

<pre><code>[
    {"dest": "roid", "value": "1234650"},
    {
        "dest": "first_name",
        "src": ".form_data[] | select(.label==\"Name\").value",
        "match": "(.+)\\s+\\S+$"
    },
    {
        "dest": "last_name",
        "src": ".form_data[] | select(.label==\"Name\").value",
        "match": ".+\\s+(\\S+)$"
    },
    {
        "dest": "email",
        "src": ".form_data[] | select(.label==\"Email\").value"
    },
    {
        "dest": "phone",
        "src": ".form_data[] | select(.label==\"Phone\").value"
    },
    {
        "dest": "company",
        "src": ".form_data[] | select(.label==\"Company\").value"
    },
    {
        "dest": "address",
        "src": [
            ".form_data[] | select(.name==\"street\").value",
            ".form_data[] | select(.name==\"city\").value"
        ],
        "sep": "; "
    },
    {
        "dest": "ip",
        "src": ".ipv4",
        "match": ["(\\d+).+(\\d+).+(\\d+).+(\\d+)", "S", "."]
    }
    {
        "dest": "lead_source",
        "src": ".hostname"
    },
    {
        "fnc": "aldryn_forms.api.utils.remove_identical_value",
        "dest": "first_name",
        "params": {
            "fields": ["first_name", "last_name"]
        }
    }
]</code></pre>
<div class="see-more">For more see <a href="https://jqlang.github.io/jq/manual/" target="_top" title="Go to ./jq Manual.">./jq Manual</a>.</div>

<h4>Source data example</h4>

<pre><code>{
    "hostname": "example.com",
    "name": "The form name",
    "language": "en",
    "sent_at": "2025-03-03T02:00:48.439766Z",
    "ipv4": "127\n0\n0\n0\n1",
    "form_recipients": [
        {
            "name": "Dave",
            "email": "dave@dwarf.red"
        }
    ],
    "form_data": [
        {
            "name": "name",
            "label": "Name",
            "field_occurrence": 1,
            "value": "Arnold Rimmer"
        },
        {
            "name": "email",
            "label": "Email",
            "field_occurrence": 1,
            "value": "arnold@dwarf.red"
        },
        {
            "name": "street",
            "label": "Street and number",
            "field_occurrence": 1,
            "value": "Upper Deck 42"
        },
        {
            "name": "city",
            "label": "City",
            "field_occurrence": 1,
            "value": "Deep space"
        },

    ]
}</code></pre>
<div class="see-more">{% if SITE_API_ROOT %}For more see <a href="{{ SITE_API_ROOT }}" target="_top">Api Root</a>.{% endif %}</div>

<h4>Result of examples</h4>

<pre><code>{
    "roid": "1234650",
    "first_name": "Arnold",
    "last_name": "Rimmer",
    "email": "arnold@dwarf.red",
    "address": "Upper Deck 42; Deep space",
    "lead_source": "example.com",
    "ip": "127.0.0.1"
}</code></pre>

</div>

{% endblock %}
