Page 1 of 1

Devoloping one's own modules

Posted: Wed Dec 26, 2007 8:42 pm
by piXelshooter
Hi,
I tried to get started with module programming. So I had a look at the tutorial in the wiki, which helped me quite much 'til now. Unfortunately there's nothing said about writing an admin panel, so I tried to have a look at other modules. I was able to install it, and it installs the link in the admin panel correctly. But, When I click on it, I get the following error:
Fatal error: Call to a member function GetFriendlyName() on a non-object in ...\admin\moduleinterface.php on line 120
The menu link is also away now, but when I go to another page, it's there again. Could anyone please help me?

That's my current code (I'm sorry for pasting about 100 lines, but I've no idea which are the relevant ones):

Code: Select all

<?php
class Sendeplan extends CMSModule{
	function GetName(){
		return 'Sendeplan';
	}
	function GetFriendlyName(){
		return "Sendeplan";#$this->Lang('friendlyname');
	}
	function GetVersion(){
		return '1.0';
	}
	function GetHelp(){
		return $this->Lang('help');
	}
	function GetAuthor(){
		return 'Fabian Lenzen';
	}
	function GetAuthorEmail(){
		return 'fabian.lenzen@web.de';
	}
	function GetChangeLog(){
		return $this->Lang('changelog');
	}
	function IsPluginModule(){
		return true;
	}
	function HasAdmin(){
		return true;
	}
	function GetAdminSection(){
		return 'content';
	}
	function GetAdminDescription(){
		return $this->Lang('moddescription');
	}
	function VisibleToAdminUser(){
        return $this->CheckPermission('Modify Sendeplan');
	}
	function GetDependencies(){
		return array();
	}
	function MinimumCMSVersion(){
		return "0.13";
	}
	function Install(){
		// The module needs a database to store the shows to be broadcasted.
		// let's set it up here.
		$db 			= $this->cms->db;
		$taboptarray 	= array('mysql' => 'TYPE=MyISAM');
		$dict			= NewDataDictionary($db);
		$flds 			= 
			"id I KEY,
             short X,
             long XL";
       $sqlarray		= $dict->CreateTableSQL(cms_db_prefix().'module_sendeplan_shows', $flds, $taboptarray);
       $dict->ExecuteSQLArray($sqlarray);
       $db->CreateSequence(cms_db_prefix().'module_sendeplan_shows_seq');
	   // left to create the permission structure.
	   $this->CreatePermission('Modify Sendeplan', $this->Lang('modsendeplan'));
	   // and the template
	   $this->SetTemplate('display', 'hiho');
	}
	function InstallPostMessage(){
		return $this->Lang('postinstall');
	}
	function Uninstall(){
		$db = $this->cms->db;
		$dict = NewDataDictionary($db);
		$sqlarray = $dict->DropTableSQL(cms_db_prefix() . 'module_sendeplan_shows');
		$dict->ExecuteSQLArray($sqlarray);
		$db->DropSequence(cms_db_prefix() . 'module_sendeplan_shows_seq' );
		$this->RemovePermission('Modify Sendeplan');
	}
	function UninstallPostMessage(){
		return $this->Lang('postuninstall');
	}
	function DoAction($action, $id, $params, $returnid=-1){
		switch ($action){
			case 'default':
				$db =& $this->GetDb();
				$sql = 'SELECT * FROM ' . cms_db_prefix() . 'module_sendeplan_shows;';
				$dbresult =& $db->Execute($sql);
				$items = Array();
				while($dbresult && $row = $dbresult->FetchRow()){                    
					$i = count($list);
					$items[$i]['name'] 	= $row['name'];
					$items[$i]['short']	= $row['short'];
					$items[$i]['long']	= $row['price'];
				}
				$this->smarty->assign('items', $items);
				echo $this->smarty->ProcessTemplate('template.tpl');
				break;
			case 'defaultadmin':
				echo $this->StartTabHeaders();
				echo $this->SetTabHeader('template', 'template', true);
				echo $this->StartTabContent();
				echo $this->StartTab('template', $params);
				echo "hallo welt";
				echo $this->EndTab();
				echo $this->EndTabContent();
		}
   }
}

?>
--edit--
and, as I see now, the table [cms_]module_sendeplan_shows is NOT created, only module_sendeplan_shows_seq. Can somebody explain me why? And by the way, what are these sequences (_seq) for?

Re: Devoloping one's own modules

Posted: Fri Dec 28, 2007 8:05 am
by Duketown
piXelshooter,

Leave the second function as it was originally:

Code: Select all

function GetFriendlyName()
{
	return $this->Lang('friendlyname');
}
Create a new directory called 'lang'. This will contain all the language translations. Create a file: en_US.php in it. Open it and enter:

Code: Select all

<?php
// Installation related messages
$lang['friendlyname'] = 'Sendeplan';
?>
At least the english version will work. Later additional translations can be added to this file. The german version will be in the file \lang\ext\de_DE.php.
Btw it could also have to do with the double quotes in stead of the single quotes as in the other functions.

On the installation question:
For what I know, installation is best to put in a different file. So not in Sendeplan.module.php. Why: when you request a list of modules, CMSMS checks the directory modules. All directory names are listed. When you try to install a module, CMSMS checks and runs the method.install.php from the module directory. The same is true for the uninstall and the upgrade. As an advice (and I'm sure you have read something about this) download the skeleton module and copy the contents to the Sendeplan directory. It gives a great start.

The _seq table is used to generate a next unique id for the table (without the extension _seq).
First generate the id and then use it in the insert query for the table.

Code: Select all

$id = $db->GenID(cms_db_prefix().'module_sendeplan_shows');
$query = 'INSERT INTO '.cms_db_prefix().'module_sendeplan_shows (id, name, short, price) VALUES (?,?,?,?)';
$dbresult = $db->Execute($query, array($id,$name,$short,Sprice);
This is a very handy table (the _seq I mean) because after the generation of the id, you have the variable $id available for showing to the end user or use it in another table. You don't have to locate the last used number if you have auto next numbering turned on on your unique field in the table 'module_sendeplan_shows'.

Good luck and nice to known that you take time to extend CMSMS.

Duketown

PS. You will see in the Skeleton module special php files for the actions default and defaultadmin as well. Since these tend to grow, best to use these php files as well. This is also how other modules are prepared so you will be working in a more standard CMSMS way (which will be much appriciated  :D).

Re: Devoloping one's own modules

Posted: Fri Dec 28, 2007 7:21 pm
by calguy1000
I use defines as much as possible in my modules.

I use defines for the table names.  i.e:
define('MYMODULE_TABLE',cms_db_prefix().'module_mymodule');

and defines for the permissions (most times). i.e:
define('MYMODULE_PERM','Manage MyModule');

I split up files as much as possible:
a) actions go into seperate files    (action.xxxxxxx.php)
b) each tab in the admin section goes into a seperate file
c) install/upgrade/uninstall go into seperate method.xxxxxxxx.php files

Check out the questions module it's a nice clean example.

Re: Devoloping one's own modules

Posted: Sat Dec 29, 2007 1:12 pm
by piXelshooter
okay. I'll separate my code : )

so, I created a file 'method.install.php' in my module's directory and wrote:

Code: Select all

<?php
global $gCms;
$db 	=& $gCms->GetDb();
$opt 	= array('mysql' => 'TYPE=MyISAM');
$dict	= NewDataDictionary($db);
$flds	= '
	entry_id	I KEY AUTO,
	short_desc 	C,
	long_desc 	XL
';
$sqlarray = $dict->CreateTableSQL(cms_db_prefix().'module_sendeplan_shows', $flds, $opt);
$dict->ExecuteSQLArray($sqlarray);
$this->CreatePermission('Sendeplan: modify shows', $this->Lang('modsendeplan'));
$this->CreatePermission('Sendeplan: add shows', $this->Lang('addsendeplan'));
$this->SetTemplate('display', '<ul>{foreach from=$items item="item"}<li>{$item.name}</li>{/foreach}');
?>
but it still does not work: The table is not created... (here I didn't use sequences, but it does not make a difference: If I use a sequence, the sequence table is created, but the mail table still isn't)

Help please  :(

Re: Devoloping one's own modules

Posted: Sat Dec 29, 2007 4:39 pm
by calguy1000
I think the short_desc needs a length:

i.e:  short_desc C(255)

and try using X2 instead of XL for the long desc.

Re: Devoloping one's own modules

Posted: Sun Dec 30, 2007 11:25 pm
by piXelshooter
Thank you! It works now!
well, would be nice of I could get some Error message for such situations, I had no idea how to get one. Is that possible?

Re: Devoloping one's own modules

Posted: Wed Jan 02, 2008 7:27 pm
by Pierre M.
Hello,
piXelshooter wrote: would be nice of I could get some Error message for such situations, I had no idea how to get one. Is that possible?
As you are writing PHP code you should search for a log feature in the PHP manual.

Pierre M.