WidgetsController::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php namespace Anomaly\DashboardModule\Http\Controller\Admin;
2
3
use Anomaly\ConfigurationModule\Configuration\Form\ConfigurationFormBuilder;
4
use Anomaly\DashboardModule\Widget\Contract\WidgetInterface;
5
use Anomaly\DashboardModule\Widget\Contract\WidgetRepositoryInterface;
6
use Anomaly\DashboardModule\Widget\Extension\Form\WidgetExtensionFormBuilder;
7
use Anomaly\DashboardModule\Widget\Extension\WidgetExtension;
8
use Anomaly\DashboardModule\Widget\Form\WidgetFormBuilder;
9
use Anomaly\DashboardModule\Widget\Table\WidgetTableBuilder;
10
use Anomaly\Streams\Platform\Addon\Extension\ExtensionCollection;
11
use Anomaly\Streams\Platform\Http\Controller\AdminController;
12
13
/**
14
 * Class WidgetsController
15
 *
16
 * @link          http://pyrocms.com/
17
 * @author        PyroCMS, Inc. <[email protected]>
18
 * @author        Ryan Thompson <[email protected]>
19
 */
20
class WidgetsController extends AdminController
21
{
22
23
    /**
24
     * Display an index of existing entries.
25
     *
26
     * @param  WidgetTableBuilder                         $table
27
     * @return \Symfony\Component\HttpFoundation\Response
28
     */
29
    public function index(WidgetTableBuilder $table)
30
    {
31
        return $table->render();
32
    }
33
34
    /**
35
     * Return the modal for choosing a widget.
36
     *
37
     * @param  ExtensionCollection                   $extensions
38
     * @return \Illuminate\Contracts\View\View|mixed
39
     */
40
    public function choose(ExtensionCollection $extensions)
41
    {
42
        return $this->view->make(
43
            'module::admin/widgets/choose',
44
            ['widgets' => $extensions->search('anomaly.module.dashboard::widget.*')]
45
        );
46
    }
47
48
    /**
49
     * Create a new entry.
50
     *
51
     * @param  ExtensionCollection                          $extensions
52
     * @param  WidgetExtensionFormBuilder|WidgetFormBuilder $form
53
     * @param  WidgetFormBuilder                            $widget
54
     * @param  ConfigurationFormBuilder                     $configuration
55
     * @return \Symfony\Component\HttpFoundation\Response
56
     */
57
    public function create(
58
        ExtensionCollection $extensions,
59
        WidgetExtensionFormBuilder $form,
60
        WidgetFormBuilder $widget,
61
        ConfigurationFormBuilder $configuration
62
    ) {
63
        /* @var WidgetExtension $extension */
64
        $extension = $extensions->get($this->request->get('widget'));
65
66
        $form->addForm('widget', $widget->setExtension($extension));
67
        $form->addForm('configuration', $configuration->setEntry($extension->getNamespace()));
68
69
        return $form->render();
70
    }
71
72
    /**
73
     * Edit an existing entry.
74
     *
75
     * @param  ExtensionCollection                          $extensions
76
     * @param  WidgetExtensionFormBuilder|WidgetFormBuilder $form
77
     * @param  WidgetFormBuilder                            $widget
78
     * @param  ConfigurationFormBuilder                     $configuration
79
     * @return \Symfony\Component\HttpFoundation\Response
80
     */
81
    public function edit(
82
        ExtensionCollection $extensions,
83
        WidgetExtensionFormBuilder $form,
84
        WidgetFormBuilder $widget,
85
        ConfigurationFormBuilder $configuration,
86
        WidgetRepositoryInterface $widgets,
87
        $id
88
    ) {
89
        /* @var WidgetInterface $entry */
90
        $entry = $widgets->find($id);
91
92
        /* @var WidgetExtension $extension */
93
        $extension = $entry->getExtension();
94
95
        $form->setEntry($id);
96
        $form->addForm('widget', $widget->setEntry($id));
97
        $form->addForm('configuration', $configuration->setScope($id)->setEntry($extension->getNamespace()));
98
99
        return $form->render();
100
    }
101
102
    public function save(WidgetRepositoryInterface $widgets)
103
    {
104
        foreach (json_decode($this->request->get('columns')) as $column => $columns) {
105
            foreach ($columns as $position => $widget) {
106
                if ($widget = $widgets->find($widget->id)) {
107
108
                    $widget->setAttribute('column', $column + 1);
109
                    $widget->setAttribute('sort_order', $position + 1);
110
111
                    $widgets->save($widget);
112
                }
113
            }
114
        }
115
    }
116
}
117