Address_book::index()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 66
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 45
nc 3
nop 0
dl 0
loc 66
rs 9.3191
c 0
b 0
f 0

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 defined('BASEPATH') OR exit('No direct script access allowed');
2
3
class Address_book extends My_Force_Login {
4
5
    public function __construct() {
6
        parent::__construct();
7
        $this->load->library('grocery_CRUD');
8
        $this->load->model('computing-support/Address_book_model', 'address_book_model');
9
    }
10
11
    public function index() {
12
13
        $this->load->helper('form');
14
        $this->load->library('form_validation');
15
        
16
        $data = array();
17
        $data['faculty'] = $this->address_book_model->get_faculty();
18
        $data['department'] = $this->address_book_model->get_department();
19
20
        // validation rules
21
        $this->form_validation->set_rules('phone', 'Phone', 'trim|numeric');
22
23
        if ($this->form_validation->run() === false) {
24
25
            $this->load->view('templates/header');
26
            $this->load->view('computing-support/address-book/view', $data);
27
            $this->load->view('templates/footer');
28
        } else {
29
30
            $faculty = $this->input->post('faculty');
31
            $department = $this->input->post('department');
32
            $name = $this->input->post('name');
33
            $room = $this->input->post('room');
34
            $phone = $this->input->post('phone');
35
            $position = $this->input->post('position');
36
            
37
            $faculty = $this->address_book_model->get_faculty_from_number($faculty);
38
            $department = $this->address_book_model->get_department_from_number($department);
39
40
            if ($this->address_book_model->change($faculty, $department, $name, $room, $phone, $position)) {
41
                
42
                $function = 'address_book_update';
43
                $this->user_model->function_log($function);
44
                
45
                $this->email->from('[email protected]', 'Address Book Update');
46
                $this->email->to('[email protected]');
47
                $this->email->subject('Address Book Update');
48
                $this->email->message('A global address book update request been made by ' . $_SESSION['ldap']['full_name'] . '
49
                       Change name to: ' . $name . '
50
                       Change Faculty to: ' . $faculty[0]['faculty'] . '  
51
                       Change Department to: ' . $department[0]['subfaculty'] . '  
52
                       Change Room to : ' . $room . '
53
                       Change Telephone to : ' . $phone.'
54
                       Change Position to : ' . $position);
55
56
                $this->email->send();
57
58
                // user created
59
                $this->load->view('templates/header');
60
                $this->load->view('computing-support/address-book/complete');
61
                $this->load->view('templates/footer');
62
            } else {
63
                
64
                $function = 'address_book_error';
65
                $this->user_model->function_log($function);
66
67
                $data = new stdClass();
68
                $data->error = 'There was a problem requesting these changes. Please try again.';
69
70
                // failed to create user
71
                $this->load->view('templates/header');
72
                $this->load->view('computing-support/address-book/view', $data);
73
                $this->load->view('templates/footer');
74
            }
75
        }
76
    }
77
    
78
    public function history() {
79
        
80
        if (in_array('CN=DG06,OU=Distribution Groups,OU=Email Groups,OU=Accounts,DC=cant-col,DC=ac,DC=uk', $_SESSION['ldap']['groups'])) {
81
        
82
            $crud = new grocery_CRUD();
83
            $crud->set_table('address_book');
84
            $crud->set_subject('Address Book Updates', 'Address Book Updates');
85
            $crud->unset_columns('id'); 
86
            $crud->unset_edit();
87
            $crud->unset_delete(); 
88
            $crud->unset_add();
89
            $crud->unset_read();
90
            $output = $crud->render();
91
92
            $this->load->view('templates/header.php');
93
            $this->load->view('computing-support/address-book/history', $output);
94
            $this->load->view('templates/footer.php');
95
            $this->load->view('templates/table_assets.php', $output);
96
            
97
        } else {
98
            redirect('permissions');
99
        }
100
        
101
    } // END user permissions
102
}
103