Amit's Garage

A garage where one can get tools to solve their problems

Enhance Read / View

November 13, 2017 by BlakDronzer

SCENARIO

The library have a basic version of values being rendered during the time of read. Like for a boolean / tinyint – the value displayed is 1 / 0 .. rather it should be true or false.

SOLUTION

Here’s a solution to enhance the same.


/******Replace / Update the below function in the library******/
protected function get_read_input_fields($field_values = null)
{
	$read_fields = $this->get_read_fields();

	$this->field_types = null;
	$this->required_fields = null;

	$read_inputs = array();
	foreach ($read_fields as $field) {
		if (!empty($this->change_field_type)
			&& isset($this->change_field_type[$field->field_name])
			&& $this->change_field_type[$field->field_name]->type == 'hidden') {
			continue;
		}
		$this->field_type($field->field_name, 'readonly');
	}

	$fields = $this->get_read_fields();
	$types     = $this->get_field_types();

	$input_fields = array();

	foreach($fields as $field_num => $field)
	{
		$field_info = $types[$field->field_name];

		if(isset($field_info->db_type) && ($field_info->db_type == 'tinyint' || ($field_info->db_type == 'int' && $field_info->db_max_length == 1))) {
			$field_value = $this->get_true_false_readonly_input($field_info, $field_values->{$field->field_name});
		} else {
			$field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null;
		}
		if(!isset($this->callback_read_field[$field->field_name]))
		{
			$field_input = $this->get_field_input($field_info, $field_value);
		}
		else
		{
			$primary_key = $this->getStateInfo()->primary_key;
			$field_input = $field_info;
			$field_input->input = call_user_func($this->callback_read_field[$field->field_name], $field_value, $primary_key, $field_info, $field_values);
		}

		switch ($field_info->crud_type) {
			case 'invisible':
				unset($this->read_fields[$field_num]);
				unset($fields[$field_num]);
				continue;
				break;
			case 'hidden':
				$this->read_hidden_fields[] = $field_input;
				unset($this->read_fields[$field_num]);
				unset($fields[$field_num]);
				continue;
				break;
		}

		$input_fields[$field->field_name] = $field_input;
	}

	return $input_fields;
}

/***** Add the new functions to the library****/
protected function get_true_false_readonly_input($field_info,$value)
{
	$value_is_null = empty($value) && $value !== '0' && $value !== 0 ? true : false;
	$true_string = is_array($field_info->extras) && array_key_exists(1,$field_info->extras) ? $field_info->extras[1] : $this->default_true_false_text[1];
	$false_string =  is_array($field_info->extras) && array_key_exists(0,$field_info->extras) ? $field_info->extras[0] : $this->default_true_false_text[0];

	return $value === '1' || ($value_is_null && $field_info->default === '1') ? $true_string : $false_string;
}

protected function get_upload_file_readonly_input($field_info,$value)
{

	$this->load_js_fancybox();

	$this->set_js_config($this->default_javascript_path.'/jquery_plugins/config/jquery.fancybox.config.js');

	$unique = mt_rand();

	$uploader_display_none     = empty($value) ? " : "display:none;";
	$file_display_none      = empty($value) ?  "display:none;" : ";

	$is_image = !empty($value) &&
	( substr($value,-4) == '.jpg'
	|| substr($value,-4) == '.png'
	|| substr($value,-5) == '.jpeg'
	|| substr($value,-4) == '.gif'
	|| substr($value,-5) == '.tiff')
	? true : false;

	$image_class = $is_image ? 'image-thumbnail' : '';

	$file_url = base_url().$field_info->extras->upload_path.'/'.$value;

	$input = "
	<div id='success_$unique' class='upload-success-url' style='$file_display_none padding-top:7px;'>";
	$input .= "<a href='".$file_url."' id='file_$unique' class='open-file"; 
	//Code altered by Amit Shah - for the sake of using thumbnails 
	$_file_url = $field_info->extras->upload_path."/$value";
	//$input .= $is_image ? " $image_class'><img src='".$file_url."' height='50px'>" : "' target='_blank'>$value";
	$input .= $is_image ? " $image_class'>" . $this->img->rimg($_file_url, array('shortside'=>50, 'sharpen'=>true)) : "' target='_blank'>$value";
	//Code Alteration ends here
	$input .= "</a> ";
	$input .= "</div>
	<div style='clear:both'></div>

	";

	return $input;
}

The above code have helped me enhance the read view. This is the method one can use to enhance the same for further needs at ones end.

Share :  

695   BlakDronzer    Grocerycrud  
Total 0 Votes:
0
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Related Knowledge Base Posts -

  • Ambigious Field error when using relation
  • Add a new functionality to GroceryCrud base library
  • Default values for Add Form
  • Search Knowledgebase