Passed
Push — master ( 6fc9bf...10dc5f )
by Laurent
01:33
created

app_CtrlCustomSection::edit()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 114
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 78
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 114
rs 8.48

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//-------------------------------------------------------------------------
3
// OVIDENTIA http://www.ovidentia.org
4
// Ovidentia is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2, or (at your option)
7
// any later version.
8
//
9
// This program is distributed in the hope that it will be useful, but
10
// WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
// See the GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
// USA.
18
//-------------------------------------------------------------------------
19
/**
20
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
21
 * @copyright Copyright (c) 2008 by CANTICO ({@link http://www.cantico.fr})
22
 */
23
24
25
require_once APP_CTRL_PATH . '/record.ctrl.php';
26
27
28
/**
29
 * This controller manages actions that can be performed on custom sections
30
 *
31
 * @method Func_App App()
32
 */
33
class app_CtrlCustomSection extends app_CtrlRecord
34
{
35
    /**
36
     * Returns an array of field names and descriptions that can be used in Full/CardFrames.
37
     *
38
     * @return string[]
39
     */
40
    public function getAvailableDisplayFields()
41
    {
42
        $recordSet = $this->getRecordSet();
43
44
        $availableFields = array();
45
46
        foreach ($recordSet->getFields() as $name => $field) {
47
            $availableFields[$name] = array(
48
                'name' => $field->getName(),
49
                'description' => $field->getDescription()
50
            );
51
        }
52
53
        return $availableFields;
54
    }
55
56
57
    /**
58
     *
59
     */
60
    public function addDisplayField($id = null, $filter = null)
61
    {
62
        $W = bab_Widgets();
0 ignored issues
show
Bug introduced by
The function bab_Widgets was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

62
        $W = /** @scrutinizer ignore-call */ bab_Widgets();
Loading history...
63
        $App = $this->App();
64
65
        $customSectionSet = $App->CustomSectionSet();
66
67
        $customSection = $customSectionSet->request($customSectionSet->id->is($id));
0 ignored issues
show
Unused Code introduced by
The call to app_CustomSectionSet::request() has too many arguments starting with $customSectionSet->id->is($id). ( Ignorable by Annotation )

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

67
        /** @scrutinizer ignore-call */ $customSection = $customSectionSet->request($customSectionSet->id->is($id));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
68
69
        $page = $App->Ui()->Page();
70
        $page->setTitle($App->translate('Add field'));
71
72
        $availableFields = $this->getAvailableDisplayFields();
73
74
        $box = $W->VBoxItems();
75
76
        foreach ($availableFields as $field) {
77
            $fieldName =  $field['name'];
78
            $fieldDescription = $field['description'];
79
            if (substr($fieldName, 0, 1) !== '_') {
80
                $fieldDescription = $App->translate($fieldDescription);
81
            }
82
            $box->addItem(
83
                $W->VBoxItems(
84
                    $W->Link(
85
                        $fieldDescription,
86
                        $this->proxy()->saveDisplayField($customSection->id, $fieldName)
87
                    )->addClass('widget-strong', 'widget-close-dialog')
88
                    ->setAjaxAction(),
89
                    $W->Label($fieldName)->addClass('widget-small'),
90
                    $W->Hidden()->setName(array('sections', $field['name']))
91
                )->setSizePolicy('widget-list-element')
92
            );
93
        }
94
95
        $page->addItem($box);
96
        return $page;
97
    }
98
99
100
    /**
101
     *
102
     * @param int $section
103
     * @param string $fieldName
104
     * @return boolean
105
     */
106
    public function saveDisplayField($section, $fieldName)
107
    {
108
        $App = $this->App();
109
        $customSectionSet = $App->CustomSectionSet();
110
111
        $customSection = $customSectionSet->request($customSectionSet->id->is($section));
0 ignored issues
show
Unused Code introduced by
The call to app_CustomSectionSet::request() has too many arguments starting with $customSectionSet->id->is($section). ( Ignorable by Annotation )

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

111
        /** @scrutinizer ignore-call */ $customSection = $customSectionSet->request($customSectionSet->id->is($section));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
        $customSection->addField($fieldName);
113
        $customSection->save();
114
115
        $this->addReloadSelector('.depends-custom-sections');
116
117
        return true;
118
    }
119
120
121
    /**
122
     *
123
     * @param int $section
124
     * @param string $fieldName
125
     * @return boolean
126
     */
127
    public function removeDisplayField($section, $fieldName)
128
    {
129
        $App = $this->App();
130
        $customSectionSet = $App->CustomSectionSet();
131
132
        $customSection = $customSectionSet->request($customSectionSet->id->is($section));
0 ignored issues
show
Unused Code introduced by
The call to app_CustomSectionSet::request() has too many arguments starting with $customSectionSet->id->is($section). ( Ignorable by Annotation )

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

132
        /** @scrutinizer ignore-call */ $customSection = $customSectionSet->request($customSectionSet->id->is($section));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
133
134
        $customSection->removeField($fieldName);
135
        $customSection->save();
136
        $this->addReloadSelector('.depends-custom-sections');
137
        return true;
138
    }
139
140
141
142
143
    /**
144
     * @param string    $view
145
     * @param int       $id
146
     * @param string    $object
147
     * @return app_Page
148
     */
149
    public function edit($view, $id = null, $object = null)
150
    {
151
        $App = $this->App();
152
        $W = bab_Widgets();
0 ignored issues
show
Bug introduced by
The function bab_Widgets was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

152
        $W = /** @scrutinizer ignore-call */ bab_Widgets();
Loading history...
153
154
        $customSectionSet = $App->CustomSectionSet();
155
        if (isset($id)) {
156
            $record = $customSectionSet->request($id);
0 ignored issues
show
Unused Code introduced by
The call to app_CustomSectionSet::request() has too many arguments starting with $id. ( Ignorable by Annotation )

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

156
            /** @scrutinizer ignore-call */ $record = $customSectionSet->request($id);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
157
        } else {
158
            $record = $customSectionSet->newRecord();
159
            $record->object = $object;
160
        }
161
162
        $page = $App->Ui()->Page();
163
164
        $page->setTitle($App->translate('Section'));
165
        $page->addClass('app-page-editor');
166
167
        $editor = new app_Editor($App);
168
        $editor->setHiddenValue('tg', $App->controllerTg);
0 ignored issues
show
Unused Code introduced by
The call to app_Editor::setHiddenValue() has too many arguments starting with 'tg'. ( Ignorable by Annotation )

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

168
        $editor->/** @scrutinizer ignore-call */ setHiddenValue('tg', $App->controllerTg);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
169
        $editor->setHiddenValue('data[view]', $view);
170
        if ($record->id) {
171
            $editor->setHiddenValue('data[id]', $record->id);
172
        }
173
        $editor->setName('data');
174
        $editor->addItem(
175
            $W->Hidden()->setName('object')
176
        );
177
178
        $editor->addItem(
179
            $W->LabelledWidget(
180
                $App->translate('Name'),
181
                $W->LineEdit()->setName('name')
182
            )
183
        );
184
185
        $sizePolicyClassnames = array(
186
            'col-md-1' => '1',
187
            'col-md-2' => '2',
188
            'col-md-3' => '3',
189
            'col-md-4' => '4',
190
            'col-md-5' => '5',
191
            'col-md-6' => '6',
192
            'col-md-7' => '7',
193
            'col-md-8' => '8',
194
            'col-md-9' => '9',
195
            'col-md-10' => '10',
196
            'col-md-11' => '11',
197
            'col-md-12' => '12',
198
        );
199
200
201
        $editor->addItem(
202
            $W->LabelledWidget(
203
                $App->translate('Size policy'),
204
                $W->Select()
205
                    ->addOptions($sizePolicyClassnames)
206
                    ->setName('sizePolicy')
207
            )
208
        );
209
        $editor->addItem(
210
            $W->LabelledWidget(
211
                $App->translate('Fields layout'),
212
                $W->Select()
213
                    ->addOptions(app_CustomSection::getFieldsLayouts())
214
                    ->setName('fieldsLayout')
215
            )
216
        );
217
        $editor->addItem(
218
            $W->LabelledWidget(
219
                $App->translate('Foldable'),
220
                $foldableCheckBox = $W->CheckBox()->setName('foldable')
221
            )
222
        );
223
        $editor->addItem(
224
            $foldedWidget = $W->LabelledWidget(
225
                $App->translate('Folded'),
226
                $W->CheckBox()->setName('folded')
227
            )
228
        );
229
        $editor->addItem(
230
            $W->LabelledWidget(
231
                $App->translate('Editable'),
232
                $W->CheckBox()->setName('editable')
233
            )
234
        );
235
        $editor->addItem(
236
            $W->LabelledWidget(
237
                $App->translate('Class'),
238
                $W->LineEdit()->setName('classname')
239
            )
240
        );
241
242
        $foldableCheckBox->setAssociatedDisplayable($foldedWidget, array(true));
243
244
        $editor->setValues($record->getFormOutputValues(), array('data'));
245
246
        $editor->addButton(
247
            $W->SubmitButton(/*'save'*/)
248
                ->validate(true)
249
                ->setAction($this->proxy()->save())
250
                ->setAjaxAction()
251
            ->setLabel($App->translate('Save'))
252
        );
253
254
        $editor->addButton(
255
            $W->SubmitButton(/*'cancel'*/)
256
                ->addClass('widget-close-dialog')
257
            ->setLabel($App->translate('Cancel'))
258
        );
259
260
        $page->addItem($editor);
261
262
        return $page;
263
    }
264
265
266
    /**
267
     *
268
     * @param int $id
269
     * @return app_Page
270
     */
271
    public function editVisibility($id = null)
272
    {
273
        $App = $this->App();
274
275
        $page = $App->Ui()->Page();
276
277
        $page->setTitle($App->translate('Section visibility'));
278
        $page->addClass('app-page-editor');
279
280
        $recordSet = $this->getRecordSet();
281
        $record = $recordSet->request($id);
282
283
        $object = $record->object . 'Set';
284
285
        $objectRecordSet = $App->$object();
286
287
288
        $editor = $App->Ui()->CriteriaEditor($objectRecordSet->all());
0 ignored issues
show
Bug introduced by
The method CriteriaEditor() does not exist on app_Ui. ( Ignorable by Annotation )

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

288
        $editor = $App->Ui()->/** @scrutinizer ignore-call */ CriteriaEditor($objectRecordSet->all());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
289
        $editor->setHiddenValue('data[id]', $record->id);
290
        $editor->setName('data');
291
292
        if (!empty($record->visibilityCriteria)) {
293
            $arrCriteria = json_decode($record->visibilityCriteria, true);
294
295
            $data = array();
296
            foreach ($arrCriteria as $fieldName => $operation) {
297
                $data['field'] = $fieldName;
298
                foreach ($operation as $condition => $value) {
299
                    $data['condition']['default'] = $condition;
300
                    $data['condition']['bool'] = $condition;
301
                    $data['value'] = $value;
302
                }
303
            }
304
305
            $editor->setValues($data, array('data'));
306
        }
307
308
309
        $editor->setSaveAction($this->proxy()->saveVisibility());
310
        $editor->isAjax = bab_isAjaxRequest();
0 ignored issues
show
Bug introduced by
The function bab_isAjaxRequest was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

310
        $editor->isAjax = /** @scrutinizer ignore-call */ bab_isAjaxRequest();
Loading history...
311
312
        $page->addItem($editor);
313
314
        return $page;
315
    }
316
317
318
319
    /**
320
     * {@inheritDoc}
321
     * @see app_Controller::save()
322
     */
323
    public function save($data = null)
324
    {
325
        parent::save($data);
326
327
        $this->addReloadSelector('.depends-custom-sections');
328
329
        return true;
330
    }
331
332
333
    /**
334
     * {@inheritDoc}
335
     * @see app_CtrlRecord::delete()
336
     */
337
    public function delete($id)
338
    {
339
        parent::delete($id);
340
341
        $this->addReloadSelector('.depends-custom-sections');
342
343
        return true;
344
    }
345
346
347
    /**
348
     * @param array $data
349
     */
350
    public function saveVisibility($data = null)
351
    {
352
        $App = $this->App();
353
354
        $recordSet = $this->getRecordSet();
355
        $record = $recordSet->request($data['id']);
356
357
        $object = $record->object . 'Set';
358
359
        $objectRecordSet = $App->$object();
360
361
        $field = $data['field'];
362
363
        $field = $objectRecordSet->$field;
364
365
        $condition = $data['condition'];
366
367
        if ($field instanceof ORM_BoolInterface) {
0 ignored issues
show
Bug introduced by
The type ORM_BoolInterface 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...
368
            $condition = $condition['bool'];
369
        } else {
370
            $condition = $condition['default'];
371
        }
372
373
        $criteria = $field->{$condition}($data['value']);
374
375
        $record->visibilityCriteria = $criteria->toJson();
376
377
        $record->save();
378
379
        $this->addReloadSelector('.depends-custom-sections');
380
381
        return true;
382
    }
383
}
384