<?php
// This file is part of mod_datalynx for Moodle - http://moodle.org/
//
// It is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 *
 * @package mod_datalynx
 * @copyright 2013 onwards edulabs.org and associated programmers
 * @copyright based on the work  by 2011 Itamar Tzadok
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once('../../../config.php');
require_once('../classes/datalynx.php');
require_once("$CFG->libdir/tablelib.php");

$urlparams = new stdClass();

$urlparams->d = optional_param('d', 0, PARAM_INT); // Datalynx id.
$urlparams->id = optional_param('id', 0, PARAM_INT); // Course module id.
$urlparams->vedit = optional_param('vedit', 0, PARAM_INT); // View id to edit.

// Views list actions.
$urlparams->default = optional_param('default', 0, PARAM_INT); // Id of view to default.
$urlparams->singleedit = optional_param('singleedit', 0, PARAM_INT); // Id of view to single edit.
$urlparams->singlemore = optional_param('singlemore', 0, PARAM_INT); // Id of view to single more.
$urlparams->visible = optional_param('visible', 0, PARAM_INT); // Id of view to hide/(show)/show.
$urlparams->reset = optional_param('reset', 0, PARAM_SEQUENCE); // Ids (comma delimited) of.
// Views to delete.
$urlparams->delete = optional_param('delete', 0, PARAM_SEQUENCE); // Ids (comma delimited) of.
// Views to delete.
$urlparams->duplicate = optional_param('duplicate', 0, PARAM_SEQUENCE); // Ids (comma delimited) of.
// Views to duplicate.
$urlparams->setfilter = optional_param('setfilter', 0, PARAM_INT); // Id of view to filter.

$urlparams->confirmed = optional_param('confirmed', 0, PARAM_INT);

// Set a datalynx object.
$dl = new mod_datalynx\datalynx($urlparams->d, $urlparams->id);

require_login($dl->data->course, false, $dl->cm);

require_capability('mod/datalynx:managetemplates', $dl->context);

$dl->set_page('view/index', ['modjs' => true, 'urlparams' => $urlparams]);

// Activate navigation node.
navigation_node::override_active_url(
    new moodle_url('/mod/datalynx/view/index.php', ['id' => $dl->cm->id])
);

// DATA PROCESSING.
if ($urlparams->duplicate && confirm_sesskey()) { // Duplicate any requested views.
    $dl->process_views('duplicate', $urlparams->duplicate, $urlparams->confirmed);
} else {
    if ($urlparams->reset && confirm_sesskey()) { // Reset to default any requested views.
        $patterncache = cache::make('mod_datalynx', 'patterns');
        $patterncache->delete($urlparams->vedit, true);
        $dl->process_views('reset', $urlparams->reset, true);
    } else {
        if ($urlparams->delete && confirm_sesskey()) { // Delete any requested views.
            $dl->process_views('delete', $urlparams->delete, $urlparams->confirmed);
        } else {
            if ($urlparams->visible && confirm_sesskey()) { // Set view's visibility.
                $dl->process_views('visible', $urlparams->visible, true); // Confirmed by default.
            } else {
                if ($urlparams->default && confirm_sesskey()) { // Set view to default.
                    $dl->process_views('default', $urlparams->default, true); // Confirmed by default.
                } else {
                    if ($urlparams->singleedit && confirm_sesskey()) { // Set view to single edit.
                        if ($urlparams->singleedit == -1) {
                            $dl->set_single_edit_view(); // Reset.
                        } else {
                            $dl->set_single_edit_view($urlparams->singleedit);
                        }
                    } else {
                        if ($urlparams->singlemore && confirm_sesskey()) { // Set view to single more.
                            if ($urlparams->singlemore == -1) {
                                $dl->set_single_more_view(); // Reset.
                            } else {
                                $dl->set_single_more_view($urlparams->singlemore);
                            }
                        } else {
                            if ($urlparams->setfilter && confirm_sesskey()) { // Re/set view filter.
                                $dl->process_views('filter', $urlparams->setfilter, true); // Confirmed by default.
                            }
                        }
                    }
                }
            }
        }
    }
}

// Any notifications?
$dl->notifications['bad']['defaultview'] = '';
$views = $dl->get_views_editable_by_user(flexible_table::get_sort_for_table('datalynxviewsindex' . $dl->id()));
if (empty($views)) {
    // Now views defined yet.
    $dl->notifications['bad']['getstartedviews'] = get_string('viewnoneindatalynx', 'datalynx');
} else {
    if (empty($dl->data->defaultview) && !empty($dl->get_view_records(true))) {
        $dl->notifications['bad']['defaultview'] = get_string('viewnodefault', 'datalynx', '');
    }
}

// Print header.
$dl->print_header(['tab' => 'views', 'urlparams' => $urlparams]);

// Display the view form jump list.
$directories = get_list_of_plugins('mod/datalynx/view/');
$menuview = [];

foreach ($directories as $directory) {
    if ($directory[0] != '_') {
        $menuview[$directory] = get_string('pluginname', "datalynxview_$directory"); // Get from.
        // Language.
        // Files.
    }
}
asort($menuview); // Sort in alphabetical order.

$br = html_writer::empty_tag('br');
$popupurl = $CFG->wwwroot . '/mod/datalynx/view/view_edit.php?d=' . $dl->id() . '&amp;sesskey=' . sesskey();
$viewselect = new single_select(
    new moodle_url($popupurl),
    'type',
    $menuview,
    null,
    ['' => 'choosedots'],
    'viewform'
);
$viewselect->set_label(get_string('viewadd', 'datalynx') . '&nbsp;');
echo $output = html_writer::tag(
    'div',
    $br . $OUTPUT->render($viewselect) . $br,
    ['class' => 'viewadd mdl-align']
);

// If there are views print admin style list of them.
if (!empty($views)) {
    $viewbaseurl = '/mod/datalynx/view.php';
    $editbaseurl = '/mod/datalynx/view/view_edit.php';
    $actionbaseurl = '/mod/datalynx/view/index.php';
    $linkparams = ['d' => $dl->id(), 'sesskey' => sesskey()];

    // Table headings.
    $strdefault = get_string('defaultview', 'datalynx');
    $strsingleedit = get_string('singleedit', 'datalynx');
    $strsinglemore = get_string('singlemore', 'datalynx');
    $strfilter = get_string('filter', 'datalynx');
    $stredit = get_string('edit');
    $strdelete = get_string('delete');
    $strduplicate = get_string('duplicate');
    $strchoose = get_string('choose');

    $selectallnone = html_writer::checkbox(
        null,
        null,
        false,
        null,
        ['onclick' => 'select_allnone(\'view\'&#44;this.checked)']
    );
    $multiactionurl = new moodle_url($actionbaseurl, $linkparams);
    $multidelete = html_writer::tag(
        'button',
        $OUTPUT->pix_icon('t/delete', get_string('multidelete', 'datalynx')),
        ['name' => 'multidelete',
                    'onclick' => 'bulk_action(\'view\'&#44; \'' . $multiactionurl->out(false) . '\'&#44; \'delete\')',
        ]
    );
    $multiduplicate = html_writer::tag(
        'button',
        $OUTPUT->pix_icon('t/copy', get_string('multiduplicate', 'datalynx')),
        ['type' => 'button', 'name' => 'multiduplicate',
                    'onclick' => 'bulk_action(\'view\'&#44; \'' . $multiactionurl->out(false) . '\'&#44; \'duplicate\')',
        ]
    );

    $strhide = get_string('hide');
    $strshow = get_string('show');
    $strreset = get_string('reset');

    $filtersmenu = $dl->get_filter_manager()->get_filters(null, true);

    // Table headers.
    $headers = ['name' => get_string('name'), 'type' => get_string('type', 'datalynx'),
            'description' => get_string('description'), 'visible' => get_string('visible'),
            'default' => $strdefault, 'singleedit' => $strsingleedit, 'singlemore' => $strsinglemore,
            'filter' => $strfilter, 'edit' => $stredit, 'reset' => $strreset,
            'duplicate' => $multiduplicate, 'delete' => $multidelete, 'selectallnone' => $selectallnone,
    ];

    $table = new flexible_table('datalynxviewsindex' . $dl->id());
    $table->define_baseurl(new moodle_url('/mod/datalynx/view/index.php', ['d' => $dl->id()]));
    $table->define_columns(array_keys($headers));
    $table->define_headers(array_values($headers));

    // Column sorting.
    $table->sortable(true);
    $table->no_sorting('description');
    $table->no_sorting('default');
    $table->no_sorting('singleedit');
    $table->no_sorting('singlemore');
    $table->no_sorting('filter');
    $table->no_sorting('edit');
    $table->no_sorting('duplicate');
    $table->no_sorting('reset');
    $table->no_sorting('delete');
    $table->no_sorting('selectallnone');

    // Styles.
    $table->set_attribute('class', 'generaltable generalbox boxaligncenter boxwidthwide');
    $table->column_style('visible', 'text-align', 'center');
    $table->column_style('edit', 'text-align', 'center');
    $table->column_style('duplicate', 'text-align', 'center');
    $table->column_style('reset', 'text-align', 'center');
    $table->column_style('delete', 'text-align', 'center');

    $table->setup();

    foreach ($views as $viewid => $view) {
        $internalview = $view->is_internal_view();
        if ($internalview) {
            $viewname = format_string($view->name());
        } else {
            $viewname = html_writer::link(
                new moodle_url($viewbaseurl, ['d' => $dl->id(), 'view' => $viewid]),
                $view->name()
            );
        }
        $viewtype = $view->typename();
        $viewdescription = shorten_text($view->view->description, 30);
        $viewedit = html_writer::link(
            new moodle_url($editbaseurl, $linkparams + ['vedit' => $viewid]),
            $OUTPUT->pix_icon('t/edit', $stredit . " " . $view->name()),
            ['title' => $stredit . " " . $view->name()]
        );
        $viewduplicate = html_writer::link(
            new moodle_url($actionbaseurl, $linkparams + ['duplicate' => $viewid]),
            $OUTPUT->pix_icon('t/copy', $strduplicate)
        );
        $viewreset = html_writer::link(
            new moodle_url($actionbaseurl, $linkparams + ['reset' => $viewid]),
            $OUTPUT->pix_icon('t/reload', $strreset)
        );
        $viewdelete = html_writer::link(
            new moodle_url($actionbaseurl, $linkparams + ['delete' => $viewid]),
            $OUTPUT->pix_icon('t/delete', $strdelete)
        );
        $viewselector = html_writer::checkbox("viewselector", $viewid, false);

        $viewvisible = '&mdash;';
        if (!$internalview) {
            $viewvisible = '';
            for ($i = 1; $i < 16; $i = $i << 1) {
                $viewvisible .= html_writer::checkbox(
                    "visible[{$i}]",
                    1,
                    ($i & $view->view->visible),
                    '',
                    ['disabled' => '', 'title' => get_string("visible{$i}", 'datalynx')]
                );
            }
        }

        // Default view.
        if ($internalview) {
            $defaultview = '&mdash;';
        } else if ($viewid == $dl->data->defaultview) {
            $defaultview = $OUTPUT->pix_icon('t/approve', get_string('isdefault', 'mod_datalynx'));
        } else {
            $defaultview = html_writer::link(
                new moodle_url($actionbaseurl, $linkparams + ['default' => $viewid]),
                $OUTPUT->pix_icon('t/switch_whole', get_string('setdefault', 'mod_datalynx'))
            );
        }

        // Single edit view.
        if ($internalview) {
            $singleedit = '&mdash;';
        } else if ($viewid == $dl->data->singleedit) {
            $singleedit = html_writer::link(
                new moodle_url($actionbaseurl, $linkparams + ['singleedit' => -1]),
                $OUTPUT->pix_icon('t/approve', get_string('isedit', 'mod_datalynx'))
            );
        } else {
            $singleedit = html_writer::link(
                new moodle_url($actionbaseurl, $linkparams + ['singleedit' => $viewid]),
                $OUTPUT->pix_icon('t/switch_whole', get_string('setedit', 'mod_datalynx'))
            );
        }

        // Single more view.
        if ($internalview) {
            $singlemore = '&mdash;';
        } else if ($viewid == $dl->data->singleview) {
            $singlemore = html_writer::link(
                new moodle_url($actionbaseurl, $linkparams + ['singlemore' => -1]),
                $OUTPUT->pix_icon('t/approve', get_string('ismore', 'mod_datalynx'))
            );
        } else {
            $singlemore = html_writer::link(
                new moodle_url($actionbaseurl, $linkparams + ['singlemore' => $viewid]),
                $OUTPUT->pix_icon('t/switch_whole', get_string('setmore', 'mod_datalynx'))
            );
        }

        // TODO MDL-000000 view filter.
        if ($internalview) {
            $viewfilter = '&mdash;';
        } else if (!empty($filtersmenu)) {
            $viewfilterid = $view->view->filter;
            if ($viewfilterid && !in_array($viewfilterid, array_keys($filtersmenu))) {
                $viewfilter = html_writer::link(
                    new moodle_url(
                        $actionbaseurl,
                        $linkparams + ['setfilter' => $viewid, 'fid' => -1]
                    ),
                    $OUTPUT->pix_icon('i/risk_xss', $strreset)
                );
            } else {
                $blankfilteroption = [-1 => get_string('blankfilter', 'datalynx')];
                if ($dl->data->defaultfilter) {
                    $defaultfilter = $dl->get_filter_manager()->get_filter_from_id($dl->data->defaultfilter);
                    $defaultfiltername = $defaultfilter->name;
                    $defaultfilteroption = [
                            0 => get_string('defaultfilterlabel', 'datalynx', $defaultfiltername),
                    ];
                } else {
                    $defaultfilteroption = [
                            0 => get_string('defaultfilterlabel', 'datalynx', get_string('blankfilter', 'datalynx')),
                    ];
                }

                if ($viewfilterid) {
                    $selected = $viewfilterid;
                } else {
                    $selected = 0;
                }

                $options = $blankfilteroption + $defaultfilteroption + $filtersmenu;

                $selecturl = new moodle_url(
                    $actionbaseurl,
                    $linkparams + ['setfilter' => $viewid]
                );
                $viewselect = new single_select($selecturl, 'fid', $options, $selected, []);

                $viewfilter = $OUTPUT->render($viewselect);
            }
        } else {
            $viewfilter = get_string('filtersnonedefined', 'datalynx');
        }

        $table->add_data(
            [$viewname, $viewtype, $viewdescription, $viewvisible, $defaultview,
                        $singleedit, $singlemore, $viewfilter, $viewedit, $viewreset, $viewduplicate,
            $viewdelete,
            $viewselector]
        );
    }

    $table->finish_output();
}

$dl->print_footer();
