Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions DynamicRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class DynamicRelations extends Widget
public $collection;
public $collectionType;
public $viewPath;
public $params;

public function init(){
parent::init();
Expand All @@ -32,13 +33,14 @@ public function run(){
}
$key = "dynamic-relations-$type";
$hash = crc32($key);
Yii::$app->session->set('dynamic-relations-'.$hash, [ 'path'=>$this->viewPath, 'cls'=>$type ]);
Yii::$app->session->set('dynamic-relations-'.$hash, [ 'path'=>$this->viewPath, 'cls'=>$type , 'params' => $this->params]);

return $this->render('template', [
'title' => $this->title,
'collection' => $this->collection,
'viewPath' => $this->viewPath,
'ajaxAddRoute' => Url::toRoute(['dynamicrelations/load/template', 'hash'=>$hash]),
'params' => $this->params,
]);
}

Expand All @@ -55,10 +57,11 @@ public function uniqueOptions($field, $uniq)

public static function relate($model, $attr, $request, $name, $clsname)
{
if($request[$name])
if(array_key_exists($name,$request))
{
if($new = $request[$name]['new'])
if(array_key_exists('new',$request[$name]))
{
$new = $request[$name]['new'];
foreach( $new as $useless=>$newattrs)
{
$newmodel = new $clsname;
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ use synatree\dynamicrelations\DynamicRelations;
'title' => 'Business Hours',
'collection' => $model->hours,
'viewPath' => '@app/views/business-hours/_inline.php',
'params' => ['firstParam' => $firstParam , 'anotherParam' => $anotherParam],

// this next line is only needed if there is a chance that the collection above will be empty. This gives the script a prototype to work with.
'collectionType' => new \app\models\BusinessHours,
Expand Down
6 changes: 3 additions & 3 deletions assets/js/dynamic-relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ jQuery(document).ready(function () {
jQuery('.add-dynamic-relation').on('click', function(event){
event.preventDefault();
var me = this;
view = jQuery(me).closest('[data-related-view]').attr('data-related-view') + "&t=" + ( new Date().getTime() );
view = jQuery(me).next('[data-related-view]').attr('data-related-view') + "&t=" + ( new Date().getTime() );
jQuery.get(view, function(result){
$result = jQuery(result);
li = jQuery(me).closest('li').clone().empty(); ul = jQuery(me).closest('ul');
ul.append( li );
jQuery(me).next('ul.list-group').append('<li class="list-group-item"></li>');
li = jQuery(me).next('ul.list-group').children().last();
li.append( $result.filter("#root") );
$result.filter('script').each(function(k,scriptNode){
if(!scriptNode.src || !scriptLoaded( scriptNode.src ) )
Expand Down
1 change: 1 addition & 0 deletions controllers/LoadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function actionTemplate($hash)
{
echo $this->render( $args['path'], [
'model' => new $args['cls'],
'params' => $args['params'],
]);
}

Expand Down
66 changes: 43 additions & 23 deletions views/template.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
<?php
use synatree\dynamicrelations\SynatreeAsset;
use yii\web\View;

SynatreeAsset::register($this);
$this->registerCSS(
"h2.panel-title a{text-decoration:none; color:inherit;font-size:26px}
h2.panel-title a i{font-size:14px;}"
);
$this->registerJs(
"$('h2.panel-title a').on('click',function (){
$(this).children('i').toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
});"
, View::POS_READY
,"template"
);
?>

<label class="form-control"><?= $title; ?></label>
<ul class="list-group" data-related-view="<?= $ajaxAddRoute; ?>">
<li class="list-group-item">
<a href="#" class="btn btn-success btn-sm add-dynamic-relation">
<i class="glyphicon glyphicon-plus"></i> Add
</a>
</li>

<?php
foreach($collection as $model)
{
?>
<li class="list-group-item">
<button type="button" class="close remove-dynamic-relation" aria-label="Remove"><span aria-hidden="true">&times;</span></button>
<div class="dynamic-relation-container">
<?= $this->renderFile( $viewPath, [ 'model' => $model ]); ?>
</div>
</li>
<?php
}
?>
</ul>
<div class="panel">
<div class="panel-heading">
<h2 class="panel-title">
<a data-toggle="collapse" href="#<?= $title ?>">
<?= $title; ?> <i class="glyphicon glyphicon-chevron-down"></i>
</a>
</h2>
</div>
<div id="<?= $title ?>" class="panel-collapse collapse">
<div class="panel-body">
<a href="#" class="btn btn-success btn-sm add-dynamic-relation">
<i class="glyphicon glyphicon-plus"></i> <?= Yii::t('app','Add'); ?>
</a>
<ul class="list-group" data-related-view="<?= $ajaxAddRoute; ?>">
<?php
foreach($collection as $model)
{
?>
<li class="list-group-item">
<button type="button" class="close remove-dynamic-relation" aria-label="Remove"><span aria-hidden="true">&times;</span></button>
<div class="dynamic-relation-container">
<?= $this->renderFile( $viewPath, [ 'model' => $model, 'params' => $params, ]); ?>
</div>
</li>
<?php
}
?>
</ul>
</div>
</div>
</div>