[eluser]Prashanth[/eluser]
Controller: controllers/administration.php
Code:
public function create_place( $type = '' )
{
// Make sure an admin or manager is logged in. (showing how to log in by group)
if( $this->require_group('employees') )
{
// Load resources
$this->load->library('csrf');
$view_data['roles'] = $this->authentication->roles;
if( ! empty( $type ) )
{
// Get level associated with type (role)
$view_data['level'] = $this->authentication->levels['manager'];
$view_data['type'] = $type;
// Confirm the type of user is permitted
if( $this->auth_level < $view_data['level'] )
{
die();
}
// Check if a valid form submission has been made
if( $this->csrf->token_match )
{
// Create the user
$this->load->model('place_model');
$this->place_model->create_place( $type );
}
if($type != 'district')
{
$this->load->model('place_model');
$view_data['dist'] = $this->place_model->get_places(3);
if( $this->csrf->token_match )
{
if( $this->input->post('place_d') )
{
$view_data['taluks'] = $this->place_model->get_taluks_in_dist();
}
}
}
// Load the appropriate place creation form
$view_data['place_creation_form'] = $this->load->view( 'administration/create_place/create_' . $type, ( isset( $view_data ) ) ? $view_data : '', TRUE );
}
$data = array(
'javascripts' => array(
'js/auto_populate/auto-populate-place.js'
),
'content' => $this->load->view( 'administration/create_place', ( isset( $view_data ) ) ? $view_data : '', TRUE ),
);
$this->load->view( $this->template, $data );
}
}
Model: models/place_model.php
Code:
public function get_places($var)
{
$query = $this->db->select('place_id,name')
->where('place_type',$var)
->order_by('name')
->get( config_item('place_table') );
if( $query->num_rows() > 0 )
{
return $query->result();
}
return FALSE;
}
public function get_taluks_in_dist()
{
$dist = $this->input->post('place_d');
$this->db->select('place_id,name');
$this->db->where('parent_id',$dist );
$query = $this->db->get( config_item('place_table') );
if( $query->num_rows() > 0 )
{
return $query->result_array();
}
return FALSE;
}
Another View: views/administration/create_place.php
Code:
echo '<h1>' . ( isset( $type ) ? ucfirst( $type ) . ' Creation' : 'Place Creation' ) . '</h1>';
if( isset( $validation_passed, $place_created ) )
{
echo '
<div class="feedback confirmation">
<p class="feedback_header">
The new ' . $type . ' has been successfully created.
</p>
</div>
';
}
else if( isset( $validation_errors ) )
{
echo '
<div class="feedback error_message">
<p class="feedback_header">
' . ucfirst( $type ) . ' Creation Contained The Following Errors:
</p>
<ul>
' . $validation_errors . '
</ul>
<p>
' . strtoupper( $type ) . ' NOT CREATED
</p>
</div>
';
}
if( isset( $level, $type ) )
{
echo $place_creation_form;
}
else
{
echo '
<p>Please choose a place type to create:</p>
<ul class="std-list">
';
/*foreach( $roles as $k => $v )
{
if( $k < $auth_level )
{ */
echo '<li>' . secure_anchor( 'administration/create_place/district', 'district' ) . '</li>';
echo '<li>' . secure_anchor( 'administration/create_place/taluk', 'taluk' ) . '</li>';
echo '<li>' . secure_anchor( 'administration/create_place/village', 'village' ) . '</li>';
/*}
}*/
echo '</ul>';
}