Completed
Pull Request — master (#489)
by Richard
10:59
created

forms.php ➔ profile_getUserForm()   F

Complexity

Conditions 25
Paths 13056

Size

Total Lines 125
Code Lines 81

Duplication

Lines 3
Ratio 2.4 %
Metric Value
cc 25
eloc 81
nc 13056
nop 3
dl 3
loc 125
rs 2

How to fix   Long Method    Complexity   

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Kernel\Handlers\XoopsUser;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsUser.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
/**
15
 * Extended User Profile
16
 *
17
 * @copyright       XOOPS Project (http://xoops.org)
18
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @package         profile
20
 * @since           2.3.0
21
 * @author          Jan Pedersen
22
 * @author          Taiwen Jiang <[email protected]>
23
 * @version         $Id$
24
 */
25
26
/**
27
 * Get {@link Xoops\Form\ThemeForm} for registering new users
28
 *
29
 * @param XoopsUser $user
30
 * @param $profile
31
 * @param null $step
32
 * @return Xoops\Form\ThemeForm
33
 */
34
function profile_getRegisterForm(XoopsUser $user, $profile, $step = null)
0 ignored issues
show
Coding Style introduced by
profile_getRegisterForm uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
profile_getRegisterForm uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
35
{
36
    $xoops = Xoops::getInstance();
37
    $action = $_SERVER['REQUEST_URI'];
38
    $step_no = $step['step_no'];
39
    $use_token = $step['step_no'] > 0 ? true : false;
40
    $reg_form = new Xoops\Form\ThemeForm($step['step_name'], 'regform', $action, 'post', $use_token);
41
42
    if ($step['step_desc']) {
43
        $reg_form->addElement(new Xoops\Form\Label('', $step['step_desc']));
44
    }
45
46
    if ($step_no == 1) {
47
        //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
48
49
        $elements[0][] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
50
            'element' => new Xoops\Form\Text(XoopsLocale::USERNAME, 'uname', 40, $xoops->getConfig('maxuname'), $user->getVar('uname', 'e')),
51
            'required' => true
52
        );
53
        $weights[0][] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$weights was never initialized. Although not strictly required by PHP, it is generally a good practice to add $weights = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
54
55
        $elements[0][] = array(
56
            'element' => new Xoops\Form\Text(XoopsLocale::EMAIL, 'email', 40, 160, $user->getVar('email', 'e')), 'required' => true
57
        );
58
        $weights[0][] = 0;
59
60
        $elements[0][] =
61
            array('element' => new Xoops\Form\Password(XoopsLocale::PASSWORD, 'pass'), 'required' => true);
62
        $weights[0][] = 0;
63
64
        $elements[0][] =
65
            array('element' => new Xoops\Form\Password(XoopsLocale::VERIFY_PASSWORD, 'vpass'), 'required' => true);
66
        $weights[0][] = 0;
67
    }
68
69
    // Dynamic fields
70
    /* @var $profile_handler ProfileProfileHandler */
71
    $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
72
    $fields = $profile_handler->loadFields();
73
    $_SESSION['profile_required'] = array();
74
    $weights = array();
75
    /* @var ProfileField $field */
76
    foreach ($fields as $field) {
77
        if ($field->getVar('step_id') == $step['step_id']) {
78
            $fieldinfo['element'] = $field->getEditElement($user, $profile);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fieldinfo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fieldinfo = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
            //assign and check (=)
80
            if ($fieldinfo['required'] = $field->getVar('field_required')) {
0 ignored issues
show
Bug introduced by
The variable $fieldinfo does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
                $_SESSION['profile_required'][$field->getVar('field_name')] = $field->getVar('field_title');
82
            }
83
84
            $key = $field->getVar('cat_id');
85
            $elements[$key][] = $fieldinfo;
0 ignored issues
show
Bug introduced by
The variable $elements does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
86
            $weights[$key][] = $field->getVar('field_weight');
87
        }
88
    }
89
    ksort($elements);
90
91
    foreach (array_keys($elements) as $k) {
92
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
93 View Code Duplication
        foreach (array_keys($elements[$k]) as $i) {
94
            $reg_form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
95
        }
96
    }
97
    //end of Dynamic User fields
98
    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
99
    if ($step_no == 1 && $xoops->getConfig('reg_dispdsclmr') != 0 && $xoops->getConfig('reg_disclaimer') != '') {
100
        $disc_tray = new Xoops\Form\ElementTray(XoopsLocale::DISCLAIMER, '<br />');
101
        $disc_text = new Xoops\Form\Label("", "<div class=\"pad5\">" . $myts->displayTarea($xoops->getConfig('reg_disclaimer'), 1) . "</div>");
102
        $disc_tray->addElement($disc_text);
103
        $agree_chk = new Xoops\Form\Checkbox('', 'agree_disc');
104
        $agree_chk->addOption(1, XoopsLocale::I_AGREE_TO_THE_ABOVE);
105
        $disc_tray->addElement($agree_chk);
106
        $reg_form->addElement($disc_tray);
107
    }
108
109
    if ($step_no == 1) {
110
        $reg_form->addElement(new Xoops\Form\Captcha(), true);
111
    }
112
113
    $reg_form->addElement(new Xoops\Form\Hidden('uid', $user->getVar('uid')));
114
    $reg_form->addElement(new Xoops\Form\Hidden('step', $step_no));
115
    $reg_form->addElement(new Xoops\Form\Button('', 'submitButton', XoopsLocale::A_SUBMIT, 'submit'));
116
    return $reg_form;
117
}
118
119
120
/**
121
 * Get {@link Xoops\Form\ThemeForm} for editing a user
122
 *
123
 * @param XoopsUser $user
124
 * @param ProfileProfile|null $profile
125
 * @param bool $action
126
 * @return Xoops\Form\ThemeForm
127
 */
128
function profile_getUserForm(XoopsUser $user, ProfileProfile $profile = null, $action = false)
0 ignored issues
show
Coding Style introduced by
profile_getUserForm uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
129
{
130
    $xoops = Xoops::getInstance();
131
132
    if ($action === false) {
133
        $action = $_SERVER['REQUEST_URI'];
134
    }
135
136
    $title = $user->isNew() ? _PROFILE_AM_ADDUSER : XoopsLocale::EDIT_PROFILE;
137
138
    $form = new Xoops\Form\ThemeForm($title, 'userinfo', $action, 'post', true);
139
140
    /* @var $profile_handler ProfileProfileHandler */
141
    $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
142
    // Dynamic fields
143
    if (!$profile) {
144
        $profile = $profile_handler->getProfile($user->getVar('uid'));
145
    }
146
    // Get fields
147
    $fields = $profile_handler->loadFields();
148
149
    // Get ids of fields that can be edited
150
    $gperm_handler = $xoops->getHandlerGroupPermission();
151
    $editable_fields = $gperm_handler->getItemIds('profile_edit', $xoops->user->getGroups(), $xoops->module->getVar('mid'));
152
153
    if ($user->isNew() || $xoops->user->isAdmin()) {
154
        $elements[0][] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
155
            'element' => new Xoops\Form\Text(XoopsLocale::USERNAME, 'uname', 40, $xoops->user->isAdmin() ? 60
156
                    : $xoops->getConfig('maxuname'), $user->getVar('uname', 'e')), 'required' => 1
157
        );
158
        $email_text = new Xoops\Form\Text('', 'email', 40, 160, $user->getVar('email'));
159
    } else {
160
        $elements[0][] = array('element' => new Xoops\Form\Label(XoopsLocale::USERNAME, $user->getVar('uname')), 'required' => 0);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
161
        $email_text = new Xoops\Form\Label('', $user->getVar('email'));
162
    }
163
    $email_tray = new Xoops\Form\ElementTray(XoopsLocale::EMAIL, '<br />');
164
    $email_tray->addElement($email_text, ($user->isNew() || $xoops->user->isAdmin()) ? 1 : 0);
165
    $weights[0][] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$weights was never initialized. Although not strictly required by PHP, it is generally a good practice to add $weights = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
166
    $elements[0][] = array('element' => $email_tray, 'required' => 0);
167
    $weights[0][] = 0;
168
169
    if ($xoops->user->isAdmin() && $user->getVar('uid') != $xoops->user->getVar('uid')) {
170
        //If the user is an admin and is editing someone else
171
        $pwd_text = new Xoops\Form\Password('', 'password');
172
        $pwd_text2 = new Xoops\Form\Password('', 'vpass');
173
        $pwd_tray = new Xoops\Form\ElementTray(XoopsLocale::PASSWORD . '<br />' . XoopsLocale::TYPE_NEW_PASSWORD_TWICE_TO_CHANGE_IT);
174
        $pwd_tray->addElement($pwd_text);
175
        $pwd_tray->addElement($pwd_text2);
176
        $elements[0][] = array('element' => $pwd_tray, 'required' => 0); //cannot set an element tray required
177
        $weights[0][] = 0;
178
179
        $level_radio = new Xoops\Form\Radio(_PROFILE_MA_USERLEVEL, 'level', $user->getVar('level'));
180
        $level_radio->addOption(1, _PROFILE_MA_ACTIVE);
181
        $level_radio->addOption(0, _PROFILE_MA_INACTIVE);
182
        //$level_radio->addOption(-1, _PROFILE_MA_DISABLED);
183
        $elements[0][] = array('element' => $level_radio, 'required' => 0);
184
        $weights[0][] = 0;
185
    }
186
187
    $elements[0][] = array('element' => new Xoops\Form\Hidden('uid', $user->getVar('uid')), 'required' => 0);
188
    $weights[0][] = 0;
189
    $elements[0][] = array('element' => new Xoops\Form\Hidden('op', 'save'), 'required' => 0);
190
    $weights[0][] = 0;
191
192
    $cat_handler = \Xoops::getModuleHelper('profile')->getHandler('category');
193
    $categories = array();
194
    $all_categories = $cat_handler->getObjects(null, true, false);
195
    $count_fields = count($fields);
196
    /* @var ProfileField $field */
197
    foreach ($fields as $field) {
198
        if (in_array($field->getVar('field_id'), $editable_fields)) {
199
            // Set default value for user fields if available
200
            if ($user->isNew()) {
201
                $default = $field->getVar('field_default');
202
                if ($default !== '' && $default !== null) {
203
                    $user->setVar($field->getVar('field_name'), $default);
204
                }
205
            }
206
207
            if ($profile->getVar($field->getVar('field_name'), 'n') === null) {
208
                $default = $field->getVar('field_default', 'n');
209
                $profile->setVar($field->getVar('field_name'), $default);
0 ignored issues
show
Bug introduced by
It seems like $profile is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
210
            }
211
212
            $fieldinfo['element'] = $field->getEditElement($user, $profile);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$fieldinfo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $fieldinfo = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
213
            $fieldinfo['required'] = $field->getVar('field_required');
0 ignored issues
show
Bug introduced by
The variable $fieldinfo does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
214
215
            $key = @$all_categories[$field->getVar('cat_id')]['cat_weight'] * $count_fields + $field->getVar('cat_id');
216
            $elements[$key][] = $fieldinfo;
217
            $weights[$key][] = $field->getVar('field_weight');
218
            $categories[$key] = @$all_categories[$field->getVar('cat_id')];
219
        }
220
    }
221
222
    if ($xoops->isUser() && $xoops->user->isAdmin()) {
223
        $xoops->loadLanguage('admin', 'profile');
224
        $gperm_handler = $xoops->getHandlerGroupPermission();
225
        //If user has admin rights on groups
226
        include_once $xoops->path('modules/system/constants.php');
227
        if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $xoops->user->getGroups(), 1)) {
228
            //add group selection
229
            $group_select = new Xoops\Form\SelectGroup(XoopsLocale::USER_GROUPS, 'groups', false, $user->getGroups(), 5, true);
230
            $elements[0][] = array('element' => $group_select, 'required' => 0);
231
            //set as latest;
232
            $weights[0][] = $count_fields + 1;
233
        }
234
    }
235
236
    ksort($elements);
237
    foreach (array_keys($elements) as $k) {
238
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
239
        $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
240
        $desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
241
        //$form->addElement(new Xoops\Form\Label("<div class='break'>{$title}</div>", $desc), false);
242
        $desc = ($desc != '' ? ' - ' . $desc : '');
243
        $form->insertBreak($title . $desc);
244 View Code Duplication
        foreach (array_keys($elements[$k]) as $i) {
245
            $form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
246
        }
247
    }
248
249
    $form->addElement(new Xoops\Form\Hidden('uid', $user->getVar('uid')));
250
    $form->addElement(new Xoops\Form\Button('', 'submit', XoopsLocale::SAVE_CHANGES, 'submit'));
251
    return $form;
252
}
253