Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
6.6 kB
4
Indexable
Never
function searchFunction($id, $action, $search_term, $input_param, $output_param, $show_value = ['name'], $min_length = 0, $title_error = '', $message_error = '', $filter = [], $check_checkbox = false)
{

    $res_param = [];
    $res_param_0 = '';
    $data_param_input = "";
    $after_check_input_param = "";

    $renderScript  = "\n\t\t var path = \"" . route('autocomplete') . "\";";
    $renderScript .= "\n\t\t var filterObject = [];";

    if (!empty($input_param)) {
        $after_check_input_param .= "\n\t\t var countCheckbox = 0;";

        $renderAlert = renderSweetAlert('Error', 'Supporting data is not complete', 'error');

        if ($title_error != '' && $message_error != '') {
            $renderAlert = renderSweetAlert($title_error, $message_error, 'error');
        }

        foreach ($input_param as $parameter) {
            $param = explode("|", $parameter);

            $res_param = $param[1];
            $param_0 = $param[0];
            $param_mode = isset($param[2]) ? $param[2] : '';

            if ($res_param != '') {
                $after_check_input_param .= "\n\t\t\t var searchInput = $('#" . $res_param . "');";
                $after_check_input_param .= "\n\t\t\t var inputValue = searchInput.val();";

                if ($param_mode == 'checkbox') {
                    $after_check_input_param .= "\n\t\t\t var isChecked = searchInput.is(':checked');";
                    $after_check_input_param .= "\n\t\t\t if (isChecked) {";
                    $after_check_input_param .= "\n\t\t\t filterObject.push({ key: '" . $param_0 . "', term: 'equal', query: inputValue })";
                    $after_check_input_param .= "\n\t\t\t countCheckbox += 1";
                    $after_check_input_param .= "\n\t\t\t }";
                } else {
                    $after_check_input_param .= "\n\t\t\t if (inputValue === undefined || inputValue.trim() === '') {";
                    $after_check_input_param .= $renderAlert;
                    $after_check_input_param .= "\n\t\t\t return;";
                    $after_check_input_param .= "\n\t\t\t } else {";
                    $after_check_input_param .= "\n\t\t\t filterObject.push({ key: '" . $param_0 . "', term: 'equal', query: inputValue })";
                    $after_check_input_param .= "\n\t\t\t }";
                }
            }
        }

        if ($check_checkbox == true) {
            $after_check_input_param .= "\n\t\t\t if (countCheckbox == 0) {";
            $after_check_input_param .= $renderAlert;
            $after_check_input_param .= "\n\t\t\t return;";
            $after_check_input_param .= "\n\t\t\t }";
        }

        $data_param_input = ",\n\t\t\t input_param: JSON.stringify(filterObject)";
    }

    $renderScript .= "console.log(filterObject);";

    $renderScript .= "\n\t\t\t $( '#" . $id . "' ).autocomplete({";
    $renderScript .= "\n\t\t\t source: function( request, response ) {";
    $renderScript .= $after_check_input_param;
    $renderScript .= "\n\t\t\t $.ajax({";
    $renderScript .= "\n\t\t\t url: path,";
    $renderScript .= "\n\t\t\t type: 'GET',";
    $renderScript .= "\n\t\t\t dataType: 'json',";
    $renderScript .= "\n\t\t\t data: {";
    $renderScript .= "\n\t\t\t search: request.term,";
    $renderScript .= "\n\t\t\t show_value: '" . json_encode($show_value) . "',";
    $renderScript .= "\n\t\t\t search_term: " . json_encode($search_term) . ",";
    $renderScript .= "\n\t\t\t filter: '" . json_encode($filter) . "',";
    $renderScript .= "\n\t\t\t action: '" . $action . "'";
    $renderScript .= $data_param_input;
    $renderScript .= "\n\t\t\t },";
    $renderScript .= "\n\t\t\t success: function(data) {";
    $renderScript .= "\n\t\t\t console.log(data);";
    $renderScript .= "\n\t\t\t if(data.success == false){
            swal('Error', data['message'], 'error')
        }";
    $renderScript .= "\n\t\t\t else{
            response(data);
        };";
    $renderScript .= "\n\t\t\t }";
    $renderScript .= "\n\t\t\t });";
    $renderScript .= "\n\t\t\t },";
    $renderScript .= "\n\t\t\t minLength: " . $min_length . ",";
    $renderScript .= "\n\t\t\t select: function (event, ui) {";
    $renderScript .= "\n\t\t\t $('#" . $id . "').val(ui.item.label);";
    $renderScript .= "\n\t\t\t var selectedItem = ui.item.label;";
    $renderScript .= "\n\t\t\t var selectedItemId = ui.item.data.id;"; // Get the selected item's ID
    $renderScript .= "\n\t\t\t if (!selectedItems.includes(selectedItem)) {";
    $renderScript .= "\n\t\t\t selectedItems.push(selectedItem);";
    $renderScript .= "\n\t\t\t selectedItemIds.push(selectedItemId);"; // Add the selected item's ID to the array
    $renderScript .= "\n\t\t\t $('#" . $id . "_tags').append('<span class=\"selected-item\" data-id=\"' + selectedItemId + '\">' + selectedItem + ' <button class=\"remove-item\">x</button></span>');"; // Append selected item with remove button
    $renderScript .= "\n\t\t\t $('#" . $id . "_ids').val(selectedItemIds.join(','));"; // Update the hidden input with the selected IDs
    $renderScript .= "\n\t\t\t }";
    if (!empty($output_param)) {
        foreach ($output_param as $parameter) {
            $param = explode("|", $parameter);
            $output_action = "val";
            if (isset($param[2])) {
                if ($param[2] == "html") {
                    $output_action = $param[2];
                }
            }
            $setOutputUI = "ui.item.data." . $param[1] . ".toString()";
            if (str_contains($param[1], '-')) {
                $renderOutputs = explode("-", $param[1]);
                $tempOutputUI = "";
                $count = 0;

                foreach ($renderOutputs as $value) {
                    $tempOutputUI .= "ui.item.data." . $value . ".toString()";
                    $count++;

                    if ($count < count($renderOutputs)) {
                        $tempOutputUI .= " + ' - ' + ";
                    }
                }
                $setOutputUI = $tempOutputUI;
            }

            $renderScript .= "\n\t\t\t $('#" . $param[0] . "')." . $output_action . "(" . $setOutputUI . ");";
        }
    }
    $renderScript .= "if(typeof getVal === 'function') {
            getVal();
        } else {
            console.log('No function defined');
        }";
    $renderScript .= "\n\t\t\t return false;";
    $renderScript .= "\n\t\t\t }";
    $renderScript .= "\n});";


    return $renderScript;
}
Leave a Comment