Jump to content

Community

Developer Documentation

Building a model for Content Items

Inheritance Chain

Your content model extends several classes. In turn, these are:

  • \IPS\Content\Item
    Provides the features of Content Items. It contains all the code for the various optional features of content items which you will activate by adding properties and interfaces to your model. The rest of the guides in this section cover implementing these features.
  • \IPS\Content
    Provides a small number of features which are common to both Content Item models and Content Comment models (explained later) such as getting the author and working with $databaseColumnMap.
  • \IPS\Patterns\ActiveRecord
    Provides the functionality to load items from the database, work with their properties, save and delete them. See the Active Records guide for more information on this class.

 

Basic Skeleton

<?php
namespace IPS\yourapp;

class _YourClass extends \IPS\Content\Item
{
	/**
	* @brief Multiton Store 
	*/
	protected static $multitons;

	/**
	* @brief Default Values 
	*/
	protected static $defaultValues = NULL;

	/**
	* @brief Application 
	*/
	public static $application = 'yourapp';

	/**
	* @brief Module 
	*/
	public static $module = 'yourmodule';

	/**
	* @brief Database Table 
	*/
	public static $databaseTable = 'table';

	/**
	* @brief Database Prefix 
	*/
	public static $databasePrefix = 'prefix_';

	/**
	* @brief Database Column Map 
	*/
	public static $databaseColumnMap = array( 
		'author' => 'author'
	);

	/**
	* @brief Title
	*/
	public static $title = thing’;

	/**
	* Get URL
	*
	* @param string|NULL
	* @return \IPS\Http\Url 
	*/
	public function url( $action=NULL )
	{
		$url = \IPS\Http\Url::internal( ... );
		if ( $action )
		{
	   		$url = $url->setQueryString( 'do', $action );
		}
		return $url;
	}
}

 

Specifying your class properties

Content item models require a few static properties to configure their behavior. Many come from the Active Record class.

public static $application = 'string';

Required. The application key that the content item belongs to.

 

public static $module = 'string';

Required. The module key that the content item belongs to.

 

public static $multitons = array();
public static $defaultValues = NULL;

Required. Inherited from \IPS\Patterns\ActiveRecord.
These two properties are requirements of \IPS\Patterns\ActiveRecord. They don't need a value assigned; they simply need to be defined.

 

public static $databaseTable = 'string';

Required. Inherited from \IPS\Patterns\ActiveRecord.
The name of the database tablet that stores these content items.

 

public static $databasePrefix = 'string';

Optional. Inherited from \IPS\Patterns\ActiveRecord
Specifies the field prefix this table uses.

 

public static $databaseColumnMap = array();

Required. Features provided by the higher classes your model extends will examine this array to find out what columns certain things are stored as in your database. The following elements are required, and in all cases the value is with the $databasePrefix omitted:

  • author
    Should contain the name of the column which contains the ID number of the member who posted the content.
  • title
    Should contain the name of the column which contains the title of the content.
  • date
    Should contain the name of the column which contains a unix timestamp of when the content item was created.
  • ip_address
    Should contain the name of the column (without the $databasePrefix) which contains the IP address of the user who posted the item.

 

public string $title = 'string';

The key for the language string that describes what your content item is (for example, 'Topic', 'File', 'Image', etc.)

 

Available methods on Content Item models

In addition to those provided by \IPS\Patterns\ActiveRecord (which work exactly the same as for nodes), a number of additional methods are available.

\IPS\Patterns\ActiveRecordIterator static getItemsWithPermission( ... )

Gets content items that the current user has permission to access. See the phpDoc in /system/Content/Item.php for all supported arguments.

 

\IPS\Member author()

Returns the \IPS\Member object for the user that posted the content item. For example:

$item = YourClass::load( 1 );
$user = $item->author();
echo $user->name;

 

boolean canView( [ \IPS\Member $member=NULL ] )

Returns a boolean value indicating if the provided member can view the content item. By default will always return TRUE, but is affected if Permissions is enabled and if Hiding is enabled.

  • $member (\IPS\Member, optional)
    If provided, uses this member's permissions when performing the check. By default, the currently-logged in member will be used.

 

boolean canEdit( [ \IPS\Member $member=NULL ] )

Returns a boolean value indicating if the provided member can edit the content item.

  • $member (\IPS\Member, optional)
    If provided, uses this member's permissions when performing the check. By default, the currently-logged in member will be used.

 

boolean canDelete( [ \IPS\Member $member=NULL ] )

Returns a boolean value indicating if the provided member can delete the content item

  • $member (\IPS\Member, optional)
    If provided, uses this member's permissions when performing the check. By default, the currently-logged in member will be used.

 

boolean static modPermission( string $type [, \IPS\Member $member=NULL [, \IPS\Node\Model $container=NULL ] ] )

Returns a boolean value indicating if the provided member has permission to perform the action specified by the $type param in the specified $container (if provided)

  • $type (string, required)
    The type of permission being checked. Acceptable values are:
    • edit
    • delete
    • move
    • feature (if Featuring is enabled)
    • unfeature (if Featuring is enabled)
    • pin (if Pinning is enabled)
    • unpin (if Pinning is enabled)
    • lock (if Locking is enabled)
    • unlock (if Locking is enabled)
    • hide (if Hiding/Approving is enabled)
    • unhide (if Hiding/Approving is enabled)
    • view_hidden (if Hiding/Approving is enabled)
  • $member (\IPS\Member, optional)
    If provided, uses this member's permissions when performing the check. By default, the currently-logged in member will be used.
  • $container (\IPS\Node\Model, optional)
    If provided, checks the permission specifically in this container node.

 

void modActionstring $type [, \IPS\Member $member=NULL [, string $reason=NULL ] ] )

Performs the specified moderation action. Throws OutOfRangeException if the member does not have permission to perform this action.

  • $type (string, required)
    The type of moderation action being performed. Consult the list in the previous method for acceptable values.
  • $member (\IPS\Member, optional)
    If provided, uses this member's permissions when performing the check. By default, the currently-logged in member will be used.
  • $reason (string, optional)
    Used only for hide/unhide actions; specifies the reason the action is being taken.

  Report Document


×
×
  • Create New...