Skip to content

Commit

Permalink
В разделе ГД добавлена возможность создания связанных документов в сп…
Browse files Browse the repository at this point in the history
…лывающем окне. После создания ID созданного документа добавляется в связанное поле.
  • Loading branch information
butschster committed Mar 26, 2014
1 parent d3e32c1 commit 47a4f22
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 111 deletions.
5 changes: 4 additions & 1 deletion cms/media/js/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var cms = {
views: {},
collections: {},
routes: {},
popup_target: null,

// Error
error: function (msg, e) {
Expand Down Expand Up @@ -590,7 +591,9 @@ cms.ui.add('flags', function() {
closeEffect : 'none',
beforeLoad: function() {
this.href = updateQueryStringParameter(this.href, 'type', 'iframe');
this.title = $(this.element).html();
this.title = this.element.html();

cms.popup_target = this.element;
},
helpers : {
title : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public function post_create()
throw HTTP_API_Exception::factory(API::ERROR_UNKNOWN, 'Datasource section not found');
}

echo debug::vars($this->params());

try
{
$doc = $ds->get_empty_document();
Expand All @@ -30,14 +28,36 @@ public function post_create()
}

$this->message(__('Document created'));
$this->response($doc);
$this->response($doc->values());
}

public function post_update()
{
$ds_id = $this->param('ds_id', NULL, TRUE);
$id = $this->param('id', NULL, TRUE);

$ds = Datasource_Data_Manager::load((int) $ds_id);
if(empty($ds))
{
throw HTTP_API_Exception::factory(API::ERROR_UNKNOWN, 'Datasource section not found');
}

try
{
$doc = $ds->get_document((int) $id);

$doc->read_values($this->params())
->validate();

$doc = $ds->update_document($doc);
}
catch (Validation_Exception $e)
{
throw new API_Validation_Exception($e->errors('validation'));
}

$this->message(__('Document updated'));
$this->response($doc->values());
}

public function post_publish()
Expand Down
18 changes: 18 additions & 0 deletions cms/plugins/hybrid/views/datasource/hybrid/document/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
})
<?php endif; ?>
var API_FORM_ACTION = '/datasource/hybrid-document.<?php if($doc->loaded()): ?>update<?php else: ?>create<?php endif; ?>';

$(function() {
$('body').on('post:api:datasource:hybrid-document.create ', update_documents);
$('body').on('put:api:datasource:hybrid-document.create ', update_documents);
});

function update_documents(e, response) {
var target_field = cms.popup_target.data('target');
if( target_field && response.id) {
var current_val = $('#'+target_field).select2("val");
if(_.isArray(current_val))
current_val.push(response.id);
else
current_val = response.id;

$('#'+target_field).select2("val", current_val)
}
}
</script>

<div class="outline">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
<script>
$(function() {
function format(state) {
return '<a target="_blank" href="/backend/hybrid/document/view?ds_id=<?php echo $field->from_ds; ?>&id='+state.id+'">' + state.text + '</a>';
}
$('input[name="<?php echo $field->name; ?>"]').select2({
placeholder: __("Type first 1 chars to find documents"),
minimumInputLength: 1,
multiple:true,
formatSelection: format,
escapeMarkup: function(m) { return m; },
ajax: {
url: '/api/datasource/hybrid-document.find',
data: function(query, pageNumber, context) {
return {
key: query,
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
}
},
dataType: 'json',
results: function (resp, page) {
return {results: resp.response};
}
},
initSelection: function(element, callback) {
var id = $(element).val(),
ids = id.split(',');

ids = _.map(ids, function(num){ return parseInt(num); });

if (id !== "") {
$.ajax('/api/datasource/hybrid-document.find', {
data: {
ids: ids,
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
},
dataType: 'json',
}).done(function(resp, page) {
selected = [];
for(row in resp.response) {
if(_.indexOf(ids, resp.response[row]['id']) >= 0){
selected.push(resp.response[row]);
}
}
callback(selected);
});
}
}
});
})
</script>

<div class="control-group">
<label class="control-label"><?php echo $field->header; ?> <?php if($field->isreq): ?>*<?php endif; ?></label>
<div class="controls">
Expand All @@ -6,59 +60,6 @@
<?php echo Form::hidden($field->name, implode( ',', array_keys( $value )), array(
'id' => $field->name, 'class' => 'span12'
)); ?>
<script>
$(function() {
function format(state) {
return '<a target="_blank" href="/backend/hybrid/document/view?ds_id=<?php echo $field->from_ds; ?>&id='+state.id+'">' + state.text + '</a>';
}
$('input[name="<?php echo $field->name; ?>"]').select2({
placeholder: __("Type first 1 chars to find documents"),
minimumInputLength: 1,
multiple:true,
formatSelection: format,
escapeMarkup: function(m) { return m; },
ajax: {
url: '/api/datasource/hybrid-document.find',
data: function(query, pageNumber, context) {
return {
key: query,
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
}
},
dataType: 'json',
results: function (resp, page) {
return {results: resp.response};
}
},
initSelection: function(element, callback) {
var id = $(element).val(),
ids = id.split(',');

ids = _.map(ids, function(num){ return parseInt(num); });

if (id !== "") {
$.ajax('/api/datasource/hybrid-document.find', {
data: {
ids: ids,
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
},
dataType: 'json',
}).done(function(resp, page) {
selected = [];
for(row in resp.response) {
if(_.indexOf(ids, resp.response[row]['id']) >= 0){
selected.push(resp.response[row]);
}
}
callback(selected);
});
}
}
});
})
</script>
</div>
<div class="span1">
<?php echo UI::button(__('Create new'), array(
Expand All @@ -68,7 +69,8 @@ function format(state) {
'action' => 'create'
)) . URL::query(array('ds_id' => $field->from_ds), FALSE),
'icon' => UI::icon('building'),
'class' => 'btn popup fancybox.iframe'
'class' => 'btn popup fancybox.iframe',
'data-target' => $field->name
)); ?>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
<script>
$(function() {

function format(state) {
return '<a target="_blank" href="/backend/hybrid/document/view?ds_id=<?php echo $field->from_ds; ?>&id='+state.id+'">' + state.text + '</a>';
}

$('input[name="<?php echo $field->name; ?>"]').select2({
placeholder: __("Type first 1 chars to find documents"),
minimumInputLength: 1,
maximumSelectionSize: 1,
multiple:false,
formatSelection: format,
escapeMarkup: function(m) { return m; },
ajax: {
url: '/api/datasource/hybrid-document.find',
data: function(query, pageNumber, context) {
return {
key: query,
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
}
},
dataType: 'json',
results: function (resp, page) {
return {results: resp.response};
}
},
initSelection: function(element, callback) {
var id = $(element).val();
if (id !== "") {
$.ajax('/api/datasource/hybrid-document.find', {
data: {
ids: [parseInt(id)],
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
},
dataType: 'json',
}).done(function(resp, page) {
for(row in resp.response) {

if(resp.response[row]['id'] == id){
return callback(resp.response[row]);
}
}

});
}
}
});
})
</script>

<div class="control-group">
<label class="control-label"><?php echo $field->header; ?> <?php if($field->isreq): ?>*<?php endif; ?></label>
<div class="controls">
Expand All @@ -6,58 +59,7 @@
<?php echo Form::hidden($field->name, $value['id'], array(
'id' => $field->name, 'class' => 'span12'
)); ?>
<script>
$(function() {

function format(state) {
return '<a target="_blank" href="/backend/hybrid/document/view?ds_id=<?php echo $field->from_ds; ?>&id='+state.id+'">' + state.text + '</a>';
}

$('input[name="<?php echo $field->name; ?>"]').select2({
placeholder: __("Type first 1 chars to find documents"),
minimumInputLength: 1,
maximumSelectionSize: 1,
multiple:false,
formatSelection: format,
escapeMarkup: function(m) { return m; },
ajax: {
url: '/api/datasource/hybrid-document.find',
data: function(query, pageNumber, context) {
return {
key: query,
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
}
},
dataType: 'json',
results: function (resp, page) {
return {results: resp.response};
}
},
initSelection: function(element, callback) {
var id = $(element).val();
if (id !== "") {
$.ajax('/api/datasource/hybrid-document.find', {
data: {
ids: [parseInt(id)],
<?php if(!empty($doc->id)): ?>id: <?php echo $doc->id; ?>,<?php endif; ?>
doc_ds: <?php echo $field->from_ds; ?>
},
dataType: 'json',
}).done(function(resp, page) {
for(row in resp.response) {

if(resp.response[row]['id'] == id){
return callback(resp.response[row]);
}
}

});
}
}
});
})
</script>

</div>
<div class="span1">
<?php echo UI::button(__('Create new'), array(
Expand All @@ -67,7 +69,8 @@ function format(state) {
'action' => 'create'
)) . URL::query(array('ds_id' => $field->from_ds), FALSE),
'icon' => UI::icon('building'),
'class' => 'btn popup fancybox.iframe'
'class' => 'btn popup fancybox.iframe',
'data-target' => $field->name
)); ?>
</div>

Expand Down

0 comments on commit 47a4f22

Please sign in to comment.