Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12475 | Rev 13335 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7397 eleazar 1
<?php
2
$acl = $this->viewModel()->getRoot()->getVariable('acl');
3
$currentUser = $this->currentUserHelper();
4
 
5
$roleName = $currentUser->getUserTypeId();
6
 
7
$routeAdd = $this->url('organizational-climate/form/add');
8
$routeDatatable = $this->url('organizational-climate/form');
9
$routeDashboard = $this->url('dashboard');
10
 
11
$allowAdd = $acl->isAllowed($roleName, 'organizational-climate/form/add') ? 1 : 0;
12
$allowEdit = $acl->isAllowed($roleName, 'organizational-climate/form/edit') ? 1 : 0;
13
$allowDelete = $acl->isAllowed($roleName, 'organizational-climate/form/delete') ? 1 : 0;
14
 
15
 
16
$this->inlineScript()->appendFile($this->basePath('plugins/ckeditor/ckeditor.js'));
17
 
18
 
19
$this->headLink()->appendStylesheet($this->basePath('plugins/nprogress/nprogress.css'));
20
$this->inlineScript()->appendFile($this->basePath('plugins/nprogress/nprogress.js'));
21
 
22
 
23
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-validation/jquery.validate.js'));
24
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-validation/additional-methods.js'));
25
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-validation/localization/messages_es.js'));
26
 
27
$this->headLink()->appendStylesheet($this->basePath('plugins/datatables-bs4/css/dataTables.bootstrap4.min.css'));
28
$this->headLink()->appendStylesheet($this->basePath('plugins/datatables-responsive/css/responsive.bootstrap4.min.css'));
29
 
30
$this->inlineScript()->appendFile($this->basePath('plugins/datatables/jquery.dataTables.min.js'));
31
$this->inlineScript()->appendFile($this->basePath('plugins/datatables-bs4/js/dataTables.bootstrap4.min.js'));
32
$this->inlineScript()->appendFile($this->basePath('plugins/datatables-responsive/js/dataTables.responsive.min.js'));
33
$this->inlineScript()->appendFile($this->basePath('plugins/datatables-responsive/js/responsive.bootstrap4.min.js'));
34
 
35
 
36
$this->headLink()->appendStylesheet($this->basePath('plugins/select2/css/select2.min.css'));
37
$this->headLink()->appendStylesheet($this->basePath('plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css'));
38
 
39
$this->inlineScript()->appendFile($this->basePath('plugins/select2/js/select2.full.min.js'));
40
 
41
$this->inlineScript()->appendFile($this->basePath('plugins/moment/moment-with-locales.min.js'));
42
$this->headLink()->appendStylesheet($this->basePath('plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.css'));
43
$this->inlineScript()->appendFile($this->basePath('plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js'));
44
 
45
 
46
$this->headLink()->appendStylesheet($this->basePath('plugins/bootstrap4-toggle/css/bootstrap4-toggle.min.css'));
47
$this->inlineScript()->appendFile($this->basePath('plugins/bootstrap4-toggle/js/bootstrap4-toggle.min.js'));
48
 
49
$this->inlineScript()->appendFile($this->basePath('plugins/bootstrap-confirmation/dist/bootstrap-confirmation.js'));
50
$this->headLink()->appendStylesheet($this->basePath('plugins/bootstrap-checkbox/awesome-bootstrap-checkbox.css'));
51
$this->inlineScript()->appendFile($this->basePath('plugins/jquery-input-number/input-number-format.jquery.js'));
52
 
53
// bootbox Alert //
54
$this->inlineScript()->appendFile($this->basePath('plugins/bootbox/bootbox.min.js'));
55
 
56
// JsRender //
57
$this->inlineScript()->appendFile($this->basePath('plugins/jsrender/jsrender.min.js'));
58
 
59
// Page Styles
60
$this->headLink()->appendStylesheet($this->basePath('css/pages/self-evaluation.css'));
61
 
62
$status_active = \LeadersLinked\Model\SurveyForm::STATUS_ACTIVE;
63
$status_inactive = \LeadersLinked\Model\SurveyForm::STATUS_INACTIVE;
64
 
65
$this->inlineScript()->captureStart();
66
echo <<<JS
67
    const classFormGenerator = function() {
68
    this.id = 0,
69
        this.table = '',
70
        this.name = '',
71
        this.text = '',
72
        this.status = 'a',
73
        this.sections = [],
74
        this.clear = function() {
75
            this.sections = [];
76
            this.render();
77
        },
78
        /**
79
         * Render array sections
80
         */
81
        this.renderSection = function(slug_section) {
82
            var s = '';
83
            for (i = 0; i < this.sections.length; i++) {
84
                if (slug_section != this.sections[i].slug_section) {
85
                    continue;
86
                }
87
                for (j = 0; j < this.sections[i].questions.length; j++) {
88
                    this.sections[i].questions[j].position = j;
89
                    if (this.sections[i].questions[j].type == 'simple' || this.sections[i].questions[j].type == 'multiple' || this.sections[i].questions[j].type == 'rating-open') {
90
                        this.sections[i].questions[j].options.sort(function(a, b) {
91
                            if (a.position > b.position) {
92
                                return 1;
93
                            }
94
                            if (a.position < b.position) {
95
                                return -1;
96
                            }
97
                            return 0;
98
                        });
99
                        for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
100
                            this.sections[i].questions[j].options[k].position = j;
101
                        }
102
                    }
103
                }
104
            }
105
            $('[data-toggle="tooltip"]').tooltip();
106
        },
107
        this.render = function() {
108
            this.sections.sort(function(a, b) {
109
                if (a.position > b.position) {
110
                    return 1;
111
                }
112
                if (a.position < b.position) {
113
                    return -1;
114
                }
115
                return 0;
116
            });
117
            var s = '';
118
            for (i = 0; i < this.sections.length; i++) {
119
                this.sections[i].position = i;
120
                this.sections[i].questions.sort(function(a, b) {
121
                    if (a.position > b.position) {
122
                        return 1;
123
                    }
124
                    if (a.position < b.position) {
125
                        return -1;
126
                    }
127
                    return 0;
128
                });
129
                for (j = 0; j < this.sections[i].questions.length; j++) {
130
                    this.sections[i].questions[j].position = j;
131
                    if (this.sections[i].questions[j].type == 'simple' || this.sections[i].questions[j].type == 'multiple' || this.sections[i].questions[j].type == 'rating-open') {
132
                        this.sections[i].questions[j].options.sort(function(a, b) {
133
                            if (a.position > b.position) {
134
                                return 1;
135
                            }
136
                            if (a.position < b.position) {
137
                                return -1;
138
                            }
139
                            return 0;
140
                        });
141
                        for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
142
                            this.sections[i].questions[j].options[k].position = j;
143
                        }
144
                    }
145
                }
146
            }
147
            $('[data-toggle="tooltip"]').tooltip();
148
        },
149
        /**
150
         * Add element to section array
151
         */
152
        this.addSection = function(name, text) {
153
            var d = new Date();
154
            var slug = 'section' + d.getTime();
155
            var position = 0;
156
            $.each(this.sections, function(index, section) {
157
                if (position < section.position) {
158
                    position = section.position;
159
                }
160
            });
161
            position++;
162
            var section = {
163
                'slug_section': slug,
164
                'name': name,
165
                'text': text,
166
                'position': position,
167
                'questions': [],
168
                'status': 0
169
            }
170
            this.sections.push(section);
171
        },
172
         /**
173
         * Edit element to section array
174
         */
175
        this.editSection = function(slug, name, text) {
176
            var renderTable = false;
177
            for (i = 0; i < this.sections.length; i++) {
178
                if (slug == this.sections[i].slug_section) {
179
                    this.sections[i].name = name;
180
                    this.sections[i].text = text;
181
                    renderTable = true;
182
                    break;
183
                }
184
            }
185
            if (renderTable) {
186
                this.renderSection(slug);
187
            }
188
        },
189
         /**
190
         * Remove element to section array
191
         */
192
        this.deleteSection = function(slug) {
193
            var renderTable = false;
194
            for (i = 0; i < this.sections.length; i++) {
195
                if (slug == this.sections[i].slug_section) {
196
                    this.sections.splice(i, 1);
197
                    renderTable = true;
198
                    break;
199
                }
200
            }
201
            if (renderTable) {
202
                $('#panel' + slug).remove();
203
            }
204
        },
205
         /**
206
         * Add element to question array
207
         */
208
        this.addQuestion = function(slug_section, text, type, maxlength, multiline, range) {
209
            var d = new Date();
210
            var slug_question = 'question' + d.getTime();
211
            var position = 0;
212
            var renderTable = false;
213
            for (i = 0; i < this.sections.length; i++) {
214
                if (slug_section == this.sections[i].slug_section) {
215
                    $.each(this.sections[i].questions, function(index, question) {
216
                        if (position < question.position) {
217
                            position = question.position;
218
                        }
219
                    });
220
                    position++;
221
                    var question = {
222
                        'slug_section': slug_section,
223
                        'slug_question': slug_question,
224
                        'text': text,
225
                        'type': type,
226
                        'position': position,
227
                        'maxlength': maxlength,
228
                        'multiline': multiline,
229
                        'range': range,
230
                        'options': [],
231
                        'answer': type=='multiple' ? [] : ''
232
                    }
233
                    this.sections[i].questions.push(question);
234
                    renderTable = true;
235
                    break;
236
                }
237
            }
238
            if (renderTable) {
239
                this.renderSection(slug_section);
240
            }
241
        },
242
         /**
243
         * Add element to question array
244
         */
245
        this.editQuestion = function(slug_section, slug_question, text, type, maxlength, multiline, range) {
246
            var renderTable = false;
247
            for (i = 0; i < this.sections.length; i++) {
248
                if (slug_section == this.sections[i].slug_section) {
249
                    for (j = 0; j < this.sections[i].questions.length; j++) {
250
                        if (slug_question == this.sections[i].questions[j].slug_question) {
251
                            this.sections[i].questions[j].text = text,
252
                                this.sections[i].questions[j].type = type;
253
                            if (type == 'open') {
254
                                this.sections[i].questions[j].maxlength = maxlength;
255
                                this.sections[i].questions[j].multiline = multiline;
256
                                this.sections[i].questions[j].options = [];
257
                            } else {
258
                                this.sections[i].questions[j].maxlength = 0;
259
                                this.sections[i].questions[j].multiline = 0;
260
                            }
261
                            if (type == 'rating-range') {
262
                                this.sections[i].questions[j].range = range;
263
                            } else {
264
                                this.sections[i].questions[j].range = 0;
265
                            }
266
 
267
                            if(type=='multiple'){
268
                                this.sections[i].questions[j].answer = [];
269
                            }else{
270
                                this.sections[i].questions[j].answer = '';
271
                            }
272
 
273
                            renderTable = true;
274
                            break;
275
                        }
276
                    }
277
                }
278
                if (renderTable) {
279
                    break;
280
                }
281
            }
282
            if (renderTable) {
283
                this.renderSection(slug_section);
284
            }
285
        },
286
        /**
287
         * Remove element to question array
288
         */
289
        this.deleteQuestion = function(slug_section, slug_question) {
290
            var renderTable = false;
291
            for (i = 0; i < this.sections.length; i++) {
292
                if (slug_section == this.sections[i].slug_section) {
293
                    for (j = 0; j < this.sections[i].questions.length; j++) {
294
                        if (slug_question == this.sections[i].questions[j].slug_question) {
295
                            this.sections[i].questions.splice(j, 1);
296
                            renderTable = true;
297
                            break;
298
                        }
299
                    }
300
                }
301
                if (renderTable) {
302
                    break;
303
                }
304
            }
305
            if (renderTable) {
306
                this.renderSection(slug_section);
307
            }
308
        },
309
        /**
310
         * Add element to option array
311
         */
312
        this.addOption = function(slug_section, slug_question, text) {
313
            var d = new Date();
314
            var slug_option = 'option' + d.getTime();
315
            var position = 0;
316
            var renderTable = false;
317
            for (i = 0; i < this.sections.length; i++) {
318
                if (slug_section == this.sections[i].slug_section) {
319
                    for (j = 0; j < this.sections[i].questions.length; j++) {
320
                        if (slug_question == this.sections[i].questions[j].slug_question) {
321
                            $.each(this.sections[i].questions[j].options, function(index, option) {
322
                                if (position < option.position) {
323
                                    position = option.position;
324
                                }
325
                            });
326
                            position++;
327
                            var option = {
328
                                'slug_section': slug_section,
329
                                'slug_question': slug_question,
330
                                'slug_option': slug_option,
331
                                'text': text,
332
                                'checked': false
333
                            }
334
                            this.sections[i].questions[j].options.push(option);
335
                            renderTable = true;
336
                            break;
337
                        }
338
                        if (renderTable) {
339
                            break;
340
                        }
341
                    }
342
                }
343
            }
344
            if (renderTable) {
345
                this.renderSection(slug_section);
346
            }
347
        },
348
        /**
349
         * Edit element to option array
350
         */
351
        this.editOption = function(slug_section, slug_question, option_slug, text) {
352
            var renderTable = false;
353
            for (i = 0; i < this.sections.length; i++) {
354
                if (slug_section == this.sections[i].slug_section) {
355
                    for (j = 0; j < this.sections[i].questions.length; j++) {
356
                        if (slug_question == this.sections[i].questions[j].slug_question) {
357
                            for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
358
                                if (option_slug == this.sections[i].questions[j].options[k].slug_option) {
359
                                    this.sections[i].questions[j].options[k].text = text;
360
                                    renderTable = true;
361
                                    break;
362
                                }
363
                            }
364
                        }
365
                        if (renderTable) {
366
                            break;
367
                        }
368
                    }
369
                }
370
                if (renderTable) {
371
                    break;
372
                }
373
            }
374
            if (renderTable) {
375
                this.renderSection(slug_section);
376
            }
377
        },
378
        /**
379
         * Remove element to option array
380
         */
381
        this.deleteOption = function(slug_section, slug_question, option_slug) {
382
            var renderTable = false;
383
            for (i = 0; i < this.sections.length; i++) {
384
                if (slug_section == this.sections[i].slug_section) {
385
                    for (j = 0; j < this.sections[i].questions.length; j++) {
386
                        if (slug_question == this.sections[i].questions[j].slug_question) {
387
                            for (k = 0; k < this.sections[i].questions[j].options.length; k++) {
388
                                if (option_slug == this.sections[i].questions[j].options[k].slug_option) {
389
                                    this.sections[i].questions[j].options.splice(k, 1);
390
                                    renderTable = true;
391
                                    break;
392
                                }
393
                            }
394
                        }
395
                        if (renderTable) {
396
                            break;
397
                        }
398
                    }
399
                }
400
                if (renderTable) {
401
                    break;
402
                }
403
            }
404
            if (renderTable) {
405
                this.renderSection(slug_section);
406
            }
407
        }
408
}
409
 
410
function htmlEntities(str) {
411
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
412
}
413
jQuery(document).ready(function($) {
414
    var objFormGenerator = new classFormGenerator();
415
    objFormGenerator.render();
416
    var allowEdit = $allowEdit;
417
    var allowDelete = $allowDelete;
418
    /**
419
     * Get rows and set data table
420
     */
421
    var tableForm = $('#gridTable').dataTable({
422
        'processing': true,
423
        'serverSide': true,
424
        'searching': true,
425
        'order': [
426
            [0, 'asc']
427
        ],
428
        'ordering': true,
429
        'ordenable': true,
430
        'responsive': true,
431
        'select': false,
432
        'paging': true,
433
        'pagingType': 'simple_numbers',
434
        'ajax': {
435
            'url': '$routeDatatable',
436
            'type': 'get',
437
            'beforeSend': function(request) {
438
                NProgress.start();
439
            },
440
            'dataFilter': function(response) {
441
                var response = jQuery.parseJSON(response);
442
                var json = {};
443
                json.recordsTotal = 0;
444
                json.recordsFiltered = 0;
445
                json.data = [];
446
                if (response.success) {
447
                    json.recordsTotal = response.data.total;
448
                    json.recordsFiltered = response.data.total;
449
                    json.data = response.data.items;
450
                } else {
451
                    $.fn.showError(response.data)
452
                }
453
                return JSON.stringify(json);
454
            }
455
        },
456
        'language': {
457
            'sProcessing': 'LABEL_DATATABLE_SPROCESSING',
458
            'sLengthMenu': 'LABEL_DATATABLE_SLENGTHMENU',
459
            'sZeroRecords': 'LABEL_DATATABLE_SZERORECORDS',
460
            'sEmptyTable': 'LABEL_DATATABLE_SEMPTYTABLE',
461
            'sInfo': 'LABEL_DATATABLE_SINFO',
462
            'sInfoEmpty': 'LABEL_DATATABLE_SINFOEMPTY',
463
            'sInfoFiltered': 'LABEL_DATATABLE_SINFOFILTERED',
464
            'sInfoPostFix': '',
465
            'sSearch': 'LABEL_DATATABLE_SSEARCH',
466
            'sUrl': '',
467
            'sInfoThousands': ',',
468
            'sLoadingRecords': 'LABEL_DATATABLE_SLOADINGRECORDS',
469
            'oPaginate': {
470
                'sFirst': 'LABEL_DATATABLE_SFIRST',
471
                'sLast': 'LABEL_DATATABLE_SLAST',
472
                'sNext': 'LABEL_DATATABLE_SNEXT',
473
                'sPrevious': 'LABEL_DATATABLE_SPREVIOUS'
474
            },
475
            'oAria': {
476
                'sSortAscending': ': LABEL_DATATABLE_SSORTASCENDING',
477
                'sSortDescending': ':LABEL_DATATABLE_SSORTDESCENDING'
478
            },
479
        },
480
        'drawCallback': function(settings) {
481
            NProgress.done();
482
            $('button.btn-delete').confirmation({
483
                rootSelector: 'button.btn-delete',
484
                title: 'LABEL_ARE_YOU_SURE',
485
                singleton: true,
486
                btnOkLabel: 'LABEL_YES',
487
                btnCancelLabel: 'LABEL_NO',
488
                onConfirm: function(value) {
489
                    action = $(this).data('href');
490
                    NProgress.start();
491
                    $.ajax({
492
                        'dataType': 'json',
493
                        'accept': 'application/json',
494
                        'method': 'post',
495
                        'url': action,
496
                    }).done(function(response) {
497
                        if (response['success']) {
498
                            $.fn.showSuccess(response['data']);
499
                            tableForm.fnDraw();
500
                        } else {
501
                            $.fn.showError(response['data']);
502
                        }
503
                    }).fail(function(jqXHR, textStatus, errorThrown) {
504
                        $.fn.showError(textStatus);
505
                    }).always(function() {
506
                        NProgress.done();
507
                    });
508
                },
509
            });
510
        },
511
        'aoColumns': [{
512
                'mDataProp': 'name'
513
            },
514
            {
11221 eleazar 515
                'mDataProp': 'description'
516
            },
517
            {
7397 eleazar 518
                'mDataProp': 'status'
519
            },
520
            {
521
                'mDataProp': 'actions'
522
            },
523
        ],
524
        'columnDefs': [{
525
                'targets': 0,
526
                'className': 'text-vertical-middle',
527
            },
528
            {
529
                'targets': 1,
530
                'className': 'text-vertical-middle',
531
            },
532
            {
11220 eleazar 533
                'targets': -2,
7397 eleazar 534
                'orderable': false,
535
                'className': 'text-center',
536
                'render': function(data, type, row) {
11218 eleazar 537
                    checked = data == 'A' ? ' checked="checked" ' : '';
7397 eleazar 538
                    return '<div class="checkbox checkbox-success">' +
539
                        '<input class="styled" type="checkbox" ' + checked + ' disabled="disabled">' +
540
                        '<label ></label></div>';
541
                }
542
            },
543
            {
544
                'targets': -1,
545
                'orderable': false,
546
                'render': function(data, type, row) {
547
                    s = '';
548
                    if (allowEdit) {
549
                        s = s + '<button class="btn btn-primary btn-edit-form" data-href="' + data['link_edit'] + '" data-toggle="tooltip" title="LABEL_EDIT"><i class="fa fa-pencil"></i> LABEL_EDIT </button>&nbsp;';
550
                    }
551
                    if (allowDelete) {
552
                        s = s + '<button class="btn btn-danger btn-delete" data-href="' + data['link_delete'] + '" data-toggle="tooltip" title="LABEL_DELETE"><i class="fa fa-trash"></i> LABEL_DELETE </button>&nbsp;';
553
                    }
554
                    return s;
555
                }
556
            }
557
        ],
558
    });
559
    /**
560
     * Clicked on edit form
561
     */
562
    $('body').on('click', 'button.btn-edit-form', function(e) {
563
        e.preventDefault();
564
        form_id = $(this).data('id')
565
        var action = $(this).data('href');
566
        $.ajax({
567
            'dataType': 'json',
568
            'accept': 'application/json',
569
            'method': 'get',
570
            'url': action,
571
        }).done(function(response) {
572
            if (response['success']) {
573
                validatorForm.resetForm();
574
                $('#form-main').attr('action', action);
575
                $('#form-main #form-id').val(response['data']['id']),
576
                $('#form-main #form-continue').val('0');
577
                $('#form-main #name').val(response['data']['name']),
578
 
579
                /*----------Set Ckeditor ------------*/
580
                CKEDITOR.instances.description.setData(response['data']['description']);
581
                CKEDITOR.instances.text.setData(response['data']['text']);
582
 
583
                $('#form-main #status').val(response['data']['status']);
584
 
585
                /*-------------Render Sections -------*/
586
 
587
                objFormGenerator.clear();
588
                objFormGenerator.sections = response['data']['content'] || [];
589
                objFormGenerator.render();
590
                renderSectionData(objFormGenerator.sections);
591
 
592
                $('#row-forms').hide();
593
                $('#row-edit').show();
594
                $('#form-main #name').focus();
595
 
596
            } else {
597
                $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
598
            }
599
        }).fail(function(jqXHR, textStatus, errorThrown) {
600
            $.fn.showError(textStatus);
601
        });
602
    });
603
    $(document).on('click', '[data-type="select_all"]', function() {
604
        if ($('input[name="select_all"]:checked').val() == 'all') {
605
            $('[data-type="select"]').prop('checked', true);
606
            $('[data-action="delete"]').removeClass('hide');
607
        } else {
608
            $('[data-type="select"]').prop('checked', false);
609
            $('[data-action="delete"]').addClass('hide');
610
        }
611
    });
612
 
613
    function getLength() {
614
        return $('[data-type="select"]').length;
615
    }
616
 
617
    function currentSelected() {
618
        var selected = [];
619
        $.each($("input[name='select[]']:checked"), function() {
620
            selected.push($(this).val());
621
        });
622
        return selected;
623
    }
624
    $(document).on('change', "input[name='select[]']", function() {
625
        var c = currentSelected();
626
        var cc = getLength();
627
        if (c.length == cc) {
628
            if ($('[data-action="delete"]').hasClass('hide')) {
629
                $('[data-action="delete"]').removeClass('hide');
630
            }
631
            return true;
632
        } else {
633
            $('input[name="select_all"]').prop('checked', false);
634
        }
635
        if (c.length > 0) {
636
            if ($('[data-action="delete"]').hasClass('hide')) {
637
                $('[data-action="delete"]').removeClass('hide');
638
            }
639
        } else {
640
            if (!$('[data-action="delete"]').hasClass('hide')) {
641
                $('[data-action="delete"]').addClass('hide');
642
            }
643
        }
644
    });
645
    var form3 = $('#form_sample_3');
646
    var error3 = $('.alert-danger', form3);
647
    var success3 = $('.alert-success', form3);
648
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
649
    $("#form-main").on('submit', function() {
650
        for (var instanceName in CKEDITOR.instances) {
651
            CKEDITOR.instances[instanceName].updateElement();
652
        }
653
    })
654
    /**
655
     * Validate rules form
656
     */
657
    var validatorForm = $("#form-main").validate({
658
        ignore: [],
659
        errorClass: 'help-block',
660
        errorElement: 'span',
661
        rules: {
662
            'form-name': {
663
                required: true,
664
                minlength: 2,
665
                maxlength: 50
666
            },
667
            'description':{
668
                updateCkeditor:function() {
669
                    CKEDITOR.instances.description.updateElement();
670
                },
671
            },
672
            'text':{
673
                updateCkeditor:function() {
674
                    CKEDITOR.instances.text.updateElement();
675
                },
676
            },
677
            'forrm-status': {
678
                required: true,
679
            },
680
        },
681
        highlight: function(element) {
682
            $(element).closest('.form-group').addClass('has-error');
683
        },
684
        unhighlight: function(element) {
685
            $(element).closest('.form-group').removeClass('has-error');
686
        },
687
        errorPlacement: function(error, element) {
688
            if (element.attr("data-error-container")) {
689
                error.appendTo(element.attr("data-error-container"));
690
            } else {
691
                error.insertAfter(element);
692
            }
693
        },
694
        invalidHandler: function(form, validator) {
695
            if (!validator.numberOfInvalids())
696
                return;
697
            $('html, body').animate({
698
                scrollTop: $(validator.errorList[0].element).offset().top - 100
699
            }, 1000);
700
        },
701
        submitHandler: function(form) {
702
            var error = false;
703
            if (objFormGenerator.sections.length == 0) {
704
                $.fn.showError('ERROR_SECCTIONS');
705
                return false;
706
            } else {
707
                for (i = 0; i < objFormGenerator.sections.length; i++) {
708
                    if (objFormGenerator.sections[i].questions.length == 0) {
709
                        $.fn.showError('ERROR_QUESTIONS'.replace('%s', objFormGenerator.sections[i].name));
710
                        return false;
711
                    }
712
                    for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
713
                        if (objFormGenerator.sections[i].questions[j].type == 'simple' ||
714
                            objFormGenerator.sections[i].questions[j].type == 'multiple' ||
715
                            objFormGenerator.sections[i].questions[j].type == 'rating-open') {
716
                            var questionNumber = j + 1;
717
                            var numberCorrect = 0;
718
                            if (objFormGenerator.sections[i].questions[j].options.length == 0) {
719
                                $.fn.showError('ERROR_OPTIONS'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
720
                                return false;
721
                            }
722
                            var totalOption = 0;
723
                            var maxOption = 0;
724
                        }
725
                    }
726
                }
727
                var formContinue = parseInt($('#form-main #form-continue').val());
728
                var data = {
729
                    'name': $('#form-main #name').val(),
730
                    'description': $('#form-main #description').val(),
731
                    'text': $('#form-main #text').val(),
732
                    'status': $('#form-main #status').val(),
733
                    'content': JSON.stringify(objFormGenerator.sections)
734
                }
735
                $.ajax({
736
                    'dataType': 'json',
737
                    'method': 'post',
738
                    'url': $('#form-main').attr('action'),
739
                    'data': data,
740
                }).done(function(response) {
741
                    if (response['success']) {
742
                        $.fn.showSuccess(response['data']);
743
                        if (formContinue == 1) {
744
                            $('#form-main').attr('action',response['action_edit']);
745
                            $('#form-main #form-id').val(response['id']);
746
                            $('#form-main #form-continue').val(0);
747
                        } else {
748
                            $('#row-edit').hide();
749
                            $('#row-forms').show();
750
                        /*---------- Reset Form -------- */
751
                        $('#form-main')[0].reset();
752
                        /*--------Reset Ckeditor ----------*/
753
                        CKEDITOR.instances.text.setData('');
754
                        CKEDITOR.instances.description.setData('');
755
                        /*--------Reset Sections ----------*/
756
                        clearSectionData();
757
                        /* ------- Refresh Table -----------*/
758
 
759
                        }
760
                        tableForm.fnDraw();
761
                    } else {
762
                        $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
763
                    }
764
                }).fail(function(jqXHR, textStatus, errorThrown) {
765
                    $.fn.showError(textStatus);
766
                });
767
                return false;
768
            }
769
        }
770
    });
771
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
772
    $("#form-section").on('submit', function() {
773
        for (var instanceName in CKEDITOR.instances) {
774
            CKEDITOR.instances[instanceName].updateElement();
775
        }
776
    })
777
    /**
778
     * Validate rules form section
779
     */
780
    var validatorFormSection = $("#form-section").validate({
781
        ignore: [],
782
        errorClass: 'help-block',
783
        errorElement: 'span',
784
        rules: {
785
            'section-name': {
786
                required: true,
787
                minlength: 2,
788
                maxlength: 50
789
            },
790
            'section-text': {
791
                required: false,
792
            },
793
        },
794
        highlight: function(element) {
795
            $(element).closest('.form-group').addClass('has-error');
796
        },
797
        unhighlight: function(element) {
798
            $(element).closest('.form-group').removeClass('has-error');
799
        },
800
        errorPlacement: function(error, element) {
801
            if (element.attr("data-error-container")) {
802
                error.appendTo(element.attr("data-error-container"));
803
            } else {
804
                error.insertAfter(element);
805
            }
806
        },
807
        invalidHandler: function(form, validator) {
808
            if (!validator.numberOfInvalids())
809
                return;
810
            $('html, body').animate({
811
                scrollTop: $(validator.errorList[0].element).offset().top - 100
812
            }, 1000);
813
        },
814
        submitHandler: function(form) {
815
            // do other things for a valid form
816
            //form.submit();
817
            var slug = $('#form-section #section-slug').val();
818
            if (slug) {
819
                objFormGenerator.editSection(
820
                    $('#form-section #section-slug').val(),
821
                    $('#form-section #section-name').val(),
822
                    $('#form-section #section-text').val(),
823
                );
824
            } else {
825
                objFormGenerator.addSection(
826
                    $('#form-section #section-name').val(),
827
                    $('#form-section #section-text').val()
828
                );
829
            }
830
            renderSectionData(objFormGenerator.sections);
831
            $('#modal-section').modal('hide');
832
            return false;
833
        }
834
    });
835
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
836
    $("#form-question").on('submit', function() {
837
        for (var instanceName in CKEDITOR.instances) {
838
            CKEDITOR.instances[instanceName].updateElement();
839
        }
840
    })
841
    /**
842
     * Validate rules form Questions
843
     */
844
    var validatorFormQuestion = $("#form-question").validate({
845
        ignore: [],
846
        errorClass: 'help-block',
847
        errorElement: 'span',
848
        rules: {
849
            'question-text': {
850
                required: true,
851
            },
852
            'question-type': {
853
                required: true,
854
            },
855
            'question-max-length': {
856
                required: true,
857
                digits: true,
858
                min: 0
859
            },
860
            'question-range': {
861
                required: true,
862
                number: true,
863
                min: 1
864
            },
865
        },
866
        highlight: function(element) {
867
            $(element).closest('.form-group').addClass('has-error');
868
        },
869
        unhighlight: function(element) {
870
            $(element).closest('.form-group').removeClass('has-error');
871
        },
872
        errorPlacement: function(error, element) {
873
            if (element.attr("data-error-container")) {
874
                error.appendTo(element.attr("data-error-container"));
875
            } else {
876
                error.insertAfter(element);
877
            }
878
        },
879
        invalidHandler: function(form, validator) {
880
            if (!validator.numberOfInvalids())
881
                return;
882
            $('html, body').animate({
883
                scrollTop: $(validator.errorList[0].element).offset().top - 100
884
            }, 1000);
885
        },
886
        submitHandler: function(form) {
887
            if ($('#form-question #question-slug').val()) {
888
                objFormGenerator.editQuestion(
889
                    $('#form-question #question-section').val(),
890
                    $('#form-question #question-slug').val(),
891
                    $('#form-question #question-text').val(),
892
                    $('#form-question #question-type').val(),
893
                    $('#form-question #question-max-length').val(),
894
                    $('#form-question #question-multiline').val(),
895
                    $('#form-question #question-range').val()
896
                );
897
            } else {
898
                objFormGenerator.addQuestion(
899
                    $('#form-question #question-section').val(),
900
                    $('#form-question #question-text').val(),
901
                    $('#form-question #question-type').val(),
902
                    $('#form-question #question-max-length').val(),
903
                    $('#form-question #question-multiline').val(),
904
                    $('#form-question #question-range').val()
905
                );
906
            }
907
            renderSectionData(objFormGenerator.sections);
908
            $('#modal-question').modal('hide');
909
            return false;
910
        }
911
    });
912
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
913
    $("#form-option").on('submit', function() {
914
        for (var instanceName in CKEDITOR.instances) {
915
            CKEDITOR.instances[instanceName].updateElement();
916
        }
917
    })
918
    /**
919
     * Validate rules form options
920
     */
921
    var validatorFormOption = $("#form-option").validate({
922
        ignore: [],
923
        errorClass: 'help-block',
924
        errorElement: 'span',
925
        rules: {
926
            'option-text': {
927
                required: true,
928
            }
929
 
930
        },
931
        highlight: function(element) {
932
            $(element).closest('.form-group').addClass('has-error');
933
        },
934
        unhighlight: function(element) {
935
            $(element).closest('.form-group').removeClass('has-error');
936
        },
937
        errorPlacement: function(error, element) {
938
            if (element.attr("data-error-container")) {
939
                error.appendTo(element.attr("data-error-container"));
940
            } else {
941
                error.insertAfter(element);
942
            }
943
        },
944
        invalidHandler: function(form, validator) {
945
            if (!validator.numberOfInvalids())
946
                return;
947
            $('html, body').animate({
948
                scrollTop: $(validator.errorList[0].element).offset().top - 100
949
            }, 1000);
950
        },
951
        submitHandler: function(form) {
952
            if ($('#form-option #option-slug').val()) {
953
                objFormGenerator.editOption(
954
                    $('#form-option #option-section').val(),
955
                    $('#form-option #option-question').val(),
956
                    $('#form-option #option-slug').val(),
957
                    $('#form-option #option-text').val()
958
                );
959
            } else {
960
                objFormGenerator.addOption(
961
                    $('#form-option #option-section').val(),
962
                    $('#form-option #option-question').val(),
963
                    $('#form-option #option-text').val()
964
                );
965
            }
966
            renderSectionData(objFormGenerator.sections);
967
            $('#modal-option').modal('hide');
968
            return false;
969
        }
970
    });
971
    /**
972
     * Clicked on add new section
973
     */
974
    $('body').on('click', 'button[id="btn-add-section"]', function(e) {
975
        e.preventDefault();
976
        validatorFormSection.resetForm();
977
        $('#form-section #section-slug').val('');
978
        $('#form-section #section-name').val('');
979
        CKEDITOR.instances['section-text'].setData('');
980
        $('#modal-section h4[class="modal-title"]').html('LABEL_ADD LABEL_SECTION');
981
        $('#modal-section').modal('show');
982
    });
983
    /**
984
     * Clicked on edit section
985
     */
986
    $('body').on('click', 'button.btn-edit-section', function(e) {
987
        e.preventDefault();
988
        var slug = $(this).data('section');
989
        var section;
990
        var showForm = false;
991
        for (i = 0; i < objFormGenerator.sections.length; i++) {
992
            section = objFormGenerator.sections[i];
993
            if (slug == section.slug_section) {
994
                validatorFormSection.resetForm();
995
                $('#form-section #section-slug').val(section.slug_section);
996
                $('#form-section #section-name').val(section.name);
997
                CKEDITOR.instances['section-text'].setData(section.text);
998
                showForm = true;
999
                break;
1000
            }
1001
        }
1002
        if (showForm) {
1003
            $('#modal-section h4[class="modal-title"]').html('LABEL_EDIT LABEL_SECTION');
1004
            $('#modal-section').modal('show');
1005
        }
1006
    });
1007
    /**
1008
     * Clicked on remove section
1009
     */
1010
    $('body').on('click', 'button.btn-delete-section', function(e) {
1011
        e.preventDefault();
1012
        var slug = $(this).data('section');
1013
        bootbox.confirm({
1014
            title: "LABEL_DELETE LABEL_SECTION",
1015
            message: "LABEL_QUESTION_DELETE",
1016
            buttons: {
1017
                cancel: {
1018
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1019
                },
1020
                confirm: {
1021
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1022
                }
1023
            },
1024
            callback: function(result) {
1025
                if (result) {
1026
                    objFormGenerator.deleteSection(slug);
1027
                    renderSectionData(objFormGenerator.sections);
1028
                }
1029
            }
1030
        });
1031
    });
1032
    /**
1033
     * Clicked add new question
1034
     */
1035
    $('body').on('click', 'button.btn-add-question', function(e) {
1036
        e.preventDefault();
1037
        validatorFormQuestion.resetForm();
1038
        var slug = $(this).data('section');
1039
        $('#form-question #question-section').val(slug);
1040
        $('#form-question #question-slug').val('');
1041
        CKEDITOR.instances['question-text'].setData('');
1042
        $('#form-question #question-type').val($('#form-question #question-type option:first').val());
1043
        $('#form-question #question-max-length').val('0');
1044
        $('#form-question #question-max-length').parent().show();
1045
        $('#form-question #question-multiline').val('0');
1046
        $('#form-question #question-multiline').parent().show();
1047
        $('#form-question #question-range').val('10');
1048
        $('#form-question #question-range').parent().hide();
1049
        $('#modal-question h4[class="modal-title"]').html('LABEL_ADD LABEL_QUESTION');
1050
        $('#modal-question').modal('show');
1051
    });
1052
    /**
1053
     * Clicked edit question
1054
     */
1055
    $('body').on('click', 'button.btn-edit-question', function(e) {
1056
        e.preventDefault();
1057
        var slug_section = $(this).data('section');
1058
        var slug = $(this).data('question');
1059
        var showForm = false;
1060
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1061
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1062
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1063
                    if (slug == objFormGenerator.sections[i].questions[j].slug_question) {
1064
                        validatorFormQuestion.resetForm();
1065
                        $('#form-question #question-section').val(objFormGenerator.sections[i].slug_section);
1066
                        $('#form-question #question-slug').val(objFormGenerator.sections[i].questions[j].slug_question);
1067
                        CKEDITOR.instances['question-text'].setData(objFormGenerator.sections[i].questions[j].text);
1068
                        $('#form-question #question-type').val(objFormGenerator.sections[i].questions[j].type);
1069
                        if (objFormGenerator.sections[i].questions[j].type == 'open') {
1070
                            $('#form-question #question-max-length').val(objFormGenerator.sections[i].questions[j].maxlength);
1071
                            $('#form-question #question-max-length').parent().show();
1072
                            $('#form-question #question-multiline').val(objFormGenerator.sections[i].questions[j].multiline);
1073
                            $('#form-question #question-multiline').parent().show();
1074
                        } else {
1075
                            $('#form-question #question-max-length').val('0');
1076
                            $('#form-question #question-max-length').parent().hide();
1077
                            $('#form-question #question-multiline').val('0');
1078
                            $('#form-question #question-multiline').parent().hide();
1079
                        }
1080
                        if (objFormGenerator.sections[i].questions[j].type == 'rating-range') {
1081
                            $('#form-question #question-range').val(objFormGenerator.sections[i].questions[j].range);
1082
                            $('#form-question #question-range').parent().show();
1083
                        } else {
1084
                            $('#form-question #question-range').val('10');
1085
                            $('#form-question #question-range').parent().hide();
1086
                        }
1087
                        showForm = true;
1088
                        break;
1089
                    }
1090
                }
1091
                break;
1092
            }
1093
        }
1094
        if (showForm) {
1095
            $('#modal-question h4[class="modal-title"]').html('LABEL_EDIT LABEL_QUESTION');
1096
            $('#modal-question').modal('show');
1097
        }
1098
    });
1099
    /**
1100
     * Clicked remove question
1101
     */
1102
    $('body').on('click', 'button.btn-delete-question', function(e) {
1103
        e.preventDefault();
1104
        var slug_section = $(this).data('section');
1105
        var slug = $(this).data('question');
1106
        bootbox.confirm({
1107
            title: "LABEL_DELETE LABEL_QUESTION",
1108
            message: "LABEL_QUESTION_DELETE",
1109
            buttons: {
1110
                cancel: {
1111
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1112
                },
1113
                confirm: {
1114
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1115
                }
1116
            },
1117
            callback: function(result) {
1118
                if (result) {
1119
                    objFormGenerator.deleteQuestion(slug_section, slug);
1120
                    renderSectionData(objFormGenerator.sections);
1121
                }
1122
            }
1123
        });
1124
    });
1125
    /**
1126
     * Clicked add new Option
1127
     */
1128
    $('body').on('click', 'button.btn-add-option', function(e) {
1129
        e.preventDefault();
1130
        var slug_section = $(this).data('section');
1131
        var slug_question = $(this).data('question');
1132
        var showForm = false;
1133
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1134
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1135
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1136
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1137
                        validatorFormOption.resetForm();
1138
                        $('#form-option #option-section').val(slug_section);
1139
                        $('#form-option #option-question').val(slug_question);
1140
                        $('#form-option #option-slug').val('');
1141
                        CKEDITOR.instances['option-text'].setData('', function() {
1142
                            editor.focus();
1143
                        });
1144
 
1145
                        renderSectionData(objFormGenerator.sections);
1146
                        $('#modal-option h4[class="modal-title"]').html('LABEL_ADD LABEL_OPTION');
1147
                        $('#modal-option').modal('show');
1148
                        return true;
1149
                    }
1150
                }
1151
            }
1152
        }
1153
    });
1154
    /**
1155
     * Clicked edit option
1156
     */
1157
    $('body').on('click', 'button.btn-edit-option', function(e) {
1158
        e.preventDefault();
1159
        var slug_section = $(this).data('section');
1160
        var slug_question = $(this).data('question');
1161
        var slug = $(this).data('slug');
1162
        var showForm = false;
1163
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1164
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1165
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1166
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1167
                        for (k = 0; k < objFormGenerator.sections[i].questions[j].options.length; k++) {
1168
                            if (slug == objFormGenerator.sections[i].questions[j].options[k].slug_option) {
1169
                                validatorFormOption.resetForm();
1170
                                $('#form-option #option-section').val(objFormGenerator.sections[i].slug_section);
1171
                                $('#form-option #option-question').val(objFormGenerator.sections[i].questions[j].slug_question);
1172
                                $('#form-option #option-slug').val(objFormGenerator.sections[i].questions[j].options[k].slug_option);
1173
                                CKEDITOR.instances['option-text'].setData(objFormGenerator.sections[i].questions[j].options[k].text,
1174
                                    function() {
1175
                                        editor.focus();
1176
                                    });
1177
                                showForm = true;
1178
                                break;
1179
                            }
1180
                        }
1181
                    }
1182
                    if (showForm) {
1183
                        break;
1184
                    }
1185
                }
1186
            }
1187
            if (showForm) {
1188
                break;
1189
            }
1190
        }
1191
        if (showForm) {
1192
            $('#modal-option h4[class="modal-title"]').html('LABEL_EDIT LABEL_OPTION');
1193
            $('#modal-option').modal('show');
1194
        }
1195
    });
1196
    /**
1197
     * Clicked remove option
1198
     */
1199
    $('body').on('click', 'button.btn-delete-option', function(e) {
1200
        e.preventDefault();
1201
        var slug_section = $(this).data('section');
1202
        var slug_question = $(this).data('question');
1203
        var slug = $(this).data('slug');
1204
        bootbox.confirm({
1205
            title: "LABEL_DELETE LABEL_OPTION",
1206
            message: "LABEL_QUESTION_DELETE",
1207
            buttons: {
1208
                cancel: {
1209
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1210
                },
1211
                confirm: {
1212
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1213
                }
1214
            },
1215
            callback: function(result) {
1216
                if (result) {
1217
                    objFormGenerator.deleteOption(slug_section, slug_question, slug);
1218
                    renderSectionData(objFormGenerator.sections);
1219
                }
1220
            }
1221
        });
1222
    })
1223
 
1224
    /**
1225
     * Format input number (Form Question)
1226
     */
1227
    $('#form-question #question-max-length').inputNumberFormat({
1228
        'decimal': 0
1229
    });
1230
    /**
1231
     * Detect when updating question status and hide/Show options
1232
     */
1233
    $('#form-question #question-type').change(function(e) {
1234
        e.preventDefault();
1235
        if ($('#form-question #question-type').val() == 'open') {
1236
            $('#form-question #question-max-length').parent().show();
1237
            $('#form-question #question-multiline').parent().show();
1238
        } else {
1239
            $('#form-question #question-max-length').val('0');
1240
            $('#form-question #question-max-length').parent().hide();
1241
            $('#form-question #question-multiline').val('0');
1242
            $('#form-question #question-multiline').parent().hide();
1243
        }
1244
        $('#form-question #question-range').val('10');
1245
        if ($('#form-question #question-type').val() == 'rating-range') {
1246
            $('#form-question #question-range').parent().show();
1247
        } else {
1248
            $('#form-question #question-range').parent().hide();
1249
        }
1250
    });
1251
    /**
1252
     * Clicked new Form
1253
     */
1254
    $('button.btn-add-form').click(function(e) {
1255
        e.preventDefault();
1256
        objFormGenerator.clear();
1257
        objFormGenerator.render();
1258
        validatorForm.resetForm();
1259
        clearSectionData();
1260
        $('#form-main').attr('action', '$routeAdd');
1261
        $('#form-main #form-continue').val('0');
1262
        $('#form-main #name').val('');
1263
        $('#form-main #status').val('$status_inactive');
1264
        CKEDITOR.instances.text.setData('');
1265
        CKEDITOR.instances.description.setData('');
1266
        $('#row-forms').hide();
1267
        $('#row-edit').show();
1268
        $('#form-main #name').focus();
1269
    });
1270
    /**
1271
     * Clicked cancel new/edit Form
1272
     */
1273
    $('button.btn-edit-cancel').click(function(e) {
1274
        e.preventDefault();
1275
        $('#row-edit').hide();
1276
        $('#row-forms').show();
1277
    });
1278
    /**
1279
     * Clicked save and continue new Form
1280
     */
1281
    $('button.btn-form-save-continue').click(function(e) {
1282
        e.preventDefault();
1283
        $('#form-main #form-continue').val('1')
1284
        $('#form-main').submit();
1285
    });
1286
    /**
1287
     * Clicked save and close new/edit Form
1288
     */
1289
    $('button.btn-form-save-close').click(function(e) {
1290
        e.preventDefault();
1291
        $('#form-main #form-continue').val('0')
1292
        $('#form-main').submit();
1293
    });
1294
    /**
1295
     * Modal Settings
1296
     */
1297
    $('#modal-section, #modal-question, #modal-option').modal({
1298
        backdrop: 'static',
1299
        keyboard: false,
1300
        show: false
1301
    });
1302
    /**
1303
     * Get Question type
1304
     */
1305
    const getQuestionTypeBySlug = (slug_section, slug_question) => {
1306
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1307
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1308
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1309
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1310
                        return objFormGenerator.sections[i].questions[j].type;
1311
                    }
1312
                }
1313
            }
1314
        }
1315
    }
1316
    /**
1317
     * Remove Html Tags
1318
     */
1319
    const removeTags = (str) => str.toString().replace( /(<([^>]+)>)/ig, '')
1320
    /**
1321
     * Render Sections data
1322
     */
1323
    const renderSectionData = (data) => $("#rows").html($("#sectionTemplate").render(data, {
1324
        getType: getQuestionTypeBySlug,
1325
        removeTags: removeTags
1326
    }));
1327
 
1328
 
1329
    /**
1330
     * Clear Div Section data
1331
     */
1332
    const clearSectionData = () => $("#rows").html('');
1333
    /**
1334
     * Clicked refresh button
1335
     */
1336
    $('button.btn-refresh').click(function(e) {
1337
        tableForm.fnDraw();
1338
    });
1339
 
11214 stevensc 1340
    CKEDITOR.replace('description', {
1341
				toolbar: [
1342
					{ name: 'editing', items: ['Scayt'] },
1343
					{ name: 'links', items: ['Link', 'Unlink'] },
1344
					{ name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1345
					{ name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1346
					'/',
1347
					{ name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1348
					{ name: 'styles', items: ['Styles', 'Format'] },
1349
					{ name: 'tools', items: ['Maximize'] }
1350
				],
1351
				removePlugins: 'elementspath,Anchor',
1352
				heigth: 100
1353
			});
1354
    CKEDITOR.replace('text', {
1355
				toolbar: [
1356
					{ name: 'editing', items: ['Scayt'] },
1357
					{ name: 'links', items: ['Link', 'Unlink'] },
1358
					{ name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1359
					{ name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1360
					'/',
1361
					{ name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1362
					{ name: 'styles', items: ['Styles', 'Format'] },
1363
					{ name: 'tools', items: ['Maximize'] }
1364
				],
1365
				removePlugins: 'elementspath,Anchor',
1366
				heigth: 100
1367
			});
7397 eleazar 1368
 
1369
});
1370
JS;
1371
$this->inlineScript()->captureEnd();
12475 stevensc 1372
 
1373
$js = <<<JS
1374
	const backendVariables= {
1375
		dashboard_link: "$routeDashboard",
1376
        add_link: "$routeAdd",
1377
        table_link: "$routeDatatable",
1378
        permisions: {
1379
            allowAdd: $allowAdd,
1380
            allowEdit: $allowEdit,
1381
            allowDelete: $allowDelete
1382
        }
1383
	}
1384
JS;
1385
 
1386
$this->inlineScript()->appendScript($js);
1387
$this->headLink()->appendStylesheet($this->basePath('/react-bundles/organizational-climate/form/main.css'));
1388
$this->inlineScript()->appendFile('/react-bundles/organizational-climate/form/organizationalFormBundle.js');
7397 eleazar 1389
?>
1390
 
12491 stevensc 1391
<div id="organizational_climate-form">
12475 stevensc 1392
</div>
1393
 
7397 eleazar 1394
<!-- Content Header (Page header) -->
1395
<section class="content-header">
1396
    <div class="container-fluid">
1397
        <div class="row mb-2">
1398
            <div class="col-sm-12">
1399
                <h1>LABEL_SELF_EVALUATION_FORMS</h1>
1400
            </div>
1401
        </div>
1402
    </div>
1403
    <!-- /.container-fluid -->
1404
</section>
1405
<section class="content">
1406
    <div class="container-fluid" id="row-forms">
1407
        <div class="row">
1408
            <div class="col-12">
1409
                <div class="card">
1410
                    <div class="card-body">
1411
                        <table id="gridTable" class="table   table-hover">
1412
                            <thead>
1413
                                <tr>
1414
                                    <th>LABEL_NAME</th>
11221 eleazar 1415
                                    <th>LABEL_DESCRIPTION</th>
7397 eleazar 1416
                                    <th>LABEL_ACTIVE</th>
1417
                                    <th>LABEL_ACTIONS</th>
1418
                                </tr>
1419
                            </thead>
1420
                            <tbody></tbody>
1421
                        </table>
1422
                    </div>
1423
                    <div class="card-footer clearfix">
1424
                        <div style="float:right;">
1425
                            <button type="button" class="btn btn-info btn-refresh"><i class="fa fa-refresh"></i> LABEL_REFRESH </button>
1426
                            <?php if ($allowAdd) : ?>
1427
                                <button type="button" class="btn btn-primary btn-add-form"><i class="fa fa-plus"></i> LABEL_ADD </button>
1428
                            <?php endif; ?>
1429
                        </div>
1430
                    </div>
1431
                </div>
1432
            </div>
1433
        </div>
1434
    </div>
1435
 
1436
    <!-- Create/Edit Form -->
1437
 
1438
    <div class="row" id="row-edit" style="display: none">
1439
        <div class="col-xs-12 col-md-12">
1440
            <form action="#" name="form-main" id="form-main">
1441
                <input type="hidden" name="form-id" id="form-id" value="0" />
1442
                <input type="hidden" name="form-continue" id="form-continue" value="0" />
1443
                <?php
11214 stevensc 1444
                //$form = $this->formAdd;
1445
                $form->setAttributes([
1446
                    'method' => 'post',
1447
                    'name' => 'form-main',
1448
                    'action' => $routeAdd,
1449
                    'id' => 'form-main'
1450
                ]);
1451
                $form->prepare();
1452
                echo $this->form()->openTag($form);
7397 eleazar 1453
                ?>
11214 stevensc 1454
                <div class="form-group">
7397 eleazar 1455
                    <?php
1456
                    $element = $form->get('name');
1457
                    $element->setOptions(['label' => 'LABEL_FIRST_NAME']);
1458
                    $element->setAttributes(['class' => 'form-control']);
1459
                    echo $this->formLabel($element);
1460
                    echo $this->formText($element);
1461
                    ?>
1462
                </div>
1463
                <div class="form-group">
11214 stevensc 1464
                    <?php
1465
                    $element = $form->get('description');
1466
                    $element->setOptions(['label' => 'LABEL_DESCRIPTION']);
1467
                    $element->setAttributes(['class' => 'form-control']);
1468
                    echo $this->formLabel($element);
1469
                    echo $this->formTextArea($element);
1470
                    ?>
7397 eleazar 1471
                </div>
1472
                <div class="form-group">
11214 stevensc 1473
                    <?php
1474
                    $element = $form->get('text');
1475
                    $element->setOptions(['label' => 'LABEL_TEXT']);
1476
                    $element->setAttributes(['class' => 'form-control']);
1477
                    echo $this->formLabel($element);
1478
                    echo $this->formTextArea($element);
1479
                    ?>
7397 eleazar 1480
                </div>
1481
                <div class="form-group">
11214 stevensc 1482
                    <?php
1483
                    $element = $form->get('status');
1484
                    $element->setAttributes(['class' => 'form-control']);
1485
                    $element->setOptions(['label' => 'LABEL_STATUS']);
1486
                    echo $this->formLabel($element);
1487
                    echo $this->formSelect($element);
1488
                    ?>
1489
                </div>
1490
                <br />
7397 eleazar 1491
                <div class="row">
1492
                    <div class="col-xs-12 col-md-12 text-right">
1493
                        <button class="btn btn-primary" id="btn-add-section" data-toggle="tooltip" title="LABEL_ADD LABEL_SECTION"><i class="fa fa-plus" aria-hidden="true"></i> LABEL_ADD LABEL_SECTION</button>
1494
                    </div>
1495
                </div>
1496
                <br />
1497
                <div class="row">
1498
                    <div class="col-xs-12 col-md-12">
1499
                        <div class="panel-group" id="rows"></div>
1500
                    </div>
1501
                </div>
1502
                <div class="form-group">
1503
                    <button type="submit" form="form-main" class="btn btn-info btn-form-save-continue">LABEL_SAVE & LABEL_CONTINUE</button>
1504
                    <button type="button" class="btn btn-primary btn-form-save-close">LABEL_SAVE & LABEL_CLOSE</button>
1505
                    <button type="button" class="btn btn-secondary btn-edit-cancel">LABEL_CANCEL</button>
1506
                </div>
1507
            </form>
1508
        </div>
1509
    </div>
1510
 
1511
    <!-- Create/Edit Form-->
1512
 
1513
    <!-- Section Modal -->
11214 stevensc 1514
    <div id="modal-section" class="modal" tabindex="-1" role="dialog">
7397 eleazar 1515
        <div class="modal-dialog modal-lg" role="document">
1516
            <form action="#" name="form-section" id="form-section">
1517
                <input type="hidden" name="section-slug" id="section-slug" value="" />
1518
                <div class="modal-content">
1519
                    <div class="modal-header">
1520
                        <h4 class="modal-title">LABEL_ADD LABEL_SECTION</h4>
1521
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1522
                            <span aria-hidden="true">&times;</span>
1523
                        </button>
1524
                    </div>
1525
                    <div class="modal-body">
1526
                        <div class="form-group">
1527
                            <label for="section-name">LABEL_FIRST_NAME</label>
1528
                            <input type="text" name="section-name" id="section-name" class="form-control" maxlength="50" value="" />
1529
                        </div>
1530
                        <div class="form-group">
1531
                            <label for="section-text">LABEL_TEXT</label>
1532
                            <!--  ckeditor -->
11214 stevensc 1533
                            <textarea name="section-text" id="section-text" rows="5" class="ckeditor form-control"></textarea>
7397 eleazar 1534
                        </div>
1535
                    </div>
1536
                    <div class="modal-footer">
1537
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1538
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1539
                    </div>
1540
                </div>
1541
            </form>
1542
        </div>
1543
    </div>
1544
    <!-- End Modal Section -->
1545
 
1546
    <!-- Question Modal -->
1547
 
11214 stevensc 1548
    <div id="modal-question" class="modal" tabindex="-1" role="dialog">
7397 eleazar 1549
        <div class="modal-dialog modal-lg" role="document">
1550
            <form action="#" name="form-question" id="form-question">
1551
                <input type="hidden" name="question-section" id="question-section" />
1552
                <input type="hidden" name="question-slug" id="question-slug" />
1553
                <div class="modal-content">
1554
                    <div class="modal-header">
1555
                        <h4 class="modal-title">LABEL_ADD LABEL_QUESTION</h4>
1556
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1557
                            <span aria-hidden="true">&times;</span>
1558
                        </button>
1559
                    </div>
1560
                    <div class="modal-body">
1561
                        <div class="form-group">
1562
                            <label for="question-text">LABEL_TEXT</label>
1563
                            <!--  ckeditor -->
11214 stevensc 1564
                            <textarea name="question-text" id="question-text" rows="5" class="ckeditor form-control"></textarea>
7397 eleazar 1565
                        </div>
1566
                        <div class="form-group">
1567
                            <label for="question-type">LABEL_TYPE</label>
1568
                            <select name="question-type" id="question-type" class="form-control">
1569
                                <option value="open">LABEL_OPEN</option>
1570
                                <option value="simple">Simple</option>
1571
                                <option value="multiple">Multiple</option>
11214 stevensc 1572
 
7397 eleazar 1573
                            </select>
1574
                        </div>
1575
                        <div class="form-group">
1576
                            <label for="question-max-length">LABEL_MAXLENGTH</label>
11214 stevensc 1577
                            <input type="text" name="question-max-length" id="question-max-length" class="form-control" />
7397 eleazar 1578
                        </div>
1579
                        <div class="form-group">
1580
                            <label for="question-multiline">LABEL_MULTI_LINE</label>
1581
                            <select name="question-multiline" id="question-multiline" class="form-control">
1582
                                <option value="1">LABEL_YES</option>
1583
                                <option value="0">LABEL_NOT</option>
1584
                            </select>
1585
                        </div>
1586
                        <div class="form-group">
1587
                            <label for="question-range">LABEL_RANGE</label>
1588
                            <select name="question-range" id="question-range" class="form-control">
1589
                                <option value="10">1-10</option>
1590
                                <option value="6">1-6</option>
1591
                                <option value="5">1-5</option>
1592
                            </select>
1593
                        </div>
1594
                    </div>
1595
                    <div class="modal-footer">
1596
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1597
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1598
                    </div>
1599
                </div>
1600
            </form>
1601
        </div>
1602
    </div>
1603
 
1604
    <!-- End Modal Question -->
1605
 
1606
    <!-- Modal Options -->
1607
 
11214 stevensc 1608
    <div id="modal-option" class="modal" tabindex="-1" role="dialog">
7397 eleazar 1609
        <div class="modal-dialog modal-lg" role="document">
1610
            <form action="#" name="form-option" id="form-option">
1611
                <input type="hidden" name="option-section" id="option-section" value="" />
1612
                <input type="hidden" name="option-question" id="option-question" value="" />
1613
                <input type="hidden" name="option-slug" id="option-slug" value="" />
1614
                <div class="modal-content">
1615
                    <div class="modal-header">
1616
                        <h4 class="modal-title">LABEL_OPTION</h4>
1617
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1618
                            <span aria-hidden="true">&times;</span>
1619
                        </button>
1620
                    </div>
1621
                    <div class="modal-body">
1622
                        <div class="form-group">
1623
                            <label for="option-text">LABEL_TEXT</label>
1624
                            <!--  ckeditor -->
11214 stevensc 1625
                            <textarea name="option-text" id="option-text" rows="5" class="ckeditor form-control"></textarea>
7397 eleazar 1626
                        </div>
1627
                    </div>
1628
                    <div class="modal-footer">
1629
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1630
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1631
                    </div>
1632
                </div>
1633
            </form>
1634
        </div>
1635
    </div>
1636
 
1637
    <!-- End Modal Options -->
1638
 
1639
    <!---Template Sections --->
1640
    <script id="sectionTemplate" type="text/x-jsrender">
11214 stevensc 1641
        <div class="panel panel-default" id="panel-{{:slug_section}}">
7397 eleazar 1642
        <div class="panel-heading">
1643
            <h4 class="panel-title">
1644
                <a class="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent="#panel-{{:slug_section}}" href="#collapse-{{:slug_section}}">
1645
                    <span class="section-name{{:slug_section}}">
1646
                        {{:name}}
1647
                    </span>
1648
                </a>
1649
            </h4>
1650
        </div>
1651
        <div id="collapse-{{:slug_section}}" class="panel-collapse in collapse show">
1652
            <div class="panel-body">
1653
                <div class="table-responsive">
1654
                    <table class="table table-bordered">
1655
                        <thead>
1656
                            <tr>
1657
                                <th style="width: 10%;">LABEL_ELEMENT</th>
1658
                                <th style="width: 50%;">LABEL_TEXT</th>
1659
                                <th style="width: 10%;">LABEL_TYPE</th>
1660
                                <th style="width: 20%;">LABEL_ACTIONS</th>
1661
                            </tr>
1662
                        </thead>
1663
                        <tbody>
1664
                            <tr class="tr-section">
1665
                                <td class="text-left">LABEL_SECTION</td>
1666
                                <td class="text-left">{{:name}}</td>
1667
                                <td></td>
1668
                                <td>
1669
                                    <button class="btn btn-default btn-edit-section" data-section="{{:slug_section}}" data-toggle="tooltip"  data-original-title="LABEL_EDIT LABEL_SECTION"><i class="fa fa-edit" aria-hidden="true"></i> LABEL_EDIT LABEL_SECTION </button>
1670
                                    <button class="btn btn-default btn-delete-section" data-section="{{:slug_section}}" data-toggle="tooltip"  data-original-title="LABEL_DELETE LABEL_SECTION"><i class="fa fa-ban" aria-hidden="true"></i> LABEL_DELETE LABEL_SECTION </button>
1671
                                    <button class="btn btn-default btn-add-question" data-section="{{:slug_section}}" data-toggle="tooltip"  data-original-title="LABEL_ADD  LABEL_QUESTION"><i class="fa fa-plus" aria-hidden="true"></i> LABEL_ADD  LABEL_QUESTION </button>
1672
                                </td>
1673
                            </tr>
1674
                            {{for questions}}
1675
                            <tr class="tr-question">
1676
                                <td class="text-left">--LABEL_QUESTION</td>
1677
                                <td class="text-left">
1678
                                    {{:~removeTags(text)}}
1679
                                </td>
1680
                                <td class="text-capitalize">
1681
                                    {{if type == 'open'}} LABEL_OPEN {{/if}}
1682
                                    {{if type == 'simple'}} Simple {{/if}}
1683
                                    {{if type == 'multiple'}} Multiple {{/if}}
1684
                                </td>
1685
                                <td>
1686
                                    <button class="btn btn-default btn-edit-question" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-toggle="tooltip"  data-original-title="LABEL_EDIT LABEL_QUESTION"><i class="fa fa-edit" aria-hidden="true"></i> LABEL_EDIT LABEL_QUESTION</button>
1687
                                    <button class="btn btn-default btn-delete-question" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-toggle="tooltip"  data-original-title="LABEL_DELETE LABEL_QUESTION"><i class="fa fa-ban" aria-hidden="true"></i> LABEL_DELETE LABEL_QUESTION</button>
1688
 
1689
                                    {{if type == 'simple' || type == 'rating-open' || type=='multiple' }}
1690
                                    <button class="btn btn-default btn-add-option" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-toggle="tooltip"  data-original-title="LABEL_ADD  LABEL_OPTION"><i class="fa fa-plus" aria-hidden="true"></i> LABEL_ADD  LABEL_OPTION</button>
1691
                                    {{/if}}
1692
 
1693
                                </td>
1694
                            </tr>
1695
                            {{for options}}
1696
                            <tr class="tr-option">
1697
                                <td class="text-left">---LABEL_OPTION</td>
1698
                                <td class="text-left">
1699
                                    {{:~removeTags(text)}}
1700
                                </td>
1701
                                <td>
1702
 
1703
                                    {{if ~getType( slug_section, slug_question) == 'multiple' || ~getType( slug_section, slug_question) == 'rating-open'  }}
1704
                                    {{/if}}
1705
                                </td>
1706
                                <td>
1707
                                    <button class="btn btn-default btn-edit-option" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-slug="{{:slug_option}}" data-toggle="tooltip"  data-original-title="LABEL_EDIT LABEL_OPTION"><i class="fa fa-edit" aria-hidden="true"></i> LABEL_EDIT LABEL_OPTION</button>
1708
                                    <button class="btn btn-default btn-delete-option" data-section="{{:slug_section}}" data-question="{{:slug_question}}" data-slug="{{:slug_option}}" data-toggle="tooltip"  data-original-title="LABEL_DELETE LABEL_OPTION"><i class="fa fa-ban" aria-hidden="true"></i> LABEL_DELETE LABEL_OPTION</button>
1709
                                </td>
1710
                            </tr>
1711
                            {{/for}}
1712
                            {{/for}}
1713
                        </tbody>
1714
                    </table>
1715
                </div>
1716
            </div>
1717
        </div>
1718
    </div>
1719
    </script>
1720
 
1721
    <!-- End Template Sections-->
1722
 
1723
</section>