Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

custom fields not showing accounting fields

Plugins
freddysf
2007-01-23
2013-04-26
  • freddysf
    freddysf
    2007-01-23

    Hello,

    First of all, thanks for this great project. I installed this distribution: xrms-2006-07-25-v1.99.2-.tar.gz

    The only plugin I have activated so far is Custom Fields.

    I wanted help from the community regarding the Custom Fields plugin. I noticed that when I create new fields associated to the Contact Accounting I could not see this new fields when creating a new contact, however when editing existing contacts I could see and edit the fields.

    QUESTION:

    What should I do to have the Custom Fields in the new contact form and have them inserted in the database?

    My little research:

    After reviewing the code I noticed that in the script contacts/new.php in line 267 there is this instruction:

    <?php echo $accounting_rows; ?>

    However I did not find where the $accounting_rows variable was initialized.

    On the other hand, in the script contacts/edit.php line 138 there is an initialization for that variable:
    $accounting_rows = do_hook_function('contact_accounting_inline_edit', $accounting_rows);
    And then in line 263 we have the instruction             <?php echo $accounting_rows; ?>

    What I did:

    I added the initialization of the $accounting_rows variable in line 158 in script contacts/new.php exactly after the instruction that initializes the variable $contact_custom_rows

    Result:

    Now, the custom fields are being displayed when creating a new contact. BUT the value is not being inserted in the database.

    QUESTION:

    What should I do to have the Custom Fields in the new contact form and have them inserted in the database?

    Thanks in advance,
    Freddy

     
    • Brian Peterson
      Brian Peterson
      2007-01-23

      I don't have time to comb the code right now, but you can probably figure this out with this little pointer...

      look in the contacts/edit-2.php page.

      There is probably a hook there like 'contact_accounting_inline_process'

      You'll need to insert this hook into the contacts/new-2.php page just like you inserted the edit hook.

      Make sure that you insert the hook *after* the contact id has been assigned.  you may need to add it to the array of data as well.

      Hopefully this will be enough to sort it out for you, and I'll take a closer look at the code when I get a chance.

      Regards,

        - Brian

       
    • freddysf
      freddysf
      2007-01-24

      Thanks a lot Brian, I will work on it.

      If I fix it I will inform the results and post the changes in this thread.

       
    • freddysf
      freddysf
      2007-01-25

      Hello, Thanks to Brian's advice I am moving to a solution.

      ** PLEASE let me know if using a global variable is OK **

      After making the changes it seems to be working now. However, I would like to know if this is the right solution, I am not 100% sure because for making it work I added global variable in the includes/utils-contact.php script. See details below.

      Here is exactly what I did:

      1) Edit file contacts/new.php:

      *** LOOK for this code (around line 158):
      $contact_custom_rows = do_hook_function('contact_custom_inline_new_display', $contact_custom_rows);

      *** ADD after that, the next block:
      [BOF_new_2]
      $accounting_rows = do_hook_function('contact_accounting_inline_edit', $accounting_rows); // new_2: itx freddy 20070123 added to display the custom fields
      [EOF_new_2]

      2) Edit file plugins/custom_fields/setup.php:

      *** LOOK for this code (around line 140):

      function cf_contact_edit_2 () {
          global $xrms_file_root, $contact_id;
          include_once("$xrms_file_root/plugins/custom_fields/display_functions.php");
          do_inline_edit_save("contact_accounting", $contact_id);
      }

      *** ADD after that, the next block:

      // [BOF_cf_new_2]
      // 20070125 freddy itx added the next function to save the custom fields in the contacts/new-2.php
      // this function is a little adaptation from cf_contact_edit_2
      function cf_contact_new_2 () {
          global $xrms_file_root, $contact_id;
          include_once("$xrms_file_root/plugins/custom_fields/display_functions.php");
          do_inline_edit_save("contact_accounting", $contact_id);
      }
      // [EOF_cf_new_2]

      *** LOOK for this code (around line 25):

          $xrms_plugin_hooks['contact_edit_2']['custom_fields']
            = 'cf_contact_edit_2';

      *** ADD after that, the next block:

          // [BOF_cf_new_2]
          // 20070125 freddy itx added the next hook to the array to save the custom fields in the contacts/new-2.php
          $xrms_plugin_hooks['contact_new_2']['custom_fields']
            = 'cf_contact_new_2';
          // [EOF_cf_new_2]

      3) Edit file includes/utils-contacts.php:

      *** LOOK for this code (around line 245):

                  do_hook_function('contact_new_2', $contact_data);

      *** REPLACE it, with the next block:

                  // [BOF_new_2]
                  // 20070125 itx freddy. replaced the old call to do_hook_function to duplicate what is done in update_contact
                  // this is to fix the insert of the custom_fields plugin when creating a new contact
                  // originally, the call to the function was:
                  //     do_hook_function('contact_new_2', $contact_data);
                  // now we added this code
                  $param_new = array($contact_array, $contact_data);
                  do_hook_function('contact_new_2', $param_new);
                  // [EOF_new_2]

      *** LOOK for this code (around line 90):

          $_retVal = false;

          global $session_user_id;

      *** ADD after that, the next block:

          // [BOF_new_2]
          global $contact_id; // [TRICKY] 20070125 freddy itx: to make the function cf_contact_new_2 in plugins/custom_fields/setup.php I added this global declaration.
          // [TODO] 20070127 freddy itx ask the developer if this is the right solution or not
          // [BOF_new_2]

      THE BIG QUESTION???

      It is working, the fields is shown in the new contact page and the field value is inserted in the database. However...

      Please let me know if this is the right solution, I am not sure if using a global declaration for $contact_id here is OK. The reason why I added this is because after debugging I realized that the cf_contact_new_2 function references $contact_id as a global variable. What I did not understand is how this works for contacts/edit-2.php without the need for declaring $contact_id as global in that scrip (edit-2.php).

      HELP greatly appreciated.

      Thanks in advance,
      Freddy

       
      • Brian Peterson
        Brian Peterson
        2007-01-25

        In contacts/edit-2.php, contact_id is already part of the data array that the hook uses to build its query.

        In contacts/new-2.php, contact_id is not in the data array, since it was just created.  You could add it to the data array that was passed to the new_contact api function, and the hook would work without having to declare it as a global.  I'd need to look to see whether using global $contact_id in your hook function would present a security risk if register_globals was on (it's supposed to be off, but we constantly see people turning it on and then complaining here that things don't work the way they expected, and it causes all sorts of security headaches).

        Thanks for the effort.  diff files as described in the README and docs would be more useful, but I can probably work from your notes.

        Regards,

          - Brian

         
    • freddysf
      freddysf
      2007-02-08

      *** NEW VERSION *** without global variables

      Thank you for your patience Brian! Now I understand a little better the hook plugin architecture.

      I have uploaded a ** NEW VERSION ** of patch files in the patches forum.

      Please disregard the version from the previous posts

      Here is the link:

      http://sourceforge.net/tracker/index.php?func=detail&aid=1655573&group_id=88850&atid=588130

      Regards,
      Freddy