Viewing 3 reply threads
  • Author
    Posts
    • #1294
      Marcus
      Member

      I have created a group:

      
      
      piklist('field', array(
      	'type' => 'group'
      	,'add_more' => false
      	,'scope' => 'post_meta'
      	,'label' => 'Booking Misc.'
      	,'field' => 'mae_sched_misc'
      	,'description' => 'Cost, Max Students, Alerts'
      	,'columns' => 12
      	,'attributes' => array (
      	)
      	,'fields' => array (
      		array (
      			'type' => 'text'
      			,'field' => 'mae_sched_cost'
      			,'label' => 'Cost $'
      			,'value' => ''
      			,'attributes' => array (
      				'class' => array (
      					'cost'
      				)
      				,'size' => 10
      			)
      			,'columns' => 6
      			,'on_post_status' => array(
      			  'value' => 'publish'
      			)
      		)
      		,array (
      			'type' => 'text'
      			,'field' => 'mae_sched_max_students'
      			,'label' => 'Max Students '
      			,'value' => ''
      			,'attributes' => array (
      				'class' => array (
      					'maxstudents'
      				)
      				,'size' => 10
      			)
      			,'columns' => 6
      			,'on_post_status' => array(
      			  'value' => 'publish'
      			)
      		)
      		,array (
      			'type' => 'checkbox'
      			,'field' => 'mae_sched_allow_overbook'
      			,'label' => 'Overbooking'
      			,'value' => 'y'
      			,'label_position' => 'after'
      			,'attributes' => array (
      				'class' => array (
      					'allowoverbook'
      				)
      			)
      			  ,'choices' => array(
      			    'y'=>'Allow'
      			  )
      			,'columns' => 6
      		)
      		,array (
      			'type' => 'checkbox'
      			,'field' => 'mae_sched_alert_full'
      			,'label' => 'Camp is Full'
      			,'value' => 'y'
      			,'label_position' => 'after'
      			,'attributes' => array (
      				'class' => array (
      					'alertonfull'
      				)
      			)
      			  ,'choices' => array(
      			    'y'=>'Alert'
      			  )
      			,'columns' => 6
      		)
      	)
      ));
      

      I have some of them set to show the value on publish: (so they are not editable)

      
      ,'on_post_status' => array(
        'value' => 'publish'
      )
      

      But the last two (checkboxes) can be edited. (not using on_post_status on them)

      Here’s the bug. If you update either of these checkboxes, it affects the first two fields values. Probably because these first two are part of a group (array in post_meta) so the whole group gets updated even though only two fields are editable.

      In the function that updates the array, maybe it should load the other parts of the array as a double check when it posts to the post_meta table. Otherwise, it will rewrite over the array destroying the values for the on_post_status fields. (as we’ll always need to use groups for some things)

      I’ll try and find it myself, but Kevin may have already fixed it for the next release.

      Marcus

    • #1295
      Steve
      Keymaster

      Hi Marcus– You always find the best bugs! We should be able to look at next week.

    • #1296
      Marcus
      Member

      Fixed it!!!! 🙂

      It was in includes/class-piklist-form.php
      in the save function of the Piklist_Form class.

      I changed this code:

      
                foreach ($data as $key => $value) 
                {
                  delete_metadata($meta_type, $id, $key);
      
                  if (is_array($value))
                  {                  
                    if (!piklist::is_flat($value) && !isset($value[0]))
                    {
                      add_metadata($meta_type, $id, $key, $value);
                    }
                    else
                    {
                      foreach ($value as $meta)
                      {
                        if (!empty($meta))
                        {
                          if (is_array($meta) && count($meta) == 1)
                          {
                            $meta = current($meta);
                          }
                          
                          add_metadata($meta_type, $id, $key, $meta);
                        }
                      }
                    }
                  }
                }
      

      To this:

      
                foreach ($data as $key => $value) 
                {
                  $orig = get_metadata($meta_type, $id, $key);
                  delete_metadata($meta_type, $id, $key);
      
                  if (is_array($value))
                  {                  
                    if (!piklist::is_flat($value) && !isset($value[0]))
                    {
                      foreach($orig[0] AS $k1=>$v1) {
                        if (!array_key_exists($k1, $value)) {
                          $value[$k1]=$v1;
                        }
                      }
                      add_metadata($meta_type, $id, $key, $value);
                    }
                    else
                    {
                      foreach ($value as $meta)
                      {
                        if (!empty($meta))
                        {
                          if (is_array($meta) && count($meta) == 1)
                          {
                            $meta = current($meta);
                          }
                          
                          add_metadata($meta_type, $id, $key, $meta);
                        }
                      }
                    }
                  }
                }
      

      All I did was before it deletes the meta data, I stored it in:

      
                  $orig = get_metadata($meta_type, $id, $key);
      

      And then added a quick check (since the surrounding if, knows its an array and not a numeric array):

      
                      foreach($orig[0] AS $k1=>$v1) {
                        if (!array_key_exists($k1, $value)) {
                          $value[$k1]=$v1;
                        }
                      }
      

      It literally builds the missing pieces of the $value array before it adds the meta field, preserving the previous sections. Now I can have multiple groups with some having an on_post_status, and some not.

      Whoohoo.

      Thanks for making something so great guys.

      Marcus

    • #1297
      chan
      Member

      Thanks for this Marcus. I updated my file but backed up my original just in case.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.