Proyectos de Subversion LeadersLinked - Backend

Rev

Autoría | Ultima modificación | Ver Log |

<?php
$acl            = $this->viewModel()->getRoot()->getVariable('acl');
$currentUser    = $this->currentUserHelper();

$roleName = $currentUser->getUserTypeId();


$routeUpload       = $this->url('media/files/upload');
$routeDatatable = $this->url('media/files');


if($categories) {

    $allowUpload = $acl->isAllowed($roleName, 'media/files/upload') ? 1 : 0;
} else {
    $allowUpload = false;
}

$allowDelete = $acl->isAllowed($roleName, 'media/files/delete') ? 1 : 0;


$this->headLink()->appendStylesheet($this->basePath('assets/vendors/nprogress/nprogress.css'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/nprogress/nprogress.js'));



$this->headLink()->appendStylesheet($this->basePath('assets/vendors/bootstrap-fileinput/css/fileinput.min.css'));
$this->headLink()->appendStylesheet($this->basePath('assets/vendors/bootstrap-fileinput/themes/explorer-fas/theme.css'));

$this->inlineScript()->appendFile($this->basePath('assets/vendors/bootstrap-fileinput/js/plugins/piexif.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/bootstrap-fileinput/js/plugins/sortable.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/bootstrap-fileinput/js/fileinput.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/bootstrap-fileinput/js/locales/es.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/bootstrap-fileinput/themes/fas/theme.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/bootstrap-fileinput/themes/explorer-fas/theme.js'));



$this->inlineScript()->appendFile($this->basePath('assets/vendors/select2/js/select2.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/select2/js/i18n/es.js'));
$this->headLink()->appendStylesheet($this->basePath('assets/vendors/select2/css/select2.css'));

$this->headLink()->appendStylesheet($this->basePath('assets/vendors/select2-bootstrap5-theme/select2-bootstrap-5-theme.css'));



$this->headLink()->appendStylesheet($this->basePath('assets/vendors/datatables.net-bs5/dataTables.bootstrap5.css'));
$this->headLink()->appendStylesheet($this->basePath('assets/vendors/datatables.net-bs5-responsive/responsive.bootstrap5.css'));

$this->inlineScript()->appendFile($this->basePath('assets/vendors/datatables.net/jquery.dataTables.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/datatables.net-bs5/dataTables.bootstrap5.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/datatables.net-bs5-responsive/dataTables.responsive.min.js'));
$this->inlineScript()->appendFile($this->basePath('assets/vendors/datatables.net-bs5-responsive/responsive.bootstrap5.min.js'));



$this->inlineScript()->captureStart();
echo <<<JS
    jQuery( document ).ready(function( $ ) {

        $.validator.setDefaults({
            debug: true,
            highlight: function(element) {
                $(element).addClass('is-invalid');
            },
            unhighlight: function(element) {
                $(element).removeClass('is-invalid');
            },
            errorElement: 'span',
            errorClass: 'error invalid-feedback',
            errorPlacement: function(error, element) {
                if(element.parent('.btn-file').length) {
                    error.insertAfter(element.parent().parent());
                } else if(element.parent('.toggle').length) {
                    error.insertAfter(element.parent().parent());
                } else {
                    error.insertAfter(element.parent());
                }
            }
        });
    

        var allowDelete = $allowDelete;
        
        var gridTable = $('#gridTable').dataTable( {
            'processing': true,
            'serverSide': true,
            'searching': true,
            'order': [[ 0, 'asc' ]],
            'ordering':  true,
            'ordenable' : true,
            'responsive': true,
            'select' : false,
                'paging': true,
            'pagingType': 'simple_numbers',
      
                'ajax': {
                        'url' : '$routeDatatable',
                'type' : 'get',
                'data': function ( d ) {
                        d.category = $('#form-filter #category').val();
                       
                 },
                'beforeSend': function (request) {
                  NProgress.start();
                },
                'dataFilter': function(response) {
                    var response = jQuery.parseJSON( response );
                    
                    var json                = {};
                    json.recordsTotal       = 0;
                    json.recordsFiltered    = 0;
                    json.data               = [];
                    
                    
                    if(response.success) {
                        json.recordsTotal       = response.data.total;
                        json.recordsFiltered    = response.data.total;
                        json.data               = response.data.items;
                    } else {
                        $.fn.showError(response.data)
                    }
                    
                    return JSON.stringify( json );
                }
                },
            'language' : {
                'sProcessing':     'LABEL_DATATABLE_SPROCESSING',
                'sLengthMenu':     'LABEL_DATATABLE_SLENGTHMENU',
                'sZeroRecords':    'LABEL_DATATABLE_SZERORECORDS',
                'sEmptyTable':     'LABEL_DATATABLE_SEMPTYTABLE',
                'sInfo':           'LABEL_DATATABLE_SINFO',
                'sInfoEmpty':      'LABEL_DATATABLE_SINFOEMPTY',
                'sInfoFiltered':   'LABEL_DATATABLE_SINFOFILTERED',
                'sInfoPostFix':    '',
                'sSearch':         'LABEL_DATATABLE_SSEARCH',
                'sUrl':            '',
                'sInfoThousands':  ',',
                'sLoadingRecords': 'LABEL_DATATABLE_SLOADINGRECORDS',
                'oPaginate': {
                    'sFirst':    'LABEL_DATATABLE_SFIRST',
                    'sLast':     'LABEL_DATATABLE_SLAST',
                    'sNext':     'LABEL_DATATABLE_SNEXT',
                    'sPrevious': 'LABEL_DATATABLE_SPREVIOUS'
                },
                'oAria': {
                    'sSortAscending':  ': LABEL_DATATABLE_SSORTASCENDING',
                    'sSortDescending': ':LABEL_DATATABLE_SSORTDESCENDING'
                },
            },
            'drawCallback': function( settings ) {
                NProgress.done();
                
            },
            'aoColumns': [
                { 'mDataProp': 'name' },
                { 'mDataProp': 'file' },
                { 'mDataProp': 'preview' },
                { 'mDataProp': 'actions' },
            ],
            'columnDefs': [
                {
                    'targets': 0,
                    'className' : 'text-vertical-middle',
                },
                {
                    'targets': 1,
                    'orderable': false,
                    'render' : function ( data, type, row ) {
                        s = '';

                        
                        if(data['type'] == 'image') {
                            s =  data['filename'] + '&nbsp; <i data-href="'  + data['url'] + '"  class="btn-view-image fa fa-external-link-alt"></i>';
                        }
                        else if(data['type'] == 'video') {
                            s = data['filename'] + '&nbsp; <i  data-href="'  + data['url'] + '" class="btn-play-video fa fa-external-link-alt"></i>';
                        }
                        else if(data['type'] == 'audio') {
                            s = data['filename'] + '&nbsp; <i href="#" data-href="'  + data['url'] + '" class="btn-play-audio fa fa-external-link-alt"></i>';
                        }


                        return s;
                    }
                },
                {
                    'targets': 2,
                    'orderable': false,
                    'render' : function ( data, type, row ) {
                        s = '';

                        
                        if(data['filename'] != '') {
                            s = data['filename'] + '&nbsp;<i  href="#" data-href="'  + data['url'] + '" class="btn-view-image fa fa-external-link-alt"></i>';
                        }


                        return s;
                    }
                },
                {
                    'targets': -1,
                    'orderable': false,
                    'render' : function ( data, type, row ) {
                        s = '';
                        
 
                        if(allowDelete && data['link_delete']  ) {
                            s = s + '<button class="btn btn-danger btn-sm btn-delete" data-href="' + data['link_delete']+ '" data-toggle="tooltip" title="LABEL_DELETE"><i class="fa fa-trash"></i> Eliminar</button>&nbsp;';
                        }
                        return s;
                    }
                }
              ],
        });


    var validator = $('#form').validate({
        debug: true,
        onclick: false,
        onkeyup: false,
        ignore: [],
        rules: {
            'name': {
                required: true,
                maxlength: 100,
            },
           'file': {
                required: true,
                extension: 'mov|webm|mp4|mpeg|jpg|jpeg|png|wav|mp3',
                accept: 'video/quicktime, video/webm, video/mp4, video/mpeg,image/jpg,image/jpeg,image/png,audio/wav,audio/mpeg'
            },
        },
        submitHandler: function(form)
        {
            var formdata = false;
            if (window.FormData){
                formdata = new FormData(form); //form[0]);
            }
                

                        $('.info_noticia').prop('disabled', true);
                        NProgress.start();
            $.ajax({
                'dataType'  : 'json',
                'accept'    : 'application/json',
                'method'    : 'post',
                'url'       :  $('#form').attr('action'),
                'data'      :  formdata,
                'processData': false,
                'contentType': false,
            }).done(function(response) {
                                console.log(response)
                                if(response['success']) {
                    $.fn.showSuccess(response['data']);

                    $('#row-upload').hide();
                    $('#row-listing').show();


                    gridTable.api().ajax.reload(null, false);
                } else {
                    validator.resetForm();
                    if(jQuery.type(response['data']) == 'string') {
                        $.fn.showError(response['data']);
                    } else  {
                        $.each(response['data'], function( fieldname, errors ) {
                            $.fn.showFormErrorValidator('#form #' + fieldname, errors);
                        });
                    }
                }
            }).fail(function( jqXHR, textStatus, errorThrown) {
                                

                $.fn.showError(textStatus);
            }).always(function() {

                NProgress.done();
            });
            return false;
        },
        invalidHandler: function(form, validator) {
            
        }
    });


    $('body').on('click', 'button.btn-delete', function(e) { 
        e.preventDefault();
        var action = $(this).data('href');


          swal.fire({
            title: 'LABEL_ARE_YOU_SURE',
            icon: 'question',
            cancelButtonText: 'LABEL_NO',
            showCancelButton: true,
            confirmButtonText: 'LABEL_YES'
          }).then((result) => {
            if (result.isConfirmed) {

                    NProgress.start();
                    $.ajax({
                        'dataType'  : 'json',
                        'accept'    : 'application/json',
                        'method'    : 'post',
                        'url'       :  action,
                    }).done(function(response) {
                        if(response['success']) {
                            $.fn.showSuccess(response['data']);
                            gridTable.api().ajax.reload(null, false);
                        } else {
                            $.fn.showError(response['data']);
                        }
                    }).fail(function( jqXHR, textStatus, errorThrown) {
                        $.fn.showError(textStatus);
                    }).always(function() {
                        NProgress.done();
                    });
                }
           });
        });

    
        $('body').on('click', '.btn-view-image-app', function(e) {
            e.preventDefault();
    
            $('#image-app').attr('src', $(this).data('href'));
            $('#modalPreviewImage').modal('show');
            return false;
        });             

       

        $('body').on('click', 'button.btn-upload', function(e) {
            e.preventDefault();
    
            $('#form #category').val( $('#form-filter #category').val() );
            $('#form #name').val('');
            $('#form #name').val('');
    
            $('#form #file').fileinput('reset');
            $('#form #file').val('');
             
            validator.resetForm();
           
        
            $('#row-listing').hide();
            $('#row-upload').show();            


            return false;
        });



          $('#form #file').fileinput({
                theme: 'fa',
                language: 'es',
                showUpload: false,
                dropZoneEnabled: false,
                maxFileCount: 1,
                allowedFileExtensions: ['mov','webm','mp4','mpeg','jpg','jpeg','png','wav','mp3'],
            });
    

        

        $('body').on('click', 'button.btn-cancel', function(e) {
            e.preventDefault();
            $('#row-upload').hide();
            $('#row-edit').hide();
            $('#row-listing').show();
            
        });
    
        $('body').on('click', 'button.btn-refresh', function(e) {
            e.preventDefault();
            gridTable.api().ajax.reload(null, false);
        });


        $('#form-filter #category').change(function(e) {
            e.preventDefault();

            gridTable.api().ajax.reload(null, false);
        });
        
        $('#form-filter #category').select2({
            theme: 'bootstrap-5',
            width: '100%',
            dropdownParent: $('#modal'),
        });



    $('body').on('click', '.btn-view-image', function(e) {
        e.preventDefault();

        $('#modal-view-image-box').modal('show');

        $('#image-view').attr('src', $(this).data('href'));

        return false;
    });


    $('body').on('click', '.btn-play-video', function(e) {
        e.preventDefault();

        $('#play-video').attr('src', $(this).data('href'));
        $('#modal-play-video-box').modal('show');
        
        return false;
    });

    $('body').on('click', '.btn-play-audio', function(e) {
        e.preventDefault();


        $('#play-audio').attr('src', $(this).data('href'));
        $('#modal-play-audio-box').modal('show');
        
        return false;
    });

    $('body').on('click', '.btn-view-document', function(e) {
        e.preventDefault();


        $('#document-view').attr('src', $(this).data('href'));
        $('#modal-view-document-box').modal('show');
        
        return false;
    });

    });
JS;
$this->inlineScript()->captureEnd();
?>

<div class="container">
        <div class="card" id="row-listing">
                <div class="card-header">
                        <h6 class="card-title">LABEL_MEDIA_FILES</h6>
                </div>
                <div class="card-body">
                        <div class="row">
                         <div class="col-12 mt-3">
                                <form name="form-filter" id="form-filter">
                                        <label form="category" ">LABEL_CATEGORY</label>
                                        <select name="category" id="category" class="form-control">
                                        <?php foreach($categories as $key => $value ) : ?>
                                        <option value="<?php echo $key ?>"><?php echo $value ?></option>
                                        <?php endforeach; ?>
                                        </select>
                                
                                </form>
                         </div>
                    </div>     
                        <div class="row">
                         <div class="col-12 mt-3">
                         
                        <table id="gridTable" class="table   table-bordered">
                                                        <thead>
                                                                <tr>
                                                                        <th>LABEL_NAME</th>
                                                                        <th>LABEL_FILE</th>
                                                                        <th>LABEL_PREVIEW</th>
                                                                        <th>LABEL_ACTIONS</th>
                                                                </tr>
                                                        </thead>
                                                        <tbody>
                                                        </tbody>
                                                </table>
                                                
                         </div>
                     </div>
                </div>
                <div class="card-footer text-right">
                        <button type="button" class="btn btn-info btn-refresh"><i class="fa fa-sync"></i> LABEL_REFRESH </button>
                        <?php if ($allowUpload) : ?>
                        <button type="button" class="btn btn-primary btn-upload"><i class="fa fa-plus"></i> LABEL_ADD </button>
                        <?php endif; ?>
                </div>
        </div>
        
        <div class="card" id="row-upload" style="display:none">
                <div class="card-header">
                        <h6 class="card-title">LABEL_ADD</h6>
                </div>
                <?php
                $form = $this->form;
                $form->setAttributes([
                        'method'  => 'post',
                        'action'  => $routeUpload,
                        'name'    => 'form',
                        'id'      => 'form',
                ]);

                $form->prepare();
                echo $this->form()->openTag($form);
                
                ?>
                <input type="hidden" name="category" id="category" value="" />
                <div class="card-body">
                        <div class="row">
                        <div class="col-12 mt-3">
                        <?php
                        $element = $form->get('name');
    
                        $element->setAttributes(['class' => 'form-control']);
                        $element->setOptions(['label' => 'LABEL_NAME']);
                        echo $this->formLabel($element);
                        echo $this->formText($element);
                        ?>
                        </div>
                </div>
    
             <div class="row">
                        <div class="col-12 mt-3">
                            <?php
                            $element = $form->get('file');
                            $element->setOptions(['label' => 'LABEL_FILE']);
                            $element->setAttributes(['class' => 'form-control']);
                            $element->setAttributes(['accept' => 'video/quicktime, video/webm, video/mp4, video/mpeg, image/jpg ,image/jpeg, image/png, audio/wav, audio/mpeg, application/pdf']);
                            echo $this->formLabel($element);
                            echo $this->formFile($element);
                            ?>
                        </div>
                </div> 
                </div>
                <div class="card-footer text-right">
                     <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
                <button type="button" class="btn btn-light btn-cancel">LABEL_CANCEL</button>
                </div>
                <?php echo $this->form()->closeTag($form); ?>
        </div>
</div>  
        
        



<!-- The Modal -->
<div class="modal" id="modal-play-video-box" >
        <div class="modal-dialog ">
        <div class="modal-content">

            <!-- Modal Header -->
                <div class="modal-header">
                        <h6 class="modal-title">LABEL_PLAY_VIDEO</h6>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="btn-close"></button>
                </div>

            <!-- Modal body --> 
            <div class="modal-body">
                                <video src=""  style="width: 300px; height: auto" controls preload="false" autoplay="false"  poster="" id="play-video"></video>
                </div>
                
            <!-- Modal footer -->
                <div class="modal-footer text-right">
                         <button type="button" class="btn btn-light" data-bs-dismiss="modal">LABEL_CLOSE</button>
                </div>
         </div>         
        </div>
</div>          
                                        
                



<!-- The Modal -->
<div class="modal" id="modal-play-audio-box" >
        <div class="modal-dialog ">
        <div class="modal-content">

            <!-- Modal Header -->
                <div class="modal-header">
                        <h6 class="modal-title">LABEL_PLAY_AUDIO</h6>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="btn-close"></button>
                </div>

            <!-- Modal body --> 
            <div class="modal-body">
                                <audio src="" controls autoplay="false"  id="play-audio" >
                  Your browser does not support the <code>audio</code> element.
                </audio>
                </div>
                
            <!-- Modal footer -->
                <div class="modal-footer text-right">
                         <button type="button" class="btn btn-light" data-bs-dismiss="modal">LABEL_CLOSE</button>
                </div>
         </div>         
        </div>
</div>          
                                        

<!-- The Modal -->
<div class="modal" id="modal-view-image-box">
        <div class="modal-dialog ">
        <div class="modal-content">

            <!-- Modal Header -->
                <div class="modal-header">
                        <h6 class="modal-title">LABEL_IMAGE</h6>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="btn-close"></button>
                </div>

            <!-- Modal body --> 
            <div class="modal-body text-center">
                                <img src="" class="img img-responsive" style="width: 300px; height: auto" id="image-view" />
                </div>
                
            <!-- Modal footer -->
                <div class="modal-footer text-right">
                         <button type="button" class="btn btn-light" data-bs-dismiss="modal">LABEL_CLOSE</button>
                </div>
         </div>         
        </div>
</div>                  


<!-- The Modal -->
<div class="modal" id="modal-view-document-box">
        <div class="modal-dialog ">
        <div class="modal-content">

            <!-- Modal Header -->
                <div class="modal-header">
                        <h6 class="modal-title">LABEL_DOCUMENT</h6>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="btn-close"></button>
                </div>

            <!-- Modal body --> 
            <div class="modal-body text-center">
                <iframe id="document-view" style="width: 450px; height: 300px; scroll: auto">
                
                </iframe>
        </div>
                
            <!-- Modal footer -->
                <div class="modal-footer text-right">
                         <button type="button" class="btn btn-light" data-bs-dismiss="modal">LABEL_CLOSE</button>
                </div>
         </div>         
        </div>
</div>