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

Close

#282 Problem outputting dynamic custom fields

Minor
open
nobody
None
1
2014-08-27
2013-10-16
Anonymous
No

function create_field_entry_custom_field($field, $key, $disabled=FALSE)
{
global $custom_fields, $tbl_entry, $select_options;
global $is_mandatory_field, $text_input_max;

   if ($key == "activity_id")

  {

    $query="SELECT id, activity_name FROM teamtrack_activity Order By id";

    $res = sql_query($query);

    if ($res === FALSE)

    {

      trigger_error(sql_error(), E_USER_WARNING);

      fatal_error(FALSE, get_vocab("fatal_db_error"));

    }

    $select_options["entry.$key"] = array();

    for ($i = 0; ($row = sql_row_keyed($res, $i)); $i++)

    {

      $select_options["entry.$key"][$row['id']] = $row['activity_name'];

    }

  }

echo "

\n";

I have several custom fields and above is an example of the custom field activity_id. I have added the field to the database on both entry and repeat and the code above. However I am having two issues. The field in the db is (int 6). The id field on teamtrack_activity is (int 6) and activity name is (varchar 100).

-1. The activity_name is being passed with the above code. What is odd is if I change the code to:

      $select_options["entry.$key"][$row['activity_name']] = $row['id'];

then it is still passed (I do see the integers as choices).

If I change it to:

      $select_options["entry.$key"][$row['id']] = $row['id'];

then the id is passed OK. However it is not nice to pick id's and I do not know how to change the vocab or even if you can for a dynamic custom field.

-2. With just mentioned that passes then I have another problem. The int does come up fine in the view page (which still no vocab assigned to it). But when I go back to edit the entry there it does not have the choice selected by default.

I feel this is a very common use for MRBS and would love anyone's help. MRBS is so well written can't believe I can't figure this out.

Discussion

  • The text should be the value and not the key. So what you want is

    $select_options["entry.$key"][$row['id']] = $row['activity_name'];
    

    Campbell

     

  • Anonymous
    2013-10-16

    Hi Campbell

    Thanks for the quick response. That is what I have labeled as problem #1 - when I use the code you have then it only outputs the activity_name. No idea why. It doesn't make since at all

    Here is an example I just did:

    Array
    (
    [f_activity_id] => Facilitate

    It should have passed "2" which is the id for that row. So when "Facilitate" hits the DB then it turns into "0" (because DB is set to int with no default). I spent 2 hours trying to figure out why all my custom fields were turning to zeros.

     
  • What version of MRBS are you using? There was a bug in this area in earlier versions.

    Campbell

     

  • Anonymous
    2013-10-16

    $select_options['entry.segment_id'] = array('1' => 'Coffee',
    '2' => 'Sandwiches',
    '3' => 'Hot Lunch');

    When I add segment_id (int 6) to DB and the above code to systemdefaults.php I again get the items in a drop down with the correct names but it only outputs the full name. Again it turns everything to zeros. So maybe just an overall issue with custom fields or I don't understand how to use them. If I get this working I will definitely add to the documentation - I have a few iterations I will be using custom fields on.

    I tried this out on 1.4.07 first but just downloaded the latest a few hours ago and still happening.

     
    Last edit: Anonymous 2013-10-16

  • Anonymous
    2013-10-16

    And by the way my company made me do an audit of available calendar programs - they do this every few years - and again MRBS beat out the pay ones. Flexibility and good code rule.

     

  • Anonymous
    2013-10-16

    Thank you. I will document the creation of dynamic custom fields. Also was tasked with creating a view of all rooms in an area over multiple days and possibly something similar for weeks.

     

  • Anonymous
    2013-10-16

    Last few versions seem to react the same way if that saves you any time. I trust MRBS so much that I tried 4 different versions and 2 different web servers. I have so much customization on this last one that I thought for sure it was me at first.

     
  • I can see what's going wrong. The problem is that the array is being treated as a simple array, rather than an associative array. This happens even in the Sandwiches example above because although the key is a string PHP casts it to an integer because it looks like an integer.

    For the moment the best thing to do is to add the line

    $params['force_assoc'] = TRUE;
    

    before the call to

    generate_input($params);
    

    in create_field_entry_custom_field()

    There may be a better way for MRBS to handle this altogether, but I'll need to think about it for a bit. However for the moment the suggestion above should solve the problem even if it's perhaps not the most elegant way.

    Campbell

     

    • Anonymous
      2013-10-16

      This works perfect - verified on two different versions. Is there a reason or issue using this? I ask because you offered another solution. Thanks again.

       
  • Another way round this is to turn your id field into a string, eg by appending an alpha character to the original id.

    Campbell

     
    • Of course, you'd have to change your fields in the database from ints into varchars.

      Campbell

       

      • Anonymous
        2013-10-16

        I actually did this with version 1.4.07 (I think?). I didn't go much further with it because when I had the field as varchar it forced a text box, even with the injected select options. I see that this does work in the latest version so it is an option for people.

         
  • I've added some comments to systemdefaults.inc.php in Rev 2765 documenting the problem.

    Campbell

     
  • Both methods are probably OK, though I suppose all other things being equal appending or prepending an alpha character may be marginally better as it eliminates one line of customisation.

    I'm mulling over whether the whole way $select_options works should change. The problem is that at the moment it assumes a distinction between simple indexed and associative arrays which doesn't exist in PHP. At the moment 'force_assoc' is the way round this, but I'm wondering whether another method might be better, for example making the array look something like this

    $select_options = array('assoc' => TRUE,
                            'data' => array(1 => 'sandwiches', 2 => 'coffee'));
    

    But I don't suppose I'll do anything anytime soon. If you or anybody else have some thoughts they'd be very welcome.

    Campbell

     

    • Anonymous
      2013-10-22

      Hi Campbell -

      Two things. Things are going very well moving MRBS into an employee tracker. The fix worked great. I looked through functions.inc and don't really see a "better" way of doing it. I guess it could be an option turned on/off in the config file...

      And I want to mention that if your custom field referenced table is set up with just two fields (id, name) then you don't even have to add the if statements to get the dropdowns populated with the corresponding names. I have to say I haven't even looked at the code behind this because half my custom fields are created this way and it works - great code! (I am not sure if this works because of my naming convention and if you use more than 2 fields you need to have code to reference which is the name field)

      And I have to leave with a question of course. I have a small dilemma and would like your opinion (or anyone who has come across this). I added an "activity" field and the powers to be want that field to be the one that determines background color on calendar opposed to type. I have already got the background color working based on the activity table color field. However I am wondering if I should have turned type into a table? What is your opinion on type being turned into its own table vs using a custom field to manage colors? (Reason being that the type/activity colors/names have to be editable using easy editor not file)

       
      Last edit: Anonymous 2013-10-22


Anonymous


Cancel   Add attachments