Relation::boot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Mascame\Artificer\Fields\Types\Relations;
2
3
use Mascame\Artificer\Fields\Field;
4
use Mascame\Artificer\Model\Model;
5
use Route;
6
use URL;
7
8
class Relation extends Field
9
{
10
11
    /**
12
     * @var Model;
13
     */
14
    public $modelObject;
15
    public $model;
16
    public $fields;
17
    public $createURL;
18
19
    public function boot()
20
    {
21
        parent::boot();
22
23
        $this->modelObject = \App::make('artificer-model');
24
    }
25
26
    public function editURL($model_route, $id)
27
    {
28
        return URL::route('admin.model.edit', array('slug' => $model_route, 'id' => $id));
29
    }
30
31
    public function createURL($model_route)
32
    {
33
        return URL::route('admin.model.create', array('slug' => $model_route));
34
    }
35
36
    public function relationModal($relatedModelRouteName, $id = 0)
37
    {
38
        ?>
39
        <!-- Modal -->
40
        <div class="modal fade standalone" id="form-modal-<?= $relatedModelRouteName ?>" tabindex="-1" role="dialog"
41
             aria-labelledby="myModalLabel" aria-hidden="true">
42
            <div class="modal-dialog modal-lg">
43
                <div class="modal-content">
44
45
                    <div class="modal-header">
46
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
47
                                class="sr-only">Close</span></button>
48
                        <h4 class="modal-title">Loading Form</h4>
49
                    </div>
50
51
                    <div id="modal-body-<?= $relatedModelRouteName ?>" class="modal-body">
52
                        <div class="alert alert-info" role="alert"><i class="fa fa-circle-o-notch fa-spin"></i> Loading
53
                        </div>
54
                    </div>
55
56
                    <div class="hidden default-modal-body">
57
                        <div class="alert alert-info" role="alert"><i class="fa fa-circle-o-notch fa-spin"></i> Loading
58
                        </div>
59
                    </div>
60
                </div>
61
            </div>
62
        </div>
63
64
        <script>
65
            $(function () {
66
67
                var $modal_<?=$relatedModelRouteName?> = $('#form-modal-<?=$relatedModelRouteName?>');
68
                var $modal_body_<?=$relatedModelRouteName?> = $("#modal-body-<?=$relatedModelRouteName?>");
69
                var url = null;
70
                var id = '<?=$id?>';
71
                var $field = $('[name="<?=$this->name?>"]');
72
73
                if ($field.is("select")) {
74
                    $field.on('change', function () {
75
//						alert( this.value ); // or $(this).val()
76
77
                        id = this.value;
78
                    });
79
                }
80
81
                $modal_<?=$relatedModelRouteName?>.on('show.bs.modal', function (e) {
82
                    url = $(e.relatedTarget).data('url');
83
                    url = url.replace(':id:', id);
84
                    //				url += " .right-side";
85
                });
86
87
                $modal_<?=$relatedModelRouteName?>.on('shown.bs.modal', function () {
88
                    $modal_body_<?=$relatedModelRouteName?>.load(url, function () {
89
                        var title = $modal_body_<?=$relatedModelRouteName?>.find('h1').html();
90
91
                        $('.modal-title').html(title);
92
93
                        var $form = $modal_body_<?=$relatedModelRouteName?>.find('form');
94
                        //                    $form.attr('action', url);
95
                        //                    $form.attr('method', 'POST');
96
                        $form.prepend('<input type="hidden" name="_standalone" value="<?=$relatedModelRouteName?>">');
97
                        $form.prepend('<input type="hidden" name="_standalone_origin" value="<?=$this->modelObject->getRouteName(Model::$current)?>">');
98
                        $form.prepend('<input type="hidden" name="_standalone_origin_id" value="<?=$id?>">');
99
100
                        <?php if (Route::currentRouteName() == 'admin.model.create') { ?>
101
                        $form.prepend('<input type="hidden" name="_set_relation_on_create" value="<?=Model::getCurrent()?>">');
102
                        $form.prepend('<input type="hidden" name="_set_relation_on_create_foreign" value="<?=$this->relation->getForeignKey()?>">');
103
                        <?php } ?>
104
105
                        var action = $form.attr('action');
106
107
                        $form.submit(function (e) {
108
                            e.preventDefault();
109
                            $.post(action, $form.serialize(), function (data) {
110
111
                                if (typeof data === 'string') {
112
                                    // validation errors
113
                                    $modal_body_<?=$relatedModelRouteName?>.html(data);
114
                                } else if (typeof data === 'object') {
115
                                    refreshRelation(data, '<?=$this->name?>');
116
                                    $modal_<?=$relatedModelRouteName?>.modal('hide');
117
                                } else {
118
                                    alert('Something is wrong.');
119
                                }
120
                            });
121
                        });
122
                    });
123
                });
124
125
                $modal_<?=$relatedModelRouteName?>.on('hidden.bs.modal', function (e) {
126
                    $modal_body_<?=$relatedModelRouteName?>.empty();
127
                    $modal_body_<?=$relatedModelRouteName?>.html($('.default-modal-body').html())
128
                });
129
130
                function refreshRelation(data, name) {
131
                    var $relation = $('[name="' + name + '"]');
132
                    var url = data.refresh;
133
134
                    url = url.replace(':fieldName:', name);
135
136
                    // After this call this whole modal will disappear
137
                    $relation.parent('.form-group').load(url, function (responseText, textStatus, req) {
138
                        if (textStatus == "error") {
139
                            return "oh noes!!!!";
140
                        } else {
141
                            $("body").trigger("relationRefresh", {
142
                                name: name,
143
                                id: id
144
                            });
145
                        }
146
                    });
147
                }
148
            });
149
        </script>
150
    <?php
151
    }
152
}