insert_update_group.php ➔ insert_update_group()   B
last analyzed

Complexity

Conditions 5
Paths 12

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 31
nc 12
nop 1
dl 0
loc 41
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * The PHP Skeleton App
4
 *
5
 * @author      Goran Halusa <[email protected]>
6
 * @copyright   2015 Goran Halusa
7
 * @link        https://github.com/ghalusa/PHP-Skeleton-App
8
 * @license     https://github.com/ghalusa/PHP-Skeleton-App/wiki/License
9
 * @version     0.1.1
10
 * @package     PHP Skeleton App
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 */
15
16
/**
17
 * Insert/Update Group
18
 *
19
 * Controller for the Group module.
20
 *
21
 * @param \Slim\Route $route The route data array
22
 * @return void
23
 */
24
25
function insert_update_group(\Slim\Route $route)
26
{
27
    $app = \Slim\Slim::getInstance();
28
    $final_global_template_vars = $app->config('final_global_template_vars');
29
    
30
    require_once $final_global_template_vars["absolute_path_to_this_module"] . "/models/group.class.php";
31
    require_once $_SERVER["PATH_TO_VENDOR"] . "wixel/gump/gump.class.php";
32
    // URL parameters matched in the route.
33
    $params = $route->getParams();
34
    $group_id = isset($params["group_id"]) ? $params["group_id"] : false;
35
    $db_conn = new \PHPSkeleton\models\db($final_global_template_vars["db_connection"]);
36
    $db_resource = $db_conn->get_resource();
37
    $group = new \PHPSkeleton\Group($db_resource, $final_global_template_vars["session_key"]);
38
    $gump = new GUMP();
39
    $rules = array(
40
        "name" => "required"
41
        ,"abbreviation" => "required|alpha_numeric"
42
        ,"state" => "alpha_numeric"
43
        ,"zip" => "numeric|exact_len,5"
44
        ,"group_parent" => "numeric"
45
    );
46
    $validated = $gump->validate($app->request()->post(), $rules);
47
    $errors = array();
48
    if ($validated !== true) {
49
        $errors = \phpskeleton\models\utility::gump_parse_errors($validated);
50
    }
51
52
    if (!$errors) {
53
        $group->insert_update_group($app->request()->post(), $group_id);
54
        // If group_id is true, then the group was modified. Otherwise, it was created.
55
        if ($group_id) {
56
            $app->flash('message', 'The group has been successfully modified.');
57
        } else {
58
            $app->flash('message', 'New group has been successfully created.');
59
        }
60
        $app->redirect($final_global_template_vars["path_to_this_module"]);
61
    } else {
62
        $env = $app->environment();
63
        $env["default_validation_errors"] = $errors;
64
    }
65
}
66