Home » V2 » Modules


Contents

Introduction

All of the features you see and interact with on phpFox2 is controlled by a module or in many cases several modules. When viewing a blog this is controlled by the blog module. There are certain features that are part of the blog module which are a part of other modules such as tagging. Tagging a blog is part of the tag module. The reason we split up features into modules is because it allows us to easily remove or build onto the core engine. Since phpFox2 is built as an engine that can support other types of products and not just a Social Networking Solution this will allow users to simply remove or uninstall features they don't like. They can also install a whole different set of modules which will allow them to start a site that is just focused on being a personal blog or web page. Since all our features are split up you will only need to install the required phrases, settings, blocks, menus etc... for that module thus saving the space in the database to not install unnecessary data. There are over 2,000 phrases at the time of this writing and if you were to load the entire language package at once this would cause a major overhead. With the usage of modules it only loads the required phrases for that module and caches it so we don't have to pull such information from the database all the time.

Another advantage is it allows other developers the ability to easily build onto the package without having to modify any of the other existing modules. Especially since the product is equipped with an advanced plug-in system which allows you to virtually access all the methods provided by other modules, developers will no need to modify other modules to accomplish what they need for their specific product.

File & Folder Structure

Our modular system is based on a MVC (module-view-controller) architecture. The following is a basic module file & folder structure that is required to create a new module.

Creating a Module

To create a new module log into your AdminCP and navigate to:

Extension >> Module >> Create New Module

Fill out the form to create your first module. Follow the detailed instructions below to properly fill out the form.

Product

Select the product:

phpFox Sample Product

Is a Core Module

If a module is part of the "core" then the module cannot be deactivated. It is mainly intended for phpFox modules that must always be enabled in order for the product to run. If you are creating a module that can be disabled and the site will still function it does not have to be part of the "core", however if the module you are creating modifies the function of the system and must be on to run the site; then its best to set it up as a "core" module.

Module ID

Unique module ID. Similar to how you create a product ID your module ID must be unique, however your module ID can only contain lowercase characters from a-z. Adding your alias or company name is a good idea. For this example lets add:

phpfoxsample

Add to Menu

In the AdminCP your module can be added to the Modules menu in case your module has controllers that need access only for Admins. Lets select Yes as we plan on adding some sample menus.

Sub Menu

Since we selected Yes for Add to Menu we must now add these menus. As you may have noticed we provide by default 4 links for your AdminCP menu. The phrase for a link is found on the left side and the URL connection is on the right. For the first Phrase we add

Menu 1

and for the first Link we add

phpfoxsample

For the 2nd Phrase we add

Menu 2

and for the 2nd Link we add

phpfoxsample.new

Info

Short description about your module. Here we can add:

This is a sample module.

Your form should then look similar to this screenshot: http://wiki.phpfox.com/images/add_new_module.png

Submit the form and you should now have created your very first module. Before we go on we need to create the module file structure. Navigate to the folder /module/ on your server where you have your phpFox2 source files. Within that folder since we used the module name phpfoxsample we must create a new folder with the same name. Within the /module/ create the folder:

phpfoxsample

Within the new folder you created create the following file structure:

/include/
/include/component/
/include/component/ajax/
/include/component/block/
/include/component/controller/
/include/plugin/
/include/service/
/static/
/static/css/
/static/image/
/static/jscript/
/template/
/template/default/
/template/default/block/
/template/default/controller/

Note that your module may not require all the folders we have just created, however in order for us to show you how to use everything a module can provide its best to create all the directories.

Now we can move on to creating our first controller.

Creating a Controller

Controllers are PHP classes that control all the pages on phpFox2. It is the first PHP class we connect to in order to control the page we are viewing. The controller then decides how the page should behave and what sort of information we should display.

Registering the Controller

To create your first controller log into the AdminCP and navigate to:

Extension >> Module >> Add Component

Now we need to complete the form...

Product

Select the product this block will belong to. In this case we select the sample product we created earlier:

phpFox Sample Product

Module

Select the module this block will belong to. In this case we select the sample module we created earlier:

phpfoxsample

Component

The name of the block class file without the PHP class suffix. We add:

index

This would then actually be index.class.php.

Type

The type of component this is, which in this case it is a controller. We select:

controller

URL Connection

The URL connection in order to connect to this controller. This is used when creating menus or blocks that will eventually connect with this controller. For this we add:

phpfoxsample.index

The period (.) represents a slash (/). The URL would then be: http://localhost/index.php?do=/phpfoxsample/ If we were to add:

phpfoxsample.test

The URL would be:

http://localhost/index.php?do=/phpfoxsample/test/

Notice in the first example and the one we are using does not include the index at the end of the URL. The reason for this is since it is the index file it defaults to it and there is no purpose in adding it.

Active

Define if the block is active or inactive. We select Yes.

The form should then look similar to the following image: http://wiki.phpfox.com/images/block_sample_5.png

Submit the form to register your controller.

Connect your Registered Controller with a PHP class

Now that we have registered the controller in the database we can create the PHP file:

/module/phpfoxsample/include/component/controller/index.class.php

Since the Component we provided in the form earlier was index the PHP class name is index.class.php.

In that file, add:

<?php
 
class Phpfoxsample_Component_Controller_Index extends Phpfox_Component
{
	public function process()
	{	
 
	}
}
 
?>

If you notice the name of the class

Phpfoxsample_Component_Controller_Index

The underscore (_) signifies a folders slash (/). The class name above means it would connect to the module file:

/module/phpfoxsample/component/controller/index.class.php

Each controller must extend the class

Phpfox_Component

which is the lib we provide to parent other components.

In order for us to run a controller we need to provide the public method process which is this part:

public function process()

Connect your Registered Controller with a HTML Template

Now you have your PHP controller setup, in order to display content we need an HTML file and since we separate HTML from PHP we create the template file:

/module/phpfoxsample/template/default/controller/index.html.php

In that file add:

Hello World!

Hello World!

Now lets see the file in action. Visit your site via your favorite web browser and navigate to the page:

http://localhost/index.php?do=/phpfoxsample/

Note in all our examples we use localhost as our domain name. Simply replace this with the full path to where your phpFox2 site is installed.

You should now see something similar to this: http://wiki.phpfox.com/images/create_new_controller.png

In the example we covered how to create an index controller, however what if we wanted to create a new page for our module. In our next example we are going to try to create the following page:

http://localhost/index.php?do=/phpfoxsample/add/

In order for us to accomplish this you need to register your controller in the AdminCP. Follow the same steps to register your new controller we went over earlier but for the Component you will need to add:

add

and for the URL Connection you need to add

phpfoxsample.add

The form should then look similar to the following image: http://wiki.phpfox.com/images/block_sample_6.png

Next we need to create the PHP file:

/module/phpfoxsample/include/component/controller/add.class.php

Within that file we add:

<?php
 
class Phpfoxsample_Component_Controller_Add extends Phpfox_Component
{
	public function process()
	{	
 
	}
}
 
?>

Next we need to create a new template:

/module/phpfoxsample/template/default/controller/add.html.php

In that file add:

We will be adding a form here...

Now lets visit the page:

http://localhost/index.php?do=/phpfoxsample/add/

You should then see the following: http://wiki.phpfox.com/images/create_2nd_controller.png

Congratulations you have created your first controllers!

Creating a Block

Blocks are what we use to connect modules with each other as well as add blocks on our side panel. An example of a side panel block can be seen here: http://wiki.phpfox.com/images/block_sample_1.png

Here you will notice the block Folder. We added this block to the mail module and added a connection to the inbox. If you wanted from your AdminCP you could move this block to any other page on your site as well as 9 other positions on the site. These are identified as the 9 blocks. Here is an example of all the positions a block can be placed: http://wiki.phpfox.com/images/block_sample_2.png

In this specific case the Folder block is positioned in Block 1.

Please remember that only controllers can modify the template headers since its executed before the main template. The script is designed to load the blocks within the main template and that is done after the template headers are created. By creating a plug-in hook you can connect to the template headers in several places.

There are 2 ways to connect to a block. Depending on the purpose of your block and the access you have to the other modules provided you need to decide which method is best.

Connecting to a Block via HTML (controller) file

If the block you plan adding needs to be placed in a specific spot and the 9 positions we provide does not suffice then connecting your block via an HTML controller is your best solution. In order to do this lets first create the file:

/module/phpfoxsample/include/component/block/display.class.php

In that file add:

<?php
 
class Phpfoxsample_Component_Block_Display extends Phpfox_Component
{	
	public function process()
	{
 
	}
}
 
?>

Similar to our controller class the block class has the same layout. The main difference is that since this class is within the block folder the class name uses _Block_ instead of _Controller_. We extend the same phpFox2 component.

Now we create the HTML file:

/module/phpfoxsample/template/default/block/display.html.php

In that file add:

<br />
<br />
Hello I am a block...

Next we need to connect this block with one of our controllers. Let us open the controller template we created earlier:

/module/phpfoxsample/template/default/controller/index.html.php

Replace the files content with the following:

Hello World!
 
{module name='phpfoxsample.display'}

You will notice we added the following to the file:

{module name='phpfoxsample.display'}

The value phpfoxsample.display is the connection to the block. This value is translated to:

/module/phpfoxsample/include/component/block/display.class.php

Save the file and let us visit the page:

http://localhost/index.php?do=/phpfoxsample/

You should then see something similar to this image: http://wiki.phpfox.com/images/block_sample_3.png

Now the above example might be pointless as we could have simply added:

Hello I am a block...

in the controller file instead of creating a new block. Which is very true and in most cases its always best to add everything you need in the controller, however if you are working with other modules and with the ability to disable modules from the AdminCP its important to use blocks in case the module you are creating is disabled. For example if the new block we created was actually part of the blog module and if a site Admin disables the blog module the hard coded PHP and HTML data will be processed, opposed to block the data not being processed if the module was disabled.

Connection to a Block via AdminCP

The block you are creating can be positioned using one of the 9 positions we provide this solution. In order to accomplish this we log into the AdminCP and navigate to:

Extension >> Module >> Add Component

Now we need to complete the form...

Registering the Block

Product

The product this block will belong to. In this case we select our sample product:

phpFox Sample Product

Module

The module this block will be long to. In this case we select our sample module:

phpfoxsample

Component

The name of the block class file without the PHP class suffix. We add:

panel

This would then actually be panel.class.php.

Type

The type of component this is, which in this case it is a block. We select:

block

URL Connection

This is used for controllers. However, since this is a block, we leave this blank.

Active

Defines if the block is active or inactive. We select Yes.

The form should then look similar to the following image: http://wiki.phpfox.com/images/block_sample_4.png

Submit the form and your block will then be inserted into the database. Now we need to create the actual PHP & HTML files.

Connect your Registered Block with a PHP class

Let's create the PHP file:

/module/phpfoxsample/include/component/block/panel.class.php

In that file add:

<?php
 
class Phpfoxsample_Component_Block_Panel extends Phpfox_Component
{	
	public function process()
	{
 
	}
}
 
?>

Connect your Registered Block with a HTML Template

Next lets create the HTML file:

/module/phpfoxsample/template/default/block/panel.html.php

In that file add:

I am a panel block...

Connect your Registered Block with a Controller

Log back into your AdminCP and navigate to:

CMS >> Blocks >> Add New Block

Complete the form to connect your new block with a controller...

Product

The product this block/controller connection will belong to. We select:

phpFox Sample Product

Type

The Type controls what type of a block this will be. You can connect a block to a PHP file or add HTML/PHP code on the spot. The method we are going to be working with is connecting to a PHP file so select the option:

PHP Block File

Controller

The controller we plan to connect our block with. We select:

-- phpfoxsample.index

Component

The name of the block we will be connecting with is the recently created controller. We select:

-- panel

Make sure the parent is phpfoxsample.

Placement

Controls the placement of a block. Click on View Sample Layout for all the available positions. For this example we select:

Block 1

Can Drag/Drop

Currently there are two areas that support dragging and dropping of blocks. The sites index page (when a user is logged in) and a users profile. If this option is enabled it will allow users the ability to drag and drop the block to a position they specify. For now lets select No.

Active

Controls whether this connection is active or inactive. We select Yes.

Allow Access

Used to control which user groups you would like to be able to see or interact with a block. Keep the default settings which is to allow all user groups access to this block.

Your form should now look similar to the following image: http://wiki.phpfox.com/images/add_block.png

Submit the form to create the block/controller connection. Since we selected that this block will be displayed on the controller phpfoxsample.index this would mean this block should show up on the page:

http://localhost/index.php?do=/phpfoxsample/

You should now see something similar to this image: http://wiki.phpfox.com/images/block_sample_7.png

If you notice on the right hand side of the site we now have:

I am a panel block... 

This is what we just recently added using this method of connecting this block with this specific controller.

Working with a Controller

Currently our new page:

http://localhost/index.php?do=/phpfoxsample/

is fairly basic and contains little information. Lets work on adding some PHP logic to the controller so it can control what is displayed on the page.

Controlling the Template (Title, Breadcrumbs and Assigning Variables)

To control aspects of the template or to assign certain variables to the template we use the method template(), which is a parent method that belongs to the extended class Phpfox_Component.

Lets open the file:

/module/phpfoxsample/include/component/controller/index.class.php

Everything we will be working with now must be placed within the process method. Example:

<?php
 
class Phpfoxsample_Component_Controller_Index extends Phpfox_Component
{
	public function process()
	{	
	      // Place all your code here...	
	}
}
 
?>

Within the process method lets add:

$this->template()->setTitle('My Sample Title');

Visit the page:

http://localhost/index.php?do=/phpfoxsample/

you should now see My Sample Title as the title for your page.

Most templates will be designed to have breadcrumb support, in order to add a breadcrumb add the following:

$this->template()->setBreadcrumb('My Sample Breadcrumb');

Your page should now look similar to the following image: http://wiki.phpfox.com/images/screenshot_17.png

Now that we have a title and a breadcrumb for the page we may need to assign certain variables to it. Lets assign our first template variable by adding the following:

$this->template()->assign(array(
		'sSampleVariable' => 'Hello, I am an assigned variable.'
	)
);

As you can see the variable name we created for the template is sSampleVariable and it follows the same rules as if we were creating a PHP variable. In order to output the value we need to add the assigned variable to the HTML template file. Let us now open the file:

/module/phpfoxsample/template/default/controller/index.html.php

Replace the content within that file with:

Hello World!
 
<br />
<br />
 
{$sSampleVariable}
 
{module name='phpfoxsample.display'}

Notice where we added:

{$sSampleVariable}

this is how we output a variable within an HTML template. Once you saved that file visit the page:

http://localhost/index.php?do=/phpfoxsample/

you should see something similar to this image: http://wiki.phpfox.com/images/screenshot_19.png

To add more variables simply add them to the array within the PHP controller class. Example:

$this->template()->assign(array(
		'sVar1' => 'Value 1',
                'sVar2' => 'Value 2',
                'sVar3' => 'Value 3'
	)
);

Instead of assigning the title, breadcrumb and variables on a new line and calling the template method each time we could add all the methods with one call. For example:

$this->template()->setTitle('My Sample Title')
	->setBreadcrumb('My Sample Breadcrumb')
	->assign(array(
		'sSampleVariable' => 'Hello, I am an assigned variable.'
	)
);

So your entire controller should look like this now:

<?php
 
class Phpfoxsample_Component_Controller_Index extends Phpfox_Component
{
	public function process()
	{	
		$this->template()->setTitle('My Sample Title')
			->setBreadcrumb('My Sample Breadcrumb')
			->assign(array(
				'sSampleVariable' => 'Hello, I am an assigned variable.'
			)
		);
	}
}
 
?>

Connect to a CSS File

The goal with modules is to keep as much of what we use for that module within our root module folder. Each module can connect to CSS or JavaScript files that are part of the core product or part of a specific module.

Earlier we were looking into how to assign a title and variable to our controller using:

$this->template()->setTitle('My Sample Title')
			->setBreadcrumb('My Sample Breadcrumb')
			->assign(array(
				'sSampleVariable' => 'Hello, I am an assigned variable.'
			)
		);

To connect to a CSS file we would add setHeader. It would look like:

$this->template()->setTitle('My Sample Title')
			->setBreadcrumb('My Sample Breadcrumb')
			->setHeader(array(
					'sample.css' => 'module_phpfoxsample'
				)
			)
			->assign(array(
				'sSampleVariable' => 'Hello, I am an assigned variable.'
			)
		);

In the block of code above we added the following:

->setHeader(array(
					'sample.css' => 'module_phpfoxsample'
				)
			)

The array can hold multiple CSS or JavaScript calls. The name of the CSS file in this case is sample.css and it connects to module_phpfoxsample. The part module_ tells the script that this CSS file is located in a module folder and the phpfoxsample part identifies what module the file belongs too.

Once you have added that you need to create the CSS file. With our example the file should be located here:

module/phpfoxsample/static/css/phpfox/default/sample.css

The reason it is placed in the folder phpfox is because this is the default theme being used. It also uses the default folder which is the default style being used.

To use images in your CSS the images must be placed in the folder:

module/phpfoxsample/static/css/phpfox/image/

Note that the folder phpfox/default is the parent location for all themes, if you are working on a custom theme you can create a new directory within the static/css folder.

The setHeader() method not only connects to CSS or JavaScript it can actually add anything within the HTML headers:

<head></head>

If you were to use the following:

->setHeader(array(
					'<!-- Add me in the header -->'
				)
			)

It would end up like:

<head><!-- Add me in the header --></head>

Adding a Menu for your Controller

Lets make things easier by adding a menu for our controller so we won't have to manually write down the full path to the page each time we want to visit it. To add a menu for your controller log into your AdminCP and navigate to:

CMS >> Menus >> Add New Menu

Fill out the form to add the menu...

Menu Form

Product

We will connect this menu to the product we created earlier:

phpFox Sample Product

Module

We will connect this menu to the module we created earlier:

phpfoxsample

Connection

The connection is where you would like to add your menu. There are 2 options, which is 'Menus or Modules. If you add a menu to one of the Menus it will be displayed on that specific menu on all the pages, however if you were to add a menu to a module it will only be displayed on the specific module you selected and will be a sub-menu of that module. Since this is our first menu for this module we need to add it as the parent menu so we need to select one of the Menus. Lets select:

main

Which is the main menu for the site.

URL

The URL is the link we will use in the anchor. If its an internal link you must use our method of creating links which in this case we add:

phpfoxsample

This will translate to:

http:://localhost/index.php?do=/phpfoxsample/

Menu

Here we can add the phrase for the menu and if you have more then one language package installed you can create a phrase for each for them. Here lets add:

Sample

Allow Access

Here you can select which user group can view the menu. For now lets allow all user groups so keep the default settings.

Once you have filled out the form it should look similar to this image: http://wiki.phpfox.com/images/screenshot_28.png

Submit the form and visit your site. You should now see a menu similar to this: http://wiki.phpfox.com/images/screenshot_29.png

Click on the link Sample and it should send you to the page:

http:://localhost/index.php?do=/phpfoxsample/

Handling Requests

Handling requests is a little different when comparing to the conventional PHP method:

$_GET['var']

Within a controller you can use the request method we provide with the extended class Phpfox_Component.

If we continue working on the current controller we have been working on and the URL string would be:

http://localhost/index.php?do=/phpfoxsample/link1/link2/link3/

and you were to add the following in your controller:

echo $this->request()->get('req1') . "<br />";
echo $this->request()->get('req2') . "<br />";
echo $this->request()->get('req3') . "<br />";
echo $this->request()->get('req4') . "<br />";

It would output:

phpfoxsample
link1
link2
link3

As you can see a req is defined by the position it is in. link2 is in 3rd place so in order to get its value we would use:

$this->request()->get('req3')

In some cases you may want to use this method to pass a request so you could also use the underscore (_). If the URL string was:

http://localhost/index.php?do=/phpfoxsample/foo_bar/

In this case if we wanted to get the value for the request foo we would use:

echo $this->request()->get('foo');

this would then output:

bar

Working with a Block

Now that we have added some PHP logic to our controller we can do the same for our blocks. Unlike a controller a block cannot control the pages title or breadcrumb since it is a block and the job of a block is to simply enhance a controller. The job of the controller is to control the main aspects of a template and include any blocks.

Assigning a Variable to a Block

To start working on our block lets visit the page:

http://localhost/index.php?do=/phpfoxsample/

The part we will be working on is where we have the following phrase:

Hello I am a block...

In order to update this block lets open the PHP block file:

/module/phpfoxsample/include/component/block/display.class.php

The file should look like the following:

<?php
 
class Phpfoxsample_Component_Block_Display extends Phpfox_Component
{	
	public function process()
	{
 
	}
}
 
?>

Similar to how a controller works we add all our code within the process method. Here we can assign a variable for our block. Lets do that now by adding the following:

$this->template()->assign(array(
		'sDisplayBlockVariable' => 'I am a variable that belongs to the Display block.'
	)
);

In order to output the variable open the file:

/module/phpfoxsample/template/default/block/display.html.php

Replace the content within that file with the following:

<br />
<br />
Hello I am a block...
<br />
<br />
{$sDisplayBlockVariable}

If you visit the page:

http://localhost/index.php?do=/phpfoxsample/

you should now see something similar: http://wiki.phpfox.com/images/screenshot_20.png

Using a Template Layout

Blocks can use other template layouts, which can be found in the folder:

/theme/frontend/default/template/

Let us enhance the block we were currently working on. Open the file:

/module/phpfoxsample/include/component/block/display.class.php

At this time your file should look like this:

<?php
 
class Phpfoxsample_Component_Block_Display extends Phpfox_Component
{	
	public function process()
	{
		$this->template()->assign(array(
				'sDisplayBlockVariable' => 'I am a variable that belongs to the Display block.'
			)
		);
	}
}
 
?>

Replace the content of that file with the following:

<?php
 
class Phpfoxsample_Component_Block_Display extends Phpfox_Component
{	
	public function process()
	{
		$this->template()->assign(array(
				'sDisplayBlockVariable' => 'I am a variable that belongs to the Display block.',
				'sHeader' => 'Display Block',
				'aFooter' => array(
					'Add New Sample' => $this->url()->makeUrl('phpfoxsample.add')
				),
			)
		);
 
		return 'content';
	}
}
 
?>

Save the file and let us first view the page:

http://localhost/index.php?do=/phpfoxsample/

you should now see something similar to this: http://wiki.phpfox.com/images/screenshot_21.png

You will notice the following was added, which can be seen in this image: http://wiki.phpfox.com/images/screenshot_22.png As you can see the simple block we created earlier now has a block header and footer menu. The idea of blocks is to output the actual data that will be displayed within the outer block template, this way all blocks in the future will look the same depending on the theme the user is browsing the site with. In this case in the PHP block file we added the following to the file:

return 'content';

All blocks can return a string identifying what outer template they should use. In this case we used content, which when we add the template suffix would be content.html.php. When you return a string the script will try to find the template and use that as the outer shell for your block. So the full path to this template would be:

/theme/frontend/default/template/content.html.php

This specific template requires certain variables to be defined in order to work. You must define the sHeader variable with your PHP block class otherwise it simply will not display the block template. We define this variable by assigning it via the template assign method, which is the same method we used earlier to define methods for our templates. In your PHP block class you will find:

'sHeader' => 'Display Block',

We used this to define the block title. Your blocks can also have a footer menu, this does not need to be defined however if you would like a menu you will need to assign the variable aFooter and the value must be an array. Checking back in our PHP block class you will find:

'aFooter' => array(
	'Add New Sample' => $this->url()->makeUrl('phpfoxsample.add')
),

The key within the array is the phrase to display and the value is the URL path for the link. To add more links simply add to the array. Example:

'aFooter' => array(
	'Link 1' => $this->url()->makeUrl('phpfoxsample.link1'),
        'Link 2' => $this->url()->makeUrl('phpfoxsample.link2'),
        'Link 3' => $this->url()->makeUrl('phpfoxsample.link3')
),

Now that we have updated this specific block to use a template, lets update our other block we created earlier. If you visit the page we have been working on you will find to the right we have:

I am a panel block...

To edit this block open the file:

/module/phpfoxsample/include/component/block/panel.class.php

The file should look like:

<?php
 
class Phpfoxsample_Component_Block_Panel extends Phpfox_Component
{	
	public function process()
	{
 
	}
}
 
?>

Lets use the layout block.html.php so in order to accomplish this it works similar to how our earlier content.html.php layout worked. We replace the entire block file with:

<?php
 
class Phpfoxsample_Component_Block_Panel extends Phpfox_Component
{	
	public function process()
	{
		$this->template()->assign(array(
				'sHeader' => 'Panel Block',
				'aFooter' => array(
					'Panel Link' => $this->url()->makeUrl('phpfoxsample.add')
				),
			)
		);
 
		return 'block';
	}
}
 
?>

Save the file and visit the page:

http://localhost/index.php?do=/phpfoxsample/

you should then find on that page something similar: http://wiki.phpfox.com/images/screenshot_23.png Similar to the block we updated earlier, here we need to define the header variable which is sHeader. We can also define footer links which is aFooter, however this is not necessary.

Moving Blocks

To get a greater understanding how blocks work and how easy they can be to connect with other modules lets try to move the block we just created to another page and then move it back. In order to do this log into the AdminCP and navigate to:

CMS >> Blocks >> Manage Blocks

Look for where we have:

phpfoxsample::panel

From the Action drop down for that specific block select Edit. You should now have reached the following page which can be seen in the image: http://wiki.phpfox.com/images/screenshot_24.png Now lets update the form. For the Controller change it from:

-- phpfoxsample.index

to

-- mail.compose

Save the form and visit the page: http://localhost/index.php?do=/mail/compose/ On the right hand side you should find the Panel Block and the page should look similar to this: http://wiki.phpfox.com/images/screenshot_25.png As you can see it is very easy to move blocks around to any other page on your site, you can even change the position of the block to the 9 available positions. Let us move the block back to the correct section. So let us go back to the form and for the Controller set it to:

-- phpfoxsample.index

Save the form and you should find your block back on the page:

http://localhost/index.php?do=/phpfoxsample/

Module Services

Introduction

A phpFox Service is what we use as a model for our module. We use services to interact with database tables, cache modular data or extend the PHP logic for a module. It is important not to use a component (controllers or blocks) for such services as this should all be handled by your module services. To use a module service you need to call it from a module controller, which we went over earlier by creating our first module. Using that same module as an example you should have the following page setup already: http://localhost/index.php?do=/phpfoxsample/ The page should look similar to the page found on this image: http://wiki.phpfox.com/images/screenshot_26.png

Creating a Module Service

Lets create our first service class by creating the PHP file:

/module/phpfoxsample/include/service/phpfoxsample.class.php

In that new file paste the following:

<?php
 
class Phpfoxsample_Service_Phpfoxsample extends Phpfox_Service 
{
	public function getUsers($iTotal)
	{
		return $this->database()->select('u.full_name')
			->from(Phpfox::getT('user'), 'u')
			->limit($iTotal)
			->execute('getRows');
	}
}
 
?>

You will notice the name of our class is Phpfoxsample_Service_Phpfoxsample, similar to how other classes work we replace the underscore with a folder slash. So the file location would be:

/module/phpfoxsample/include/service/phpfoxsample.class.php

All service classes must extend our Phpfox_Service class.

With this class we create a method getUsers. This method simply gets a specific number of members from the user database table. The number is specified by the variable $iTotal which is defined when calling this specific method. In order for us to use this class and call this method we need to do this from a module controller. Which we already have and created when we were looking into creating controllers. Lets open the file:

/module/phpfoxsample/include/component/controller/index.class.php

Replace the files content with the following:

<?php
 
class Phpfoxsample_Component_Controller_Index extends Phpfox_Component
{
	public function process()
	{	
		$this->template()->setTitle('My Sample Title')
			->setBreadcrumb('My Sample Breadcrumb')
			->assign(array(
				'sSampleVariable' => 'Hello, I am an assigned variable.',
				'aUsers' => Phpfox::getService('phpfoxsample')->getUsers(10)
			)
		);
	}
}
 
?>

We added the following to that file:

'aUsers' => Phpfox::getService('phpfoxsample')->getUsers(10)

We defined the template variable aUsers since we want to display the users in the actual HTML template. We initiated the service using the static method getService:

Phpfox::getService('phpfoxsample')

This will return the classes object and the above call would be similar to the conventional method:

new Phpfoxsample_Component_Controller_Index();

Since we only plan to use this class to call one method we do the following:

Phpfox::getService('phpfoxsample')->getUsers(10)

however, if you plan to use the object more then once or within a loop its best to do the following:

$oServicePhpfoxsample = Phpfox::getService('phpfoxsample');
$oServicePhpfoxsample ->getUsers(10);

Now that we have assigned the variable lets open the file:

/module/phpfoxsample/template/default/controller/index.html.php

Replace the entire files content with:

Hello World!
 
<br />
<br />
 
{$sSampleVariable}
 
{module name='phpfoxsample.display'}
 
<br />
<br />
Members:
<ul class="p_4">
{foreach from=$aUsers item=aUser}
	<li>{$aUser.full_name}</li>
{/foreach}
</ul>

Save the file and visit the page:

http://localhost/index.php?do=/phpfoxsample/

Near the bottom of that page you should find:

Members:

Under that there should be a list of max 10 members. It should look similar to this image: http://wiki.phpfox.com/images/screenshot_27.png


Previous Chapter: Products | Next Chapter: Plugins



Copyright © Benc Enterprises AB, 2005-2010. All Rights Reserved.
Toll Free (US/Canada):