Vijayakrishnan 3 лет назад
Родитель
Сommit
c256d66f92

+ 12 - 0
app/Http/Controllers/StatTreeController.php

@@ -92,6 +92,18 @@ class StatTreeController extends Controller
         return $this->pass();
     }
 
+    public function instantiate(Request $request, StatTree $statTree) {
+        if(!$statTree->is_template) {
+            return $this->fail("State tree is not a template!");
+        }
+        $pro = Pro::where('uid', $request->input('proUid'))->first();
+        if(!$pro) {
+            return $this->fail("Pro not found!");
+        }
+        $instance = $statTree->instantiateForPro($pro);
+        return $this->pass($instance->uid);
+    }
+
     private function traverseLines($_lines, &$result) {
         foreach ($_lines as $line) {
             $result[] = $line;

+ 26 - 0
app/Models/StatTree.php

@@ -4,6 +4,8 @@ namespace App\Models;
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+use Ramsey\Uuid\Uuid;
 
 class StatTree extends Model
 {
@@ -27,4 +29,28 @@ class StatTree extends Model
     public function rootLines(){
         return $this->hasMany(StatTreeLine::class, 'stat_tree_id', 'id')->whereNull('parent_stat_tree_line_id')->orderBy('tree_order_position_index', 'ASC');
     }
+
+    public function instantiateForPro(Pro $pro) {
+
+        // create stat tree
+        $instance = new StatTree();
+        $nextId = DB::select("select nextval('stat_tree_id_seq')");
+        $instance->id = $nextId[0]->nextval;
+        $instance->uid = Uuid::uuid4();
+        $instance->name = $this->name;
+        $instance->model = $this->model;
+        $instance->slug = $this->slug . ':' . $pro->uid;
+        $instance->is_template = false;
+        $instance->pro_id = $pro->id;
+        $instance->pro_scope_clause = $this->pro_scope_clause;
+        $instance->save();
+
+        // create tree lines
+        foreach ($this->rootLines as $line) {
+            $line->copy(null, $instance);
+        }
+
+        return $instance;
+
+    }
 }

+ 47 - 0
app/Models/StatTreeLine.php

@@ -4,6 +4,8 @@ namespace App\Models;
 
 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\DB;
+use Ramsey\Uuid\Uuid;
 
 class StatTreeLine extends Model
 {
@@ -77,4 +79,49 @@ class StatTreeLine extends Model
         }
         return json_encode($args);
     }
+
+    public function copy($newParentStatTreeLine, StatTree $newStatTree) {
+
+        // create line
+        $newStatTreeLine = new StatTreeLine;
+        $nextId = DB::select("select nextval('stat_tree_line_id_seq')");
+        $newStatTreeLine->id = $nextId[0]->nextval;
+        $newStatTreeLine->uid = Uuid::uuid4();
+        $newStatTreeLine->stat_tree_id = $newStatTree->id;
+        $newStatTreeLine->tree_order_position_index = $this->tree_order_position_index;
+        $newStatTreeLine->parent_stat_tree_line_id = $newParentStatTreeLine ? $newParentStatTreeLine->id : null;
+        $newStatTreeLine->save();
+
+        // line clauses
+        foreach ($this->lineClauses as $srcLineClause) {
+            $nextId = DB::select("select nextval('stat_tree_line_clause_id_seq')");
+            $newStatTreeLineClause = new StatTreeLineClause;
+            $newStatTreeLineClause->id = $nextId[0]->nextval;
+            $newStatTreeLineClause->uid = Uuid::uuid4();
+            $newStatTreeLineClause->stat_tree_line_id = $newStatTreeLine->id;
+            $newStatTreeLineClause->clause_id = $srcLineClause->clause_id;
+            $newStatTreeLineClause->clause_label = $srcLineClause->clause_label;
+            $newStatTreeLineClause->position_index = $srcLineClause->position_index;
+            $newStatTreeLineClause->stat_tree_id = $newStatTree->id;
+            $newStatTreeLineClause->save();
+
+            // line clause args
+            foreach ($srcLineClause->statTreeLineClauseArgs as $srcLineClauseArg) {
+                $newStatTreeLineClauseArg = new StatTreeLineClauseArg();
+                $nextId = DB::select("select nextval('stat_tree_line_clause_arg_id_seq')");
+                $newStatTreeLineClauseArg->id = $nextId[0]->nextval;
+                $newStatTreeLineClauseArg->stat_tree_line_clause_id = $newStatTreeLineClause->id;
+                $newStatTreeLineClauseArg->clause_arg_id = $srcLineClauseArg->clause_arg_id;
+                $newStatTreeLineClauseArg->access_level = $srcLineClauseArg->access_level;
+                $newStatTreeLineClauseArg->stat_tree_id = $newStatTree->id;
+                $newStatTreeLineClauseArg->default_value = $srcLineClauseArg->default_value;
+                $newStatTreeLineClauseArg->save();
+            }
+        }
+
+        foreach ($this->children as $child) {
+            $child->copy($newStatTreeLine, $newStatTree);
+        }
+
+    }
 }

+ 60 - 46
resources/views/app/stat-tree/stat-trees/list.blade.php

@@ -57,52 +57,66 @@
                         </div>
                     </div>
                     <div class="card-body p-0">
-                        <div class="row">
-                            <div class="col-md-12">
-                                <div class="table-responsive">
-                                    <table class="table table-condensed p-0 m-0">
-                                        <thead class="bg-light">
-                                            <tr>
-                                                <th class="border-0">ID</th>
-                                                <th class="border-0">Name</th>
-                                                <th class="border-0">Model</th>
-                                                <th class="border-0">Slug</th>
-                                                <th class="border-0"></th>
-                                            </tr>
-                                        </thead>
-                                        <tbody>
-                                            @foreach($statTrees as $statTree)
-                                            <tr>
-                                                <td>{{ $statTree->id }}</td>
-                                                <td><a href="{{ route('practice-management.statTrees.view.edit', $statTree) }}">{{ $statTree->name }}</a>
-                                                    @if($statTree->is_template)
-                                                        <span class="mt-1 text-sm bg-info rounded text-white d-inline-block px-2 py-1 ml-1 font-weight-bold">TEMPLATE</span>
-                                                    @endif
-                                                </td>
-                                                <td>{{ $statTree->model }}</td>
-                                                <td>{{ $statTree->slug }}</td>
-                                                <td>
-                                                    <div class="d-flex align-items-baseline">
-                                                        <a href="{{ route('practice-management.statTrees.view.edit', $statTree) }}">Edit</a>
-                                                        @if($statTree->is_template)
-                                                            <div class="position-relative on-click-menu ml-2">
-                                                                <span class="text-primary c-pointer px-2 py-1 border rounded bg-aliceblue">…</span>
-                                                                <div menu class="bg-white border">
-                                                                    <a native target="_blank" href="#" class="px-2 py-1 d-block text-nowrap">Instantiate</a>
-                                                                    <a native target="_blank" href="#" class="px-2 py-1 d-block text-nowrap">Instantiate for multiple pros</a>
-                                                                    <a native target="_blank" href="#" class="px-2 py-1 d-block text-nowrap">Clone</a>
-                                                                </div>
-                                                            </div>
-                                                        @endif
-                                                    </div>
-                                                </td>
-                                            </tr>
-                                            @endforeach
-                                        </tbody>
-                                    </table>
-                                </div>
-                            </div>
-                        </div>
+                        <table class="table table-condensed p-0 m-0">
+                            <thead class="bg-light">
+                            <tr>
+                                <th class="border-0">ID</th>
+                                <th class="border-0">Name</th>
+                                <th class="border-0">Model</th>
+                                <th class="border-0">Slug</th>
+                                <th class="border-0">Pro</th>
+                                <th class="border-0"></th>
+                            </tr>
+                            </thead>
+                            <tbody>
+                            @foreach($statTrees as $statTree)
+                                <tr>
+                                    <td>{{ $statTree->id }}</td>
+                                    <td><a href="{{ route('practice-management.statTrees.view.edit', $statTree) }}">{{ $statTree->name }}</a>
+                                        @if($statTree->is_template)
+                                            <span class="mt-1 text-sm bg-info rounded text-white d-inline-block px-2 py-1 ml-1 font-weight-bold">TEMPLATE</span>
+                                        @endif
+                                    </td>
+                                    <td>{{ $statTree->model }}</td>
+                                    <td>{{ $statTree->slug }}</td>
+                                    <td>
+                                        @if($statTree->pro)
+                                            {{$statTree->pro->displayName()}}
+                                        @endif
+                                        @if($statTree->pro_scope_clause)
+                                            <div class="text-secondary text-sm">{{$statTree->pro_scope_clause}}</div>
+                                        @endif
+                                    </td>
+                                    <td>
+                                        <div class="d-flex align-items-baseline">
+                                            <a href="{{ route('practice-management.statTrees.view.edit', $statTree) }}">Edit</a>
+                                            @if($statTree->is_template)
+                                                <span class="mx-2 text-secondary text-sm">|</span>
+                                                <div moe relative>
+                                                    <a href="#" start show class="text-nowrap">Inst.</a>
+                                                    <form url="{{ route('practice-management.statTrees.instantiate', $statTree) }}" right redir="/practice-management/stat-trees/view/[data]/edit">
+                                                        @csrf
+                                                        <p class="mb-2 font-weight-bold">Instantiate for Pro</p>
+                                                        <div class="mb-2">
+                                                            <label class="text-sm mb-1">Pro</label>
+                                                            <select name="proUid" class="form-control input-sm" provider-search provider-type="hcp">
+                                                                <option value="">--select--</option>
+                                                            </select>
+                                                        </div>
+                                                        <div class="d-flex align-items-center">
+                                                            <button class="btn btn-sm btn-primary mr-2" type="button" submit>Save</button>
+                                                            <button class="btn btn-sm btn-default mr-2 border" type="button" cancel>Cancel</button>
+                                                        </div>
+                                                    </form>
+                                                </div>
+
+                                            @endif
+                                        </div>
+                                    </td>
+                                </tr>
+                            @endforeach
+                            </tbody>
+                        </table>
                     </div>
                 </div>
             </div>

+ 1 - 0
routes/web.php

@@ -324,6 +324,7 @@ Route::middleware('pro.auth')->group(function () {
 
                 Route::get('old', 'StatTreeController@dashboard')->name('dashboard2');
             });
+            Route::post('instantiate/{statTree}', 'StatTreeController@instantiate')->name('instantiate');
         });
         Route::name('statTreeLines.')->prefix('stat-tree-lines/')->group(function () {
             Route::get('', 'StatTreeLineController@list')->name('list');