What kind of data should be sended to template from module?

Author Message

Jerry Jalava

Thursday 24 July 2003 5:00:19 am

Hi,

I'm building a new module and using a own database table for the data retrieve and saving... I would like to know what kind of array should I send to template as a result when fetch is done? (ie. "fetch(mymodule, list, hash(category, 1))" )

What kind of array the template need to receive from module if I wanted to show ie. "name" and "photo" info list from database?
How do I put them in right place in the template? (ie. "$List:item.name")

Lets say my database would be like this:
Table mymodule
Rows id,category,name,photo

I use arrayquery to get the data from table to variable witch I need return to template...

What kind of array does the arrayquery make to the variable?
Should I parse it somehow before sending it to template?

I think answers to these questions would get me finnish this module... I really wish that I get answers 'cause these few modules are the last thing I have to do to this Portal project I'm building with eZ...

Thanks,
Jerry

Jerry Jalava

Thursday 24 July 2003 10:01:01 am

Please someone give me even a hint, 'cause I'm desperate... I really need to find this out. I'm runnin' out of time... I should do four modules in three weeks, but I haven't even got the first one ready, 'cause there are some things I need to know about modules before I can continue...

Regards,
Jerry

Esben Maaløe

Thursday 24 July 2003 3:41:16 pm

If you got your fingers buried that deep in the fetch function - why not look at the arrays that other fetch'es give ?

Use

print_r( $returnArray )

And you can see exactly how those arrays are put together ?

I am in the same bind as you are with a deadline - and I need to know exactly what files implements the fetch function. The fetch in the upcoming 3.2 is able to do sorting and filtering - the 3.1 is not. I need to know what files to patch - can you help me ?

Francisco Felix

Friday 25 July 2003 1:51:54 pm

I have read the documentation but I havent a fair amount of information on developing custom modules.

As you have to develop four of them I am shure you at this time know a lot more about it than I, Would you please give me some pointers or advice?

I really have read the online documentation but its not enough, probably I have not found the rigth one.

Best regards and thanks in advance.

Jerry Jalava

Friday 25 July 2003 3:21:51 pm

I'm sorry... :((( I just was wroting a big tutorial but then I accidently pressed the back button in my mouse.. :( And I lost it all... Grr... I think I'm too tired to write it again... Sorry

I try... But this time I write it in notepad...

Hates M$,
Jerry

Jerry Jalava

Friday 25 July 2003 4:08:09 pm

OK! I try to start from beginning again... :(
This isn't going to be as much documented example, 'cause I'm tired and pissed...

In this example we make a module named "mymodule".
"mymodule" has 1 view called "list".
We have one template where we use fetch to list all our data from our database table called "mytable".

Folders and files:
-----------
In your ezpublish directory there is "extension"-folder (If not, create one).
Inside that folder we make folder for our module "mymodule".
Inside that folder we make folder structure like this:
(/extension/mymodule)/modules/mymodule
(extension/mymodule)/settings

The we create folder under the "standard" design.
/design/standard/templates/mymodule/

Inside folder: extension/mymodule/modules/mymodule,
we put all the files expect the "list.tpl" -file.

Inside folder: extension/mymodule/settings,
we put the module.ini.append file.

Inside folder: /design/standard/templates/mymodule/,
we will put the list.tpl -file.
-----------

1. Our module.ini.append file:
-----------
[ModuleSettings]
ExtensionRepositories[]=mymodule
-----------

2. The database:
-----------
We create a table named "mytable" to ez database and make 2 cells, "id" and "name".
We add some data to the table:
id name
1 First row
2 Second row
-----------

3. Our module.php file:
-----------
$Module = array( 'name' => 'mymodule' );

$ViewList = array();
$ViewList['list'] = array(
'script' => 'list.php',
"unordered_params" => array( "offset" => "Offset" ) );

What happens here is that we make a view "list" for the module,
witch points to file "list.php" and we send parameter "Offset" to it.
----------

4. Our list.php file:
----------
$Module =& $Params['Module'];

$Offset = $Params['Offset'];
if ( !is_numeric( $Offset ) )
$Offset = 0;

$viewParameters = array( 'offset' => $Offset );

include_once( 'kernel/common/template.php' );
$tpl =& templateInit();

$tpl->setVariable( 'view_parameters', $viewParameters );

$Result = array();
$Result['content'] = $tpl->fetch( "design:mymodule/list.tpl" );
$Result['path'] = array( array( 'url' => false,
'text' => 'MY Module' ),
array( 'url' => false,
'text' => 'List' ) );

In here we receive the parameters from module.php and send them forward to list.tpl with "setVariable" function.
Then we load the list.tpl to screen...
-----------

5. Our list.tpl file:
-----------
{let data_limit=10
data_list=fetch('mymodule','list',hash(offset,$view_parameters.offset,limit,$data_limit))}
<h1>All URLs</h1>
{section name=DATA loop=$data_list sequence=array(bglight,bgdark)}
<b>{$:item.id}</b>{$:item.name}
{/section}
{/let}

Actually we don't need that "data_limit" and "hash(offset...)" thing in our example now,
but it's there just to show how the Offset parameter goes around the files... ;)

Hint: {$:item.xxx} is the name of our database tables cell... ;)
-----------

6. Our fetch function:
-----------
To archieve in this we need 2 files: mymodulefunctioncollection.php and function_definition.php.

First, function_definition.php
-----
$FunctionList = array();
$FunctionList['list'] = array(
'name' => 'list',
'operation_types' => array( 'read' ),
'call_method' => array( 'include_file' => 'extension/mymodule/modules/mymodule/mymodulefunctioncollection.php',
'class' => 'MyModuleFunctionCollection',
'method' => 'fetchList' ),
'parameter_type' => 'standard',
'parameters' => array( array( array( 'name' => 'offset',
'required' => false,
'default' => false ),
array( 'name' => 'limit',
'required' => false,
'default' => false ) ) );
-----

Second, mymodulefunctioncollection.php:
-----
include_once( 'extension/mymodule/modules/mymodule/mymodule.php' );

class MyModuleFunctionCollection
{

function MyModuleFunctionCollection()
{
}

function &fetchList( $offset, $limit )
{
$parameters = array( 'offset' => $offset,
'limit' => $limit );
$lista =& Mymodule::fetchListFromDB( $parameters );

return array( 'result' => &$lista );
}

}

-----

Third, mymodule.php:
-----
include_once( 'kernel/classes/ezpersistentobject.php' );

class Mymodule extends eZPersistentObject
{

function Mymodule( $row )
{
$this->eZPersistentObject( $row );
}

function &definition()
{
return array( 'fields' => array(
'id' => array(
'name' => 'id',
'datatype' => 'integer',
'default' => 0,
'required' => true ),
'name' => array(
'name' => 'name',
'datatype' => 'string',
'default' => '',
'required' => true ) ),
'keys' => array( 'id' ),
'increment_key' => 'id',
'class_name' => 'mymodule',
'name' => 'mytable' );
}

function &fetchListFromDB( $parameters = array() )
{
return Mymodule::handleList( $parameters, false );
}

function &handleList( $parameters = array(), $asCount = false )
{
$parameters = array_merge( array( 'as_object' => true,
'offset' => false,
'limit' => false ),
$parameters );
$asObject = $parameters['as_object'];
$offset = $parameters['offset'];
$limit = $parameters['limit'];
$limitArray = null;
if ( !$asCount and $offset !== false and $limit !== false )
$limitArray = array( 'offset' => $offset,
'length' => $limit );

return eZPersistentObject::fetchObjectList( Mymodule::definition(),
null, null, null, $limitArray,
$asObject );
}

}
-----

Now we just login to our admin site and goto url: /mymodule/list.
We see our list.tpl showing the 2 variables from the database... (Hopefully)

I'm sorry but I cannot write anymore notes right now... I'm too tired. (And still pissed,
because I lost the original example what I was writing... It had a lot more notes and hints...)

Hope this helps,
Jerry

Esben Maaløe

Saturday 26 July 2003 3:10:44 am

Jerry - thnx a lot !!

BTW. I really hate loosing data like that - use Mozilla (Firebird) - It remembers the values you fill in even when you go forth and back

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 18:40:46
Script start
Timing: Jan 18 2025 18:40:46
Module start 'layout'
Timing: Jan 18 2025 18:40:46
Module start 'content'
Timing: Jan 18 2025 18:40:47
Module end 'content'
Timing: Jan 18 2025 18:40:47
Script end

Main resources:

Total runtime0.7069 sec
Peak memory usage4,096.0000 KB
Database Queries71

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0060 589.3047152.6563
Module start 'layout' 0.00600.0028 741.960939.5078
Module start 'content' 0.00870.6969 781.4688631.1016
Module end 'content' 0.70560.0013 1,412.570320.0938
Script end 0.7069  1,432.6641 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00300.4250160.0002
Check MTime0.00130.1860160.0001
Mysql Total
Database connection0.00080.120110.0008
Mysqli_queries0.611086.4318710.0086
Looping result0.00070.0953690.0000
Template Total0.677295.820.3386
Template load0.00180.258920.0009
Template processing0.675495.540020.3377
Template load and register function0.00010.018010.0001
states
state_id_array0.00090.129210.0009
state_identifier_array0.00140.194220.0007
Override
Cache load0.00200.28782090.0000
Sytem overhead
Fetch class attribute can translate value0.00060.086730.0002
Fetch class attribute name0.00120.165770.0002
XML
Image XML parsing0.00030.040930.0001
class_abstraction
Instantiating content class attribute0.00000.001970.0000
General
dbfile0.00060.0856100.0001
String conversion0.00000.001340.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
50content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
44content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 103
 Number of unique templates used: 5

Time used to render debug report: 0.0001 secs