Jump to content

hook on \IPS\cms\Recordsx


BomAle

Recommended Posts

Posted

I would extend $databaseColumnMap adding "poll" => "poll_state" but I don't know how.

Currently I have a hook on __construct() Records.php but when i try to call statically the class like \IPS\cms\Records1::$databaseColumnMap["poll"] it is not called yet...

I see \IPS\cms\Application have a autoload, how I can interact when class is evalued/initialized?

$multitons could help me here?

anyone use another spl_autoload_register to permise it?

Posted

Can I extend $databaseColumnMap instead of override?

	/**
	 * @brief	Database Column Map
	 */
	public static $databaseColumnMap;
	
	/**
	 * @brief	[Records] Custom Database Id
	 */
	public static $customDatabaseId;
	
	/**
	 * Constructor - Create a blank object with default values
	 *
	 * @return	void
	 */
	public function __construct()
	{
		$return = call_user_func_array( 'parent::__construct', func_get_args() );
		static::$databaseColumnMap['poll'] = 'poll_state';
		return $return;
	}

currently I have this

Posted

Try array_merge() and don't re-declare $databaseColumnMap or $customDatabaseId - so just do this:

/**
	 * Constructor - Create a blank object with default values
	 *
	 * @return	void
	 */
	public function __construct()
	{
		$return = call_user_func_array( 'parent::__construct', func_get_args() );
		static::$databaseColumnMap = array_merge( static::$databaseColumnMap, array( 'poll' => 'poll_state' ) );
		return $return;
	}

 

Posted

but when I call statically the class it don't call constructor... only if I create new object "new \IPS\cms\Records1" I would extend the variable when call like \IPS\cms\Records1::$databaseColumnMap["poll"]

Posted

It's not currently possible to overload static properties via class extensions without redefining the entire property all over again - this is a PHP limitation (which may cause some complications if there are multiple plugins attempting to do the same thing - depending on the order of execution, you may overwrite another plugins changes).

Can you explain what it is you're attempting to accomplish? Typically, you really shouldn't need to reference the column map outside of an object context.

Posted

i will enable polls for pages app, according to

Next, add a poll key to your $databaseColumnMap with the value being the name of the database column that stores the poll ID for this item (note, you can only have one poll per content item).

I have some check to create poll_state column into each database but I am stopped at this point... I will take care of others plugin/app otherwise I had overriden the var.

Posted

Oh, well doing it the way I suggested is fine - correct, it will not be present when called statically, but you should never need to call it statically outside of an object context, in which __construct() will always be run.

So, if you take a look at the methods in that article - they are all in an object context except for canCreatePoll(), which doesn't need to reference the column map - just to ensure the user can post polls.

So, your plugin would be (as an example):

class _MyClass extends \IPS\cms\Records implements \IPS\Content\Polls, \SplObserver
{
	public function __construct()
	{
		$return = parent::__construct();
		static::$databaseColumnMap = array_merge( static::$databaseColumnMap, array( 'poll' => 'poll_state' ) );
		return $return;
	}

	public static function canCreatePoll( \IPS\Member $member = NULL, $container = NULL )
	{
		return TRUE;
	}

	public function getPoll()
	{
		$pollColumn = static::$databaseColumnMap['poll'];
		$poll = NULL;
		if ( $this->$pollColumn )
		{
			try
			{
				$poll = \IPS\Poll::load( $this->$pollColumn );
			}
			catch( \OutOfRangeException $e ) {}
		}
		return $poll;
	}

	public function update( \IPS\Poll $poll )
	{
		
	}
}

 

Posted

When I try to build custom table and check the RouterContent extension the constructor is not called for each class... like:

foreach ( \IPS\Content::routedClasses( FALSE, FALSE, TRUE ) as $class )
{
	/*FIX*/
	if ( mb_strpos( $class, 'IPS\cms\Records' ) !== FALSE and is_numeric( mb_substr( $class, 15, 1 ) ) )
	{
		if ( !in_array( 'IPS\cms\Records', get_declared_classes() ) )
		{
			new $class; // this call the contructor... bad?
		}
	}
	if ( in_array( 'IPS\Content\Polls', class_implements( $class ) ) AND isset( $class::$databaseColumnMap['poll'] ) )
	{
		\IPS\Db::i()->select( $class::$databasePrefix . $class::$databaseColumnId .",". $class::$databasePrefix . $class::$databaseColumnMap['poll'].",".........................
	}
}

I would avoid to call constructor into this way...

Posted
foreach (\IPS\Content::routedClasses(false, false, true) as $class)
{
	if (in_array('IPS\Content\Polls', class_implements($class)))
	{
		$obj = new $class;
		
		if (isset($class::$databaseColumnMap['poll']))
		{
			/**/
		}
	}
}

 

Posted

As I said, it looks like you will need to redefine $databaseColumnMap to add the poll key. You can have a hook like this and nothing else if you need:

class hookX extends classY
{
 static protected $databaseColumnMap = array
 (
	....
	'poll'	=> 'pollcolumn'
 );
}

 

As Ryan noted, multiple hooks on the same area of code attempting to do the same thing would overwrite each other, however, so keep that in mind.

Posted
3 hours ago, bfarber said:

As Ryan noted, multiple hooks on the same area of code attempting to do the same thing would overwrite each other, however, so keep that in mind.

Yes, the solution here seems to extend constructor to avoid conflicts... but personally I don't like it.

I won't override some multitons instance (I am not sure what I am talking ?) or create one and don't take care of __destruct... if I found a better way I report here.

Many thanks to all answers.

  • 2 months later...
Posted

Yes I solved now, spl_autoload_register was bring me crazy

class advpolls_hook_application extends _HOOK_CLASS_
{
	public static function afterSPLAutoloads( $class )
	{
		if ( mb_substr( $class, 0, 15 ) === 'IPS\cms\Records' and is_numeric( mb_substr( $class, 15, 1 ) ) )
		{
			\spl_autoload_unregister(['\IPS\cms\advpolls_hook_application','afterSPLAutoloads']);
			\spl_autoload_call($class);
			$class::$databaseColumnMap['poll'] = 'poll_state';
		}
	}
}
\spl_autoload_register( ['\IPS\cms\advpolls_hook_application','afterSPLAutoloads'], TRUE, TRUE);

I can continue, if you have any advice, I would appreciate it

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...