Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 8964 | Rev 8966 | 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
            },
787
            'section-text': {
788
                required: false,
789
            },
790
        },
791
        highlight: function(element) {
792
            $(element).closest('.form-group').addClass('has-error');
793
        },
794
        unhighlight: function(element) {
795
            $(element).closest('.form-group').removeClass('has-error');
796
        },
797
        errorPlacement: function(error, element) {
798
            if (element.attr("data-error-container")) {
799
                error.appendTo(element.attr("data-error-container"));
800
            } else {
801
                error.insertAfter(element);
802
            }
803
        },
804
        invalidHandler: function(form, validator) {
805
            if (!validator.numberOfInvalids())
806
                return;
807
            $('html, body').animate({
808
                scrollTop: $(validator.errorList[0].element).offset().top - 100
809
            }, 1000);
810
        },
811
        submitHandler: function(form) {
812
            // do other things for a valid form
813
            //form.submit();
814
            var slug = $('#form-section #section-slug').val();
815
            if (slug) {
816
                objFormGenerator.editSection(
817
                    $('#form-section #section-slug').val(),
818
                    $('#form-section #section-name').val(),
819
                    $('#form-section #section-text').val(),
820
                );
821
            } else {
822
                objFormGenerator.addSection(
823
                    $('#form-section #section-name').val(),
4546 eleazar 824
                    $('#form-section #section-text').val()
4384 eleazar 825
                );
826
            }
827
            renderSectionData(objFormGenerator.sections);
828
            $('#modal-section').modal('hide');
829
            return false;
830
        }
831
    });
832
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
833
    $("#form-question").on('submit', function() {
834
        for (var instanceName in CKEDITOR.instances) {
835
            CKEDITOR.instances[instanceName].updateElement();
836
        }
837
    })
838
    /**
839
     * Validate rules form Questions
840
     */
841
    var validatorFormQuestion = $("#form-question").validate({
842
        ignore: [],
843
        errorClass: 'help-block',
844
        errorElement: 'span',
845
        rules: {
846
            'question-text': {
847
                required: true,
848
            },
849
            'question-type': {
850
                required: true,
851
            },
852
            'question-max-length': {
853
                required: true,
854
                digits: true,
855
                min: 0
856
            },
6467 eleazar 857
            'question-range': {
858
                required: true,
859
                number: true,
860
                min: 1
861
            },
4384 eleazar 862
        },
863
        highlight: function(element) {
864
            $(element).closest('.form-group').addClass('has-error');
865
        },
866
        unhighlight: function(element) {
867
            $(element).closest('.form-group').removeClass('has-error');
868
        },
869
        errorPlacement: function(error, element) {
870
            if (element.attr("data-error-container")) {
871
                error.appendTo(element.attr("data-error-container"));
872
            } else {
873
                error.insertAfter(element);
874
            }
875
        },
876
        invalidHandler: function(form, validator) {
877
            if (!validator.numberOfInvalids())
878
                return;
879
            $('html, body').animate({
880
                scrollTop: $(validator.errorList[0].element).offset().top - 100
881
            }, 1000);
882
        },
883
        submitHandler: function(form) {
884
            if ($('#form-question #question-slug').val()) {
885
                objFormGenerator.editQuestion(
886
                    $('#form-question #question-section').val(),
887
                    $('#form-question #question-slug').val(),
888
                    $('#form-question #question-text').val(),
889
                    $('#form-question #question-type').val(),
890
                    $('#form-question #question-max-length').val(),
6467 eleazar 891
                    $('#form-question #question-multiline').val(),
892
                    $('#form-question #question-range').val()
4384 eleazar 893
                );
894
            } else {
895
                objFormGenerator.addQuestion(
896
                    $('#form-question #question-section').val(),
897
                    $('#form-question #question-text').val(),
898
                    $('#form-question #question-type').val(),
899
                    $('#form-question #question-max-length').val(),
6467 eleazar 900
                    $('#form-question #question-multiline').val(),
901
                    $('#form-question #question-range').val()
4384 eleazar 902
                );
903
            }
904
            renderSectionData(objFormGenerator.sections);
905
            $('#modal-question').modal('hide');
906
            return false;
907
        }
908
    });
909
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
910
    $("#form-option").on('submit', function() {
911
        for (var instanceName in CKEDITOR.instances) {
912
            CKEDITOR.instances[instanceName].updateElement();
913
        }
914
    })
915
    /**
916
     * Validate rules form options
917
     */
918
    var validatorFormOption = $("#form-option").validate({
919
        ignore: [],
920
        errorClass: 'help-block',
921
        errorElement: 'span',
922
        rules: {
923
            'option-text': {
924
                required: true,
925
            }
4546 eleazar 926
 
4384 eleazar 927
        },
928
        highlight: function(element) {
929
            $(element).closest('.form-group').addClass('has-error');
930
        },
931
        unhighlight: function(element) {
932
            $(element).closest('.form-group').removeClass('has-error');
933
        },
934
        errorPlacement: function(error, element) {
935
            if (element.attr("data-error-container")) {
936
                error.appendTo(element.attr("data-error-container"));
937
            } else {
938
                error.insertAfter(element);
939
            }
940
        },
941
        invalidHandler: function(form, validator) {
942
            if (!validator.numberOfInvalids())
943
                return;
944
            $('html, body').animate({
945
                scrollTop: $(validator.errorList[0].element).offset().top - 100
946
            }, 1000);
947
        },
948
        submitHandler: function(form) {
949
            if ($('#form-option #option-slug').val()) {
950
                objFormGenerator.editOption(
951
                    $('#form-option #option-section').val(),
952
                    $('#form-option #option-question').val(),
953
                    $('#form-option #option-slug').val(),
6276 eleazar 954
                    $('#form-option #option-text').val()
4384 eleazar 955
                );
956
            } else {
957
                objFormGenerator.addOption(
958
                    $('#form-option #option-section').val(),
959
                    $('#form-option #option-question').val(),
6276 eleazar 960
                    $('#form-option #option-text').val()
4384 eleazar 961
                );
962
            }
963
            renderSectionData(objFormGenerator.sections);
964
            $('#modal-option').modal('hide');
965
            return false;
966
        }
967
    });
968
    /**
969
     * Clicked on add new section
970
     */
971
    $('body').on('click', 'button[id="btn-add-section"]', function(e) {
972
        e.preventDefault();
973
        validatorFormSection.resetForm();
974
        $('#form-section #section-slug').val('');
975
        $('#form-section #section-name').val('');
8965 stevensc 976
        CKEDITOR.instances['section-text'].setData('');
4384 eleazar 977
        $('#modal-section h4[class="modal-title"]').html('LABEL_ADD LABEL_SECTION');
978
        $('#modal-section').modal('show');
979
    });
980
    /**
981
     * Clicked on edit section
982
     */
983
    $('body').on('click', 'button.btn-edit-section', function(e) {
984
        e.preventDefault();
985
        var slug = $(this).data('section');
986
        var section;
987
        var showForm = false;
988
        for (i = 0; i < objFormGenerator.sections.length; i++) {
989
            section = objFormGenerator.sections[i];
990
            if (slug == section.slug_section) {
991
                validatorFormSection.resetForm();
992
                $('#form-section #section-slug').val(section.slug_section);
993
                $('#form-section #section-name').val(section.name);
8965 stevensc 994
                $('#section-text').ckeditor({
995
                    toolbar: [
996
                    { name: 'editing', items: ['Scayt'] },
997
                    { name: 'links', items: ['Link', 'Unlink'] },
998
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
999
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1000
                    '/',
1001
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1002
                    { name: 'styles', items: ['Styles', 'Format'] },
1003
                    { name: 'tools', items: ['Maximize'] }
1004
                ],
1005
                removePlugins: 'elementspath,Anchor',
1006
                heigth: 100
1007
                }).val(section.text);
4384 eleazar 1008
                showForm = true;
1009
                break;
1010
            }
1011
        }
1012
        if (showForm) {
1013
            $('#modal-section h4[class="modal-title"]').html('LABEL_EDIT LABEL_SECTION');
1014
            $('#modal-section').modal('show');
1015
        }
1016
    });
1017
    /**
1018
     * Clicked on remove section
1019
     */
1020
    $('body').on('click', 'button.btn-delete-section', function(e) {
1021
        e.preventDefault();
1022
        var slug = $(this).data('section');
1023
        bootbox.confirm({
1024
            title: "LABEL_DELETE LABEL_SECTION",
1025
            message: "LABEL_QUESTION_DELETE",
1026
            buttons: {
1027
                cancel: {
1028
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1029
                },
1030
                confirm: {
1031
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1032
                }
1033
            },
1034
            callback: function(result) {
1035
                if (result) {
1036
                    objFormGenerator.deleteSection(slug);
1037
                    renderSectionData(objFormGenerator.sections);
1038
                }
1039
            }
1040
        });
1041
    });
1042
    /**
1043
     * Clicked add new question
1044
     */
1045
    $('body').on('click', 'button.btn-add-question', function(e) {
1046
        e.preventDefault();
1047
        validatorFormQuestion.resetForm();
1048
        var slug = $(this).data('section');
1049
        $('#form-question #question-section').val(slug);
1050
        $('#form-question #question-slug').val('');
1051
        CKEDITOR.instances['question-text'].setData('');
1052
        $('#form-question #question-type').val($('#form-question #question-type option:first').val());
1053
        $('#form-question #question-max-length').val('0');
1054
        $('#form-question #question-max-length').parent().show();
1055
        $('#form-question #question-multiline').val('0');
1056
        $('#form-question #question-multiline').parent().show();
6467 eleazar 1057
        $('#form-question #question-range').val('10');
1058
        $('#form-question #question-range').parent().hide();
4384 eleazar 1059
        $('#modal-question h4[class="modal-title"]').html('LABEL_ADD LABEL_QUESTION');
1060
        $('#modal-question').modal('show');
1061
    });
1062
    /**
1063
     * Clicked edit question
1064
     */
1065
    $('body').on('click', 'button.btn-edit-question', function(e) {
1066
        e.preventDefault();
1067
        var slug_section = $(this).data('section');
1068
        var slug = $(this).data('question');
1069
        var showForm = false;
1070
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1071
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1072
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1073
                    if (slug == objFormGenerator.sections[i].questions[j].slug_question) {
1074
                        validatorFormQuestion.resetForm();
1075
                        $('#form-question #question-section').val(objFormGenerator.sections[i].slug_section);
1076
                        $('#form-question #question-slug').val(objFormGenerator.sections[i].questions[j].slug_question);
1077
                        CKEDITOR.instances['question-text'].setData(objFormGenerator.sections[i].questions[j].text);
1078
                        $('#form-question #question-type').val(objFormGenerator.sections[i].questions[j].type);
1079
                        if (objFormGenerator.sections[i].questions[j].type == 'open') {
1080
                            $('#form-question #question-max-length').val(objFormGenerator.sections[i].questions[j].maxlength);
1081
                            $('#form-question #question-max-length').parent().show();
1082
                            $('#form-question #question-multiline').val(objFormGenerator.sections[i].questions[j].multiline);
1083
                            $('#form-question #question-multiline').parent().show();
1084
                        } else {
1085
                            $('#form-question #question-max-length').val('0');
1086
                            $('#form-question #question-max-length').parent().hide();
1087
                            $('#form-question #question-multiline').val('0');
1088
                            $('#form-question #question-multiline').parent().hide();
1089
                        }
6467 eleazar 1090
                        if (objFormGenerator.sections[i].questions[j].type == 'rating-range') {
1091
                            $('#form-question #question-range').val(objFormGenerator.sections[i].questions[j].range);
1092
                            $('#form-question #question-range').parent().show();
1093
                        } else {
1094
                            $('#form-question #question-range').val('10');
1095
                            $('#form-question #question-range').parent().hide();
1096
                        }
4384 eleazar 1097
                        showForm = true;
1098
                        break;
1099
                    }
1100
                }
1101
                break;
1102
            }
1103
        }
1104
        if (showForm) {
1105
            $('#modal-question h4[class="modal-title"]').html('LABEL_EDIT LABEL_QUESTION');
1106
            $('#modal-question').modal('show');
1107
        }
1108
    });
1109
    /**
1110
     * Clicked remove question
1111
     */
1112
    $('body').on('click', 'button.btn-delete-question', function(e) {
1113
        e.preventDefault();
1114
        var slug_section = $(this).data('section');
1115
        var slug = $(this).data('question');
1116
        bootbox.confirm({
1117
            title: "LABEL_DELETE LABEL_QUESTION",
1118
            message: "LABEL_QUESTION_DELETE",
1119
            buttons: {
1120
                cancel: {
1121
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1122
                },
1123
                confirm: {
1124
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1125
                }
1126
            },
1127
            callback: function(result) {
1128
                if (result) {
1129
                    objFormGenerator.deleteQuestion(slug_section, slug);
1130
                    renderSectionData(objFormGenerator.sections);
1131
                }
1132
            }
1133
        });
1134
    });
1135
    /**
1136
     * Clicked add new Option
1137
     */
1138
    $('body').on('click', 'button.btn-add-option', function(e) {
1139
        e.preventDefault();
1140
        var slug_section = $(this).data('section');
1141
        var slug_question = $(this).data('question');
1142
        var showForm = false;
1143
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1144
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1145
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1146
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1147
                        validatorFormOption.resetForm();
1148
                        $('#form-option #option-section').val(slug_section);
1149
                        $('#form-option #option-question').val(slug_question);
1150
                        $('#form-option #option-slug').val('');
1151
                        CKEDITOR.instances['option-text'].setData('', function() {
1152
                            editor.focus();
1153
                        });
6276 eleazar 1154
 
4384 eleazar 1155
                        renderSectionData(objFormGenerator.sections);
1156
                        $('#modal-option h4[class="modal-title"]').html('LABEL_ADD LABEL_OPTION');
1157
                        $('#modal-option').modal('show');
1158
                        return true;
1159
                    }
1160
                }
1161
            }
1162
        }
1163
    });
1164
    /**
1165
     * Clicked edit option
1166
     */
1167
    $('body').on('click', 'button.btn-edit-option', function(e) {
1168
        e.preventDefault();
1169
        var slug_section = $(this).data('section');
1170
        var slug_question = $(this).data('question');
1171
        var slug = $(this).data('slug');
1172
        var showForm = false;
1173
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1174
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1175
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1176
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1177
                        for (k = 0; k < objFormGenerator.sections[i].questions[j].options.length; k++) {
1178
                            if (slug == objFormGenerator.sections[i].questions[j].options[k].slug_option) {
1179
                                validatorFormOption.resetForm();
1180
                                $('#form-option #option-section').val(objFormGenerator.sections[i].slug_section);
1181
                                $('#form-option #option-question').val(objFormGenerator.sections[i].questions[j].slug_question);
1182
                                $('#form-option #option-slug').val(objFormGenerator.sections[i].questions[j].options[k].slug_option);
1183
                                CKEDITOR.instances['option-text'].setData(objFormGenerator.sections[i].questions[j].options[k].text,
1184
                                    function() {
1185
                                        editor.focus();
1186
                                    });
1187
                                showForm = true;
1188
                                break;
1189
                            }
1190
                        }
1191
                    }
1192
                    if (showForm) {
1193
                        break;
1194
                    }
1195
                }
1196
            }
1197
            if (showForm) {
1198
                break;
1199
            }
1200
        }
1201
        if (showForm) {
1202
            $('#modal-option h4[class="modal-title"]').html('LABEL_EDIT LABEL_OPTION');
1203
            $('#modal-option').modal('show');
1204
        }
1205
    });
1206
    /**
1207
     * Clicked remove option
1208
     */
1209
    $('body').on('click', 'button.btn-delete-option', function(e) {
1210
        e.preventDefault();
1211
        var slug_section = $(this).data('section');
1212
        var slug_question = $(this).data('question');
1213
        var slug = $(this).data('slug');
1214
        bootbox.confirm({
1215
            title: "LABEL_DELETE LABEL_OPTION",
1216
            message: "LABEL_QUESTION_DELETE",
1217
            buttons: {
1218
                cancel: {
1219
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
1220
                },
1221
                confirm: {
1222
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
1223
                }
1224
            },
1225
            callback: function(result) {
1226
                if (result) {
1227
                    objFormGenerator.deleteOption(slug_section, slug_question, slug);
1228
                    renderSectionData(objFormGenerator.sections);
1229
                }
1230
            }
1231
        });
1232
    })
4546 eleazar 1233
 
4384 eleazar 1234
    /**
1235
     * Format input number (Form Question)
1236
     */
1237
    $('#form-question #question-max-length').inputNumberFormat({
1238
        'decimal': 0
1239
    });
1240
    /**
1241
     * Detect when updating question status and hide/Show options
1242
     */
1243
    $('#form-question #question-type').change(function(e) {
1244
        e.preventDefault();
1245
        if ($('#form-question #question-type').val() == 'open') {
1246
            $('#form-question #question-max-length').parent().show();
1247
            $('#form-question #question-multiline').parent().show();
1248
        } else {
1249
            $('#form-question #question-max-length').val('0');
1250
            $('#form-question #question-max-length').parent().hide();
1251
            $('#form-question #question-multiline').val('0');
1252
            $('#form-question #question-multiline').parent().hide();
1253
        }
6467 eleazar 1254
        $('#form-question #question-range').val('10');
1255
        if ($('#form-question #question-type').val() == 'rating-range') {
1256
            $('#form-question #question-range').parent().show();
1257
        } else {
1258
            $('#form-question #question-range').parent().hide();
1259
        }
4384 eleazar 1260
    });
1261
    /**
1262
     * Clicked new Form
1263
     */
1264
    $('button.btn-add-form').click(function(e) {
1265
        e.preventDefault();
1266
        objFormGenerator.clear();
1267
        objFormGenerator.render();
1268
        validatorForm.resetForm();
1269
        clearSectionData();
1270
        $('#form-main').attr('action', '$routeAdd');
1271
        $('#form-main #form-continue').val('0');
4459 eleazar 1272
        $('#form-main #name').val('');
1273
        $('#form-main #status').val('$status_inactive');
4466 eleazar 1274
        CKEDITOR.instances.text.setData('');
1275
        CKEDITOR.instances.description.setData('');
4384 eleazar 1276
        $('#row-forms').hide();
1277
        $('#row-edit').show();
4459 eleazar 1278
        $('#form-main #name').focus();
4384 eleazar 1279
    });
1280
    /**
1281
     * Clicked cancel new/edit Form
1282
     */
1283
    $('button.btn-edit-cancel').click(function(e) {
1284
        e.preventDefault();
1285
        $('#row-edit').hide();
1286
        $('#row-forms').show();
1287
    });
1288
    /**
1289
     * Clicked save and continue new Form
1290
     */
1291
    $('button.btn-form-save-continue').click(function(e) {
8365 eleazar 1292
        e.preventDefault();
1293
        $('#form-main #form-continue').val('1')
8366 eleazar 1294
        $('#form-main').submit();
4384 eleazar 1295
    });
1296
    /**
1297
     * Clicked save and close new/edit Form
1298
     */
1299
    $('button.btn-form-save-close').click(function(e) {
1300
        e.preventDefault();
1301
        $('#form-main #form-continue').val('0')
1302
        $('#form-main').submit();
1303
    });
8364 eleazar 1304
 
1305
    const saveData = (action) => {
1306
 
1307
            $.ajax({
1308
                'dataType': 'json',
1309
                'method': 'post',
1310
                'url': $('#form-main').attr('action'),
1311
                'data': {
1312
                    'name': $('#form-main #form-name').val(),
1313
 
1314
                    'status': $('#form-main #form-status').val(),
1315
                    'description': $('#form-main #form-description').val(),
1316
                    'content': JSON.stringify(sections)
1317
                },
1318
            }).done(function(response) {
1319
                if (response['success']) {
1320
                    $.fn.showSuccess(response['data']);
1321
                    if (action == 1) {
1322
                        $('#form-main').attr('action', response['action_edit']);
1323
                    } else {
1324
                        $('#row-form').hide();
1325
                        $('#row-lists').show();
1326
                        /*---------- Reset Form -------- */
1327
                        $('#form-main')[0].reset();
1328
                        /*--------Reset Sections ----------*/
1329
                        sections = [];
1330
                        tableForm.fnDraw();
1331
                    }
1332
                    return;
1333
                } else {
1334
                    $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
1335
                    return;
1336
                }
1337
            });
1338
 
1339
    }
1340
 
4384 eleazar 1341
    /**
1342
     * Modal Settings
1343
     */
1344
    $('#modal-section, #modal-question, #modal-option').modal({
1345
        backdrop: 'static',
1346
        keyboard: false,
1347
        show: false
1348
    });
1349
    /**
1350
     * Get Question type
1351
     */
1352
    const getQuestionTypeBySlug = (slug_section, slug_question) => {
1353
        for (i = 0; i < objFormGenerator.sections.length; i++) {
1354
            if (slug_section == objFormGenerator.sections[i].slug_section) {
1355
                for (j = 0; j < objFormGenerator.sections[i].questions.length; j++) {
1356
                    if (slug_question == objFormGenerator.sections[i].questions[j].slug_question) {
1357
                        return objFormGenerator.sections[i].questions[j].type;
1358
                    }
1359
                }
1360
            }
1361
        }
1362
    }
1363
    /**
1364
     * Remove Html Tags
1365
     */
1366
    const removeTags = (str) => str.toString().replace( /(<([^>]+)>)/ig, '')
1367
    /**
1368
     * Render Sections data
1369
     */
1370
    const renderSectionData = (data) => $("#rows").html($("#sectionTemplate").render(data, {
1371
        getType: getQuestionTypeBySlug,
1372
        removeTags: removeTags
1373
    }));
1374
 
1375
 
1376
    /**
1377
     * Clear Div Section data
1378
     */
1379
    const clearSectionData = () => $("#rows").html('');
1380
    /**
1381
     * Clicked refresh button
1382
     */
1383
    $('button.btn-refresh').click(function(e) {
1384
        tableForm.fnDraw();
1385
    });
4466 eleazar 1386
 
8767 stevensc 1387
    CKEDITOR.replace('description', {
1388
        toolbar: [
1389
                    { name: 'editing', items: ['Scayt'] },
1390
                    { name: 'links', items: ['Link', 'Unlink'] },
1391
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1392
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1393
                    '/',
1394
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1395
                    { name: 'styles', items: ['Styles', 'Format'] },
1396
                    { name: 'tools', items: ['Maximize'] }
1397
                ],
1398
                removePlugins: 'elementspath,Anchor',
1399
                heigth: 100
1400
    });
1401
    CKEDITOR.replace('text', {
1402
        toolbar: [
1403
                    { name: 'editing', items: ['Scayt'] },
1404
                    { name: 'links', items: ['Link', 'Unlink'] },
1405
                    { name: 'paragraph', items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote'] },
1406
                    { name: 'basicstyles', items: ['Bold', 'Italic', 'Strike', 'RemoveFormat'] },
1407
                    '/',
1408
                    { name: 'insert', items: ['Image', 'Table', 'HorizontalRule', 'SpecialChar'] },
1409
                    { name: 'styles', items: ['Styles', 'Format'] },
1410
                    { name: 'tools', items: ['Maximize'] }
1411
                ],
1412
                removePlugins: 'elementspath,Anchor',
1413
                heigth: 100
1414
    });
4384 eleazar 1415
 
1416
});
1417
JS;
1418
$this->inlineScript()->captureEnd();
1419
?>
1420
 
1421
<!-- Content Header (Page header) -->
1422
<section class="content-header">
1423
    <div class="container-fluid">
1424
        <div class="row mb-2">
1425
            <div class="col-sm-12">
1426
                <h1>LABEL_SELF_EVALUATION_FORMS</h1>
1427
            </div>
1428
        </div>
1429
    </div>
1430
    <!-- /.container-fluid -->
1431
</section>
1432
<section class="content">
1433
    <div class="container-fluid" id="row-forms">
1434
        <div class="row">
1435
            <div class="col-12">
1436
                <div class="card">
1437
                    <div class="card-body">
1438
                        <table id="gridTable" class="table   table-hover">
1439
                            <thead>
1440
                                <tr>
1441
                                    <th>LABEL_NAME</th>
1442
                                    <th>LABEL_ACTIVE</th>
1443
                                    <th>LABEL_ACTIONS</th>
1444
                                </tr>
1445
                            </thead>
1446
                            <tbody></tbody>
1447
                        </table>
1448
                    </div>
1449
                    <div class="card-footer clearfix">
1450
                        <div style="float:right;">
1451
                            <button type="button" class="btn btn-info btn-refresh"><i class="fa fa-refresh"></i> LABEL_REFRESH </button>
1452
                            <?php if ($allowAdd) : ?>
1453
                                <button type="button" class="btn btn-primary btn-add-form"><i class="fa fa-plus"></i> LABEL_ADD </button>
1454
                            <?php endif; ?>
1455
                        </div>
1456
                    </div>
1457
                </div>
1458
            </div>
1459
        </div>
1460
    </div>
1461
 
1462
    <!-- Create/Edit Form -->
1463
 
1464
    <div class="row" id="row-edit" style="display: none">
1465
        <div class="col-xs-12 col-md-12">
1466
            <form action="#" name="form-main" id="form-main">
1467
                <input type="hidden" name="form-id" id="form-id" value="0" />
1468
                <input type="hidden" name="form-continue" id="form-continue" value="0" />
4450 eleazar 1469
                <?php
1470
                     //$form = $this->formAdd;
1471
                     $form->setAttributes([
1472
                         'method' => 'post',
1473
                         'name' => 'form-main',
1474
                         'action' => $routeAdd,
1475
                         'id' => 'form-main'
1476
                     ]);
1477
                     $form->prepare();
1478
                     echo $this->form()->openTag($form);
1479
                ?>
1480
               <div class="form-group">
1481
                    <?php
1482
                    $element = $form->get('name');
1483
                    $element->setOptions(['label' => 'LABEL_FIRST_NAME']);
1484
                    $element->setAttributes(['class' => 'form-control']);
1485
                    echo $this->formLabel($element);
1486
                    echo $this->formText($element);
1487
                    ?>
4384 eleazar 1488
                </div>
1489
                <div class="form-group">
4450 eleazar 1490
                        <?php
1491
                        $element = $form->get('description');
6269 eleazar 1492
                        $element->setOptions(['label' => 'LABEL_DESCRIPTION']);
4450 eleazar 1493
                        $element->setAttributes(['class' => 'form-control']);
1494
                        echo $this->formLabel($element);
4468 eleazar 1495
                        echo $this->formTextArea($element);
4450 eleazar 1496
                        ?>
4384 eleazar 1497
                </div>
1498
                <div class="form-group">
4450 eleazar 1499
                        <?php
1500
                        $element = $form->get('text');
1501
                        $element->setOptions(['label' => 'LABEL_TEXT']);
1502
                        $element->setAttributes(['class' => 'form-control']);
1503
                        echo $this->formLabel($element);
4468 eleazar 1504
                        echo $this->formTextArea($element);
4450 eleazar 1505
                        ?>
4384 eleazar 1506
                </div>
1507
                <div class="form-group">
4450 eleazar 1508
                        <?php
1509
                        $element = $form->get('status');
1510
                        $element->setAttributes(['class' => 'form-control']);
1511
                        $element->setOptions(['label' => 'LABEL_STATUS']);
1512
                        echo $this->formLabel($element);
1513
                        echo $this->formSelect($element);
1514
                        ?>
1515
                    </div>
4384 eleazar 1516
                <br/>
1517
                <div class="row">
1518
                    <div class="col-xs-12 col-md-12 text-right">
1519
                        <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>
1520
                    </div>
1521
                </div>
1522
                <br />
1523
                <div class="row">
1524
                    <div class="col-xs-12 col-md-12">
1525
                        <div class="panel-group" id="rows"></div>
1526
                    </div>
1527
                </div>
1528
                <div class="form-group">
1529
                    <button type="submit" form="form-main" class="btn btn-info btn-form-save-continue">LABEL_SAVE & LABEL_CONTINUE</button>
1530
                    <button type="button" class="btn btn-primary btn-form-save-close">LABEL_SAVE & LABEL_CLOSE</button>
1531
                    <button type="button" class="btn btn-secondary btn-edit-cancel">LABEL_CANCEL</button>
1532
                </div>
1533
            </form>
1534
        </div>
1535
    </div>
1536
 
1537
    <!-- Create/Edit Form-->
1538
 
1539
    <!-- Section Modal -->
1540
    <div  id="modal-section" class="modal" tabindex="-1" role="dialog">
1541
        <div class="modal-dialog modal-lg" role="document">
1542
            <form action="#" name="form-section" id="form-section">
1543
                <input type="hidden" name="section-slug" id="section-slug" value="" />
1544
                <div class="modal-content">
1545
                    <div class="modal-header">
1546
                        <h4 class="modal-title">LABEL_ADD LABEL_SECTION</h4>
1547
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1548
                            <span aria-hidden="true">&times;</span>
1549
                        </button>
1550
                    </div>
1551
                    <div class="modal-body">
1552
                        <div class="form-group">
1553
                            <label for="section-name">LABEL_FIRST_NAME</label>
1554
                            <input type="text" name="section-name" id="section-name" class="form-control" maxlength="50" value="" />
1555
                        </div>
1556
                        <div class="form-group">
1557
                            <label for="section-text">LABEL_TEXT</label>
1558
                            <!--  ckeditor -->
1559
                            <textarea  name="section-text" id="section-text" rows="5" class="ckeditor form-control"></textarea>
1560
                        </div>
1561
                    </div>
1562
                    <div class="modal-footer">
1563
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1564
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1565
                    </div>
1566
                </div>
1567
            </form>
1568
        </div>
1569
    </div>
1570
    <!-- End Modal Section -->
1571
 
1572
    <!-- Question Modal -->
1573
 
1574
    <div  id="modal-question" class="modal" tabindex="-1" role="dialog">
1575
        <div class="modal-dialog modal-lg" role="document">
1576
            <form action="#" name="form-question" id="form-question">
1577
                <input type="hidden" name="question-section" id="question-section" />
1578
                <input type="hidden" name="question-slug" id="question-slug" />
1579
                <div class="modal-content">
1580
                    <div class="modal-header">
1581
                        <h4 class="modal-title">LABEL_ADD LABEL_QUESTION</h4>
1582
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1583
                            <span aria-hidden="true">&times;</span>
1584
                        </button>
1585
                    </div>
1586
                    <div class="modal-body">
1587
                        <div class="form-group">
1588
                            <label for="question-text">LABEL_TEXT</label>
1589
                            <!--  ckeditor -->
1590
                            <textarea  name="question-text" id="question-text" rows="5" class="ckeditor form-control"></textarea>
1591
                        </div>
1592
                        <div class="form-group">
1593
                            <label for="question-type">LABEL_TYPE</label>
1594
                            <select name="question-type" id="question-type" class="form-control">
1595
                                <option value="open">LABEL_OPEN</option>
1596
                                <option value="simple">Simple</option>
1597
                                <option value="multiple">Multiple</option>
6467 eleazar 1598
 
4384 eleazar 1599
                            </select>
1600
                        </div>
1601
                        <div class="form-group">
1602
                            <label for="question-max-length">LABEL_MAXLENGTH</label>
1603
                            <input type="text" name="question-max-length" id="question-max-length"  class="form-control" />
1604
                        </div>
1605
                        <div class="form-group">
1606
                            <label for="question-multiline">LABEL_MULTI_LINE</label>
1607
                            <select name="question-multiline" id="question-multiline" class="form-control">
1608
                                <option value="1">LABEL_YES</option>
1609
                                <option value="0">LABEL_NOT</option>
1610
                            </select>
1611
                        </div>
6467 eleazar 1612
                        <div class="form-group">
1613
                            <label for="question-range">LABEL_RANGE</label>
1614
                            <select name="question-range" id="question-range" class="form-control">
1615
                                <option value="10">1-10</option>
1616
                                <option value="6">1-6</option>
1617
                                <option value="5">1-5</option>
1618
                            </select>
1619
                        </div>
4384 eleazar 1620
                    </div>
1621
                    <div class="modal-footer">
1622
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1623
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1624
                    </div>
1625
                </div>
1626
            </form>
1627
        </div>
1628
    </div>
1629
 
1630
    <!-- End Modal Question -->
1631
 
1632
    <!-- Modal Options -->
1633
 
1634
    <div  id="modal-option" class="modal" tabindex="-1" role="dialog">
1635
        <div class="modal-dialog modal-lg" role="document">
1636
            <form action="#" name="form-option" id="form-option">
1637
                <input type="hidden" name="option-section" id="option-section" value="" />
1638
                <input type="hidden" name="option-question" id="option-question" value="" />
1639
                <input type="hidden" name="option-slug" id="option-slug" value="" />
1640
                <div class="modal-content">
1641
                    <div class="modal-header">
1642
                        <h4 class="modal-title">LABEL_OPTION</h4>
1643
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
1644
                            <span aria-hidden="true">&times;</span>
1645
                        </button>
1646
                    </div>
1647
                    <div class="modal-body">
1648
                        <div class="form-group">
1649
                            <label for="option-text">LABEL_TEXT</label>
1650
                            <!--  ckeditor -->
1651
                            <textarea  name="option-text" id="option-text" rows="5" class="ckeditor form-control"></textarea>
1652
                        </div>
1653
                    </div>
1654
                    <div class="modal-footer">
1655
                        <button type="submit" class="btn btn-primary">LABEL_SAVE</button>
1656
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">LABEL_CLOSE</button>
1657
                    </div>
1658
                </div>
1659
            </form>
1660
        </div>
1661
    </div>
1662
 
1663
    <!-- End Modal Options -->
1664
 
1665
    <!---Template Sections --->
1666
    <script id="sectionTemplate" type="text/x-jsrender">
1667
    <div class="panel panel-default" id="panel-{{:slug_section}}">
1668
        <div class="panel-heading">
1669
            <h4 class="panel-title">
1670
                <a class="accordion-toggle" data-toggle="collapse" aria-expanded="true" data-parent="#panel-{{:slug_section}}" href="#collapse-{{:slug_section}}">
1671
                    <span class="section-name{{:slug_section}}">
1672
                        {{:name}}
1673
                    </span>
1674
                </a>
1675
            </h4>
1676
        </div>
1677
        <div id="collapse-{{:slug_section}}" class="panel-collapse in collapse show">
1678
            <div class="panel-body">
1679
                <div class="table-responsive">
1680
                    <table class="table table-bordered">
1681
                        <thead>
1682
                            <tr>
1683
                                <th style="width: 10%;">LABEL_ELEMENT</th>
1684
                                <th style="width: 50%;">LABEL_TEXT</th>
1685
                                <th style="width: 10%;">LABEL_TYPE</th>
1686
                                <th style="width: 20%;">LABEL_ACTIONS</th>
1687
                            </tr>
1688
                        </thead>
1689
                        <tbody>
1690
                            <tr class="tr-section">
1691
                                <td class="text-left">LABEL_SECTION</td>
1692
                                <td class="text-left">{{:name}}</td>
1693
                                <td></td>
1694
                                <td>
1695
                                    <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>
1696
                                    <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>
1697
                                    <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>
1698
                                </td>
1699
                            </tr>
1700
                            {{for questions}}
1701
                            <tr class="tr-question">
1702
                                <td class="text-left">--LABEL_QUESTION</td>
1703
                                <td class="text-left">
1704
                                    {{:~removeTags(text)}}
1705
                                </td>
1706
                                <td class="text-capitalize">
1707
                                    {{if type == 'open'}} LABEL_OPEN {{/if}}
1708
                                    {{if type == 'simple'}} Simple {{/if}}
1709
                                    {{if type == 'multiple'}} Multiple {{/if}}
1710
                                </td>
1711
                                <td>
1712
                                    <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>
1713
                                    <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>
1714
 
6467 eleazar 1715
                                    {{if type == 'simple' || type == 'rating-open' || type=='multiple' }}
4384 eleazar 1716
                                    <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>
1717
                                    {{/if}}
1718
 
1719
                                </td>
1720
                            </tr>
1721
                            {{for options}}
1722
                            <tr class="tr-option">
1723
                                <td class="text-left">---LABEL_OPTION</td>
1724
                                <td class="text-left">
1725
                                    {{:~removeTags(text)}}
1726
                                </td>
1727
                                <td>
1728
 
1729
                                    {{if ~getType( slug_section, slug_question) == 'multiple' || ~getType( slug_section, slug_question) == 'rating-open'  }}
1730
                                    {{/if}}
1731
                                </td>
1732
                                <td>
1733
                                    <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>
1734
                                    <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>
1735
                                </td>
1736
                            </tr>
1737
                            {{/for}}
1738
                            {{/for}}
1739
                        </tbody>
1740
                    </table>
1741
                </div>
1742
            </div>
1743
        </div>
1744
    </div>
1745
    </script>
1746
 
1747
    <!-- End Template Sections-->
1748
 
1749
</section>