AppCtrlCustomField::toolbar()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 12
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
// -------------------------------------------------------------------------
4
// OVIDENTIA http://www.ovidentia.org
5
// Ovidentia is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
8
// any later version.
9
//
10
// This program is distributed in the hope that it will be useful, but
11
// WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with this program; if not, write to the Free Software
17
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
18
// USA.
19
// -------------------------------------------------------------------------
20
/**
21
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
22
 * @copyright Copyright (c) 2022 by SI4YOU ({@link https://www.siforyou.com})
23
 */
24
namespace Capwelton\LibApp\Ctrl;
25
26
use Capwelton\Widgets\Widgets\Page\WidgetPage;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Page\WidgetPage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Capwelton\Widgets\Widgets\Table\WidgetTableModelView;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widget...le\WidgetTableModelView was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Capwelton\LibApp\Ui\AppToolbar;
29
use Capwelton\LibApp\Exceptions\AppAccessException;
30
use Capwelton\LibApp\Ui\Page\AppPage;
31
use Capwelton\Widgets\Widgets\Item\WidgetLink;
0 ignored issues
show
Bug introduced by
The type Capwelton\Widgets\Widgets\Item\WidgetLink was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use Capwelton\LibApp\Set\AppCustomField;
33
use Capwelton\LibOrm\MySql\ORMMySqlBackend;
0 ignored issues
show
Bug introduced by
The type Capwelton\LibOrm\MySql\ORMMySqlBackend was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
35
/**
36
 * This controller manages actions that can be performed on custom fields
37
 * @method self proxy()
38
 */
39
class AppCtrlCustomField extends AppCtrlRecord
40
{
41
    
42
    protected function toolbar(WidgetTableModelView $tableView)
43
    {
44
        $W = bab_Widgets();
45
        $App = $this->App();
46
        
47
        $toolbar = new AppToolbar();
48
        $toolbar->addItem($W->Link($App->translate('Add a custom field'), $this->proxy()
49
            ->edit())
50
            ->addClass('icon', \Func_Icons::ACTIONS_LIST_ADD)
51
            ->setOpenMode(WidgetLink::OPEN_DIALOG_AND_RELOAD));
52
        
53
        return $toolbar;
54
    }
55
    
56
    /**
57
     * {@inheritdoc}
58
     * @see AppCtrlRecord::display()
59
     */
60
    public function display($id)
61
    {
62
    }
63
    
64
    /**
65
     * Display form to add a custom field on specified object class.
66
     *
67
     * @param string $object
68
     *            'Contact', 'Organization'...
69
     * @throws AppAccessException::
70
     * @return AppPage
71
     */
72
    public function add($object = null)
73
    {
74
        $App = $this->App();
75
        $Ui = $App->Ui();
76
        $page = $Ui->Page();
77
        
78
        $page->addClass('app-page-editor');
79
        $page->setTitle($App->translate('Add a custom field'));
80
        
81
        $set = $App->CustomFieldSet();
82
        $customfield = $set->newRecord();
83
        $customfield->object = $object;
84
        
85
        $form = $Ui->CustomFieldEditor($customfield);
86
        
87
        $page->addItem($form);
88
        
89
        return $page;
90
    }
91
    
92
    /**
93
     * @return WidgetPage
94
     */
95
    public function edit($customfield = null)
96
    {
97
        $W = bab_Widgets();
98
        $App = $this->App();
99
        $Ui = $App->Ui();
100
        $page = $Ui->Page();
101
        
102
        $page->addClass('app-page-editor');
103
        $page->setTitle($App->translate('Edit custom field'));
104
        
105
        if(null !== $customfield){
106
            $set = $App->CustomFieldSet();
107
            $customfield = $set->request($customfield);
108
        }
109
        
110
        $form = $Ui->CustomFieldEditor($customfield);
111
        
112
        $page->addItem($form);
113
        
114
        if($customfield instanceof AppCustomField){
115
            $actionsFrame = $page->ActionsFrame();
116
            $page->addContextItem($actionsFrame);
117
            
118
            $actionsFrame->addItem($W->Link($W->Icon($App->translate('Delete'), \Func_Icons::ACTIONS_EDIT_DELETE), $this->proxy()
0 ignored issues
show
Bug introduced by
$this->proxy()->delete($customfield->id) of type true is incompatible with the type Widget_Action|bab_url|string expected by parameter $url of Func_Widgets::Link(). ( Ignorable by Annotation )

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

118
            $actionsFrame->addItem($W->Link($W->Icon($App->translate('Delete'), \Func_Icons::ACTIONS_EDIT_DELETE), /** @scrutinizer ignore-type */ $this->proxy()
Loading history...
119
                ->delete($customfield->id)));
120
        }
121
        
122
        return $page;
123
    }
124
    
125
    /**
126
     * @return bool
127
     */
128
    public function save($customfield = null)
129
    {
130
        $App = $this->App();
131
        
132
        $set = $App->CustomFieldSet();
133
        
134
        if(empty($customfield['id'])){
135
            $record = $set->newRecord();
136
        }
137
        else{
138
            $record = $set->get($customfield['id']);
139
        }
140
        
141
        if('Enum' === $customfield['fieldtype'] || 'Set' === $customfield['fieldtype']){
142
            $enumvalues = array();
143
            foreach ($customfield['enumvalues'] as $enumkey => $enumvalue){
144
                $enumvalues[$enumkey] = $set->enumvalues->input($enumvalue);
145
            }
146
            $record->enumvalues = serialize($enumvalues);
147
        }
148
        else{
149
            $record->enumvalues = '';
150
        }
151
        
152
        unset($customfield['enumvalues']);
153
        $record->setFormInputValues($customfield);
154
        
155
        $record->save();
156
        
157
        // refresh target table structure
158
        
159
        $object = $record->object . 'Set';
160
        $mysqlbackend = new ORMMySqlBackend($GLOBALS['babDB']);
161
        
162
        $recordSet = $App->$object();
163
        if(method_exists($recordSet, 'useLang')){
164
            // This is necessary if the recordSet constructor uses a setLang().
165
            // We need to revert to multilang fields before synchronizing.
166
            $recordSet->useLang(false);
167
        }
168
        
169
        $sql = $mysqlbackend->setToSql($recordSet);
170
        
171
        require_once $GLOBALS['babInstallPath'] . 'utilit/devtools.php';
172
        $synchronize = new \bab_synchronizeSql();
173
        $synchronize->fromSqlString($sql);
174
        
175
        return true;
176
    }
177
    
178
    /**
179
     * {@inheritdoc}
180
     * @see AppCtrlRecord::delete()
181
     */
182
    public function delete($customfield)
183
    {
184
        $App = $this->App();
185
        
186
        if(! $customfield){
187
            throw new AppAccessException($App->translate('Access denied'));
188
        }
189
        
190
        $set = $App->CustomFieldSet();
191
        $record = $set->request($customfield);
192
        $object = $record->object . 'Set';
193
        $set->delete($set->id->is($customfield));
194
        
195
        $recordSet = $App->$object();
196
        if(method_exists($recordSet, 'useLang')){
197
            // This is necessary if the recordSet constructor uses a setLang().
198
            // We need to revert to multilang fields before synchronizing.
199
            $recordSet->useLang(false);
200
        }
201
        
202
        $mysqlbackend = new ORMMySqlBackend($GLOBALS['babDB']);
203
        $sql = $mysqlbackend->setToSql($recordSet);
204
        
205
        require_once $GLOBALS['babInstallPath'] . 'utilit/devtools.php';
206
        $synchronize = new \bab_synchronizeSql();
207
        $synchronize->fromSqlString($sql);
208
        
209
        return true;
210
    }
211
}