Building extensions - interact with the content page extension

The content page extension is primarily used to add other extensions to the extension picker, and links to the link picker, in the content page editor (TinyMCE). However, it is also possible to use the SMPagesPage class for adding and modifying content pages.


A simple content page extension

As described in the section Content Page Extension, it is possible to create a small extension that may be inserted in to an ordinary content page

Building a content page extension is rather simple - see the following snippet of code.

ContentPageExtension.class.php

SMExtensionManager::Import("SMPages", "SMPagesExtension.class.php", true);

// Notice how class is prefixed with the name of the extension (MyTest)
class MyTestContentPageExtension extends SMPagesExtension
{
    public function Render()
    {
        $content = "
        <br> Hello world. This is a small example of a content page extension.
        <br> Instance ID: " . $this->instanceId . "
        <br> Context: " . $this->context->GetExtensionName() . "
        ";

        return $content;
    }
}
In the first line we import the file SMPagesExtension.class.php from the SMPages extension, which contain the class SMPagesExtension. In the next couple of lines we define a class that extends the class SMPagesExtension, and overrides the Render function defined in the SMPagesExtension class. In the Render function all we have to do is implement our logic, and return the content we want to display on the content page, which can be both simple text and an advanced user interface.

Notice that an instance ID is written. $this->instanceId (defined on the SMPagesExtension class) is useful if we want to support multiple instances of the content page extension on the same content page - for instance for defining unique function names for JavaScripts and unique names for HTML/GUI components.
The context object is an instance of SMContext. The function GetExtensionName returns the name of the extension (in this case SMPages which executes the content page extension). The context object is defined on the class SMPagesExtension.

In order for the new content page extension to show up in the extension list in the page editor, the following lines of code should be added to the Main.class.php file.

$smPagesExists = SMExtensionManager::Import("SMPages", "SMPagesExtension.class.php");

if ($smPagesExists === true)
{
    if (SMPagesExtensionList::GetInstance()->GetReadyState() === true)
    {
        $extList = SMPagesExtensionList::GetInstance();
        $extList->AddExtension("My content page extensions", "MyTest page extension", "MyTest", "ContentPageExtension.class.php", "MyTestContentPageExtension");
    }
}
The interesting line of code is the call to the AddExtension function. Invoking the code above during InitComplete or PreRender, will add the new content page extension to the list of available extensions in the page editor. See further information about the arguments used in the class definition further down this page.



Adding links to link list

The content page editor allows for links to be added to any page by selecting some text and clicking the link icon. In the window that shows up, a link can be defined by either entering an URL (http://www.some-domain.com), or by selecting a link from a drop down menu. The latter is used to create links between content pages, but may also be used to create links to other things. The following lines of code demonstrates this.

$smPagesExists = SMExtensionManager::Import("SMPages", "SMPages.classes.php");

if ($smPagesExists === true)
{
    if (SMPagesLinkList::GetInstance()->GetReadyState() === true)
    {
        $pagesLinkList = SMPagesLinkList::GetInstance();
        $pagesLinkList->AddLink("Search engines", "Google", "http://google.com");
        $pagesLinkList->AddLink("Search engines", "Altavista", "http://av.com");
    }
}
The code above must be executed during InitComplete or PreRender. The interesting lines of code are the calls to the function AddLink. This function, along with the arguments, are described in greater details, in the class definition further down this page.


SMPages API

In the following section the API of the SMPages extension is described.


SMPagesLinkList class

Function Return type Description
::GetInstance() SMPagesLinkList
Get instance of SMPagesLinkList on which links may be added.
GetLinkCollection() Array Returns array used internally in the class to store link information. The array contains string arrays. Each of these arrays are indexed like this: category, title, url. Example: $array[0]["url"]
SetLinkCollection($arr:array)   Set array containing link information. The array must have the exact same layout as specified in the description for GetLinkCollection. All string arrays must contain both category, title, and url.
AddLink($category, $title, $url)

Create a new link with the specified title and URL. Multiple links created with the same category will be grouped together in the link picker. Links may be added during InitComplete and PreRender.
GetReadyState() Boolean
Returns True when the link picker is being used, otherwise False. Use this function to determine whether to add links or not. The ready state is set during Init and will be available during InitComplete.
SetReadyState()
Used by the SMPages extension controller to indicate that the link collection is ready to be populated. Specifying True will therefore result in link list to be populated by all other extensions.


SMPagesExtensionList class

Function Return type Description
::GetInstance() SMPagesExtensionList Get instance of SMPagesExtensionList on which extensions may be added.
GetExtensionCollection() Array Returns array used internally in the class to store extension information. The array contains string arrays. Each of these arrays are indexed like this: category, title, extension, file, class, argument, width, height. Example: $array[0]["title"]
SetLinkCollection($arr:array)   Set array containing extension information. The array must have the exact same layout as specified in the description for GetExtensionCollection. All string arrays must contain category, title, extension, file, class, argument, width, and height. Width and height are specified as ie "100px".
AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string)
Add information about a content page extension, allowing it to be inserted and used on a content page. This function should be called during InitComplete or PreRender. Arguments:
$category - extensions with the same values are grouped together.
$title - extension title.
$extension - name of the extension (extension folder).
$file - file containing the content page extension.
$class - name of the class defining the content page extension.
AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string, $argument:string)   See description for AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string).
$argument - value is passed to the content page extension instance. It may be accessed here using $this->arguments.
AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string, $argument:string, $width:integer)   See description for AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string, $argument:string).
$width - width of the place holder image representing the content page extension in the editor.
AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string, $argument:string, $width:integer, $height:integer)   See description for AddExtension($category:string, $title:string, $extension:string, $file:string, $class:string, $argument:string, $width:integer).
$height - height of the place holder image representing the content page extension in the editor.
GetReadyState() Boolean
Returns True when the extension picker is being used, otherwise False. Use this function to determine whether to add extensions or not. The ready state is set during Init and will be available during InitComplete.
SetReadyState()
Used by the SMPages extension controller to indicate that the extension collection is going to be used. Specifying True will therefore result in extension list to be populated by all other extensions.


SMPagesPage class

Function Return type Description
__construct($guid:string, $filename:string)   Create a new content page. The argument $guid must be unique. Use SMRandom::CreateGuid() to create a Guid. $filename should be unique as well.
GetId()
String
Get unique ID (Guid).
SetFilename($value:string)

Change filename.
GetFilename()
String
Get filename.
SetTitle($value:string)

Change content page title.
GetTitle()
String
Get content page title.
SetContent($value:string)

Set content of content page (HTML)
GetContent()
String
Get content of content page (HTML)
SetAccessible($value:boolean)

Set True to make page available to the public, False to protect it from being displayed.
GetAccessible() Boolean Get value indicating whether content page is accessible to the public.
SetKeywords($value:string)   Keywords used to describe the content of the content page. Keywords should be separated by comma.
GetKeywords() String Get keywords describing content.
SetDescription($value:string)   Set short text describing the content of the content page.
GetDescription() String Get short description of the content of the content page.
SetAllowIndexing($value:boolean)   Set True to allow search engines to index the page, False to instruct them not to index the page. Notice that this value might be ignored by certain search engines, and is therefore not suitable for protecting data. See SetAccessible($value:boolean).
GetAllowIndexing() Boolean Get value indicating whether search engines are instructed to index the page or requested not to index the page.
GetLastModified() Integer Get timestamp indicating last modification.
CommitPersistent() Boolean Make content page permanent by committing it to the data source. Returns True on success, otherwise False.
DeletePersistent() Boolean Remove content page from data source. Returns True on success, otherwise False.
::GetPersistentByGuid($guid:string) SMPagesPage Get content page instance by unique ID (Guid). Returns null if not found.
::GetPersistentByFilename($filename:string) SMPagesPage Get content page instance by filename. Returns null if not found.


SMPagesLoader class

Function Return type Description
::GetPages() SMPagesPage[] Get all content pages. Content (HTML) is not included.
::GetPages($includeContent:boolean) SMPagesPage[] Get all content pages. Set argument True to include the content of the content pages.


SMPagesExtension class

Function
Return type
Description
__construct($context:SMContext, $pageId:string, $instanceId:integer, $arg:string)

Used by SMPages extension to create instances of content page extensions, which inherit from SMPagesExtension class.
Render()
String
Renders and returns client code representing the content page extension.
     
Variable Type Description
$context SMContext Private class member. Contains access to template, form etc. (see documentation for SMContext class).
$pageId String Private class member. Unique page ID (Guid)
$instanceId Integer Private class member. Instance ID. Several identical extensions on the same page will get different instance IDs. Use this ID to create unique JavaScript function names, HTML identifiers etc., to avoid conflicts client side.
$argument String Private class member. Adding content page extensions to the SMPagesExtensionList class allows for an argument to be passed to the extension. This is useful if the extension is supposed to work on different data. For instance the SMGallery extension uses this argument to pass the name of a gallery to display.