<?php

/*
 * *************************************************
 * Copyright (C) 2020 Earworks.                    *
 * Codeigniter Project Template Modification       *
 * Please contact our email for more information : *
 * contact@earworks.co.id                          *
 * *************************************************
 */

defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Showing message from session flash if exist
 * Note : Call this function only on begining of main layouts
 * @param array $config
 * @return array
 */
function show_message($config = []) {
    $CI =& get_instance();
    $return = [];
    if (($error = $CI->session->flashdata('error'))) {
        $return[] = message_template($error, isset($config['errorClass']) ? $config['errorClass']: 'danger');
    }
    if (($error = $CI->session->flashdata('danger'))) {
        $return[] = message_template($error, isset($config['errorClass']) ? $config['errorClass']: 'danger');
    }
    if (($success = $CI->session->flashdata('success'))) {
        $return[] = message_template($success, isset($config['successClass']) ? $config['successClass']: 'success');
    }
    if (($info = $CI->session->flashdata('info'))) {
        $return[] = message_template($info, isset($config['infoClass']) ? $config['infoClass']: 'info');
    }
    if (($warning = $CI->session->flashdata('warning'))) {
        $return[] = message_template($warning, isset($config['warningClass']) ? $config['warningClass']: 'warning');
    }
    return $return;
}

/**
 * Override this function for change the default html template
 * for showing message
 * @param mixed $messages
 * @param string $containerClass
 * @return string
 */
if (!function_exists('message_template')) {

    function message_template($messages = null, $containerClass = null) {
        $return = ($containerClass) ? '<div class="message message-' . $containerClass . '">' : '<div>';
        if (is_array($messages)) {
            $lists = "<li>" . implode("</li>\n<li>", $messages) . "</li>\n";
            $ul = ($containerClass) ? '<ul class="' . $containerClass . '">' : '<ul>';
            $return .= $ul . "\n" . $lists . "</ul>\n";
        } else {
            $return .= '<p>'.$messages.'</p>';
        }
        return $return. '<button class="close-message"><i class="fa fa-times"></i></button>'
                . '</div>';
    }

}