Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6388 | Rev 6523 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

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

$routeQuestions     = $this->url('my-coach/questions');
$routeQuestionAdd   = $this->url('my-coach/questions/add');
$allowQuestionAdd   = $acl->isAllowed($roleName, 'my-coach/questions/add') ? 1 : 0;




$this->inlineScript()->appendFile($this->basePath('vendors/ckeditor/ckeditor.js'));

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

$this->headLink()->appendStylesheet($this->basePath('vendors/select2/css/select2.min.css'));
$this->headLink()->appendStylesheet($this->basePath('vendors/select2-bootstrap4-theme/select2-bootstrap4.min.css'));
$this->inlineScript()->appendFile($this->basePath('vendors/select2/js/select2.min.js'));

$this->inlineScript()->appendFile($this->basePath('vendors/bootstrap-confirmation/dist/bootstrap-confirmation.js'));
$this->inlineScript()->appendFile($this->basePath('vendors/jsrender/jsrender.min.js'));


$this->inlineScript()->appendFile($this->basePath('vendors/jquery-validation/jquery.validate.js'));
$this->inlineScript()->appendFile($this->basePath('vendors/jquery-validation/additional-methods.js'));
$this->inlineScript()->appendFile($this->basePath('vendors/jquery-validation/localization/messages_es.js'));



$this->inlineScript()->captureStart();
echo <<<JS

jQuery( document ).ready(function( $ ) {
$.fn.reload = function() {

        NProgress.start();
        $.ajax({
            'dataType'  : 'json',
            'accept'    : 'application/json',
            'method'    : 'get',
            'url'       :  '$routeQuestions',
            'data'      : {
                'search' : $('#form-filter #search').val(),
                'category_id' : $('li.my-coach-category-li-selected > a.my-coach-category-a').data('uuid') ,

            } 
        }).done(function(response) {
            if(response['success']) {
                $('#my-coach-record-list').empty();


                if(response['data']['items'].length > 0) {



                    $( "#my-coach-record-list" ).html(
                                $( "#recordTemplate" ).render( response['data']['items'] )
                        );
                } else {
                    var html = '<div class="company-title"><div class="widget widget-jobs"><div class="sd-title">ERROR_NO_RECORD_MATCHED_YOUR_QUERY</div></div></div>';
                    $( "#my-coach-record-list" ).html(html);
                }     


                $('button.btn-my-coach-delete').confirmation({
                    rootSelector: 'button.btn-my-coach-delete',
                    title : 'LABEL_ARE_YOU_SURE',
                    singleton : true,
                    btnOkLabel: 'LABEL_YES',
                    btnCancelLabel: 'LABEL_NO',
                    onConfirm: function(value) {
                        var action = $(this).data('link');
                        NProgress.start();
                        $.ajax({
                            'dataType'  : 'json',
                            'accept'    : 'application/json',
                            'method'    : 'post',
                            'url'       :  action,
                        }).done(function(response) {
                            if(response['success']) {
                                $.fn.showSuccess(response['data']);

                                $.fn.reload();
                            } else {
                                $.fn.showError(response['data']);
                            }
                        }).fail(function( jqXHR, textStatus, errorThrown) {
                            $.fn.showError(textStatus);
                        }).always(function() {
                            NProgress.done();
                        });
                       
                        return false;
                    },
                });
 
            } else {
                $.fn.showError(response['data']);
            } 
        }).fail(function( jqXHR, textStatus, errorThrown) {
            $.fn.showError(textStatus);
        }).always(function() {
            NProgress.done();
        });

    }


    $.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());
            }
        }
    });
    

    $.fn.showFormErrorValidator = function(fieldname, errors) {
        var field = $(fieldname);
        if(field) {
            $(field).addClass('is-invalid');
    

            var error = $('<span id="' + fieldname +'-error" class="error invalid-feedback">' + errors + '</div>');
            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 validatorQuestion = $('#form-question').validate({
        debug: true,
        onclick: false,
        onkeyup: false,
        ignore: [],
        rules: {
            'category_id': {
                required: true,
            },
            'title': {
                required: true,
                maxlength: 128,
            },
            'description': {
                updateCkeditor:function() {
                        CKEDITOR.instances.description_question.updateElement();
                },
                
                required: true
            },
          
        },
        submitHandler: function(form)
        {


                        NProgress.start();
            $.ajax({
                'dataType'  : 'json',
                'accept'    : 'application/json',
                'method'    : 'post',
                'url'       :  $('#form-question').attr('action'),
                'data'      :  $('#form-question').serialize(),

            }).done(function(response) {

                                if(response['success']) {
            
                    $('#modal-question').modal('hide');

                    $.fn.showSuccess(response['data']);
                    $.fn.reload();

            

                } else {
                    validatorQuestion.resetForm();
                    if(jQuery.type(response['data']) == 'string') {
                        $.fn.showError(response['data']);
                    } else  {
                        $.each(response['data'], function( fieldname, errors ) {
                            $.fn.showFormErrorValidator('#form-question #' + fieldname, errors);
                        });
                    }
                }
            }).fail(function( jqXHR, textStatus, errorThrown) {
                $.fn.showError(textStatus);
            }).always(function() {
                NProgress.done();
            });
            return false;
        },
        invalidHandler: function(form, validator) {
            
        }
    });


    $('a.my-coach-category-a').click(function(e) {
        e.preventDefault();


        $('li.my-coach-category-li').removeClass('my-coach-category-li-selected');
        $(this).closest('li.my-coach-category-li').addClass('my-coach-category-li-selected');

        $.fn.reload();
    });

    $('body').on('click', 'button.btn-my-coach-edit', function(e) {
        e.preventDefault();
        var action = $(this).data('link');
        NProgress.start();
        $.ajax({
            'dataType'  : 'json',
            'accept'    : 'application/json',
            'method'    : 'get',
            'url'       :  action,
        }).done(function(response) {
            if(response['success']) {

                $('#form-question').attr('action', action);
                $('#form-question #category_id').val(response['data']['category_id']).trigger('change');
                $('#form-question #title').val(response['data']['title']);
                CKEDITOR.instances.description_question.setData(response['data']['description']);
        
                $('#modal-question').modal('show');
            } else {
                $.fn.showError(response['data']);
            }
        }).fail(function( jqXHR, textStatus, errorThrown) {
            $.fn.showError(textStatus);
        }).always(function() {
            NProgress.done();
        });

    });


    $('#form-filter #search').keyup(function(e) {

        e.preventDefault();

        $.fn.reload();
    })

    $('button.btn-search').click(function(e) {
        e.preventDefault();

        $.fn.reload();
    });

    $('#my-coach-add-question').click(function(e) {
        e.preventDefault();

        $('#form-question').attr('action', '$routeQuestionAdd');
        $('#form-question #title').val('');
        $('#form-question #category_id').val('').trigger('change');
        CKEDITOR.instances.description_question.setData('');
     
        $('#modal-question').modal('show');
    });

    $('#form-question #category_id').select2({
        theme: 'bootstrap4'
    })

    $('button.btn-cancel').click(function(e) {
        e.preventDefault();


        $('#modal-question').modal('hide');
    });

    $.fn.reload()


    CKEDITOR.replace('description_question',{
        toolbar: [
                    { name: 'editing', items: ['Scayt'] },
                    { name: 'links', items: ['Link', 'Unlink'] },
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
                    '/',
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
                    { name: 'styles', items: ['Styles', 'Format'] },
                    { name: 'tools', items: ['Maximize'] }
                ],
                removePlugins: 'elementspath,Anchor',
                heigth: 100
    });
});
JS;

$this->inlineScript()->captureEnd();
$this->headLink()->appendStylesheet('/look-and-field/my-coach.css');
?>

<section class="companies-info container px-0">
    <div class="company-title">
        <h1 class="title mx-auto">LABEL_MY_COACH</h1>
        <?php if($allowQuestionAdd) : ?>
            <h2 class="title cursor-pointer" id="my-coach-add-question">
                LABEL_MY_COACH_QUESTION_ADD
            </h2>
       <?php endif; ?>     

    </div>

    <div class="row gap-3" id="row-list">
        <div class="my-coach-category-list col-12 col-md-3">
            <ul>
                <li class="my-coach-category-li my-coach-category-li-selected">
                    <a class="my-coach-category-a" href="" data-uuid="">LABEL_MY_COACH_CATEGORY_ALL</a>
                </li>
                <?php
                foreach ($categories as $categorie) :
                ?>
                    <li class="my-coach-category-li">
                        <a class="my-coach-category-a" href="" data-uuid="<?php echo $categorie['uuid'] ?>"><?php echo $categorie['name'] ?></a>
                    </li>

                <?php endforeach; ?>
            </ul>
        </div>


        <div class="col px-0">
            <div class="search-box">
                <form id="form-filter" name="form-filter">
                    <input type="text" name="search" id="search" class="form-control"><button class="btn btn-search">LABEL_SEARCH</button>
                </form>
            </div>

            <div id="my-coach-record-list" class="my-coach-record-list mt-3">
            </div>

            <div id="my-coach-pagination">
            </div>

        </div>
    </div>


<!-- The Modal -->
<div class="modal" id="modal-question">
    <div class="modal-dialog  modal-xl">
        <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header">
                <h4 class="modal-title">LABEL_QUESTION</h4>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- Modal body -->
            <div class="modal-body">
                <?php
                $form = $this->formQuestion;
                $form->setAttributes([
                    'method'    => 'post',
                    'name'      => 'form-questiom',
                    'id'        => 'form-question'
                ]);

                $form->prepare();
                echo $this->form()->openTag($form);
                ?>
                <div class="form-group">
                    <?php
                    $element = $form->get('title');
                    $element->setOptions(['label' => 'LABEL_TITLE']);
                    $element->setAttributes(['class' => 'form-control']);

                    echo $this->formLabel($element);
                    echo $this->formText($element);
                    ?>
                </div>
                <div class="form-group">
                    <?php
                    $element = $form->get('description');
                    $element->setOptions(['label' => 'LABEL_DESCRIPTION']);
                    $element->setAttributes(['class' => 'form-control', 'id' => 'description_question']);
                    
                    echo $this->formLabel($element);
                    echo $this->formTextArea($element);
                    ?>
                </div>
                <div class="form-group">
                    <?php
                    
                    $element = $form->get('category_id');
                    $element->setOptions(['label' => 'LABEL_CATEGORY']);
                    
                    echo $this->formLabel($element);
                    echo $this->formSelect($element);
                    ?>
                </div>
                <div class="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>
    </div>
</div>
   


</section>


<script id="recordTemplate" type="text/x-jsrender">
    <div class="my-coach-record-card" >
        <div class="my-coach-record-card-container" >
            <div class="my-coach-record-card-resume" >
                <ul>
                    <li><span id="answers-{{:uuid}}"> {{:answers}} </span> LABEL_MY_COACH_ANSWERS</li>
                    <li><span id="reactions-{{:uuid}}"> {{:reactions}} </span> LABEL_MY_COACH_REACTIONS</li>
                    <li><span id="views-{{:uuid}}"> {{:views}} </span> LABEL_MY_COACH_VIEWS</li>
                </ul>
            </div>
            <div class="my-coach-record-card-content" >
                <h2><a href="{{:link_view}}">{{>title}}</a></h2>
                <p>{{>description}}</p> 
                <div class="d-flex align-items-center justify-content-between">
                    <ul class="d-flex align-items-center  my-coach-record-card-categories">
                        {{for categories}}
                        <li>{{:category}}</li>
                        {{/for}}
                    </ul> 
                    <span>LABEL_MY_COACH_QUESTION {{:added_on}} </span>  
                </div>
            </div>
        </div>
 
        <div class="d-flex align-items-center justify-content-between">
            {{if link_edit }}
            <button data-link="{{>link_edit}}" class="btn-my-coach-edit"> <i class="fa fa-pencil" aria-hidden="true"></i> </button>
            {{/if}}
            {{if link_delete }}
            <button data-link="{{>link_delete}}" class="btn-my-coach-delete"> <i class="fa fa-trash" aria-hidden="true"></i> </button>
            {{/if}}
        </div>
    </div>
</script>