Création de module - problème d'url et de pages -
Annadrill - 28/12/2012
Citation :#~~~~~ DEBUT BLOC A NE PAS SUPPRIMER ~~~~~
#~ Version du CMS: 1.11.3
#~ Url du site :
#~ Hébergeur / Soft :
#~ Informations Système :
#~
#~~~~~ FIN BLOC A NE PAS SUPPRIMER ~~~~~
Bonjour,
voilà, je suis entrain de réaliser un module qui aura en bref,
l'affichage d'une "liste" de catégories, puis une fois choisi la catégorie, une "liste" d'oeuvres de cette catégorie,
puis enfin la page de l'oeuvre.
mais je cherche à faire que les urls donnent :
- site/page (default)
- site/page/nomdelacategoriechoisie
- site/page/nomedelacategorie/nomdeloeuvrechoisie/
j'arrive à peu près à le faire mais j'ai découverts qu'en fait, lorsque je clique sur le lien qui m'amène par exemple sur la liste des oeuvres d'une catégorie, même si dans l'url il est écrit : site/galerie/acryliques/voldoiseau
l'alias de la page est la page d'accueil, car mon module est alors chargé "dans" la page d'accueil.
voici le code de action.default.php
Code :
[== PHP ==]
<?php
if (!isset($gCms)) exit;
// get our records from the database
$db = $gCms->GetDb();
if(isset($gCms->variables['page_name'])){
$mapage = $gCms->variables['page_name'];
}
$query = 'SELECT gamma_id, gamma_alias, gamma_title, gamma_cat, gamma_content from '.cms_db_prefix().
'module_gamma';
if (isset($params['gamma_cat']) && isset($params['gamma_alias']))
{
// *ALWAYS* use parameterized queries with user-provided input
// to prevent SQL-Injection attacks!
$query .= ' where gamma_alias = ?';
$result = $db->Execute($query,array($params['gamma_alias']));
$mode = 'gdetail'; // we're viewing a single record
}
elseif (isset($params['gamma_cat']))
{
// *ALWAYS* use parameterized queries with user-provided input
// to prevent SQL-Injection attacks!
$query .= ' where gamma_cat = ?';
$result = $db->Execute($query,array($params['gamma_cat']));
$mode = 'gcat'; // we're viewing a single record
}
else
{
// we're not getting a specific record, so show 'em all. Probably should paginate.
$result = $db->Execute($query);
$mode = 'gsummary'; // we're viewing a list of records
}
$records = array();
while ($result !== false && $row=$result->FetchRow())
{
// create a new object for every record that we retrieve
$rec = new stdClass();
$rec->gid = $row['gamma_id'];
$rec->galias = $row['gamma_alias'];
$rec->gtitle = $row['gamma_title'];
$rec->gcat = $row['gamma_cat'];
$rec->gcontent = $row['gamma_content'];
// create attributes for rendering "view" links for the object.
// $id and $returnid are predefined for us by the module API
// that last parameter is the Pretty URL link
$rec->view = $this->CreateFrontendLink($id, $returnid, 'default', $this->Lang('link_view'),
array('gamma_alias'=>$rec->galias),'',false,true,'',false,$mapage.'/'.$rec->gcat.'/'.$rec->galias.'/');
$rec->edit = $this->CreateFrontendLink($id, $returnid, 'default', $this->Lang('edit'),
array('gamma_cat'=>$rec->gcat),'',false,true,'',false,$mapage.'/'.$rec->gcat.'/');
array_push($records,$rec);
}
// Expose the list to smarty.
$this->smarty->assign('records',$records);
// Tell Smarty which mode we're in
$this->smarty->assign('mode',$mode);
// and a count of records
$this->smarty->assign('title_num_records',$this->Lang('title_num_records',array(count($records))));
if (isset($params['module_message']))
{
$this->smarty->assign('module_message',$params['module_message']);
}
else
{
$this->smarty->assign('module_message','');
}
// Display the populated template
echo $this->ProcessTemplate('gamma_first.tpl');
?>
et voici le code de Gamma.module.php
Code :
[== PHP ==]
<?php
class Gamma extends CMSModule
{
function GetName()
{
return 'Gamma';
}
function GetFriendlyName()
{
return 'Gallery Magic Mannager';
}
function GetVersion()
{
return '1.8.1';
}
function GetHelp()
{
return $this->Lang('help');
}
function GetAuthor()
{
return 'DavidPerroud';
}
function GetAuthorEmail()
{
return 'perrouddavid@me.com';
}
function GetChangeLog()
{
return $this->Lang('changelog');
}
function IsPluginModule()
{
return true;
}
function HasAdmin()
{
return true;
}
/**
* GetAdminSection()
* If your module has an Admin Panel, you can specify
* which Admin Section (or top-level Admin Menu) it shows
* up in. This method returns a string to specify that
* section. Valid return values are:
*
* main - the Main menu tab.
* content - the Content menu
* layout - the Layout menu
* usersgroups - the Users and Groups menu
* extensions - the Extensions menu (this is the default)
* siteadmin - the Site Admin menu
* myprefs - the My Preferences menu
* ecomm - the Ecommerce menu (if ecommerce suite is installed)
*
* Note that if you place your module in the main,
* viewsite, or logout sections, it will show up in the
* menus, but will not be visible in any top-level
* section pages.
* @return string Which admin section this module belongs to
*/
function GetAdminSection()
{
return 'content';
}
function GetAdminDescription()
{
return $this->Lang('moddescription');
}
function VisibleToAdminUser()
{
return $this->CheckPermission('Use Gamma');
}
function GetDashboardOutput()
{
global $gCms;
$db = &$gCms->GetDb();
$rcount = $db->GetOne('select count(*) from '.cms_db_prefix().'module_gamma');
return $this->Lang('dash_record_count',$rcount);
}
function GetNotificationOutput($priority=2)
{
global $gCms;
$db = &$gCms->GetDb();
$rcount = $db->GetOne('select count(*) from '.cms_db_prefix().'module_gamma');
if ($priority < 4 && $rcount == 0 )
{
$ret = new stdClass;
$ret->priority = 2;
$ret->html=$this->Lang('alert_no_records');
return $ret;
}
return '';
}
function GetDependencies()
{
return array();
}
function MinimumCMSVersion()
{
return "1.9-beta1";
}
function MaximumCMSVersion()
{
return "2.0";
}
/**
* SetParameters()
* This function serves as a module initialization area.
* Specifically, it enables you to:
* 1) Simplify your module's tag (if you're writing a plug-in module)
* 2) Create mappings for your module when using "Pretty Urls".
* 3) Impose security by controlling incoming parameters
* 4) Register the events your module handles
*
* 1. Simply module tag:
* Simple!
* Calling RegisterModulePlugin allows you to use the tag {Skeleton} in your
* template or page; otherwise, you would have to use the more cumbersome
* tag {cms_module module='Skeleton'}
*
* 2. Pretty URLS:
* Typically, modules create internal links that have
* big ugly strings along the lines of:
* index.php?mact=ModName,cntnt01,actionName,0&cntnt01param1=1&cntnt01param2=2&cntnt01returnid=3
*
* You might prefer these to look like:
* /ModuleFunction/2/3
*
* To do this, you have to register routes and map
* your parameters in a way that the API will be able
* to understand.
*
* Also note that any calls to CreateLink will need to
* be updated to pass the pretty url parameter.
*
* 3. Security:
* By using the RestrictUnknownParams function, your module will not
* receive any parameters other than the ones you declare here.
* Furthermore, the parameters your module does receive will be filtered
* according to the rules you set here.
*
* 4. Event Handling
* If your module generates events, you register that fact in your install method
* (see method.install.php for an example of this). However, if your module will
* handle/process/consume events generated by the Core or other modules, you
* can register that in this method.
*
*/
function SetParameters()
{
/*
* 1. Simply module tag
* This next line allows you to use the tag {Skeleton} in your template or page; otherwise,
* you would have to use the more cumbersome tag {cms_module module='Skeleton'}
*/
$this->RegisterModulePlugin();
/*
* 2. Pretty URLS
*
For example:
$this->RegisterRoute('/skeleton\/add\/(?P<skeleton_id>[0-9]+)\/(?P<returnid>[0-9]+)$/',
array('action'=>'default'));
now, any url that looks like:
/skeleton/view/3/5
would call the default action, with:
$params['skeleton_id'] set to 3
and $returnid set to 5
Be sure to take a look in action.default.php, where the links are created for viewing, editing, and adding
records. You'll see that the CreateFrontendLink takes all the parameters to create a link for non-pretty
URLs, but also takes a string parameter which is a fully-assembled Pretty URL. Just registering the routes
is not enough; you module's links need to create the URLs on their side as well.
*/
$this->RegisterRoute('/francais\/galerie\/(?P<gamma_cat>[a-z0-9]+)$/',array('action'=>'default'));
$this->RegisterRoute('/english\/gallery\/(?P<gamma_cat>[a-z0-9]+)$/',array('action'=>'default'));
$this->RegisterRoute('/francais\/galerie\/(?P<gamma_cat>[a-z0-9]+)\/(?P<gamma_alias>[a-z0-9]+)$/',array('action'=>'default'));
$this->RegisterRoute('/english\/gallery\/(?P<gamma_cat>[a-z0-9]+)\/(?P<gamma_alias>[a-z0-9]+)$/',array('action'=>'default'));
/*
* 2a. Custom URLs for Specific Content
*
As of CMSMS 1.9, you can create a specific URL and map it to whatever you want. Say you knew that the
record with skeleton_id = 1 was going to be a special record, and you wanted to associate it with the URL:
http://yoursite.com/this/is/insanely/great/stuff
To do this, you'd register a route with the correct paramters using the CMS Route Manager:
*/
$gCms = cmsms();
$contentops = $gCms->GetContentOperations();
$returnid = $contentops->GetDefaultContent();
// The previous three lines are to get a returnid; many modules, like News, have a default
// page in which to display detail views. In that case, the page_id would be used for returnid.
// The next three lines are where we map the URL to our detail page.
$parms = array('action'=>'default','gamma_id'=>1,'returnid'=>$returnid);
$route = new CmsRoute('this/is/',$this->GetName(),$parms,TRUE);
cms_route_manager::register($route);
/*
* 3. Security
*
*/
// Don't allow parameters other than the ones you've explicitly defined
$this->RestrictUnknownParams();
// syntax for creating a parameter is parameter name, default value, description
$this->CreateParameter('gamma_id', -1, $this->Lang('help_skeleton_id'));
// skeleton_id must be an integer
$this->SetParameterType('gamma_id',CLEAN_INT);
// module_message must be a string
$this->CreateParameter('module_message','',$this->Lang('help_module_message'));
$this->SetParameterType('module_message',CLEAN_STRING);
// description must be a string
$this->CreateParameter('description','',$this->Lang('help_description'));
$this->SetParameterType('description',CLEAN_STRING);
// explanation must be a string
$this->CreateParameter('explanation','',$this->Lang('help_explanation'));
$this->SetParameterType('explanation',CLEAN_STRING);
/*
* 4. Event Handling
*
Typical example: specify the originator, the event name, and whether or not
the event is removable (used for one-time events)
$this->AddEventHandler( 'Core', 'ContentPostRender', true );
*/
}
function GetEventDescription ( $eventname )
{
return $this->Lang('event_info_'.$eventname );
}
function GetEventHelp ( $eventname )
{
return $this->Lang('event_help_'.$eventname );
}
function InstallPostMessage()
{
return $this->Lang('postinstall');
}
/**
* UninstallPostMessage()
* After removing a module, there may be things you want to
* communicate to your admin. This function returns a
* string which will be displayed.
*
* See the note on localization at the top of this file.
* @return string Message to be shown after uninstallation
*/
function UninstallPostMessage()
{
return $this->Lang('postuninstall');
}
function UninstallPreMessage()
{
return $this->Lang('really_uninstall');
}
/**
* Your methods here
*
* This would be a good place to define some general methods for your module
*
* Its a good practice to have underscore in front of your own methods
*/
function _SetStatus($oid, $status) {
//...
}
} //end class
?>
Je me dis que je n'ai pas du bien comprendre le système de réécritures des urls, surtout que je force la langue dans
le Gamma.module.php je ne pense pas que soit juste, mais si je ne le mets pas là, je me retrouve avec site/lacategorie...
En espérant que quelqu'un puisse m'aider ...
Bonnes fêtes de fin d'année