/**
 * Build Sort Header.
 */
function build_sort_header($function, $module, $fields, $sort)
{
    $line = array();
    if ($fields) {
        $i = 0;
        foreach ($fields as $field) {
            if ($field['field']) {
                $field['display'] = translate($field['display'], $_SESSION['log_preferred_language']);
                if ($sort == $field['field'] . ' ASC') {
                    $line[$i]['display'] = theme_link_process_information($field['display'], $function . '_table_info', $function . '_sort', $module, array('extra_value' => 'sort|' . $field['field'] . ' DESC'));
                    $line[$i]['class'] = 'sorting_asc';
                } elseif ($sort == $field['field'] . ' DESC') {
                    $line[$i]['display'] = theme_link_process_information($field['display'], $function . '_table_info', $function . '_sort', $module, array('extra_value' => 'sort|' . $field['field'] . ' ASC'));
                    $line[$i]['class'] = 'sorting_desc';
                } else {
                    $line[$i]['display'] = theme_link_process_information($field['display'], $function . '_table_info', $function . '_sort', $module, array('extra_value' => 'sort|' . $field['field'] . ' ASC'));
                    $line[$i]['class'] = 'sorting';
                }
            } else {
                $line[$i]['display'] = $field['display'];
            }
            $i++;
        }
    }
    return $line;
}
/**
 * List items
 */
function menu_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'id ASC';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $db = DataConnection::readOnly();
    $total_records = 0;
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'label', 'func', 'module');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $menus = $db->menu()->where($row_id)->and($search_query)->order($sort)->limit($limit, $offset);
    } else {
        $menus = $db->menu()->where($row_id)->order($sort)->limit($limit, $offset);
    }
    $total_records = $db->menu()->count("*");
    $i = 0;
    if (count($menus)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'Label', 'field' => 'label');
        $headers[] = array('display' => 'Function', 'field' => 'func');
        $headers[] = array('display' => 'Module', 'field' => 'module');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('menu_list', 'menu', $headers, $sort);
        foreach ($menus as $menu) {
            $j = $i + 1;
            //This is important for the row update/delete
            $rows[$j]['row_id'] = $menu['id'];
            /////////////////////////////////////////////
            $rows[$j]['id'] = $menu['id'];
            $rows[$j]['label'] = $menu['label'];
            $rows[$j]['func'] = $menu['func'];
            $rows[$j]['module'] = $menu['module'];
            if ($menu['system'] == 1) {
                $disabled = 'disabled';
            } else {
                $disabled = '';
            }
            $rows[$j]['edit'] = theme_link_process_information('', 'menu_edit_form', 'menu_edit_form', 'menu', array('extra_value' => 'id|' . $menu['id'], 'response_type' => 'modal', 'icon' => NATURAL_EDIT_ICON, 'class' => $disabled));
            $rows[$j]['delete'] = theme_link_process_information('', 'menu_delete_form', 'menu_delete_form', 'menu', array('extra_value' => 'id|' . $menu['id'], 'response_type' => 'modal', 'icon' => NATURAL_REMOVE_ICON, 'class' => $disabled));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Users List'), 'page_subtitle' => translate('Manage Menus'), 'empty_message' => translate('No menu found!'), 'table_prefix' => theme_link_process_information(translate('Create New Menu'), 'menu_create_form', 'menu_create_form', 'menu', array('response_type' => 'modal')), 'pager_items' => build_pager('menu_list', 'menu', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'menu_list', 'module' => 'menu', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * User List.
 */
function user_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'first_name ASC';
    }
    $limit = PAGER_LIMIT;
    // PAGER_LIMIT
    $offset = $page * $limit - $limit;
    $db = DataConnection::readOnly();
    $total_records = 0;
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'first_name', 'last_name', 'username');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $users = $db->user()->where($row_id)->and($search_query)->order($sort)->limit($limit, $offset);
    } else {
        $users = $db->user()->where($row_id)->order($sort)->limit($limit, $offset);
    }
    $total_records = $db->user()->count("*");
    if (count($users) > 0) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'First Name', 'field' => 'first_name');
        $headers[] = array('display' => 'Last Name', 'field' => 'last_name');
        $headers[] = array('display' => 'Username', 'field' => 'username');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('user_list', 'user', $headers, $sort);
        $i = 0;
        foreach ($users as $user) {
            $class = "";
            if ($user['username'] == "admin") {
                $class = "disabled";
            }
            //This is important for the row update
            $rows[$i]['row_id'] = $user['id'];
            $rows[$i]['id'] = $user['id'];
            $rows[$i]['first_name'] = $user['first_name'];
            $rows[$i]['last_name'] = $user['last_name'];
            $rows[$i]['username'] = $user['username'];
            $rows[$i]['edit'] = theme_link_process_information('', 'user_edit_form', 'user_edit_form', 'user', array('extra_value' => 'user_id|' . $user['id'], 'response_type' => 'modal', 'icon' => constant("NATURAL_EDIT_ICON")));
            $rows[$i]['delete'] = theme_link_process_information('', 'user_delete_form', 'user_delete_form', 'user', array('extra_value' => 'user_id|' . $user['id'], 'response_type' => 'modal', 'icon' => constant("NATURAL_REMOVE_ICON"), 'class' => $class));
            $i++;
        }
    }
    //count($users)
    $options = array('show_headers' => TRUE, 'page_title' => translate('Users List'), 'page_subtitle' => translate('Manage Users'), 'empty_message' => translate('No user found!'), 'table_prefix' => theme_link_process_information(translate('Create New User'), 'user_create_form', 'user_create_form', 'user', array('response_type' => 'modal')), 'pager_items' => build_pager('user_list', 'user', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'user_list', 'module' => 'user', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * List items
 */
function bank_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'bank_name asc';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $total_records = 0;
    $pdo = new PDO(NATURAL_PDO_DSN_READ, NATURAL_PDO_USER_READ, NATURAL_PDO_PASS_READ);
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'bank_name', 'account_number', 'comment');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $sql = 'select SQL_CALC_FOUND_ROWS  * from bank where church_id = ' . $_SESSION['log_church_id'] . ' and ' . $search_query . ' order by ' . $sort . ' limit ' . $limit . ' offset ' . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    } else {
        $sql = 'select SQL_CALC_FOUND_ROWS  * from bank where church_id = ' . $_SESSION['log_church_id'] . ' order by ' . $sort . ' limit ' . $limit . ' offset ' . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    }
    $total_records = $pdo->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN);
    $i = 0;
    if (count($records)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'Bank Name', 'field' => 'bank_name');
        $headers[] = array('display' => 'Account Number', 'field' => 'bank_account_number');
        $headers[] = array('display' => 'Comment', 'field' => 'comment');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('bank_list', 'bank', $headers, $sort);
        foreach ($records as $bank) {
            $j = $i + 1;
            //This is important for the row update/delete
            $rows[$j]['row_id'] = $bank['id'];
            /////////////////////////////////////////////
            $rows[$j]['id'] = $bank['id'];
            $rows[$j]['bank_name'] = $bank['bank_name'];
            $rows[$j]['account_number'] = $bank['bank_account_number'];
            $rows[$j]['comment'] = $bank['comment'];
            $rows[$j]['edit'] = theme_link_process_information('', 'bank_edit_form', 'bank_edit_form', 'bank', array('extra_value' => 'id|' . $bank['id'], 'response_type' => 'modal', 'icon' => NATURAL_EDIT_ICON));
            $rows[$j]['delete'] = theme_link_process_information('', 'bank_delete_form', 'bank_delete_form', 'bank', array('extra_value' => 'id|' . $bank['id'], 'response_type' => 'modal', 'icon' => NATURAL_REMOVE_ICON));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Banks List'), 'page_subtitle' => translate('Manage Banks'), 'empty_message' => translate('No bank found!'), 'table_prefix' => theme_link_process_information(translate('Create New Bank'), 'bank_create_form', 'bank_create_form', 'bank', array('response_type' => 'modal')), 'pager_items' => build_pager('bank_list', 'bank', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'bank_list', 'module' => 'bank', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * List items
 */
function categories_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'tt.id = ' . $row_id;
    } else {
        $row_id = 'tt.id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'tt.type_id, tt.name asc';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $total_records = 0;
    $pdo = new PDO(NATURAL_PDO_DSN_READ, NATURAL_PDO_USER_READ, NATURAL_PDO_PASS_READ);
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'bank_name', 'account_number', 'comment');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $sql = "select SQL_CALC_FOUND_ROWS tt.id, tt.name, tt.budget, (case tt.type_id when 0 then 'Income' else 'Withdraw' end) type from categories tt\n              where tt.church_id = " . $_SESSION['log_church_id'] . " and " . $search_query . " order by " . $sort . " limit " . $limit . " offset " . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    } else {
        $sql = "select SQL_CALC_FOUND_ROWS tt.id, tt.name, tt.budget, (case tt.type_id when 0 then 'Income' else 'Withdraw' end) type  from categories tt\n              where tt.church_id = " . $_SESSION['log_church_id'] . "  order by " . $sort . " limit " . $limit . " offset " . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    }
    $total_records = $pdo->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN);
    $i = 0;
    if (count($records)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Type', 'field' => 'type');
        $headers[] = array('display' => 'Name', 'field' => 'name');
        $headers[] = array('display' => 'Budget', 'field' => 'budget');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('categories_list', 'categories', $headers, $sort);
        foreach ($records as $record) {
            $j = $i + 1;
            //This is important for the row update/delete
            $rows[$j]['row_id'] = $record['id'];
            /////////////////////////////////////////////
            $rows[$j]['type'] = $record['type'];
            $rows[$j]['name'] = $record['name'];
            $rows[$j]['budget'] = $record['budget'];
            $rows[$j]['edit'] = theme_link_process_information('', 'categories_edit_form', 'categories_edit_form', 'categories', array('extra_value' => 'id|' . $record['id'], 'response_type' => 'modal', 'icon' => NATURAL_EDIT_ICON));
            $rows[$j]['delete'] = theme_link_process_information('', 'categories_delete_form', 'categories_delete_form', 'categories', array('extra_value' => 'id|' . $record['id'], 'response_type' => 'modal', 'icon' => NATURAL_REMOVE_ICON));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Categories List'), 'page_subtitle' => translate('Manage Categories'), 'empty_message' => translate('No category found!'), 'table_prefix' => theme_link_process_information(translate('Create New Category'), 'categories_create_form', 'categories_create_form', 'categories', array('response_type' => 'modal')), 'pager_items' => build_pager('categories_list', 'categories', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'categories_list', 'module' => 'categories', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * List items
 */
function dashboard_widgets_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'id ASC';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $db = DataConnection::readOnly();
    $total_records = 0;
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'title', 'description');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $dashboard_widgets = $db->dashboard_widgets()->where($row_id)->and($search_query)->order($sort)->limit($limit, $offset);
    } else {
        $dashboard_widgets = $db->dashboard_widgets()->where($row_id)->order($sort)->limit($limit, $offset);
    }
    $total_records = $db->dashboard_widgets()->count("*");
    if (count($dashboard_widgets) > 0) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'Title', 'field' => 'title');
        $headers[] = array('display' => 'Description', 'field' => 'description');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('dashboard_widgets_list', 'dashboard_widgets', $headers, $sort);
        $i = 0;
        foreach ($dashboard_widgets as $widget) {
            $rows[$i]['row_id'] = $widget['id'];
            $rows[$i]['id'] = $widget['id'];
            $rows[$i]['title'] = $widget['title'];
            if (strlen($widget['description']) > 50) {
                $rows[$i]['description'] = substr($widget['description'], 0, 50) . '...';
            } else {
                $rows[$i]['description'] = $widget['description'];
            }
            $rows[$i]['edit'] = theme_link_process_information('', 'dashboard_widgets_edit_form', 'dashboard_widgets_edit_form', 'dashboard_widgets', array('extra_value' => 'id|' . $widget['id'], 'response_type' => 'modal', 'icon' => NATURAL_EDIT_ICON, 'class' => $disabled));
            $rows[$i]['delete'] = theme_link_process_information('', 'dashboard_widgets_delete_form', 'dashboard_widgets_delete_form', 'dashboard_widgets', array('extra_value' => 'id|' . $widget['id'], 'response_type' => 'modal', 'icon' => NATURAL_REMOVE_ICON, 'class' => $disabled));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Users List'), 'page_subtitle' => translate('Manage Dashboard Widgetss'), 'empty_message' => translate('No dashboard widgets found!'), 'table_prefix' => theme_link_process_information(translate('Create New Dashboard Widget'), 'dashboard_widgets_graph_line_template', 'dashboard_widgets_graph_line_template', 'dashboard_widgets', array('response_type' => 'in_modal')), 'pager_items' => build_pager('dashboard_widgets_list', 'dashboard_widgets', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'dashboard_widgets_list', 'module' => 'dashboard_widgets', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * List items
 */
function report_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'id ASC';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $db = DataConnection::readOnly();
    $total_records = 0;
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'report_name', 'report_content');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $reports = $db->report()->where($row_id)->and($search_query)->order($sort)->limit($limit, $offset);
    } else {
        $reports = $db->report()->where($row_id)->order($sort)->limit($limit, $offset);
    }
    $total_records = $db->report()->count("*");
    $i = 0;
    if (count($reports)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'Report Name', 'field' => 'report_name');
        $headers[] = array('display' => 'Pdf', 'field' => NULL);
        $headers = build_sort_header('report_list', 'report', $headers, $sort);
        foreach ($reports as $report) {
            $j = $i + 1;
            //This is important for the row update/delete
            $rows[$j]['row_id'] = $report['id'];
            /////////////////////////////////////////////
            $rows[$j]['id'] = $report['id'];
            $rows[$j]['report_name'] = $report['report_name'];
            $rows[$j]['Pdf'] = theme_link_process_information('Pdf', 'report_pdf', 'report_pdf', 'report', array('extra_value' => 'id|' . $report['id'], 'response_type' => 'modal'));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Reports List'), 'page_subtitle' => translate('Generate Reports'), 'empty_message' => translate('No report found!'), 'pager_items' => build_pager('report_list', 'report', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'report_list', 'module' => 'report', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * User List.
 */
function user_list_vendors($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'u.id = ' . $row_id;
    } else {
        $row_id = 'u.id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'u.first_name, u.last_name asc';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $total_records = 0;
    $pdo = new PDO(NATURAL_PDO_DSN_READ, NATURAL_PDO_USER_READ, NATURAL_PDO_PASS_READ);
    // Search
    if (!empty($search)) {
        $search_fields = array('u.id', 'u.first_name', 'u.last_name', 'u.phone_number', 'u.email');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $sql = 'select SQL_CALC_FOUND_ROWS  u.`id`, u.`first_name`, u.`last_name`, u.`username`, u.`email`, u.`phone_number`, al.description, al.access_level
						       from church_link cl
									 left outer join user u on u.id = cl.user_id
									 left outer join acl_levels al on al.id = cl.acl_levels_id
						       where cl.church_id = ' . $_SESSION['log_church_id'] . ' and u.vendor = 1 and ' . $search_query . ' order by ' . $sort . ' limit ' . $limit . ' offset ' . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    } else {
        $sql = 'select SQL_CALC_FOUND_ROWS  u.`id`, u.`first_name`, u.`last_name`, u.`username`, u.`email`, u.`phone_number`, al.description, al.access_level
						       from church_link cl
									 left outer join user u on u.id = cl.user_id
									 left outer join acl_levels al on al.id = cl.acl_levels_id
						       where cl.church_id = ' . $_SESSION['log_church_id'] . ' and u.vendor = 1 order by ' . $sort . ' limit ' . $limit . ' offset ' . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    }
    $total_records = $pdo->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN);
    if (count($records)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'First Name', 'field' => 'first_name');
        $headers[] = array('display' => 'Last Name', 'field' => 'last_name');
        $headers[] = array('display' => 'User Name', 'field' => 'username');
        $headers[] = array('display' => 'Email', 'field' => 'email');
        $headers[] = array('display' => 'Phone', 'field' => 'phone_number');
        $headers[] = array('display' => 'Rule', 'field' => 'description');
        $headers[] = array('display' => 'Level', 'field' => 'access_level');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('user_list_vendors', 'user', $headers, $sort);
        foreach ($records as $record) {
            $j = $i + 1;
            //This is important for the row update/delete
            $rows[$j]['row_id'] = $record['id'];
            /////////////////////////////////////////////
            $rows[$j]['id'] = $record['id'];
            $rows[$j]['first_name'] = $record['first_name'];
            $rows[$j]['last_name'] = $record['last_name'];
            $rows[$j]['username'] = $record['username'];
            $rows[$j]['email'] = $record['email'];
            $rows[$j]['phone_number'] = $record['phone_number'];
            $rows[$j]['description'] = $record['description'];
            $rows[$j]['level'] = $record['access_level'];
            $rows[$j]['edit'] = theme_link_process_information('', 'user_edit_form', 'user_edit_form', 'user', array('extra_value' => 'user_id|' . $record['id'], 'response_type' => 'modal', 'icon' => constant("NATURAL_EDIT_ICON")));
            $rows[$j]['delete'] = theme_link_process_information('', 'user_delete_form', 'user_delete_form', 'user', array('extra_value' => 'user_id|' . $record['id'], 'response_type' => 'modal', 'icon' => constant("NATURAL_REMOVE_ICON"), 'class' => $class));
            $i++;
        }
    }
    //count($users)
    $options = array('show_headers' => TRUE, 'page_title' => translate('Vendors List'), 'page_subtitle' => translate('Manage Vendors'), 'empty_message' => translate('No vendor found!'), 'table_prefix' => theme_link_process_information(translate('Create New Vendor'), 'user_create_form', 'user_create_form', 'user', array('response_type' => 'modal')), 'pager_items' => build_pager('user_list_vendors', 'user', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'user_list_vendors', 'module' => 'user', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
function field_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'form_reference ASC';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $db = DataConnection::readOnly();
    $total_records = 0;
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'field_name', 'form_reference', 'html_type', 'def_label');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $fields = $db->field_templates()->where($row_id)->and($search_query)->order($sort)->limit($limit, $offset);
    } else {
        $fields = $db->field_templates()->where($row_id)->order($sort)->limit($limit, $offset);
    }
    $total_records = $db->field_templates()->count("*");
    $i = 0;
    if (count($fields)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'Form Reference', 'field' => 'form_reference');
        $headers[] = array('display' => 'Position', 'field' => 'form_field_order');
        $headers[] = array('display' => 'Name', 'field' => 'field_name');
        $headers[] = array('display' => 'HTML Type', 'field' => 'html_type');
        $headers[] = array('display' => 'Label', 'field' => 'def_label');
        $headers[] = array('display' => 'Edit', 'field' => NULL);
        $headers[] = array('display' => 'Delete', 'field' => NULL);
        $headers = build_sort_header('field_list', 'natural', $headers, $sort);
        $total = 0;
        foreach ($fields as $field) {
            $j = $i + 1;
            //This is important for the row update
            $rows[$j]['row_id'] = $field['id'];
            //////////////////////////////////////
            $rows[$j]['id'] = $field['id'];
            $rows[$j]['form_reference'] = $field['form_reference'];
            $rows[$j]['form_field_order'] = $field['form_field_order'];
            $rows[$j]['field_name'] = $field['field_name'];
            $rows[$j]['html_type'] = $field['html_type'];
            $rows[$j]['def_label'] = $field['def_label'];
            $rows[$j]['edit'] = theme_link_process_information('', 'field_edit_form', 'field_edit_form', 'natural', array('extra_value' => 'id|' . $field['id'], 'response_type' => 'modal', 'icon' => NATURAL_EDIT_ICON));
            $rows[$j]['delete'] = theme_link_process_information('', 'field_delete_form', 'field_delete_form', 'natural', array('extra_value' => 'id|' . $field['id'], 'response_type' => 'modal', 'icon' => NATURAL_REMOVE_ICON));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Field List'), 'page_subtitle' => translate('Manage Fields'), 'empty_message' => translate('No field found!'), 'table_prefix' => theme_link_process_information(translate('Create New Field'), 'field_create_form', 'field_create_form', 'natural', array('response_type' => 'modal')), 'pager_items' => build_pager('field_list', 'natural', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'field_list', 'module' => 'natural', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}
/**
 * List items
 */
function church_list($row_id = NULL, $search = NULL, $sort = NULL, $page = 1)
{
    $view = new ListView();
    // Row Id for update only row
    if (!empty($row_id)) {
        $row_id = 'id = ' . $row_id;
    } else {
        $row_id = 'id != 0';
    }
    // Sort
    if (empty($sort)) {
        $sort = 'c.name asc';
    }
    $limit = PAGER_LIMIT;
    $offset = $page * $limit - $limit;
    $total_records = 0;
    $pdo = new PDO(NATURAL_PDO_DSN_READ, NATURAL_PDO_USER_READ, NATURAL_PDO_PASS_READ);
    // Search
    if (!empty($search)) {
        $search_fields = array('id', 'name', 'rowcount');
        $exceptions = array();
        $search_query = build_search_query($search, $search_fields, $exceptions);
        $sql = 'select SQL_CALC_FOUND_ROWS c.id, c.name,
                 (select count(id) from church_link where church_id = c.id and acl_levels_id = 1) visitors,
                 (select count(id) from church_link where church_id = c.id and acl_levels_id = 2) members,
                 (select count(id) from church_link where church_id = c.id and acl_levels_id = 3) pastors,
                 (select count(id) from church_link where church_id = c.id and acl_levels_id = 4) treasurers,
                 (select count(id) from church_link where church_id = c.id and acl_levels_id = 5) administrators
       from church_link cl
       left outer join church c on c.id = cl.church_id
       where cl.user_id = ' . $_SESSION['log_id'] . ' ' . $search_query . ' order by ' . $sort . ' limit ' . $limit . ' offset ' . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    } else {
        $sql = 'select SQL_CALC_FOUND_ROWS c.id, c.name,
                   (select count(id) from church_link where church_id = c.id and acl_levels_id = 1) visitors,
                   (select count(id) from church_link where church_id = c.id and acl_levels_id = 2) members,
                   (select count(id) from church_link where church_id = c.id and acl_levels_id = 3) pastors,
                   (select count(id) from church_link where church_id = c.id and acl_levels_id = 4) treasurers,
                   (select count(id) from church_link where church_id = c.id and acl_levels_id = 5) administrators
              from church_link cl
              left outer join church c on c.id = cl.church_id
              where cl.user_id = ' . $_SESSION['log_id'] . ' order by ' . $sort . ' limit ' . $limit . ' offset ' . $offset;
        $records = $pdo->prepare($sql);
        $records->execute();
    }
    $total_records = $pdo->query('SELECT FOUND_ROWS();')->fetch(PDO::FETCH_COLUMN);
    $i = 0;
    if (count($records)) {
        // Building the header with sorter
        $headers[] = array('display' => 'Id', 'field' => 'id');
        $headers[] = array('display' => 'Church', 'field' => 'name');
        $headers[] = array('display' => 'Visitors', 'field' => 'visitors');
        $headers[] = array('display' => 'Members', 'field' => 'members');
        $headers[] = array('display' => 'Pastors', 'field' => 'pastors');
        $headers[] = array('display' => 'Treasurers', 'field' => 'treasurers');
        $headers[] = array('display' => 'Administrators', 'field' => 'administrators');
        $headers[] = array('display' => 'Access', 'field' => NULL);
        $headers = build_sort_header('church_list', 'church', $headers, $sort);
        foreach ($records as $record) {
            $j = $i + 1;
            //This is important for the row update/delete
            $rows[$j]['row_id'] = $record['id'];
            /////////////////////////////////////////////
            $rows[$j]['id'] = $record['id'];
            $rows[$j]['name'] = $record['name'];
            $rows[$j]['visitors'] = $record['visitors'];
            $rows[$j]['members'] = $record['members'];
            $rows[$j]['pastors'] = $record['pastors'];
            $rows[$j]['treasurers'] = $record['treasurers'];
            $rows[$j]['administrators'] = $record['administrators'];
            $rows[$j]['account_management'] = theme_link_process_information('Manage', '', '', 'church', array('response_type' => null, 'href' => 'jump.php?url=dashboard_church.php&church_id=' . $record['id']));
            $i++;
        }
    }
    $options = array('show_headers' => TRUE, 'page_title' => translate('Churchs List'), 'page_subtitle' => translate('Manage Churchs'), 'empty_message' => translate('No church found!'), 'table_prefix' => theme_link_process_information(translate('Create New Church'), 'church_create_form', 'church_create_form', 'church', array('response_type' => 'modal')), 'pager_items' => build_pager('church_list', 'church', $total_records, $limit, $page), 'page' => $page, 'sort' => $sort, 'search' => $search, 'show_search' => TRUE, 'function' => 'church_list', 'module' => 'church', 'update_row_id' => '', 'table_form_id' => '', 'table_form_process' => '');
    $listview = $view->build($rows, $headers, $options);
    return $listview;
}