As an semi-experienced procedural PHP developer, my OOP still needs a lot of work. I am still working through SOLID principles and other theories and guidelines of OOP, and am stuck with something that just looks UGLY to say the least, and definitely does not confirm to SOLID.
I am busy redoing my in-house CMS, using CodeIgniter 3.0.x and MaterializeCSS for the front-end.
Here is the bit of code in question that simply does not work for me:
public function add($uid)
{
$data['userdata'] = $this->usercontact_model->get_user($uid);
if (empty($data['userdata']))
{
return false;
}
else
{
$temp = $this->usercontact_model->get_contact_types();
$temp_options = array();
if (!empty($temp))
{
foreach ($temp as $tk => $tv)
{
$temp_options[$tk]['value'] = $tv['id'];
$temp_options[$tk]['display'] = $tv['name'];
}
}
$data['sidebar'] = array(
0 => array(
'type' => 'accordion',
'content' => array(
0 => array(
'icon' => 'settings',
'active' => 'active',
'heading' => 'Page actions',
'prompt' => lang('user_contacts_page_actions_head') . '<br /> <br />',
'body' => '
» <a href="' . base_url('user/usercontact/list_contacts') . '/' . $this->session->userdata('id') . '">' . lang('user_contacts_return_contacts') . '</a><br />
» <a href="' . base_url('user/login/control_panel') . '">' . lang('user_contacts_return_cpanel') . '</a><br />
'
)
)
)
);
$data['open_data'] = array(
'action' => '',
'method' => 'post',
'hidden' => array(
'user_id' => $this->session->userdata('id')
)
);
$data['close_data'] = array(
'datepicker_init' => 'true',
'select_months' => 'true',
'select_years' => 15,
'format' => 'yyyy-mm-dd',
'clockpicker_init' => 'true'
);
$data['contact_type_data'] = array(
'id' => 'contact_type_select',
'heading' => 'user_contact_type',
'options' => $temp_options
);
$data['active_data'] = array(
'id' => 'active_select',
'heading' => 'user_contact_active',
'options' => array(
0 => array(
'value' => 'Yes',
'display' => lang('user_contact_yes')
),
1 => array(
'value' => 'No',
'display' => lang('user_contact_no')
)
)
);
$data['value_data'] = array(
'id' => 'value',
'validate' => 'validate',
'placeholder' => 'Content',
'label' => 'user_contact_label_value'
);
$data['vacation_from_data'] = array(
'id' => 'vacation_from',
'placeholder' => 'Select vacation start date',
'label' => 'user_contact_vacation_from'
);
$data['vacation_to_data'] = array(
'id' => 'vacation_to',
'placeholder' => 'Select vacation end date',
'label' => 'user_contact_vacation_to'
);
$data['time_from_data'] = array(
'id' => 'time_from',
'placeholder' => 'Select available start time',
'label' => 'user_contact_time_from'
);
$data['time_to_data'] = array(
'id' => 'time_to',
'placeholder' => 'Select available end time',
'label' => 'user_contact_time_to'
);
$data['submit_data'] = array(
'id' => 'submit_add',
'label' => lang('user_contact_save'),
);
$posts = $this->input->post();
if (empty($posts))
{
$this->core->render_view('template_admin', 'usercontact/add', $data);
}
else
{
$this->core->render_view('template_admin', 'usercontact/add', $data);
preint($posts);
}
}
}
Now - I have not built in validation to this yet, but that will make this undoubtedly more ugly using my current single function approach.
preint() is just a function I created that does a print_r wrapped in <pre></pre>
Specific questions:
- How, using CI 3.0.x, can I make this more SOLID compliant?
- How can I make this more readable?
- How can I shorten this? Move things such as validation and creating the from fields and sidebar arrays to separate functions/classes?
Answers or suggestions to any or all of the above would be greatly appreciated.
}else{, cause theifbody already returns afalseso the method will not be executed any further if the condition is true. This makes the else body a bit unnecessary. To my opinion it's a bit more readible and less error-prone without the else block. And maybe, if you use the same hardcoded array more than once, you could put that one in a separate method or file. In that case you could put that array in a variable whenever you need it and just modify the keys and values you need to change.