Jump to content

Does formatFormValues load form data?


Recommended Posts

Hi all,

I've been trying to make my own plugin that creates threads automatically when a new gallery image or album is posted for the last few days but I am stuck on the form for each category. For whatever reason that data does not load the data back onto the form but it does save onto the database. 

This is what I have for formatFormValues:

	public function formatFormValues( $values )
	{
	
			$values = parent::formatFormValues( $values );
				
	
			if ( $this->id )
			{
				$save = array( 'threadgen_gallery_forum_on' => (int)$values['threadgen_gallery_forum_on'], 'threadgen_gallery_category_id' => $this->id, 'threadgen_gallery_forum_id' => $values['threadgen_gallery_forum_id']->id, 'threadgen_gallery_topic_prefix' => (string)$values['threadgen_gallery_topic_prefix'], 'threadgen_gallery_topic_suffix' => (string)$values['threadgen_gallery_topic_suffix'], 'threadgen_gallery_topic_delete' => (int)$values['threadgen_gallery_topic_delete'] );
				foreach ( $values as $k => $v )
				{
					if ( \in_array( $k, array( 'threadgen_gallery_category_id', 'threadgen_gallery_forum_id', 'threadgen_gallery_topic_prefix', 'threadgen_gallery_topic_suffix', 'threadgen_gallery_topic_delete' ) ) )
					{
						unset( $values[$k] );
					}
				}
				
	
				if ( \count( $save ) )
				{
					\IPS\Db::i()->insert( 'gallery_threadgen_addon', $save, TRUE );
				}
			}
			return $values;
		
	}

Basically the question is does formatFormValues load the values back or only save them? If it only saves them how would I load them back?

Thanks for your time and help! 🙂

Second parameter for forms is default value whoops 😅

Edited by flashpoint
Link to comment
Share on other sites

After seeing 

gallery_threadgen_addon

I have a minor suggestion => Please use your own database table prefix! It's going to be much easier for you (but also for our staff to not mix up any 3rd party database tables with our own!

We have also covered this in our guidelines

1 hour ago, flashpoint said:

Second parameter for forms is default value whoops 😅

YEP;)

Link to comment
Share on other sites

30 minutes ago, Daniel F said:

After seeing 

gallery_threadgen_addon

I have a minor suggestion => Please use your own database table prefix! It's going to be much easier for you (but also for our staff to not mix up any 3rd party database tables with our own!

We have also covered this in our guidelines

YEP;)

Will do! Still having issues loading the saved data from the database though

    public function save()

    {

        try
        {
            $data = $this->_data;
\IPS\Log::log( $data, 'save function threadgen'  );
            $joinTableData = array();
\IPS\Log::log( $joinTableData, 'save function threadgen data'  );
\IPS\Log::log( $databasePrefix, 'thread_gen_db_prefix'  );
            foreach ( ( $this->_new ? $this->_data : $this->changed ) as $k => $v )
            {

                if ( \in_array( $k, array('threadgen_gallery_forum_on', 'gallery_forum_on', 'threadgen_gallery_category_id', 'threadgen_gallery_forum_id', 'threadgen_gallery_topic_prefix', 'threadgen_gallery_topic_suffix', 'threadgen_gallery_topic_delete'  ) ) )
                {
\IPS\Log::log( $k . $v, 'save function threadgen foreach'  );
                    $joinTableData[ static::$databasePrefix . $k ] = $v;
                    unset( $this->_data[ $k ] );
                    unset( $this->changed[ $k ] );
                }
            }


    
            parent::save();
    
            $data['id'] = $this->_data['id'];
            $this->_data = $data;
    
            if ( \count( $joinTableData ) )
            {
                $joinTableData['threadgen_gallery_category_id'] = $this->id;
    
                \IPS\Db::i()->insert( 'autothreadgen_gallery_addon', $joinTableData, TRUE );
            }
        }
        catch ( \RuntimeException $e )
        {
            if ( method_exists( get_parent_class(), __FUNCTION__ ) )
            {
                return \call_user_func_array( 'parent::' . __FUNCTION__, \func_get_args() );
            }
            else
            {
                throw $e;
            }
        }
    }

 

It only appears to save the default data into $this

 

array (
  'id' => 2,
  'parent_id' => 0,
  'name_seo' => 'test-321',
  'count_imgs' => 0,
  'count_comments' => 0,
  'count_imgs_hidden' => 0,
  'count_comments_hidden' => 0,
  'cover_img_id' => 0,
  'last_img_id' => 0,
  'last_img_date' => 0,
  'sort_options' => 'album_last_img_date',
  'allow_comments' => true,
  'allow_rating' => true,
  'approve_img' => false,
  'approve_com' => false,
  'rating_aggregate' => 0,
  'rating_count' => 0,
  'rating_total' => 0,
  'watermark' => 1,
  'position' => 2,
  'can_tag' => true,
  'preset_tags' => '',
  'public_albums' => 0,
  'nonpublic_albums' => 0,
  'tag_prefixes' => true,
  'allow_albums' => '1',
  'show_rules' => '0',
  'rules_link' => NULL,
  'allow_reviews' => false,
  'review_moderate' => 0,
  'club_id' => NULL,
  'sort_options_img' => 'updated',
  'allow_anonymous' => 0,
  'last_poster_anon' => 0,
)

 

I'll keep trying but hopefully someone can point me in the right direction 🙂

Link to comment
Share on other sites

17 hours ago, Daniel F said:

Are the data stored to autothreadgen_gallery_addon ? Are you actually loading the data from autothreadgen_gallery_addon and when the object is loaded? 

Without seeing the full code I can only guess..:D

Yes, data is inserted and updated correctly. I believe that the data is loaded but can't seem to find a way to confirm that using \ips\log

 

protected static function constructLoadQuery( $id, $idField, $extraWhereClause )
		{
			try
			{
				return parent::constructLoadQuery( $id, $idField, $extraWhereClause )->join( 'autothreadgen_gallery_addon', 'autothreadgen_gallery_addon.threadgen_gallery_category_id=gallery_categories.category_id' );
				\IPS\Log::log( constructLoadQuery, 'constructLoadQuery' );
			}
			catch ( \RuntimeException $e )
			{
				\IPS\Log::log( $e, 'load query e' );
				if ( method_exists( get_parent_class(), __FUNCTION__ ) )
				{
					\IPS\Log::log( $e, 'load query e' );
					return \call_user_func_array( 'parent::' . __FUNCTION__, \func_get_args() );
				}
				else
				{
					\IPS\Log::log( $e, 'load query e' );
					throw $e;
				}
			}
		}

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Upcoming Events

    No upcoming events found
×
×
  • Create New...