Passed
Push — master ( 9c82b2...472045 )
by Prateek
07:54 queued 04:44
created

View::getParentDisplay()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 14
rs 9.9666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
namespace Prateekkarki\Laragen\Generators\Backend;
3
4
use Prateekkarki\Laragen\Generators\BaseGenerator;
5
use Prateekkarki\Laragen\Generators\GeneratorInterface;
6
use Prateekkarki\Laragen\Models\DataOption;
7
use Prateekkarki\Laragen\Models\Module;
8
9
class View extends BaseGenerator implements GeneratorInterface
10
{    
11
    protected static $initializeFlag = 0;
12
13
    public function generate()
14
    {
15
        
16
		$viewsToBeGenerated = ['index', 'create', 'edit'];
17
18
		$generatedFiles = [];
19
		
20
        foreach ($viewsToBeGenerated as $view) {
21
            $viewTemplate = $this->buildTemplate('backend/views/' . $view, [
22
                '{{headings}}' 			 => $this->getHeadings(),
23
                '{{moduleDisplayName}}'  => $this->module->getModuleDisplayName(),
24
                '{{modelNameLowercase}}' => str_singular($this->module->getModuleName()),
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

24
                '{{modelNameLowercase}}' => /** @scrutinizer ignore-deprecated */ str_singular($this->module->getModuleName()),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
25
                '{{modelName}}'          => $this->module->getModelName(),
26
                '{{moduleName}}'         => $this->module->getModuleName()
27
            ]);
28
29
            $fullFilePath = $this->getPath("resources/views/backend/" . $this->module->getModuleName()) . "/{$view}.blade.php";
30
            file_put_contents($fullFilePath, $viewTemplate);
31
            $generatedFiles[] =  $fullFilePath;
32
        }
33
34
        $mainMenuFile = $this->getPath("resources/views/backend/includes/")."main_menu.blade.php";
35
36
        if(self::$initializeFlag++ == 0){
37
            $this->initializeFiles([
38
                $mainMenuFile => "backend/views/includes/main_menu",
39
            ]);
40
        }
41
42
        
43
44
        $this->insertIntoFile(
45
            $mainMenuFile,
46
            '{{-- Main Menu --}}',
47
			"\n".'<li class="nav-item dropdown">
48
                    <a href="#" class="nav-link has-dropdown" data-toggle="dropdown"><i class="fas fa-columns"></i> <span> '.str_plural($this->module->getModelName()).' </span></a>
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

48
                    <a href="#" class="nav-link has-dropdown" data-toggle="dropdown"><i class="fas fa-columns"></i> <span> './** @scrutinizer ignore-deprecated */ str_plural($this->module->getModelName()).' </span></a>

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
49
                    <ul class="dropdown-menu">
50
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.create") }}"> Add new '.str_plural($this->module->getModelName()).'</a></li>
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

50
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.create") }}"> Add new './** @scrutinizer ignore-deprecated */ str_plural($this->module->getModelName()).'</a></li>

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
51
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All '.str_plural($this->module->getModelName()).'</a></li>
0 ignored issues
show
Deprecated Code introduced by
The function str_plural() has been deprecated: Str::plural() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

51
                        <li><a class="nav-link" href="{{ route("backend.'.$this->module->getModuleName().'.index") }}">All './** @scrutinizer ignore-deprecated */ str_plural($this->module->getModelName()).'</a></li>

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
52
                    </ul>
53
                </li>'
54
		);
55
        $generatedFiles = array_merge($generatedFiles, $this->formGenerateCreate());
56
        return $generatedFiles;
57
	}
58
59
	public function getHeadings(){
60
        $columns = $this->module->getBackendColumnTitles();
61
        $headings= "";
62
        foreach ($columns as $column) {
63
            $headings .= "<th>".$column."</th>";
64
        }
65
		return $headings;
66
	}
67
68
    public function formGenerateCreate()
69
    {
70
        $viewTemplate = '';
71
        foreach($this->module->getData() as $column => $options){
72
            $columnOptions = new DataOption($column, $options);
73
            $type = $columnOptions->getType();
74
            $viewTemplate .= $this->buildTemplate('backend/views/formelements/'.$type, [
75
                '{{key}}'                   => $column,
76
                '{{display}}'               => $columnOptions->getDisplay(),
77
                '{{options}}'               => $columnOptions->getFormOptions(),
78
                '{{parentModule}}'          => $columnOptions->getParentModule(),
79
                '{{parentModuleSinglular}}' => str_singular($columnOptions->getParentModule()),
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

79
                '{{parentModuleSinglular}}' => /** @scrutinizer ignore-deprecated */ str_singular($columnOptions->getParentModule()),

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
80
                '{{parentDisplay}}'         => $this->getParentDisplay($columnOptions->getParentModule()),
81
                '{{modelNameLowercase}}'    => $this->module->getModelNameLowercase()
82
            ]);
83
        }
84
85
        $formTemplate = $this->buildTemplate('backend/views/formelements/_form', [
86
            '{{createElements}}'             => $viewTemplate,
87
        ]);
88
89
        $formFilePath = $this->getPath("resources/views/backend/" . $this->module->getModuleName()) . "/_form.blade.php";
90
        file_put_contents($formFilePath, $formTemplate);
91
92
        $generatedFiles[] =  $formFilePath;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$generatedFiles was never initialized. Although not strictly required by PHP, it is generally a good practice to add $generatedFiles = array(); before regardless.
Loading history...
93
        return $generatedFiles;
94
95
    }
96
97
    public function getParentDisplay($parentModule)
98
    {
99
        $modules = config('laragen.modules');
100
        $displayColumn = "";
101
        if(isset($modules[$parentModule])){
102
            $module = $modules[$parentModule];
103
            $module = new Module($parentModule, $module);
104
            $displayColumn = $module->getDisplayColumn();
105
        }
106
107
        if  ($parentModule=='users'){
108
            $displayColumn = 'name';
109
        }
110
        return $displayColumn;
111
    }
112
}
113