Jump to content


callback_add/edit_field changes the display on the "read" method

callback_edit_field callback_add_field

  • Please log in to reply
22 replies to this topic

#1 jonathanw

jonathanw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 26 March 2014 - 03:06 PM

Hi,

 

I have a database table which contains swedish corporate identity numbers (in lack of better translation).

It is in the form of: 123456-9999

 

Although, in the database, I want it without the hyphen, so I remove it with a callback (containing a basic str_replace) used on callback_before_insert/update.

 

The field in the database is a char(10), so the input field generated has a max length of 10. And here comes the twist - I want users to be able to input numbers *with* the hyphen.

Easy to fix, I use a callback_add/edit_field to create an input with max length 11 instead.

 

But, the problem I get is that when clicking on "View" (which points to the "read" method), there is all of a sudden an input field.

I can change the value to whatever - but of course I can't save it. It's not a problem from a security perspective, but it doesn't look so good.

 

The callback_add/edit_field should not change anything for the "read" method. Or am I wrong?

 

Regards

Jonathan



#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 26 March 2014 - 07:50 PM

well i believe you should apply callback_read method too.... this is there in the code of the last release .. they have not updated the function calls but it is there for sure,..!!



#3 jonathanw

jonathanw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 27 March 2014 - 12:26 PM

Shouldn't it be the other way around?

If I want something to change on the read method, I would use callback_read. But that not the case, and it should not change anything in the read method, as I'm only telling it to change fields on add and edit.

 

Something called callback_read_field doesn't exist in my version of GC :(

Using 1.2



#4 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 28 March 2014 - 04:16 AM

well then i recommend you either upgrade it to v 1.4.1 - that way it should be better.



#5 jonathanw

jonathanw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 March 2014 - 10:06 AM

Rechecked it, I've downloaded 1.4.1, not 1.2 as I said earlier.



#6 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 28 March 2014 - 10:36 AM

1.4.1 do have callback_read_field function in the same!!



#7 jonathanw

jonathanw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 March 2014 - 12:14 PM

Now I'm confused. The zip-file I've downloaded says 1.4.1 (downloaded from github).

And also, the Grocery_CRUD.php in application\libraries\Grocery_CRUD.php contains:

 

* @version        1.4.1

 

So, it seems it's something wrong. Is it a manual patch?



#8 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 28 March 2014 - 12:24 PM

You confused with what brother???



#9 jonathanw

jonathanw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 28 March 2014 - 12:34 PM

I use v1.4.1 - but nothing called callback_read_field!



#10 Nitin Thokare

Nitin Thokare

    Member

  • Members
  • PipPip
  • 25 posts

Posted 31 May 2014 - 10:52 AM

Anybody got solution on this issue??

 

I too got to it..

 

ref at:  http://www.grocerycr...s-on-read-page/



#11 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 31 May 2014 - 07:40 PM

///In the class mentioned below.. add a variable ... callback_read_field
class Grocery_CRUD extends grocery_CRUD_States
{
*************
protected $callback_read_field		= array();

//And a function the accept the callbacks
/**
	 *
	 * Used to bypass the default formatting and allow user to achieve custom formatting for the view of a field
	 * @param string $field
	 * @param mixed $callback
	 */
	public function callback_read_field($field, $callback = null)
	{
		$this->callback_read_field[$field] = $callback;
	
		return $this;
	}


//Now update this function to manage the field outputs using callbacks if they are defined for the same
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;
	}

Heres the alteration have had made to my library... sorry i had been using the same for so long i 4got it not being part of the original library..

Have it.. use it .. enjoy it..

 

Happy GCing:)



#12 Nitin Thokare

Nitin Thokare

    Member

  • Members
  • PipPip
  • 25 posts

Posted 01 June 2014 - 03:50 AM

How is function get_read_fields() defined?



#13 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 01 June 2014 - 06:27 AM

protected function get_read_fields()
	{
		if($this->read_fields_checked === false)
		{
			$field_types = $this->get_field_types();
			if(!empty($this->read_fields))
			{
				foreach($this->read_fields as $field_num => $field)
				{
					if(isset($this->display_as[$field]))
						$this->read_fields[$field_num] = (object)array('field_name' => $field, 'display_as' => $this->display_as[$field]);
					else
						$this->read_fields[$field_num] = (object)array('field_name' => $field, 'display_as' => $field_types[$field]->display_as);
				}
			}
			else
			{
				$this->read_fields = array();
				foreach($field_types as $field)
				{
					//Check if an unset_read_field is initialize for this field name
					if($this->unset_read_fields !== null && is_array($this->unset_read_fields) && in_array($field->name,$this->unset_read_fields))
						continue;

					if(!isset($field->db_extra) || $field->db_extra != 'auto_increment')
					{
						if(isset($this->display_as[$field->name]))
							$this->read_fields[] = (object)array('field_name' => $field->name, 'display_as' => $this->display_as[$field->name]);
						else
							$this->read_fields[] = (object)array('field_name' => $field->name, 'display_as' => $field->display_as);
					}
				}
			}

			$this->read_fields_checked = true;
		}
		return $this->read_fields;
	}

Sorry thought it was already there with others!!



#14 Nitin Thokare

Nitin Thokare

    Member

  • Members
  • PipPip
  • 25 posts

Posted 01 June 2014 - 10:11 AM

Thanks Amit Shah...

 

With little more additions related to read, I got it working.  :)



#15 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 02 June 2014 - 05:14 AM

Happy GCing :)



#16 Evandro

Evandro

    Member

  • Members
  • PipPip
  • 11 posts

Posted 02 July 2014 - 08:56 PM

I also added these lines in the Grocery_CRUD.php and it worked.

 

private $read_fields_checked = false;

 

protected $unset_read_fields = null;
 
protected function get_true_false_readonly_input($field_info,$value)
{
$this->set_css($this->default_css_path.'/jquery_plugins/uniform/uniform.default.css');
$this->set_js_lib($this->default_javascript_path.'/jquery_plugins/jquery.uniform.min.js');
$this->set_js_config($this->default_javascript_path.'/jquery_plugins/config/jquery.uniform.config.js');
 
$value_is_null = empty($value) && $value !== '0' && $value !== 0 ? true : false;
 
$input = "<div class='pretty-radio-buttons'>";
 
$true_string = is_array($field_info->extras) && array_key_exists(1,$field_info->extras) ? $field_info->extras[1] : $this->default_true_false_text[1];
$checked = $value === '1' || ($value_is_null && $field_info->default === '1') ? "checked = 'checked'" : "";
$input .= "<label><input id='field-{$field_info->name}-true' class='radio-uniform' readonly='readonly' type='radio' name='{$field_info->name}' value='1' $checked /> ".$true_string."</label> ";
 
$false_string =  is_array($field_info->extras) && array_key_exists(0,$field_info->extras) ? $field_info->extras[0] : $this->default_true_false_text[0];
$checked = $value === '0' || ($value_is_null && $field_info->default === '0') ? "checked = 'checked'" : "";
$input .= "<label><input id='field-{$field_info->name}-false' class='radio-uniform' readonly='readonly' type='radio' name='{$field_info->name}' value='0' $checked /> ".$false_string."</label>";
 
$input .= "</div>";
 
return $input;
}


#17 larasmith

larasmith

    Advanced Member

  • Members
  • PipPipPip
  • 199 posts
  • LocationPhilippines

Posted 25 July 2014 - 03:15 AM

protected function get_read_fields()
	{
		if($this->read_fields_checked === false)
		{
			$field_types = $this->get_field_types();
			if(!empty($this->read_fields))
			{
				foreach($this->read_fields as $field_num => $field)
				{
					if(isset($this->display_as[$field]))
						$this->read_fields[$field_num] = (object)array('field_name' => $field, 'display_as' => $this->display_as[$field]);
					else
						$this->read_fields[$field_num] = (object)array('field_name' => $field, 'display_as' => $field_types[$field]->display_as);
				}
			}
			else
			{
				$this->read_fields = array();
				foreach($field_types as $field)
				{
					//Check if an unset_read_field is initialize for this field name
					if($this->unset_read_fields !== null && is_array($this->unset_read_fields) && in_array($field->name,$this->unset_read_fields))
						continue;

					if(!isset($field->db_extra) || $field->db_extra != 'auto_increment')
					{
						if(isset($this->display_as[$field->name]))
							$this->read_fields[] = (object)array('field_name' => $field->name, 'display_as' => $this->display_as[$field->name]);
						else
							$this->read_fields[] = (object)array('field_name' => $field->name, 'display_as' => $field->display_as);
					}
				}
			}

			$this->read_fields_checked = true;
		}
		return $this->read_fields;
	}

Sorry thought it was already there with others!!

 

Dear Amit,

 

I've been following your posts and was trying to implement this... Please tell me where to place this code and kindly provide a sample on how to use it... I think this is the answer to the question I posted at: http://www.grocerycr...lues-in-fields/ but im not successful in implementing your solution yet... Please help... I'm just a newbie here and I did my best to search for the solution before asking for help but still not successful. Thank you in advance!  :)



#18 skekeu

skekeu

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 21 April 2015 - 12:15 AM

///In the class mentioned below.. add a variable ... callback_read_field
class Grocery_CRUD extends grocery_CRUD_States
{
*************
protected $callback_read_field		= array();

//And a function the accept the callbacks
/**
	 *
	 * Used to bypass the default formatting and allow user to achieve custom formatting for the view of a field
	 * @param string $field
	 * @param mixed $callback
	 */
	public function callback_read_field($field, $callback = null)
	{
		$this->callback_read_field[$field] = $callback;
	
		return $this;
	}


//Now update this function to manage the field outputs using callbacks if they are defined for the same
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;
	}

Heres the alteration have had made to my library... sorry i had been using the same for so long i 4got it not being part of the original library..

Have it.. use it .. enjoy it..

 

Happy GCing:)

 

 

Thanks for the code.

Works great!!!

 

CST



#19 Akshay Hegde

Akshay Hegde

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationGoa, India

Posted 23 April 2015 - 09:13 AM

Even I noticed on GC 1.5.1 latest, callback_column and callback_field has no effect in read state, and unfortunately callback_read_field doesn't exists, but didn't understand why he called  callback_read_field in line number 2805 and 2813 in latest release, and also

$crud->columns(array('c1','c2','c3'));
$crud->fields(array('c1','c2','c3'));

in read state has no effect, which shows all columns , I think we have to use set_read_fields function for this.

 

I am still using 1.4.1



#20 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 25 April 2015 - 03:59 AM

Well - this was my altered code and i used it across all my projects .. thought it would have been included since it was shared across for long time.. but u still can make the said changes and have it running







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users