• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tattersoftware / codeigniter4-workflows / 7572213916

18 Jan 2024 03:25PM UTC coverage: 64.13%. First build
7572213916

Pull #73

github

web-flow
Merge e71ae039e into 5a856276b
Pull Request #73: Bump actions/cache from 3 to 4

413 of 644 relevant lines covered (64.13%)

7.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Controllers/Workflows.php
1
<?php
2

3
namespace Tatter\Workflows\Controllers;
4

5
use CodeIgniter\HTTP\RedirectResponse;
6
use Tatter\Workflows\Factories\ActionFactory;
7
use Tatter\Workflows\Models\StageModel;
8
use Tatter\Workflows\Models\WorkflowModel;
9

10
class Workflows extends BaseController
11
{
12
    /**
13
     * Displays a list of available Workflows.
14
     */
15
    public function index(): string
16
    {
17
        /** @var WorkflowModel $workflows */
18
        $workflows = model(WorkflowModel::class);
×
19

20
        $data = [
×
21
            'layout'    => config('Layouts')->manage,
×
22
            'workflows' => $workflows->orderBy('name')->findAll(),
×
23
        ];
×
24

25
        // Prefetch the stages
26
        $data['stages'] = $workflows->fetchStages($data['workflows']);
×
27

28
        return view('Tatter\Workflows\Views\workflows\index', $data);
×
29
    }
30

31
    /**
32
     * Shows details for one Workflow.
33
     */
34
    public function show(string $workflowId): string
35
    {
36
        $data = [
×
37
            'config'    => config('Workflows'),
×
38
            'layout'    => config('Layouts')->manage,
×
39
            'workflow'  => model(WorkflowModel::class)->find($workflowId),
×
40
            'workflows' => model(WorkflowModel::class)->orderBy('name', 'asc')->findAll(),
×
41
            'actions'   => ActionFactory::getAllAttributes(),
×
42
        ];
×
43

44
        // Add the stages
45
        $data['stages'] = $data['workflow']->stages;
×
46

47
        return view('Tatter\Workflows\Views\workflows\show', $data);
×
48
    }
49

50
    /**
51
     * Displays the form for a new Workflow.
52
     */
53
    public function new(): string
54
    {
55
        $data = [
×
56
            'layout'  => config('Layouts')->manage,
×
57
            'actions' => ActionFactory::getAllAttributes(),
×
58
        ];
×
59

60
        // Prepare action data to be JSON encoded for JSSortable
61
        $json = [];
×
62

63
        foreach ($data['actions'] as $action) {
×
64
            $json[$action['id']] = $action;
×
65
        }
66

67
        $data['json'] = json_encode($json);
×
68

69
        return view('Tatter\Workflows\Views\workflows\new', $data);
×
70
    }
71

72
    /**
73
     * Creates a Workflow from the new form data.
74
     */
75
    public function create(): RedirectResponse
76
    {
77
        // Validate
78
        $rules = [
×
79
            'name'    => 'required|max_length[255]',
×
80
            'summary' => 'required|max_length[255]',
×
81
            'actions' => 'required',
×
82
        ];
×
83

84
        if (! $this->validate($rules)) {
×
85
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
×
86
        }
87

88
        // Try to create the workflow
89
        $workflow = $this->request->getPost();
×
90
        if (! $workflowId = model(WorkflowModel::class)->insert($workflow, true)) {
×
91
            return redirect()->back()->withInput()->with('errors', model(WorkflowModel::class)->errors());
×
92
        }
93

94
        // Create action-to-workflow stages
95
        foreach (explode(',', $this->request->getPost('actions')) as $actionId) {
×
96
            $stage = [
×
97
                'workflow_id' => $workflowId,
×
98
                'action_id'   => $actionId,
×
99
            ];
×
100

101
            model(StageModel::class)->insert($stage);
×
102
        }
103

104
        return redirect()->to('/workflows/' . $workflowId)->with('success', lang('Workflows.newWorkflowSuccess'));
×
105
    }
106

107
    /**
108
     * Update workflow details.
109
     */
110
    public function update(string $workflowId): RedirectResponse
111
    {
112
        // Validate
113
        $rules = [
×
114
            'name'    => 'required|max_length[255]',
×
115
            'summary' => 'required|max_length[255]',
×
116
        ];
×
117
        if (! $this->validate($rules)) {
×
118
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
×
119
        }
120

121
        // try to update the workflow
122
        $workflow = $this->request->getPost();
×
123
        if (! model(WorkflowModel::class)->update($workflowId, $workflow)) {
×
124
            return redirect()->back()->withInput()->with('errors', model(WorkflowModel::class)->errors());
×
125
        }
126

127
        return redirect()->to('/workflows/' . $workflowId)->with('success', lang('Workflows.updateWorkflowSuccess'));
×
128
    }
129

130
    /**
131
     * Delete the workflow (soft).
132
     */
133
    public function delete(string $workflowId): RedirectResponse
134
    {
135
        model(WorkflowModel::class)->delete($workflowId);
×
136

137
        return redirect()->to('/workflows')->with('success', lang('Workflows.deletedWorkflowSuccess'));
×
138
    }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc