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