Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set_read_fields() BUG FIXING - TO BE REVIEWED #491

Closed
wants to merge 1 commit into from

Conversation

fede72bari
Copy link

@fede72bari fede72bari commented Jul 23, 2020

  1. THE PROBLEM I FOUND

I need to add some custom fields in the view page and obviously populate them with a callback function for the read mode. I have not found any reference to functions for adding fields in the read page in grocery crud documentation; just a reference in a very old topic here mentioning the function set_read_fields that i cannot find after 5 years from that forum topic in the decumentation. Anyway, even if the function gives no errors, when the callback function is called i got a lot of errors; here the log

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index: officies_location /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 4477

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get property of non-object /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 4477
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index: officies_location /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2939
ERROR - 2020-07-22 17:26:28 --> Severity: Warning --> Creating default object from empty value /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2950
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get property of non-object /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2953
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined property: stdClass::$display_as /home/phar263d/public_html/adminPanel/assets/grocery_crud/themes/bootstrap-v4/views/read.php 39

This is the code i used

$this->grocery_crud->add_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_category','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled');
    $this->grocery_crud->edit_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_category','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled');  
    $this->grocery_crud->set_read_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_cate...gory','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled','officies_location');

[...]

$this->grocery_crud->callback_read_field('officies_location',array($this,'_callback_officies_location'));

The callback function is fine (tested); tracking the error i found out that it happens when the rendering function is called

$output = $this->grocery_crud->render();
  1. GROCERY CRUD LIBRARY INVESTIGATION AND MODIFICATION

in get_field_types(){..} it seems missing a block like these

if(!empty($this->add_fields))
   foreach($this->add_fields as $field_object)
   {       ....
   }

if(!empty($this->edit_fields))
   foreach($this->edit_fields as $field_object)
   {      ...
    }

for the read_fields so that these are never added to the main data structure $this->field_types. Therefore, I have partially fixed the problem modifying the core controller adding this code

 if(!empty($this->read_fields))            
      foreach($this->read_fields as $field_object)
        {
            $field_name = isset($field_object->field_name) ? $field_object->field_name : $field_object;

            if(!isset($types[$field_name]))//Doesn't exist in the database? Create it for the CRUD
            {
                $extras = false;
                if($this->change_field_type !== null && isset($this->change_field_type[$field_name]))
                {
                    $field_type = $this->change_field_type[$field_name];
                    $extras     =  $field_type->extras;
                }


                $field_info = (object)array(
                    'name' => $field_name,
                    'crud_type' => $this->change_field_type !== null && isset($this->change_field_type[$field_name]) ?
                                        $this->change_field_type[$field_name]->type :
                                        'string',
                    'display_as' => isset($this->display_as[$field_name]) ?
                                            $this->display_as[$field_name] :
                                            ucfirst(str_replace("_"," ",$field_name)),
                    'required'    => isset($this->required_fields) && !empty($this->required_fields) && in_array($field_name,$this->required_fields) ? true : false,
                    
                    'extras'    => $extras
                );


                $types[$field_name] = $field_info;
            }
        }

to the function get_field_types(). But please consider that there could other things to be better fixed for which i have not a clear vision due to the fact that I cannot master the whole logic of Grocery Crud library code:

A. at the time get_field_types() is called it seems that $this->required_fields is not yet existing so that i get the error

in_array() expects parameter 2 to be array, null given     /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 220

That is why I have added the control

isset($this->required_fields) &&

This allows at skipping the error, but still there could be a problem with the rendering sequence: maybe the first operation should be the creation of all the arrays and data structures even if null, and only after that get_field_types() should be called.

B. PLEASE NOTE THAT

i. in foreach($this->edit_fields as $field_object) it has been forgotten the control

!empty($this->required_fields) && 

that is present in the other similar for loops.

ii. the control

isset($this->required_fields) &&

should be added to all the other for loops because it is a latent bug.

  1. I saw in another part of the core code that there is this control

    elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'readonly')
    {
    //This empty if statement is to make sure that a readonly field will never inserted/updated
    }

but my modifications do not set the crud type to 'read_only' and this should be investigated; here a data structure print adding a read custom field "officies_location"

[officies_location] => stdClass Object
    (
        [name] => officies_location
        [crud_type] => string
        [display_as] => Officies location
        [required] => 
        [extras] => 
    )

1. THE PROBLEM I FOUND

I need to add some custom fields in the view page and obviously populate them with a callback function for the read mode. I have not found any reference to functions for adding fields in the read page in grocery crud documentation; just a reference in a very old topic here mentioning the function set_read_fields that i cannot find after 5 years from that forum topic in the decumentation. Anyway, even if the function gives no errors, when the callback function is called i got a lot of errors; here the log 

 

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index: officies_location /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 4477

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get property of non-object /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 4477
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index: officies_location /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2939
ERROR - 2020-07-22 17:26:28 --> Severity: Warning --> Creating default object from empty value /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2950
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get property of non-object /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 2953
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined property: stdClass::$display_as /home/phar263d/public_html/adminPanel/assets/grocery_crud/themes/bootstrap-v4/views/read.php 39
 

 

This is the code i used

$this->grocery_crud->add_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_category','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled');
        $this->grocery_crud->edit_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_category','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled');  
        $this->grocery_crud->set_read_fields('id','vendor_name','vendor_web_site_url','default_currencies_id','affiliation_cate...gory','external_affiliation_services_id','affiliation_vendor_id','remark','last_modified','creation','enabled','officies_location');

[...]

$this->grocery_crud->callback_read_field('officies_location',array($this,'_callback_officies_location'));
The callback function is fine (tested); tracking the error i found out that it happens when the rendering function is called

$output = $this->grocery_crud->render();

2. GROCERY CRUD LIBRARY INVESTIGATION AND MODIFICATION


in get_field_types(){..} it seems missing a block like these

 

if(!empty($this->add_fields))
   foreach($this->add_fields as $field_object)
   {       ....
   }

if(!empty($this->edit_fields))
   foreach($this->edit_fields as $field_object)
   {      ...
    }
for the read_fields so that these are never added to the main data structure $this->field_types. Therefore, I have partially fixed the problem modifying the core controller adding this code

 if(!empty($this->read_fields))            
      foreach($this->read_fields as $field_object)
            {
                $field_name = isset($field_object->field_name) ? $field_object->field_name : $field_object;

                if(!isset($types[$field_name]))//Doesn't exist in the database? Create it for the CRUD
                {
                    $extras = false;
                    if($this->change_field_type !== null && isset($this->change_field_type[$field_name]))
                    {
                        $field_type = $this->change_field_type[$field_name];
                        $extras     =  $field_type->extras;
                    }


                    $field_info = (object)array(
                        'name' => $field_name,
                        'crud_type' => $this->change_field_type !== null && isset($this->change_field_type[$field_name]) ?
                                            $this->change_field_type[$field_name]->type :
                                            'string',
                        'display_as' => isset($this->display_as[$field_name]) ?
                                                $this->display_as[$field_name] :
                                                ucfirst(str_replace("_"," ",$field_name)),
                        'required'    => isset($this->required_fields) && !empty($this->required_fields) && in_array($field_name,$this->required_fields) ? true : false,
                        
                        'extras'    => $extras
                    );


                    $types[$field_name] = $field_info;
                }
            }

to the function get_field_types(). But please consider that there could other things to be better fixed for which i have not a clear vision due to the fact that I cannot master the whole logic of Grocery Crud library code:

 

A. at the time get_field_types() is called it seems that $this->required_fields is not yet existing so that i get the error 

in_array() expects parameter 2 to be array, null given /home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php 220

That is why I have added the control 

isset($this->required_fields) &&

This allows at skipping the error, but still there could be a problem with the rendering sequence: maybe the first operation should be the creation of all the arrays and data structures even if null, and only after that get_field_types() should be called.

B. PLEASE NOTE THAT 

   i. in foreach($this->edit_fields as $field_object) it has been forgotten the control 

!empty($this->required_fields) && 

that is present in the other similar for loops.

   ii. the control 

isset($this->required_fields) &&

should be added to all the other for loops because it is a latent bug.

3. I saw in another part of the core code that there is this control

elseif(isset($types[$field->field_name]->crud_type) && $types[$field->field_name]->crud_type == 'readonly')
{
//This empty if statement is to make sure that a readonly field will never inserted/updated
}

but my modifications do not set the crud type to 'read_only' and this should be investigated; here a data structure print adding a read custom field "officies_location"

    [officies_location] => stdClass Object
        (
            [name] => officies_location
            [crud_type] => string
            [display_as] => Officies location
            [required] => 
            [extras] => 
        )
@scoumbourdis
Copy link
Owner

Hello @fede72bari,

I am sorry but your PR is very big and to be honest I don't understand what is the issue.

Can you please describe with simple steps within the following template what is the problem and what we are trying to solve?

More specifically please send me:

Steps to reproduce:
Step 1.
Step 2.
....

Expected Results:

Actual Results:

Why this is a bug?

What is this PR solving?

Regards
Johnny

@fede72bari
Copy link
Author

fede72bari commented Jul 25, 2020 via email

scoumbourdis added a commit that referenced this pull request Jul 27, 2020
@scoumbourdis
Copy link
Owner

Now it is very clear :) . I've created a new commit 9bf6e33 that is fixing that. You can download the latest master from here: https://github.com/scoumbourdis/grocery-crud/archive/master.zip and let me know if this is now fixed ;-)

Regards
Johnny

@fede72bari
Copy link
Author

fede72bari commented Jul 27, 2020 via email

@scoumbourdis
Copy link
Owner

Do you mean that this is not working with the combination of required_fields? I will have it a look and let you know.

@fede72bari
Copy link
Author

fede72bari commented Jul 27, 2020 via email

@fede72bari
Copy link
Author

fede72bari commented Jul 30, 2020 via email

@scoumbourdis
Copy link
Owner

Hello @fede72bari ,

As I can't reproduce the errors that you have can you please just describe me the way to reproduce your errors and I will try to fix them? Please don't send me code fix just the ways to reproduce. Also before you write some code in comments press 4 times space and magically github will add some code blocks like the one below ;-) For example:

Steps to reproduce:

Step 1. By adding the combination of those two functions:

  $crud->set_read_fields('myCustomName','contactLastName');
  $crud->callback_read_field('myCustomName', function ($value) {
        return 'test' . $value;
  });

Expected Results:

To not see any errors

Actual Results:

I can see those errors instead:

ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Undefined index:
officies_location
/home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php
4477
ERROR - 2020-07-22 17:26:28 --> Severity: Notice --> Trying to get
property of non-object
/home/phar263d/public_html/adminPanel/application/libraries/Grocery_CRUD.php
4477

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants