Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
4384 eleazar 1
<?php
2
$acl = $this->viewModel()->getRoot()->getVariable('acl');
3
$currentUser = $this->currentUserHelper();
4
 
5
$roleName = $currentUser->getUserTypeId();
6
 
7
$routeAdd = $this->url('survey/form/add');
8
$routeDatatable = $this->url('survey/form');
9
$routeDashboard = $this->url('dashboard');
10
 
11
$allowAdd = $acl->isAllowed($roleName, 'survey/form/add') ? 1 : 0;
12
$allowEdit = $acl->isAllowed($roleName, 'survey/form/edit') ? 1 : 0;
13
$allowDelete = $acl->isAllowed($roleName, 'survey/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
 
4399 eleazar 62
$status_active = \LeadersLinked\Model\SurveyForm::STATUS_ACTIVE;
63
$status_inactive = \LeadersLinked\Model\SurveyForm::STATUS_INACTIVE;
4384 eleazar 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;
6467 eleazar 89
                    if (this.sections[i].questions[j].type == 'simple' || this.sections[i].questions[j].type == 'multiple' || this.sections[i].questions[j].type == 'rating-open') {
4384 eleazar 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;
6467 eleazar 131
                    if (this.sections[i].questions[j].type == 'simple' || this.sections[i].questions[j].type == 'multiple' || this.sections[i].questions[j].type == 'rating-open') {
4384 eleazar 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
         */
4546 eleazar 152
        this.addSection = function(name, text) {
4384 eleazar 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
         */
4546 eleazar 175
        this.editSection = function(slug, name, text) {
4384 eleazar 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
         */
6467 eleazar 208
        this.addQuestion = function(slug_section, text, type, maxlength, multiline, range) {
4384 eleazar 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,
6467 eleazar 229
                        'range': range,
4384 eleazar 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
         */
6467 eleazar 245
        this.editQuestion = function(slug_section, slug_question, text, type, maxlength, multiline, range) {
4384 eleazar 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
                            }
6467 eleazar 261
                            if (type == 'rating-range') {
262
                                this.sections[i].questions[j].range = range;
263
                            } else {
264
                                this.sections[i].questions[j].range = 0;
265
                            }
4384 eleazar 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
         */
6276 eleazar 312
        this.addOption = function(slug_section, slug_question, text) {
4384 eleazar 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
         */
6276 eleazar 351
        this.editOption = function(slug_section, slug_question, option_slug, text) {
4384 eleazar 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
            {
515
                'mDataProp': 'status'
516
            },
517
            {
518
                'mDataProp': 'actions'
519
            },
520
        ],
521
        'columnDefs': [{
522
                'targets': 0,
523
                'className': 'text-vertical-middle',
524
            },
525
            {
526
                'targets': 1,
527
                'className': 'text-vertical-middle',
528
            },
529
            {
530
                'targets': -2,
531
                'orderable': false,
532
                'className': 'text-center',
533
                'render': function(data, type, row) {
8303 eleazar 534
                    checked = data == 'A' ? ' checked="checked" ' : '';
4384 eleazar 535
                    return '<div class="checkbox checkbox-success">' +
536
                        '<input class="styled" type="checkbox" ' + checked + ' disabled="disabled">' +
537
                        '<label ></label></div>';
538
                }
539
            },
540
            {
541
                'targets': -1,
542
                'orderable': false,
543
                'render': function(data, type, row) {
544
                    s = '';
545
                    if (allowEdit) {
546
                        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;';
547
                    }
548
                    if (allowDelete) {
549
                        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;';
550
                    }
551
                    return s;
552
                }
553
            }
554
        ],
555
    });
556
    /**
557
     * Clicked on edit form
558
     */
559
    $('body').on('click', 'button.btn-edit-form', function(e) {
560
        e.preventDefault();
561
        form_id = $(this).data('id')
562
        var action = $(this).data('href');
563
        $.ajax({
564
            'dataType': 'json',
565
            'accept': 'application/json',
566
            'method': 'get',
567
            'url': action,
568
        }).done(function(response) {
569
            if (response['success']) {
570
                validatorForm.resetForm();
571
                $('#form-main').attr('action', action);
572
                $('#form-main #form-id').val(response['data']['id']),
573
                $('#form-main #form-continue').val('0');
4459 eleazar 574
                $('#form-main #name').val(response['data']['name']),
4384 eleazar 575
 
576
                /*----------Set Ckeditor ------------*/
4466 eleazar 577
                CKEDITOR.instances.description.setData(response['data']['description']);
578
                CKEDITOR.instances.text.setData(response['data']['text']);
4384 eleazar 579
 
4493 eleazar 580
                $('#form-main #status').val(response['data']['status']);
4384 eleazar 581
 
582
                /*-------------Render Sections -------*/
583
 
584
                objFormGenerator.clear();
585
                objFormGenerator.sections = response['data']['content'] || [];
586
                objFormGenerator.render();
587
                renderSectionData(objFormGenerator.sections);
588
 
589
                $('#row-forms').hide();
590
                $('#row-edit').show();
4459 eleazar 591
                $('#form-main #name').focus();
4384 eleazar 592
 
593
            } else {
594
                $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
595
            }
596
        }).fail(function(jqXHR, textStatus, errorThrown) {
597
            $.fn.showError(textStatus);
598
        });
599
    });
600
    $(document).on('click', '[data-type="select_all"]', function() {
601
        if ($('input[name="select_all"]:checked').val() == 'all') {
602
            $('[data-type="select"]').prop('checked', true);
603
            $('[data-action="delete"]').removeClass('hide');
604
        } else {
605
            $('[data-type="select"]').prop('checked', false);
606
            $('[data-action="delete"]').addClass('hide');
607
        }
608
    });
609
 
610
    function getLength() {
611
        return $('[data-type="select"]').length;
612
    }
613
 
614
    function currentSelected() {
615
        var selected = [];
616
        $.each($("input[name='select[]']:checked"), function() {
617
            selected.push($(this).val());
618
        });
619
        return selected;
620
    }
621
    $(document).on('change', "input[name='select[]']", function() {
622
        var c = currentSelected();
623
        var cc = getLength();
624
        if (c.length == cc) {
625
            if ($('[data-action="delete"]').hasClass('hide')) {
626
                $('[data-action="delete"]').removeClass('hide');
627
            }
628
            return true;
629
        } else {
630
            $('input[name="select_all"]').prop('checked', false);
631
        }
632
        if (c.length > 0) {
633
            if ($('[data-action="delete"]').hasClass('hide')) {
634
                $('[data-action="delete"]').removeClass('hide');
635
            }
636
        } else {
637
            if (!$('[data-action="delete"]').hasClass('hide')) {
638
                $('[data-action="delete"]').addClass('hide');
639
            }
640
        }
641
    });
642
    var form3 = $('#form_sample_3');
643
    var error3 = $('.alert-danger', form3);
644
    var success3 = $('.alert-success', form3);
645
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
646
    $("#form-main").on('submit', function() {
647
        for (var instanceName in CKEDITOR.instances) {
648
            CKEDITOR.instances[instanceName].updateElement();
649
        }
650
    })
651
    /**
652
     * Validate rules form
653
     */
654
    var validatorForm = $("#form-main").validate({
655
        ignore: [],
656
        errorClass: 'help-block',
657
        errorElement: 'span',
658
        rules: {
659
            'form-name': {
660
                required: true,
661
                minlength: 2,
662
                maxlength: 50
663
            },
4466 eleazar 664
            'description':{
665
                updateCkeditor:function() {
666
                    CKEDITOR.instances.description.updateElement();
667
                },
4384 eleazar 668
            },
4466 eleazar 669
            'text':{
670
                updateCkeditor:function() {
671
                    CKEDITOR.instances.text.updateElement();
672
                },
4384 eleazar 673
            },
674
            'forrm-status': {
675
                required: true,
676
            },
677
        },
678
        highlight: function(element) {
679
            $(element).closest('.form-group').addClass('has-error');
680
        },
681
        unhighlight: function(element) {
682
            $(element).closest('.form-group').removeClass('has-error');
683
        },
684
        errorPlacement: function(error, element) {
685
            if (element.attr("data-error-container")) {
686
                error.appendTo(element.attr("data-error-container"));
687
            } else {
688
                error.insertAfter(element);
689
            }
690
        },
691
        invalidHandler: function(form, validator) {
692
            if (!validator.numberOfInvalids())
693
                return;
694
            $('html, body').animate({
695
                scrollTop: $(validator.errorList[0].element).offset().top - 100
696
            }, 1000);
697
        },
698
        submitHandler: function(form) {
699
            var error = false;
700
            if (objFormGenerator.sections.length == 0) {
701
                $.fn.showError('ERROR_SECCTIONS');
702
                return false;
703
            } else {
704
                for (i = 0; i < objFormGenerator.sections.length; i++) {
705
                    if (objFormGenerator.sections[i].questions.length == 0) {
706
                        $.fn.showError('ERROR_QUESTIONS'.replace('%s', objFormGenerator.sections[i].name));
707
                        return false;
708
                    }
709
                    for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
710
                        if (objFormGenerator.sections[i].questions[j].type == 'simple' ||
6467 eleazar 711
                            objFormGenerator.sections[i].questions[j].type == 'multiple' ||
712
                            objFormGenerator.sections[i].questions[j].type == 'rating-open') {
4384 eleazar 713
                            var questionNumber = j + 1;
714
                            var numberCorrect = 0;
715
                            if (objFormGenerator.sections[i].questions[j].options.length == 0) {
716
                                $.fn.showError('ERROR_OPTIONS'.replace('%s', objFormGenerator.sections[i].name).replace('%n', questionNumber));
717
                                return false;
718
                            }
719
                            var totalOption = 0;
720
                            var maxOption = 0;
721
                        }
722
                    }
723
                }
724
                var formContinue = parseInt($('#form-main #form-continue').val());
725
                var data = {
4459 eleazar 726
                    'name': $('#form-main #name').val(),
727
                    'description': $('#form-main #description').val(),
728
                    'text': $('#form-main #text').val(),
729
                    'status': $('#form-main #status').val(),
4384 eleazar 730
                    'content': JSON.stringify(objFormGenerator.sections)
731
                }
732
                $.ajax({
733
                    'dataType': 'json',
734
                    'method': 'post',
735
                    'url': $('#form-main').attr('action'),
736
                    'data': data,
737
                }).done(function(response) {
738
                    if (response['success']) {
739
                        $.fn.showSuccess(response['data']);
740
                        if (formContinue == 1) {
741
                            $('#form-main').attr('action',response['action_edit']);
742
                            $('#form-main #form-id').val(response['id']);
8366 eleazar 743
                            $('#form-main #form-continue').val(0);
4384 eleazar 744
                        } else {
745
                            $('#row-edit').hide();
746
                            $('#row-forms').show();
747
                        /*---------- Reset Form -------- */
748
                        $('#form-main')[0].reset();
749
                        /*--------Reset Ckeditor ----------*/
4466 eleazar 750
                        CKEDITOR.instances.text.setData('');
751
                        CKEDITOR.instances.description.setData('');
4384 eleazar 752
                        /*--------Reset Sections ----------*/
753
                        clearSectionData();
754
                        /* ------- Refresh Table -----------*/
755
 
756
                        }
757
                        tableForm.fnDraw();
758
                    } else {
759
                        $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
760
                    }
761
                }).fail(function(jqXHR, textStatus, errorThrown) {
762
                    $.fn.showError(textStatus);
763
                });
764
                return false;
765
            }
766
        }
767
    });
768
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
769
    $("#form-section").on('submit', function() {
770
        for (var instanceName in CKEDITOR.instances) {
771
            CKEDITOR.instances[instanceName].updateElement();
772
        }
773
    })
774
    /**
775
     * Validate rules form section
776
     */
777
    var validatorFormSection = $("#form-section").validate({
778
        ignore: [],
779
        errorClass: 'help-block',
780
        errorElement: 'span',
781
        rules: {
782
            'section-name': {
783
                required: true,
784
                minlength: 2,
785
                maxlength: 50
786
            },
8974 stevensc 787
            'section_text': {
8980 stevensc 788
                updateCkeditor:function() {
789
                    CKEDITOR.instances.section_text.updateElement();
790
                },
4384 eleazar 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(),
8974 stevensc 822
                    $('#form-section #section_text').val(),
4384 eleazar 823
                );
824
            } else {
825
                objFormGenerator.addSection(
826
                    $('#form-section #section-name').val(),
8974 stevensc 827
                    $('#form-section #section_text').val()
4384 eleazar 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
            },
6467 eleazar 860
            'question-range': {
861
                required: true,
862
                number: true,
863
                min: 1
864
            },
4384 eleazar 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(),
6467 eleazar 894
                    $('#form-question #question-multiline').val(),
895
                    $('#form-question #question-range').val()
4384 eleazar 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(),
6467 eleazar 903
                    $('#form-question #question-multiline').val(),
904
                    $('#form-question #question-range').val()
4384 eleazar 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
            }
4546 eleazar 929
 
4384 eleazar 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(),
6276 eleazar 957
                    $('#form-option #option-text').val()
4384 eleazar 958
                );
959
            } else {
960
                objFormGenerator.addOption(
961
                    $('#form-option #option-section').val(),
962
                    $('#form-option #option-question').val(),
6276 eleazar 963
                    $('#form-option #option-text').val()
4384 eleazar 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('');
8974 stevensc 979
        CKEDITOR.instances.section_text.setData('');
4384 eleazar 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);
8974 stevensc 997
                CKEDITOR.instances.section_text.setData(section.text);
4384 eleazar 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();
6467 eleazar 1047
        $('#form-question #question-range').val('10');
1048
        $('#form-question #question-range').parent().hide();
4384 eleazar 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
                        }
6467 eleazar 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
                        }
4384 eleazar 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
                        });
6276 eleazar 1144
 
4384 eleazar 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
    })
4546 eleazar 1223
 
4384 eleazar 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
        }
6467 eleazar 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
        }
4384 eleazar 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');
4459 eleazar 1262
        $('#form-main #name').val('');
1263
        $('#form-main #status').val('$status_inactive');
4466 eleazar 1264
        CKEDITOR.instances.text.setData('');
1265
        CKEDITOR.instances.description.setData('');
4384 eleazar 1266
        $('#row-forms').hide();
1267
        $('#row-edit').show();
4459 eleazar 1268
        $('#form-main #name').focus();
4384 eleazar 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) {
8365 eleazar 1282
        e.preventDefault();
1283
        $('#form-main #form-continue').val('1')
8366 eleazar 1284
        $('#form-main').submit();
4384 eleazar 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
    });
8364 eleazar 1294
 
1295
    const saveData = (action) => {
1296
 
1297
            $.ajax({
1298
                'dataType': 'json',
1299
                'method': 'post',
1300
                'url': $('#form-main').attr('action'),
1301
                'data': {
1302
                    'name': $('#form-main #form-name').val(),
1303
 
1304
                    'status': $('#form-main #form-status').val(),
1305
                    'description': $('#form-main #form-description').val(),
1306
                    'content': JSON.stringify(sections)
1307
                },
1308
            }).done(function(response) {
1309
                if (response['success']) {
1310
                    $.fn.showSuccess(response['data']);
1311
                    if (action == 1) {
1312
                        $('#form-main').attr('action', response['action_edit']);
1313
                    } else {
1314
                        $('#row-form').hide();
1315
                        $('#row-lists').show();
1316
                        /*---------- Reset Form -------- */
1317
                        $('#form-main')[0].reset();
1318
                        /*--------Reset Sections ----------*/
1319
                        sections = [];
1320
                        tableForm.fnDraw();
1321
                    }
1322
                    return;
1323
                } else {
1324
                    $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
1325
                    return;
1326
                }
1327
            });
1328
 
1329
    }
1330
 
4384 eleazar 1331
    /**
1332
     * Modal Settings
1333
     */
1334
    $('#modal-section, #modal-question, #modal-option').modal({
1335
        backdrop: 'static',
1336
        keyboard: false,
1337
        show: false
1338
    });
1339
    /**
1340
     * Get Question type
1341
     */
1342
    const getQuestionTypeBySlug = (slug_section, slug_question) => {
1343
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1344
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1345
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1346
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1347
                        return objFormGenerator.sections[i].questions[j].type;
1348
                    }
1349
                }
1350
            }
1351
        }
1352
    }
1353
    /**
1354
     * Remove Html Tags
1355
     */
1356
    const removeTags = (str) => str.toString().replace( /(<([^>]+)>)/ig, '')
1357
    /**
1358
     * Render Sections data
1359
     */
1360
    const renderSectionData = (data) => $("#rows").html($("#sectionTemplate").render(data, {
1361
        getType: getQuestionTypeBySlug,
1362
        removeTags: removeTags
1363
    }));
1364
 
1365
 
1366
    /**
1367
     * Clear Div Section data
1368
     */
1369
    const clearSectionData = () => $("#rows").html('');
1370
    /**
1371
     * Clicked refresh button
1372
     */
1373
    $('button.btn-refresh').click(function(e) {
1374
        tableForm.fnDraw();
1375
    });
4466 eleazar 1376
 
8974 stevensc 1377
    CKEDITOR.replace('section_text', {
8967 stevensc 1378
        toolbar: [
1379
                    { name: 'editing', items: ['Scayt'] },
1380
                    { name: 'links', items: ['Link', 'Unlink'] },
1381
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1382
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1383
                    '/',
1384
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1385
                    { name: 'styles', items: ['Styles', 'Format'] },
1386
                    { name: 'tools', items: ['Maximize'] }
1387
                ],
1388
                removePlugins: 'elementspath,Anchor',
1389
                heigth: 100
1390
    });
8767 stevensc 1391
    CKEDITOR.replace('description', {
1392
        toolbar: [
1393
                    { name: 'editing', items: ['Scayt'] },
1394
                    { name: 'links', items: ['Link', 'Unlink'] },
1395
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1396
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1397
                    '/',
1398
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1399
                    { name: 'styles', items: ['Styles', 'Format'] },
1400
                    { name: 'tools', items: ['Maximize'] }
1401
                ],
1402
                removePlugins: 'elementspath,Anchor',
1403
                heigth: 100
1404
    });
1405
    CKEDITOR.replace('text', {
1406
        toolbar: [
1407
                    { name: 'editing', items: ['Scayt'] },
1408
                    { name: 'links', items: ['Link', 'Unlink'] },
1409
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1410
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1411
                    '/',
1412
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1413
                    { name: 'styles', items: ['Styles', 'Format'] },
1414
                    { name: 'tools', items: ['Maximize'] }
1415
                ],
1416
                removePlugins: 'elementspath,Anchor',
1417
                heigth: 100
1418
    });
4384 eleazar 1419
 
1420
});
1421
JS;
1422
$this->inlineScript()->captureEnd();
1423
?>
1424
 
1425
<!-- Content Header (Page header) -->
1426
<section class="content-header">
1427
    <div class="container-fluid">
1428
        <div class="row mb-2">
1429
            <div class="col-sm-12">
1430
                <h1>LABEL_SELF_EVALUATION_FORMS</h1>
1431
            </div>
1432
        </div>
1433
    </div>
1434
    <!-- /.container-fluid -->
1435
</section>
1436
<section class="content">
1437
    <div class="container-fluid" id="row-forms">
1438
        <div class="row">
1439
            <div class="col-12">
1440
                <div class="card">
1441
                    <div class="card-body">
1442
                        <table id="gridTable" class="table   table-hover">
1443
                            <thead>
1444
                                <tr>
1445
                                    <th>LABEL_NAME</th>
1446
                                    <th>LABEL_ACTIVE</th>
1447
                                    <th>LABEL_ACTIONS</th>
1448
                                </tr>
1449
                            </thead>
1450
                            <tbody></tbody>
1451
                        </table>
1452
                    </div>
1453
                    <div class="card-footer clearfix">
1454
                        <div style="float:right;">
1455
                            <button type="button" class="btn btn-info btn-refresh"><i class="fa fa-refresh"></i> LABEL_REFRESH </button>
1456
                            <?php if ($allowAdd) : ?>
1457
                                <button type="button" class="btn btn-primary btn-add-form"><i class="fa fa-plus"></i> LABEL_ADD </button>
1458
                            <?php endif; ?>
1459
                        </div>
1460
                    </div>
1461
                </div>
1462
            </div>
1463
        </div>
1464
    </div>
1465
 
1466
    <!-- Create/Edit Form -->
1467
 
1468
    <div class="row" id="row-edit" style="display: none">
1469
        <div class="col-xs-12 col-md-12">
1470
            <form action="#" name="form-main" id="form-main">
1471
                <input type="hidden" name="form-id" id="form-id" value="0" />
1472
                <input type="hidden" name="form-continue" id="form-continue" value="0" />
4450 eleazar 1473
                <?php
1474
                     //$form = $this->formAdd;
1475
                     $form->setAttributes([
1476
                         'method' => 'post',
1477
                         'name' => 'form-main',
1478
                         'action' => $routeAdd,
1479
                         'id' => 'form-main'
1480
                     ]);
1481
                     $form->prepare();
1482
                     echo $this->form()->openTag($form);
1483
                ?>
1484
               <div class="form-group">
1485
                    <?php
1486
                    $element = $form->get('name');
1487
                    $element->setOptions(['label' => 'LABEL_FIRST_NAME']);
1488
                    $element->setAttributes(['class' => 'form-control']);
1489
                    echo $this->formLabel($element);
1490
                    echo $this->formText($element);
1491
                    ?>
4384 eleazar 1492
                </div>
1493
                <div class="form-group">
4450 eleazar 1494
                        <?php
1495
                        $element = $form->get('description');
6269 eleazar 1496
                        $element->setOptions(['label' => 'LABEL_DESCRIPTION']);
4450 eleazar 1497
                        $element->setAttributes(['class' => 'form-control']);
1498
                        echo $this->formLabel($element);
4468 eleazar 1499
                        echo $this->formTextArea($element);
4450 eleazar 1500
                        ?>
4384 eleazar 1501
                </div>
1502
                <div class="form-group">
4450 eleazar 1503
                        <?php
1504
                        $element = $form->get('text');
1505
                        $element->setOptions(['label' => 'LABEL_TEXT']);
1506
                        $element->setAttributes(['class' => 'form-control']);
1507
                        echo $this->formLabel($element);
4468 eleazar 1508
                        echo $this->formTextArea($element);
4450 eleazar 1509
                        ?>
4384 eleazar 1510
                </div>
1511
                <div class="form-group">
4450 eleazar 1512
                        <?php
1513
                        $element = $form->get('status');
1514
                        $element->setAttributes(['class' => 'form-control']);
1515
                        $element->setOptions(['label' => 'LABEL_STATUS']);
1516
                        echo $this->formLabel($element);
1517
                        echo $this->formSelect($element);
1518
                        ?>
1519
                    </div>
4384 eleazar 1520
                <br/>
1521
                <div class="row">
1522
                    <div class="col-xs-12 col-md-12 text-right">
1523
                        <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>
1524
                    </div>
1525
                </div>
1526
                <br />
1527
                <div class="row">
1528
                    <div class="col-xs-12 col-md-12">
1529
                        <div class="panel-group" id="rows"></div>
1530
                    </div>
1531
                </div>
1532
                <div class="form-group">
1533
                    <button type="submit" form="form-main" class="btn btn-info btn-form-save-continue">LABEL_SAVE & LABEL_CONTINUE</button>
1534
                    <button type="button" class="btn btn-primary btn-form-save-close">LABEL_SAVE & LABEL_CLOSE</button>
1535
                    <button type="button" class="btn btn-secondary btn-edit-cancel">LABEL_CANCEL</button>
1536
                </div>
1537
            </form>
1538
        </div>
1539
    </div>
1540
 
1541
    <!-- Create/Edit Form-->
1542
 
1543
    <!-- Section Modal -->
1544
    <div  id="modal-section" class="modal" tabindex="-1" role="dialog">
1545
        <div class="modal-dialog modal-lg" role="document">
1546
            <form action="#" name="form-section" id="form-section">
1547
                <input type="hidden" name="section-slug" id="section-slug" value="" />
1548
                <div class="modal-content">
1549
                    <div class="modal-header">
1550
                        <h4 class="modal-title">LABEL_ADD LABEL_SECTION</h4>
1551
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1552
                            <span aria-hidden="true">&times;</span>
1553
                        </button>
1554
                    </div>
1555
                    <div class="modal-body">
1556
                        <div class="form-group">
1557
                            <label for="section-name">LABEL_FIRST_NAME</label>
1558
                            <input type="text" name="section-name" id="section-name" class="form-control" maxlength="50" value="" />
1559
                        </div>
1560
                        <div class="form-group">
8981 stevensc 1561
                        <div class="form-group">
1562
                            <label for="section_text">LABEL_TEXT</label>
1563
                            <!--  ckeditor -->
1564
                            <textarea  name="section_text" id="section_text" rows="5" class="ckeditor form-control"></textarea>
4384 eleazar 1565
                        </div>
1566
                    </div>
1567
                    <div class="modal-footer">
1568
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1569
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1570
                    </div>
1571
                </div>
1572
            </form>
1573
        </div>
1574
    </div>
1575
    <!-- End Modal Section -->
1576
 
1577
    <!-- Question Modal -->
1578
 
1579
    <div  id="modal-question" class="modal" tabindex="-1" role="dialog">
1580
        <div class="modal-dialog modal-lg" role="document">
1581
            <form action="#" name="form-question" id="form-question">
1582
                <input type="hidden" name="question-section" id="question-section" />
1583
                <input type="hidden" name="question-slug" id="question-slug" />
1584
                <div class="modal-content">
1585
                    <div class="modal-header">
1586
                        <h4 class="modal-title">LABEL_ADD LABEL_QUESTION</h4>
1587
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1588
                            <span aria-hidden="true">&times;</span>
1589
                        </button>
1590
                    </div>
1591
                    <div class="modal-body">
1592
                        <div class="form-group">
1593
                            <label for="question-text">LABEL_TEXT</label>
1594
                            <!--  ckeditor -->
1595
                            <textarea  name="question-text" id="question-text" rows="5" class="ckeditor form-control"></textarea>
1596
                        </div>
1597
                        <div class="form-group">
1598
                            <label for="question-type">LABEL_TYPE</label>
1599
                            <select name="question-type" id="question-type" class="form-control">
1600
                                <option value="open">LABEL_OPEN</option>
1601
                                <option value="simple">Simple</option>
1602
                                <option value="multiple">Multiple</option>
6467 eleazar 1603
 
4384 eleazar 1604
                            </select>
1605
                        </div>
1606
                        <div class="form-group">
1607
                            <label for="question-max-length">LABEL_MAXLENGTH</label>
1608
                            <input type="text" name="question-max-length" id="question-max-length"  class="form-control" />
1609
                        </div>
1610
                        <div class="form-group">
1611
                            <label for="question-multiline">LABEL_MULTI_LINE</label>
1612
                            <select name="question-multiline" id="question-multiline" class="form-control">
1613
                                <option value="1">LABEL_YES</option>
1614
                                <option value="0">LABEL_NOT</option>
1615
                            </select>
1616
                        </div>
6467 eleazar 1617
                        <div class="form-group">
1618
                            <label for="question-range">LABEL_RANGE</label>
1619
                            <select name="question-range" id="question-range" class="form-control">
1620
                                <option value="10">1-10</option>
1621
                                <option value="6">1-6</option>
1622
                                <option value="5">1-5</option>
1623
                            </select>
1624
                        </div>
4384 eleazar 1625
                    </div>
1626
                    <div class="modal-footer">
1627
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1628
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1629
                    </div>
1630
                </div>
1631
            </form>
1632
        </div>
1633
    </div>
1634
 
1635
    <!-- End Modal Question -->
1636
 
1637
    <!-- Modal Options -->
1638
 
1639
    <div  id="modal-option" class="modal" tabindex="-1" role="dialog">
1640
        <div class="modal-dialog modal-lg" role="document">
1641
            <form action="#" name="form-option" id="form-option">
1642
                <input type="hidden" name="option-section" id="option-section" value="" />
1643
                <input type="hidden" name="option-question" id="option-question" value="" />
1644
                <input type="hidden" name="option-slug" id="option-slug" value="" />
1645
                <div class="modal-content">
1646
                    <div class="modal-header">
1647
                        <h4 class="modal-title">LABEL_OPTION</h4>
1648
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1649
                            <span aria-hidden="true">&times;</span>
1650
                        </button>
1651
                    </div>
1652
                    <div class="modal-body">
1653
                        <div class="form-group">
1654
                            <label for="option-text">LABEL_TEXT</label>
1655
                            <!--  ckeditor -->
1656
                            <textarea  name="option-text" id="option-text" rows="5" class="ckeditor form-control"></textarea>
1657
                        </div>
1658
                    </div>
1659
                    <div class="modal-footer">
1660
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1661
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1662
                    </div>
1663
                </div>
1664
            </form>
1665
        </div>
1666
    </div>
1667
 
1668
    <!-- End Modal Options -->
1669
 
1670
    <!---Template Sections --->
1671
    <script id="sectionTemplate" type="text/x-jsrender">
1672
    <div class="panel panel-default" id="panel-{{:slug_section}}">
1673
        <div class="panel-heading">
1674
            <h4 class="panel-title">
1675
                <a class="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent="#panel-{{:slug_section}}" href="#collapse-{{:slug_section}}">
1676
                    <span class="section-name{{:slug_section}}">
1677
                        {{:name}}
1678
                    </span>
1679
                </a>
1680
            </h4>
1681
        </div>
1682
        <div id="collapse-{{:slug_section}}" class="panel-collapse in collapse show">
1683
            <div class="panel-body">
1684
                <div class="table-responsive">
1685
                    <table class="table table-bordered">
1686
                        <thead>
1687
                            <tr>
1688
                                <th style="width: 10%;">LABEL_ELEMENT</th>
1689
                                <th style="width: 50%;">LABEL_TEXT</th>
1690
                                <th style="width: 10%;">LABEL_TYPE</th>
1691
                                <th style="width: 20%;">LABEL_ACTIONS</th>
1692
                            </tr>
1693
                        </thead>
1694
                        <tbody>
1695
                            <tr class="tr-section">
1696
                                <td class="text-left">LABEL_SECTION</td>
1697
                                <td class="text-left">{{:name}}</td>
1698
                                <td></td>
1699
                                <td>
1700
                                    <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>
1701
                                    <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>
1702
                                    <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>
1703
                                </td>
1704
                            </tr>
1705
                            {{for questions}}
1706
                            <tr class="tr-question">
1707
                                <td class="text-left">--LABEL_QUESTION</td>
1708
                                <td class="text-left">
1709
                                    {{:~removeTags(text)}}
1710
                                </td>
1711
                                <td class="text-capitalize">
1712
                                    {{if type == 'open'}} LABEL_OPEN {{/if}}
1713
                                    {{if type == 'simple'}} Simple {{/if}}
1714
                                    {{if type == 'multiple'}} Multiple {{/if}}
1715
                                </td>
1716
                                <td>
1717
                                    <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>
1718
                                    <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>
1719
 
6467 eleazar 1720
                                    {{if type == 'simple' || type == 'rating-open' || type=='multiple' }}
4384 eleazar 1721
                                    <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>
1722
                                    {{/if}}
1723
 
1724
                                </td>
1725
                            </tr>
1726
                            {{for options}}
1727
                            <tr class="tr-option">
1728
                                <td class="text-left">---LABEL_OPTION</td>
1729
                                <td class="text-left">
1730
                                    {{:~removeTags(text)}}
1731
                                </td>
1732
                                <td>
1733
 
1734
                                    {{if ~getType( slug_section, slug_question) == 'multiple' || ~getType( slug_section, slug_question) == 'rating-open'  }}
1735
                                    {{/if}}
1736
                                </td>
1737
                                <td>
1738
                                    <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>
1739
                                    <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>
1740
                                </td>
1741
                            </tr>
1742
                            {{/for}}
1743
                            {{/for}}
1744
                        </tbody>
1745
                    </table>
1746
                </div>
1747
            </div>
1748
        </div>
1749
    </div>
1750
    </script>
1751
 
1752
    <!-- End Template Sections-->
1753
 
1754
</section>