Okay this way has got a error while uploading files.
Previously while uploading files (while we dont have 3rd uri segment), it adds extra segments on the 3rd uri to define the state of the image_crud. I modified it to work while having 3rd uri segment.So in this case, I have a two conditions. First while having third uri segment and second, not having third uri segment. So $extra_segments variable is set to false( false for no extra segments,i.e localhost/my_site/admin/pictures in my case) while we don't have 3rd uri segment. For checking 3rd uri segment, the function set_table of image_crud.phpis modified as:
function set_table($table_name)
{
$this->table_name = $table_name;
if($table_name == $this->ci->uri->segment(3))
{
$this->extra_segments = true;
}
return $this;
}
also variable is to be declared inside image_crud class(on line 47 I did) as: protected $extra_segments = false; Now for actual work, getState() function is modified as below. It can be furthur optimised to make it dry and modular.
protected function getState()
{
$rsegments_array = $this->ci->uri->rsegment_array();
if($this->extra_segments == true){
if(isset($rsegments_array[4]) && is_numeric($rsegments_array[4]))
{
$upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/upload_file/'.$rsegments_array[3]);
$ajax_list_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/'.$rsegments_array[3].'/ajax_list');
$ordering_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ordering');
$insert_title_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/insert_title');
$state = array( 'name' => 'list', 'upload_url' => $upload_url, 'relation_value' => $rsegments_array[4]);
$state['ajax'] = isset($rsegments_array[5]) && $rsegments_array[5] == 'ajax_list' ? true : false;
$state['ajax_list_url'] = $ajax_list_url;
$state['ordering_url'] = $ordering_url;
$state['insert_title_url'] = $insert_title_url;
return (object)$state;
}
elseif( (empty($rsegments_array[4]) && empty($this->relation_field)) || (!empty($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list'))
{
$upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/upload_file');
$ajax_list_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ajax_list');
$ordering_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ordering');
$insert_title_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/insert_title');
$state = array( 'name' => 'list', 'upload_url' => $upload_url);
$state['ajax'] = isset($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list' ? true : false;
$state['ajax_list_url'] = $ajax_list_url;
$state['ordering_url'] = $ordering_url;
$state['insert_title_url'] = $insert_title_url;
return (object)$state;
}
elseif(isset($rsegments_array[4]) && $rsegments_array[4] == 'upload_file')
{
#region Just rename my file
$new_file_name = '';
//$old_file_name = $this->_to_greeklish($_GET['qqfile']);
$old_file_name = $this->_convert_foreign_characters($_GET['qqfile']);
$max = strlen($old_file_name);
for($i=0; $i< $max;$i++)
{
$numMatches = preg_match('/^[A-Za-z0-9.-_]+$/', $old_file_name[$i], $matches);
if($numMatches >0)
{
$new_file_name .= strtolower($old_file_name[$i]);
}
else
{
$new_file_name .= '-';
}
}
$file_name = substr( substr( uniqid(), 9,13).'-'.$new_file_name , 0, 100) ;
#endregion
$results = array( 'name' => 'upload_file', 'file_name' => $file_name);
if(isset($rsegments_array[5]) && is_numeric($rsegments_array[5]))
{
$results['relation_value'] = $rsegments_array[5];
}
return (object)$results;
}
elseif(isset($rsegments_array[4]) && isset($rsegments_array[5]) && $rsegments_array[4] == 'delete_file' && is_numeric($rsegments_array[5]))
{
$state = array( 'name' => 'delete_file', 'id' => $rsegments_array[4]);
return (object)$state;
}
elseif(isset($rsegments_array[4]) && $rsegments_array[4] == 'ordering')
{
$state = array( 'name' => 'ordering');
return (object)$state;
}
elseif(isset($rsegments_array[4]) && $rsegments_array[4] == 'insert_title')
{
$state = array( 'name' => 'insert_title');
return (object)$state;
}
else
{
$upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/upload_file');
$ajax_list_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ajax_list');
$ordering_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ordering');
$insert_title_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/insert_title');
$state = array( 'name' => 'list', 'upload_url' => $upload_url);
$state['ajax'] = isset($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list' ? true : false;
$state['ajax_list_url'] = $ajax_list_url;
$state['ordering_url'] = $ordering_url;
$state['insert_title_url'] = $insert_title_url;
return (object)$state;
}
}
elseif($this->extra_segments == false)
{
if(isset($rsegments_array[3]) && is_numeric($rsegments_array[3]))
{
$upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/upload_file/'.$rsegments_array[3]);
$ajax_list_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/'.$rsegments_array[3].'/ajax_list');
$ordering_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ordering');
$insert_title_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/insert_title');
$state = array( 'name' => 'list', 'upload_url' => $upload_url, 'relation_value' => $rsegments_array[3]);
$state['ajax'] = isset($rsegments_array[4]) && $rsegments_array[4] == 'ajax_list' ? true : false;
$state['ajax_list_url'] = $ajax_list_url;
$state['ordering_url'] = $ordering_url;
$state['insert_title_url'] = $insert_title_url;
return (object)$state;
}
elseif( (empty($rsegments_array[3]) && empty($this->relation_field)) || (!empty($rsegments_array[3]) && $rsegments_array[3] == 'ajax_list'))
{
$upload_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/upload_file');
$ajax_list_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ajax_list');
$ordering_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/ordering');
$insert_title_url = site_url($rsegments_array[1].'/'.$rsegments_array[2].'/insert_title');
$state = array( 'name' => 'list', 'upload_url' => $upload_url);
$state['ajax'] = isset($rsegments_array[3]) && $rsegments_array[3] == 'ajax_list' ? true : false;
$state['ajax_list_url'] = $ajax_list_url;
$state['ordering_url'] = $ordering_url;
$state['insert_title_url'] = $insert_title_url;
return (object)$state;
}
elseif(isset($rsegments_array[3]) && $rsegments_array[3] == 'upload_file')
{
#region Just rename my file
$new_file_name = '';
//$old_file_name = $this->_to_greeklish($_GET['qqfile']);
$old_file_name = $this->_convert_foreign_characters($_GET['qqfile']);
$max = strlen($old_file_name);
for($i=0; $i< $max;$i++)
{
$numMatches = preg_match('/^[A-Za-z0-9.-_]+$/', $old_file_name[$i], $matches);
if($numMatches >0)
{
$new_file_name .= strtolower($old_file_name[$i]);
}
else
{
$new_file_name .= '-';
}
}
$file_name = substr( substr( uniqid(), 9,13).'-'.$new_file_name , 0, 100) ;
#endregion
$results = array( 'name' => 'upload_file', 'file_name' => $file_name);
if(isset($rsegments_array[4]) && is_numeric($rsegments_array[4]))
{
$results['relation_value'] = $rsegments_array[4];
}
return (object)$results;
}
elseif(isset($rsegments_array[3]) && isset($rsegments_array[4]) && $rsegments_array[3] == 'delete_file' && is_numeric($rsegments_array[4]))
{
$state = array( 'name' => 'delete_file', 'id' => $rsegments_array[4]);
return (object)$state;
}
elseif(isset($rsegments_array[3]) && $rsegments_array[3] == 'ordering')
{
$state = array( 'name' => 'ordering');
return (object)$state;
}
elseif(isset($rsegments_array[3]) && $rsegments_array[3] == 'insert_title')
{
$state = array( 'name' => 'insert_title');
return (object)$state;
}
}
}
Also url helper must be autoloaded or loaded to uri checking function.